PostgreSQL/PgSchema.pm

filter: Statementstmt 30.0% 15 / 50 Branchbran 8.3% 1 / 12 Conditioncond 11.1% 3 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 30.7% 4 / 13 Podpod 87.5% 7 / 8 totaltotal 24.0% 30 / 125 CC 19 SCAR 24.4
CC
19
Cov
21%
new
20.0
pg_type
20.0
pg_function
20.0
CRAP 11.5
« PostgreSQL/Introspect/Schemas.pm ↑ Summary PostgreSQL/Introspect/Normalize.pm »
1 package DBIO::PostgreSQL::PgSchema;
2 # ABSTRACT: Base class for PostgreSQL schema namespaces
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 my (%_CLASS_ENUM_DEFS, %_CLASS_TYPE_DEFS, %_CLASS_FUNCTION_DEFS);
8  
9  
10 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
11 0 my ($class, %args) = @_;
12 0 ▶ $args{_enums} //= {};
Condition0%0/2
$args{"_enums"} //= {}
l!l
00
MC/DC0%0/1
$args{"_enums"} //= {}
$args{"_enums"}
Truth table0%0/2
A
A= $args{"_enums"}
Aresult
11
00
13 0 ▶ $args{_types} //= {};
Condition0%0/2
$args{"_types"} //= {}
l!l
00
MC/DC0%0/1
$args{"_types"} //= {}
$args{"_types"}
Truth table0%0/2
A
A= $args{"_types"}
Aresult
11
00
14 0 ▶ $args{_functions} //= {};
Condition0%0/2
$args{"_functions"} //= {}
l!l
00
MC/DC0%0/1
$args{"_functions"} //= {}
$args{"_functions"}
Truth table0%0/2
A
A= $args{"_functions"}
Aresult
11
00
15 0 bless \%args, $class;
16 }
17  
18 0 ▶sub pg_schema_name { $_[0]->{pg_schema_name} }
Subroutine pg_schema_name: not called CC=1 SCAR=6.9
Pod documented
19  
20 3 ▶sub _pg_enum_defs { $_CLASS_ENUM_DEFS{ ref $_[0] || $_[0] } // [] }
Subroutine _pg_enum_defs: called CC=3 SCAR=14.2
Condition40%2/5
$_CLASS_ENUM_DEFS{ref $_[0] or $_[0]} // []
l!l
30
ref $_[0] || $_[0]
l!l&&r!l&&!r
030
MC/DC0%0/1
$_CLASS_ENUM_DEFS{ref $_[0] or $_[0]} // []
$_CLASS_ENUM_DEFS{ref $_[0] or $_[0]}
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table50%1/2
A
A= $_CLASS_ENUM_DEFS{ref $_[0] or $_[0]}
Aresult
11
00
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
21 0 ▶sub _pg_type_defs { $_CLASS_TYPE_DEFS{ ref $_[0] || $_[0] } // [] }
Subroutine _pg_type_defs: not called CC=3 SCAR=24.8
Condition0%0/5
$_CLASS_TYPE_DEFS{ref $_[0] or $_[0]} // []
l!l
00
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/1
$_CLASS_TYPE_DEFS{ref $_[0] or $_[0]} // []
$_CLASS_TYPE_DEFS{ref $_[0] or $_[0]}
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/2
A
A= $_CLASS_TYPE_DEFS{ref $_[0] or $_[0]}
Aresult
11
00
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
22 0 ▶sub _pg_function_defs { $_CLASS_FUNCTION_DEFS{ ref $_[0] || $_[0] } // [] }
Subroutine _pg_function_defs: not called CC=3 SCAR=24.8
Condition0%0/5
$_CLASS_FUNCTION_DEFS{ref $_[0] or $_[0]} // []
l!l
00
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/1
$_CLASS_FUNCTION_DEFS{ref $_[0] or $_[0]} // []
$_CLASS_FUNCTION_DEFS{ref $_[0] or $_[0]}
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/2
A
A= $_CLASS_FUNCTION_DEFS{ref $_[0] or $_[0]}
Aresult
11
00
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
23  
24  
25  
26 2 ▶sub pg_enum {
Subroutine pg_enum: called CC=4 SCAR=16.6
Pod documented
27 2 my ($self, $name, $values) = @_;
28 2 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 2 2
29 0 ▶ $self->{_enums}{$name} = $values if $values;
Branch: 1 branch
BranchTrueFalseTotal
if $values 0 0 0
30 0 return $self->{_enums}{$name};
31 }
32 # Class method usage for declarative API
33 2 my $class = $self;
34 2 ▶ $_CLASS_ENUM_DEFS{$class} //= [];
Condition50%1/2
$_CLASS_ENUM_DEFS{$class} //= []
l!l
02
MC/DC0%0/1
$_CLASS_ENUM_DEFS{$class} //= []
$_CLASS_ENUM_DEFS{$class}
Truth table50%1/2
A
A= $_CLASS_ENUM_DEFS{$class}
Aresult
11
00
35 2 push @{ $_CLASS_ENUM_DEFS{$class} }, [$name, $values];
36 }
37  
38  
39 0 ▶sub pg_type {
Subroutine pg_type: not called CC=4 SCAR=30.0
Pod documented
40 0 my ($self, $name, $fields) = @_;
41 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
42 0 ▶ $self->{_types}{$name} = $fields if $fields;
Branch: 1 branch
BranchTrueFalseTotal
if $fields 0 0 0
43 0 return $self->{_types}{$name};
44 }
45 0 my $class = $self;
46 0 ▶ $_CLASS_TYPE_DEFS{$class} //= [];
Condition0%0/2
$_CLASS_TYPE_DEFS{$class} //= []
l!l
00
MC/DC0%0/1
$_CLASS_TYPE_DEFS{$class} //= []
$_CLASS_TYPE_DEFS{$class}
Truth table0%0/2
A
A= $_CLASS_TYPE_DEFS{$class}
Aresult
11
00
47 0 push @{ $_CLASS_TYPE_DEFS{$class} }, [$name, $fields];
48 }
49  
50  
51 0 ▶sub pg_function {
Subroutine pg_function: not called CC=4 SCAR=30.0
Pod documented
52 0 my ($self, $name, $sql) = @_;
53 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
54 0 ▶ $self->{_functions}{$name} = $sql if $sql;
Branch: 1 branch
BranchTrueFalseTotal
if $sql 0 0 0
55 0 return $self->{_functions}{$name};
56 }
57 0 my $class = $self;
58 0 ▶ $_CLASS_FUNCTION_DEFS{$class} //= [];
Condition0%0/2
$_CLASS_FUNCTION_DEFS{$class} //= []
l!l
00
MC/DC0%0/1
$_CLASS_FUNCTION_DEFS{$class} //= []
$_CLASS_FUNCTION_DEFS{$class}
Truth table0%0/2
A
A= $_CLASS_FUNCTION_DEFS{$class}
Aresult
11
00
59 0 push @{ $_CLASS_FUNCTION_DEFS{$class} }, [$name, $sql];
60 }
61  
62  
63 0 ▶sub pg_enums {
Subroutine pg_enums: not called CC=1 SCAR=6.9
Pod documented
64 0 my ($self) = @_;
65 0 return { %{ $self->{_enums} } };
66 }
67  
68  
69 0 ▶sub pg_types {
Subroutine pg_types: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self) = @_;
71 0 return { %{ $self->{_types} } };
72 }
73  
74  
75 0 ▶sub pg_functions {
Subroutine pg_functions: not called CC=1 SCAR=6.9
Pod documented
76 0 my ($self) = @_;
77 0 return { %{ $self->{_functions} } };
78 }
79  
80  
81 2 1;
82  
83 __END__
84  
85 =pod
86  
87 =encoding UTF-8
88  
89 =head1 NAME
90  
91 DBIO::PostgreSQL::PgSchema - Base class for PostgreSQL schema namespaces
92  
93 =head1 VERSION
94  
95 version 0.900001
96  
97 =head1 DESCRIPTION
98  
99 C<DBIO::PostgreSQL::PgSchema> is the base class for the intermediate
100 PostgreSQL schema (namespace) layer in DBIO. Subclass it to declare the
101 enums, composite types, and functions that belong to a specific PostgreSQL
102 namespace.
103  
104 package MyApp::DB::PgSchema::Auth;
105 use base 'DBIO::PostgreSQL::PgSchema';
106  
107 __PACKAGE__->pg_schema_name('auth');
108  
109 __PACKAGE__->pg_enum('role_type' => [qw( admin moderator user guest )]);
110  
111 __PACKAGE__->pg_type('address_type' => {
112 street => 'text',
113 city => 'text',
114 zip => 'varchar(10)',
115 });
116  
117 __PACKAGE__->pg_function('update_modified_at' => q{
118 CREATE OR REPLACE FUNCTION auth.update_modified_at()
119 RETURNS TRIGGER AS $$ BEGIN NEW.modified_at = NOW(); RETURN NEW; END;
120 $$ LANGUAGE plpgsql
121 });
122  
123 The class-method forms of C<pg_enum>, C<pg_type>, and C<pg_function> record
124 definitions that L<DBIO::PostgreSQL::DDL> reads when generating install DDL.
125 The instance-method forms are used at runtime when a C<PgSchema> object is
126 instantiated.
127  
128 =head1 ATTRIBUTES
129  
130 =head2 pg_schema_name
131  
132 The PostgreSQL schema name (e.g. C<auth>, C<public>, C<api>). Required.
133  
134 =head1 METHODS
135  
136 =head2 _pg_enum_defs
137  
138 my $defs = $class->_pg_enum_defs;
139  
140 Returns the arrayref of C<[$name, $values]> pairs declared on this specific
141 class via L</pg_enum>. Does not inherit from parent classes.
142  
143 =head2 _pg_type_defs
144  
145 my $defs = $class->_pg_type_defs;
146  
147 Returns the arrayref of C<[$name, $fields]> pairs declared on this class
148 via L</pg_type>.
149  
150 =head2 _pg_function_defs
151  
152 my $defs = $class->_pg_function_defs;
153  
154 Returns the arrayref of C<[$name, $sql]> pairs declared on this class
155 via L</pg_function>.
156  
157 =head2 pg_enum
158  
159 # Class method (declarative — records for DDL generation):
160 __PACKAGE__->pg_enum('role_type' => [qw( admin moderator user guest )]);
161  
162 # Instance method (runtime access):
163 my $values = $obj->pg_enum('role_type');
164  
165 Declare or retrieve an enum type definition. The C<values> arrayref preserves
166 declaration order, which PostgreSQL requires. Class-method calls accumulate
167 definitions consumed by L<DBIO::PostgreSQL::DDL>.
168  
169 =head2 pg_type
170  
171 # Class method:
172 __PACKAGE__->pg_type('address_type' => {
173 street => 'text',
174 city => 'text',
175 zip => 'varchar(10)',
176 country => 'varchar(2)',
177 });
178  
179 # Instance method:
180 my $fields = $obj->pg_type('address_type');
181  
182 Declare or retrieve a composite type definition. The fields hashref maps
183 attribute names to PostgreSQL type strings.
184  
185 =head2 pg_function
186  
187 # Class method:
188 __PACKAGE__->pg_function('update_modified_at' => q{
189 CREATE OR REPLACE FUNCTION auth.update_modified_at()
190 RETURNS TRIGGER AS $$ ... $$ LANGUAGE plpgsql
191 });
192  
193 # Instance method:
194 my $sql = $obj->pg_function('update_modified_at');
195  
196 Declare or retrieve a function definition. The SQL string is emitted verbatim
197 by L<DBIO::PostgreSQL::DDL> during C<install_ddl>.
198  
199 =head2 pg_enums
200  
201 my $all = $obj->pg_enums; # hashref of name => values
202  
203 Returns a copy of all enum definitions held by this instance.
204  
205 =head2 pg_types
206  
207 my $all = $obj->pg_types; # hashref of name => fields
208  
209 Returns a copy of all composite type definitions held by this instance.
210  
211 =head2 pg_functions
212  
213 my $all = $obj->pg_functions; # hashref of name => sql
214  
215 Returns a copy of all function definitions held by this instance.
216  
217 =seealso
218  
219 =over 4
220  
221 =item * L<DBIO::PostgreSQL> - schema component (Database layer)
222  
223 =item * L<DBIO::PostgreSQL::Result> - Result component (Table layer)
224  
225 =item * L<DBIO::PostgreSQL::DDL> - reads PgSchema definitions to generate DDL
226  
227 =back
228  
229 =head1 AUTHOR
230  
231 DBIO & DBIx::Class Authors
232  
233 =head1 COPYRIGHT AND LICENSE
234  
235 Copyright (C) 2026 DBIO Authors
236 Portions Copyright (C) 2005-2025 DBIx::Class Authors
237 Based on DBIx::Class, heavily modified.
238  
239 This is free software; you can redistribute it and/or modify it under
240 the same terms as the Perl 5 programming language system itself.
241  
242 =cut
« PostgreSQL/Introspect/Schemas.pm ↑ Summary PostgreSQL/Introspect/Normalize.pm »