| 1 | package DBIO::PostgreSQL::Age::Storage; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL storage with Apache AGE graph support | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | my $JSON = JSON::MaybeXS->new(utf8 => 1, canonical => 1); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub connect_call_load_age { | ||||||||||||||||||||||||||
|
Subroutine
connect_call_load_age: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 14 | 0 | $self->_do_query(q{LOAD 'age'}); | |||||||||||||||||||||||||||
| 15 | 0 | $self->_do_query(q{SET search_path = ag_catalog, "$user", public}); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub create_graph { | ||||||||||||||||||||||||||
|
Subroutine
create_graph: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 21 | 1 | ▶ | $self->dbh_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||
| 23 | 1 | $dbh->do('SELECT * FROM ag_catalog.create_graph(?)', undef, $name); | |||||||||||||||||||||||||||
| 24 | 1 | }); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | sub drop_graph { | ||||||||||||||||||||||||||
|
Subroutine
drop_graph: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 1 | my ($self, $name, $cascade) = @_; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | $self->dbh_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 31 | 1 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||
| 32 | 1 | ▶ | $dbh->do( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 'SELECT * FROM ag_catalog.drop_graph(?, ?)', | ||||||||||||||||||||||||||||
| 34 | undef, $name, $cascade ? 1 : 0, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | 1 | }); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | sub cypher { | ||||||||||||||||||||||||||
|
Subroutine
cypher: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 2 | my ($self, $graph, $query, $columns, $params, $opts) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my ($sql, $bind) = $self->_cypher_sql_bind($graph, $query, $columns, $params); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my $rows = $self->dbh_do(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 2 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||
| 47 | 2 | return $dbh->selectall_arrayref($sql, { Slice => {} }, @$bind); | |||||||||||||||||||||||||||
| 48 | 2 | }); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | if ($opts && $opts->{auto_decode}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts and $opts->{"auto_decode"}
MC/DC0%0/2
$opts and $opts->{"auto_decode"}
$opts
$opts->{"auto_decode"}
Truth table33%1/3
A and B
A= $optsB= $opts->{"auto_decode"}
| |||||||||||||||||||||||||||||
| 51 | 0 | for my $row (@$rows) { | |||||||||||||||||||||||||||
| 52 | 0 | for my $col (keys %$row) { | |||||||||||||||||||||||||||
| 53 | 0 | $row->{$col} = $self->decode_agtype($row->{$col}); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | return $rows; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Async dispatchers for the graph methods, mirroring core's CRUD *_async | ||||||||||||||||||||||||||||
| 62 | # (DBIO::Storage::select_async etc.): each is a thin shift->_run_async($op) | ||||||||||||||||||||||||||||
| 63 | # whose three-way contract is (1) route ${op}_async to the embedded async | ||||||||||||||||||||||||||||
| 64 | # backend when one is live (future_io/ev -> the composed Age async layer's real | ||||||||||||||||||||||||||||
| 65 | # cypher_async), (2) in 'immediate' mode run the sync method in-process and wrap | ||||||||||||||||||||||||||||
| 66 | # its result in an immediately-resolved Future (no event loop), (3) on a plain | ||||||||||||||||||||||||||||
| 67 | # sync connection croak "not an async connection". This is why cypher() (and the | ||||||||||||||||||||||||||||
| 68 | # graph lifecycle methods) can also be reached as *_async on the live storage, | ||||||||||||||||||||||||||||
| 69 | # degrading cleanly under { async => 'immediate' } exactly like select_async. | ||||||||||||||||||||||||||||
| 70 | # Graph async methods have a sync equivalent (cypher/create_graph/drop_graph), | ||||||||||||||||||||||||||||
| 71 | # so unlike backend-only ops (listen/copy) they belong in the degrade-capable | ||||||||||||||||||||||||||||
| 72 | # tier alongside the core CRUD methods. | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub cypher_async { shift->_run_async('cypher', @_) } | ||||||||||||||||||||||||||
|
Subroutine
cypher_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub create_graph_async { shift->_run_async('create_graph', @_) } | ||||||||||||||||||||||||||
|
Subroutine
create_graph_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub drop_graph_async { shift->_run_async('drop_graph', @_) } | ||||||||||||||||||||||||||
|
Subroutine
drop_graph_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Build the AGE cypher() SQL and its bind values. Pure (no DB) so the SQL | ||||||||||||||||||||||||||||
| 79 | # generation can be unit-tested offline; cypher() wraps this with execution. | ||||||||||||||||||||||||||||
| 80 | 14 | ▶ | sub _cypher_sql_bind { | ||||||||||||||||||||||||||
|
Subroutine
_cypher_sql_bind: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 81 | 14 | my ($self, $graph, $query, $columns, $params) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Apache AGE requires the graph name to be a string literal in the | ||||||||||||||||||||||||||||
| 84 | # cypher() call -- it cannot be passed as a bind parameter. Validate the | ||||||||||||||||||||||||||||
| 85 | # name as a plain identifier so we can safely inline it. | ||||||||||||||||||||||||||||
| 86 | 14 | ▶ | $graph =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or $self->throw_exception("Invalid AGE graph name '$graph'"); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 11 | my $col_spec = join ', ', map { "$_ agtype" } @$columns; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 11 | my @bind; | |||||||||||||||||||||||||||
| 92 | 11 | ▶ | if ($params && %$params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params and %$params
MC/DC100%2/2
$params and %$params
$params
%$params
Truth table100%3/3
A and B
A= $paramsB= %$params
| |||||||||||||||||||||||||||||
| 93 | 3 | push @bind, $JSON->encode($params); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 11 | ▶ | my $param_slot = @bind ? ', ?' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 11 | my $sql = "SELECT * FROM cypher('$graph', \$\$\n$query\n\$\$${param_slot}) AS ($col_spec)"; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 11 | return ($sql, \@bind); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Decode a single agtype text value into native Perl data. | ||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | # AGE returns agtype values as text. The shapes we handle: | ||||||||||||||||||||||||||||
| 104 | # - JSON-like maps/lists -> decode_json into hashref/arrayref | ||||||||||||||||||||||||||||
| 105 | # - Quoted string scalar -> strip the surrounding quotes | ||||||||||||||||||||||||||||
| 106 | # - Integer / float -> return as Perl number | ||||||||||||||||||||||||||||
| 107 | # - true / false / null -> JSON booleans / undef | ||||||||||||||||||||||||||||
| 108 | # - Cast annotations -> strip a trailing ::vertex / ::edge / ::path | ||||||||||||||||||||||||||||
| 109 | # before further decoding (older AGE only). | ||||||||||||||||||||||||||||
| 110 | # Anything we don't recognise is returned as-is so callers can still | ||||||||||||||||||||||||||||
| 111 | # post-process it themselves. | ||||||||||||||||||||||||||||
| 112 | 19 | ▶ | sub decode_agtype { | ||||||||||||||||||||||||||
|
Subroutine
decode_agtype: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 19 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Strip a trailing agtype cast annotation. Older AGE versions (1.3 and | ||||||||||||||||||||||||||||
| 116 | # earlier) append "::vertex" / "::edge" / "::path" to the rendered text; | ||||||||||||||||||||||||||||
| 117 | # newer versions do not. Be defensive about both. | ||||||||||||||||||||||||||||
| 118 | 19 | my $raw = $value; | |||||||||||||||||||||||||||
| 119 | 19 | $raw =~ s/::(?:vertex|edge|path)\s*\z//; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 19 | ▶ | return $raw unless defined $raw && length $raw; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $raw and length $raw
MC/DC0%0/2
defined $raw and length $raw
defined $raw
length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 19 | my $first = substr($raw, 0, 1); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Map or list: hand off to JSON. | ||||||||||||||||||||||||||||
| 126 | 19 | ▶ | if ($first eq '{' || $first eq '[') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first eq "{" or $first eq "["
MC/DC100%2/2
$first eq "{" or $first eq "["
$first eq "{"
$first eq "["
Truth table100%3/3
A or B
A= $first eq "{"B= $first eq "["
| |||||||||||||||||||||||||||||
| 127 | 9 | my $decoded = eval { $JSON->decode($raw) }; | |||||||||||||||||||||||||||
| 128 | 9 | ▶ | return $decoded unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | return $raw; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Quoted string scalar: "foo" -> foo. Decode any JSON-escaped chars. | ||||||||||||||||||||||||||||
| 133 | 10 | ▶ | if ($first eq '"') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 4 | my $decoded = eval { $JSON->decode($raw) }; | |||||||||||||||||||||||||||
| 135 | 4 | ▶ | return $decoded unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | # Fallback: naive strip of outer quotes. | ||||||||||||||||||||||||||||
| 137 | 0 | my $inner = substr($raw, 1, length($raw) - 2); | |||||||||||||||||||||||||||
| 138 | 0 | return $inner; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Unquoted scalar. true/false/null/number. | ||||||||||||||||||||||||||||
| 142 | 6 | ▶ | if ($raw eq 'true') { return $JSON->true; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 5 | ▶ | if ($raw eq 'false') { return $JSON->false; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 4 | ▶ | if ($raw eq 'null') { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Looks numeric? | ||||||||||||||||||||||||||||
| 147 | 3 | ▶ | if ($raw =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 2 | return $raw + 0; # +0 keeps int as int, float as float | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Anything else: leave alone. | ||||||||||||||||||||||||||||
| 152 | 1 | return $raw; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =pod | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 NAME | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | DBIO::PostgreSQL::Age::Storage - PostgreSQL storage with Apache AGE graph support | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head1 VERSION | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | version 0.900001 | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # Loaded automatically via DBIO::PostgreSQL::Age component. | ||||||||||||||||||||||||||||
| 176 | # Use connect_call_load_age to initialize AGE on each connection: | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | MyApp::Schema->connect( | ||||||||||||||||||||||||||||
| 179 | $dsn, $user, $pass, | ||||||||||||||||||||||||||||
| 180 | { on_connect_call => 'load_age' }, | ||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my $storage = $schema->storage; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $storage->create_graph('social'); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | my $rows = $storage->cypher( | ||||||||||||||||||||||||||||
| 188 | 'social', | ||||||||||||||||||||||||||||
| 189 | $$ MATCH (a:Person {name: $name})-[:KNOWS]->(b) RETURN b.name $$, | ||||||||||||||||||||||||||||
| 190 | ['friend'], | ||||||||||||||||||||||||||||
| 191 | { name => 'Alice' }, | ||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | $storage->drop_graph('social', 1); # cascade | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | A storage B<layer> that adds Apache AGE graph database support -- connection | ||||||||||||||||||||||||||||
| 199 | initialization, graph lifecycle management, and Cypher query execution -- to a | ||||||||||||||||||||||||||||
| 200 | PostgreSQL storage. It is B<not> a storage subclass: it is a plain method | ||||||||||||||||||||||||||||
| 201 | package composed over the resolved driver storage at connection time (see | ||||||||||||||||||||||||||||
| 202 | L<DBIO::Storage::Composed>). L<DBIO::PostgreSQL::Age> registers it via | ||||||||||||||||||||||||||||
| 203 | L<DBIO::Schema/register_storage_layer>, so on C<< $schema->connect >> the live | ||||||||||||||||||||||||||||
| 204 | storage isa B<both> this layer and L<DBIO::PostgreSQL::Storage>, and the methods | ||||||||||||||||||||||||||||
| 205 | below are callable on that composed storage. Because it is a layer, it stacks | ||||||||||||||||||||||||||||
| 206 | cleanly with other extension layers (its methods are disjoint from, e.g., | ||||||||||||||||||||||||||||
| 207 | L<DBIO::PostgreSQL::PostGIS::Storage>). | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head2 Layer rules | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Per the storage-layer composition model (DBIO core, karr #70): this package does | ||||||||||||||||||||||||||||
| 212 | B<not> C<use base> a driver storage, defines no constructor, and calls only the | ||||||||||||||||||||||||||||
| 213 | documented public storage surface -- C<dbh_do> and C<throw_exception> for the | ||||||||||||||||||||||||||||
| 214 | graph methods, plus the connect-action seam C<_do_query> for | ||||||||||||||||||||||||||||
| 215 | C<connect_call_load_age> -- each resolved through the composed MRO to the driver | ||||||||||||||||||||||||||||
| 216 | base at runtime. C<_do_query> is deliberate: it is the same seam | ||||||||||||||||||||||||||||
| 217 | C<connect_call_do_sql> uses, and it is what routes the C<LOAD 'age'> replay onto | ||||||||||||||||||||||||||||
| 218 | each freshly-spawned async pool connection (core karr #68); a bare | ||||||||||||||||||||||||||||
| 219 | C<< $self->dbh->do >> would run against the sync dbh instead and defeat the | ||||||||||||||||||||||||||||
| 220 | replay. The pure helpers C<_cypher_sql_bind> and C<decode_agtype> are DB-free | ||||||||||||||||||||||||||||
| 221 | class-level helpers, shared by composition with the async layer | ||||||||||||||||||||||||||||
| 222 | (L<DBIO::PostgreSQL::Age::Storage::Async>) so sync and async build identical SQL | ||||||||||||||||||||||||||||
| 223 | and decode identically. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | All result columns from C<cypher()> are declared as C<agtype> — Apache AGE's | ||||||||||||||||||||||||||||
| 226 | JSON-superset type that represents vertices, edges, paths, and scalar values. | ||||||||||||||||||||||||||||
| 227 | Values are returned as strings and can be decoded with a JSON parser. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 METHODS | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 connect_call_load_age | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | { on_connect_call => 'load_age' } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Connection callback that loads the Apache AGE shared library into the session | ||||||||||||||||||||||||||||
| 236 | and sets C<search_path> to include C<ag_catalog>. Must be called before any | ||||||||||||||||||||||||||||
| 237 | graph operations. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 create_graph | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | $storage->create_graph('social'); | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Creates a new Apache AGE graph with the given name. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 drop_graph | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | $storage->drop_graph('social'); | ||||||||||||||||||||||||||||
| 248 | $storage->drop_graph('social', 1); # cascade | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Drops the named graph. Pass a true second argument to cascade the drop to all | ||||||||||||||||||||||||||||
| 251 | vertices and edges within the graph. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head2 cypher_async | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head2 create_graph_async | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head2 drop_graph_async | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | The async counterparts of L</cypher>, L</create_graph> and L</drop_graph>, | ||||||||||||||||||||||||||||
| 260 | reachable on the live (composed) storage. Each dispatches through core's | ||||||||||||||||||||||||||||
| 261 | L<DBIO::Storage/_run_async|three-way async contract>: on an async connection | ||||||||||||||||||||||||||||
| 262 | (C<< { async => 'future_io' } >>, C<'ev'>, ...) they route to the embedded async | ||||||||||||||||||||||||||||
| 263 | backend (the composed L<DBIO::PostgreSQL::Age::Storage::Async> layer, which does | ||||||||||||||||||||||||||||
| 264 | the real non-blocking work); under C<< { async => 'immediate' } >> they run the | ||||||||||||||||||||||||||||
| 265 | sync method in-process and return an immediately-resolved L<Future> with no event | ||||||||||||||||||||||||||||
| 266 | loop; on a plain sync connection they croak. This mirrors the core CRUD | ||||||||||||||||||||||||||||
| 267 | C<*_async> methods (L<DBIO::Storage/select_async> and friends) -- graph queries | ||||||||||||||||||||||||||||
| 268 | have a sync equivalent, so they degrade the same way rather than being | ||||||||||||||||||||||||||||
| 269 | backend-only. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head2 decode_agtype | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | my $name = $storage->decode_agtype('"alice"'); # "alice" | ||||||||||||||||||||||||||||
| 274 | my $age = $storage->decode_agtype('30'); # 30 | ||||||||||||||||||||||||||||
| 275 | my $v = $storage->decode_agtype( | ||||||||||||||||||||||||||||
| 276 | '{"id": 1, "label": "Person", "properties": {"name": "alice"}}::vertex' | ||||||||||||||||||||||||||||
| 277 | ); # { id => 1, label => "Person", properties => { name => "alice" } } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | my $rows = $storage->cypher( | ||||||||||||||||||||||||||||
| 280 | 'social', | ||||||||||||||||||||||||||||
| 281 | $$ RETURN n $$, | ||||||||||||||||||||||||||||
| 282 | ['n'], | ||||||||||||||||||||||||||||
| 283 | ); | ||||||||||||||||||||||||||||
| 284 | my $vertex = $storage->decode_agtype($rows->[0]{n}); # manual decode | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Decodes a single C<agtype> text value (as returned by C<cypher()>) into native | ||||||||||||||||||||||||||||
| 287 | Perl data. Recognised shapes: | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =over 4 | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item * String scalar (quoted, e.g. C<"alice">) — string with quotes stripped | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =item * Integer / float scalar (e.g. C<42>, C<3.14>) — Perl number | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item * Boolean (C<true>, C<false>) — C<JSON::MaybeXS> true/false objects | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =item * Null (C<null>) — C<undef> | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =item * Map / list (e.g. C<{"name": "alice"}>, C<[1, 2, 3]>) — hashref / arrayref | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =item * Vertex / edge — same as the underlying map; a trailing C<::vertex> / | ||||||||||||||||||||||||||||
| 302 | C<::edge> cast annotation (only emitted by older AGE versions) is stripped | ||||||||||||||||||||||||||||
| 303 | before decoding. C<id>, C<label>, C<start_id>, C<end_id>, C<properties> are | ||||||||||||||||||||||||||||
| 304 | preserved as JSON keys. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item * Path — arrayref of decoded vertices and edges (structure preserved, | ||||||||||||||||||||||||||||
| 307 | not unwrapped) | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item * Anything else — returned as-is so the caller can post-process it | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =back | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head2 cypher | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | my $rows = $storage->cypher( | ||||||||||||||||||||||||||||
| 316 | 'social', | ||||||||||||||||||||||||||||
| 317 | $$ MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a.name, b.name $$, | ||||||||||||||||||||||||||||
| 318 | [qw( person friend )], | ||||||||||||||||||||||||||||
| 319 | ); | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # With Cypher parameters: | ||||||||||||||||||||||||||||
| 322 | my $rows = $storage->cypher( | ||||||||||||||||||||||||||||
| 323 | 'social', | ||||||||||||||||||||||||||||
| 324 | $$ MATCH (n:Person {name: $name}) RETURN n $$, | ||||||||||||||||||||||||||||
| 325 | ['node'], | ||||||||||||||||||||||||||||
| 326 | { name => 'Alice' }, | ||||||||||||||||||||||||||||
| 327 | ); | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # Auto-decode each cell into native Perl data: | ||||||||||||||||||||||||||||
| 330 | my $rows = $storage->cypher( | ||||||||||||||||||||||||||||
| 331 | 'social', | ||||||||||||||||||||||||||||
| 332 | $$ MATCH (n:Person {name: $name}) RETURN n $$, | ||||||||||||||||||||||||||||
| 333 | ['node'], | ||||||||||||||||||||||||||||
| 334 | { name => 'Alice' }, | ||||||||||||||||||||||||||||
| 335 | { auto_decode => 1 }, | ||||||||||||||||||||||||||||
| 336 | ); | ||||||||||||||||||||||||||||
| 337 | # $rows->[0]{node} is now a hashref, not a string. | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Executes a Cypher query against the named graph. C<$columns> is an arrayref | ||||||||||||||||||||||||||||
| 340 | of result column names; all are declared as C<agtype>. Returns an arrayref | ||||||||||||||||||||||||||||
| 341 | of hashrefs with one key per column. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | An optional C<$params> hashref is JSON-encoded and passed as AGE's third | ||||||||||||||||||||||||||||
| 344 | argument to C<cypher()> for parameterized queries. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Pass an optional fifth argument, a hashref of options, to control result | ||||||||||||||||||||||||||||
| 347 | handling: | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =over 4 | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =item * C<auto_decode =E<gt> 1> — apply L</decode_agtype> to every cell of | ||||||||||||||||||||||||||||
| 352 | every row before returning. Without this option, every cell is a raw agtype | ||||||||||||||||||||||||||||
| 353 | string and decoding is the caller's responsibility. | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =back | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =seealso | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =over 4 | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =item * L<DBIO::PostgreSQL::Age> - Schema component that activates this storage | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =back | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | DBIO Authors | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 374 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||