PostGIS/Storage.pm

filter: Statementstmt 66.6% 20 / 30 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 100% 3 / 3 totaltotal 71.4% 35 / 49 CC 4 SCAR 6.5
CC
4
Cov
72%
_ensure_postgis_extension
3.0
ensure_postgis
2.0
__ANON__
2.0
CRAP 1.9
« PostGIS.pm ↑ Summary PostGIS/Deploy.pm »
1 package DBIO::PostgreSQL::PostGIS::Storage;
2 # ABSTRACT: Storage class with PostGIS spatial extensions
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 0 ▶sub ensure_postgis {
Subroutine ensure_postgis: not called CC=1 SCAR=6.9
Pod documented
9 0 my $self = shift;
10 0 ▶ $self->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 0 my (undef, $dbh) = @_;
12 0 $dbh->do('CREATE EXTENSION IF NOT EXISTS postgis');
13 0 });
14 }
15  
16  
17 0 ▶sub postgis_version {
Subroutine postgis_version: not called CC=1 SCAR=6.9
Pod documented
18 0 my $self = shift;
19 0 ▶ return $self->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 my (undef, $dbh) = @_;
21 0 my ($v) = eval { $dbh->selectrow_array('SELECT PostGIS_Full_Version()') };
22 0 return $v;
23 0 });
24 }
25  
26  
27 3 ▶sub _ensure_postgis_extension {
Subroutine _ensure_postgis_extension: called CC=3 SCAR=11.0
28 3 my $self = shift;
29 3 ▶ my $installed = $self->dbh_do(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
30 3 my (undef, $dbh) = @_;
31 3 my ($row) = $dbh->selectrow_array(
32 "SELECT 1 FROM pg_extension WHERE extname = 'postgis'",
33 );
34 3 ▶ return defined $row ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $row ? : 1 2 3
35 3 });
36 3 ▶ return if $installed;
Branch: 1 branch
BranchTrueFalseTotal
if $installed 1 2 3
37 2 ▶ my $dbname = $self->dbh_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 my (undef, $dbh) = @_;
39 2 my ($n) = $dbh->selectrow_array('SELECT current_database()');
40 2 return $n;
41 2 });
42 2 ▶ $dbname = defined $dbname ? "'$dbname'" : '(unknown)';
Branch: 1 branch
BranchTrueFalseTotal
defined $dbname ? : 1 1 2
43 2 $self->throw_exception(
44 "PostGIS extension is not installed on database $dbname "
45 . '(required by DBIO::PostgreSQL::PostGIS)'
46 );
47 }
48  
49 2 ▶sub dbio_deploy_class { 'DBIO::PostgreSQL::PostGIS::Deploy' }
Subroutine dbio_deploy_class: called CC=1 SCAR=0.0
Pod documented
50  
51  
52 4 1;
53  
54 __END__
55  
56 =pod
57  
58 =encoding UTF-8
59  
60 =head1 NAME
61  
62 DBIO::PostgreSQL::PostGIS::Storage - Storage class with PostGIS spatial extensions
63  
64 =head1 VERSION
65  
66 version 0.900001
67  
68 =head1 DESCRIPTION
69  
70 A storage B<layer> that adds PostGIS-aware helper methods (extension
71 ensure/probe, version, deploy-class selection) to a PostgreSQL storage. It is
72 B<not> a storage subclass: it is a plain method package composed over the
73 resolved driver storage at connection time (see L<DBIO::Storage::Composed>).
74 L<DBIO::PostgreSQL::PostGIS> registers it via
75 L<DBIO::Schema/register_storage_layer>, so on C<< $schema->connect >> the live
76 storage isa B<both> this layer and L<DBIO::PostgreSQL::Storage>, and its methods
77 below are callable on that composed storage.
78  
79 =head2 Layer rules
80  
81 Per the storage-layer composition model (DBIO core, storage-layer composition):
82 this package does B<not> C<use base> a driver storage, defines no constructor,
83 and calls only the documented public storage surface (C<dbh_do>,
84 C<throw_exception>) -- which resolves through the composed MRO to the driver
85 base at runtime. New methods here (C<ensure_postgis>, C<postgis_version>,
86 C<_ensure_postgis_extension>) do not shadow any base method; C<dbio_deploy_class>
87 below is a deliberate override of the base hook.
88  
89 =head2 dbio_deploy_class is a single-owner base-hook override
90  
91 L</dbio_deploy_class> overrides the base storage's deploy-class hook to route
92 C<< $schema->deploy >> through the PostGIS-aware deploy class. Under the core
93 composition collision rule, exactly B<one> composed layer may own a given
94 method: if a second deploy-hooking extension (e.g. another spatial/graph layer
95 that also defines its own C<dbio_deploy_class>) is registered alongside this
96 one, composition B<croaks at compose time> naming C<dbio_deploy_class> and every
97 defining package. That is fail-loud by design -- the deploy class cannot be
98 silently decided by registration order. Two deploy-extending extensions on one
99 schema is a new core requirement (deploy-hook chaining), not a workaround here.
100  
101 =head2 No async layer
102  
103 PostGIS ships B<no> C<::Async> sibling: its storage surface (extension ensure,
104 version, deploy class) is synchronous, and the spatial codecs / ResultSet
105 helpers are not storage-async-level. Core composition B<skips silently> any
106 registered layer that has no C<::Async> mirror, so under any async connection
107 mode (C<< { async => ... } >>) the composed async backend simply carries no
108 PostGIS async layer, and CRUD on geometry columns flows through the transport
109 unchanged. If async deploy or codec hooks are ever needed, that is a separate
110 ticket.
111  
112 =head1 METHODS
113  
114 =head2 ensure_postgis
115  
116 $storage->ensure_postgis;
117  
118 Issues C<CREATE EXTENSION IF NOT EXISTS postgis> against the connected
119 database. Idempotent.
120  
121 =head2 postgis_version
122  
123 my $v = $storage->postgis_version;
124  
125 Returns the PostGIS extension version string from C<PostGIS_Full_Version()>,
126 or undef if the extension is not installed.
127  
128 =head2 _ensure_postgis_extension
129  
130 $storage->_ensure_postgis_extension;
131  
132 Cheap fail-fast check that the C<postgis> extension is installed on the
133 connected database. Throws a L<DBIO::Exception> with a message naming
134 the missing extension and the database when the extension is not
135 present. A no-op when the extension is installed.
136  
137 Intended to be wrapped around first-use storage operations by callers
138 that require PostGIS (deploy, schema load, geometry inflation). The
139 core driver does not call this automatically so that storage
140 construction against a non-PostGIS database remains possible for
141 introspection and other PostGIS-agnostic tasks.
142  
143 =head2 dbio_deploy_class
144  
145 Returns C<DBIO::PostgreSQL::PostGIS::Deploy> to activate PostGIS-aware
146 deploy when C<< $schema->deploy >> is called on a PostGIS-enabled schema.
147 This overrides the base storage's deploy-class hook; as a single-owner base
148 override it composes cleanly, but a second layer that also defines its own
149 C<dbio_deploy_class> makes composition croak (see L</dbio_deploy_class is a
150 single-owner base-hook override>).
151  
152 =head1 AUTHOR
153  
154 DBIO Authors
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 Copyright (C) 2026 DBIO Authors
159  
160 This is free software; you can redistribute it and/or modify it under
161 the same terms as the Perl 5 programming language system itself.
162  
163 =cut
« PostGIS.pm ↑ Summary PostGIS/Deploy.pm »