Pg.pm

filter: Statementstmt 38.0% 59 / 155 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.2% 15 / 55 Podpod 100% 20 / 20 totaltotal 34.6% 94 / 271 CC 23 SCAR 26.9
CC
23
Cov
30%
colums_information
90.0
type
20.0
index_information
20.0
CRAP 14.8
↑ Summary Pg/Index.pm »
1 package Geoffrey::Converter::Pg;
2  
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 $Geoffrey::Converter::Pg::VERSION = '0.000100';
10  
11 4 ▶use parent 'Geoffrey::Role::Converter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 Readonly::Scalar my $I_CONST_LENGTH_VALUE => 2;
14 4 Readonly::Scalar my $I_CONST_NOT_NULL_VALUE => 3;
15 4 Readonly::Scalar my $I_CONST_PRIMARY_KEY_VALUE => 4;
16 4 Readonly::Scalar my $I_CONST_DEFAULT_VALUE => 5;
17  
18 {
19  
20 package Geoffrey::Converter::Pg::Constraints;
21  
22 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new {
25 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
26 0 return bless $class->SUPER::new(
27 not_null => 'NOT NULL',
28 unique => 'UNIQUE',
29 primary_key => 'PRIMARY KEY',
30 foreign_key => 'FOREIGN KEY',
31 check => 'CHECK',
32 default => 'DEFAULT',
33 ), $class;
34 }
35 }
36 {
37  
38 4 package Geoffrey::Converter::Pg::View;
39  
40 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 0 ▶ sub add { return 'CREATE VIEW {0} AS {1}'; }
Subroutine add: not called CC=1 SCAR=6.9
43  
44 0 ▶ sub drop { return 'DROP VIEW {0}'; }
Subroutine drop: not called CC=1 SCAR=6.9
45  
46 sub list {
47 0 ▶ my ($self, $schema) = @_;
Subroutine list: not called CC=1 SCAR=6.9
48 0 require Geoffrey::Utils;
49 0 return q~SELECT * FROM pg_views WHERE schemaname NOT IN('information_schema', 'pg_catalog')~;
50 }
51 }
52 {
53  
54 4 package Geoffrey::Converter::Pg::ForeignKey;
55 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 0 ▶ sub add { return 'FOREIGN KEY ({0}) REFERENCES {1}({2})' }
Subroutine add: not called CC=1 SCAR=6.9
57  
58 sub list {
59 0 ▶ return q~SELECT
Subroutine list: not called CC=1 SCAR=6.9
60 source_table::regclass,
61 source_attr.attname AS source_column,
62 target_table::regclass,
63 target_attr.attname AS target_column
64 FROM
65 pg_attribute target_attr,
66 pg_attribute source_attr,
67 (
68 SELECT
69 source_table,
70 target_table,
71 source_constraints[i] AS source_constraints,
72 target_constraints[i] AS target_constraints
73 FROM (
74 SELECT
75 conrelid as source_table,
76 confrelid AS target_table,
77 conkey AS source_constraints,
78 confkey AS target_constraints,
79 generate_series(1, array_upper(conkey, 1)) AS i
80 FROM
81 pg_constraint
82 WHERE
83 contype = 'f'
84 ) query1
85 ) query2
86 WHERE
87 target_attr.attnum = target_constraints
88 AND target_attr.attrelid = target_table
89 AND source_attr.attnum = source_constraints
90 AND source_attr.attrelid = source_table~;
91 }
92 }
93 {
94  
95 4 package Geoffrey::Converter::Pg::Sequence;
96 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 0 ▶ sub add { return 'CREATE SEQUENCE {0} INCREMENT {1} MINVALUE {2} MAXVALUE {3} START {4} CACHE {5}' }
Subroutine add: not called CC=1 SCAR=6.9
98 0 ▶ sub nextval { return q~DEFAULT nextval('{0}'::regclass~ }
Subroutine nextval: not called CC=1 SCAR=6.9
99 }
100  
101 {
102  
103 4 package Geoffrey::Converter::Pg::PrimaryKey;
104 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 0 ▶ sub add { return 'CONSTRAINT {0} PRIMARY KEY ( {1} )'; }
Subroutine add: not called CC=1 SCAR=6.9
106  
107 sub list {
108 0 ▶ return q~SELECT
Subroutine list: not called CC=1 SCAR=6.9
109 tc.table_schema,
110 tc.table_name,
111 kc.column_name,
112 kc.constraint_name
113 FROM
114 information_schema.table_constraints tc,
115 information_schema.key_column_usage kc
116 WHERE
117 tc.constraint_type = 'PRIMARY KEY'
118 AND kc.table_name = tc.table_name
119 AND kc.table_schema = tc.table_schema
120 AND kc.constraint_name = tc.constraint_name~;
121 }
122 }
123 {
124  
125 4 package Geoffrey::Converter::Pg::UniqueIndex;
126 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 0 ▶ sub append { return 'CREATE UNIQUE INDEX IF NOT EXISTS {0} ON {1} ( {2} )'; }
Subroutine append: not called CC=1 SCAR=6.9
128 0 ▶ sub add { return 'CONSTRAINT {0} UNIQUE ( {1} )'; }
Subroutine add: not called CC=1 SCAR=6.9
129 0 ▶ sub drop { return 'DROP INDEX IF EXISTS {1}'; }
Subroutine drop: not called CC=1 SCAR=6.9
130  
131 sub list {
132 0 ▶ list => q~SELECT
Subroutine list: not called CC=1 SCAR=6.9
133 U.usename AS user_name,
134 ns.nspname AS schema_name,
135 idx.indrelid :: REGCLASS AS table_name,
136 i.relname AS index_name,
137 am.amname AS index_type,
138 idx.indkey,
139 ARRAY(
140 SELECT
141 pg_get_indexdef(idx.indexrelid, k + 1, TRUE)
142 FROM
143 generate_subscripts(idx.indkey, 1) AS k
144 ORDER BY k
145 ) AS index_keys,
146 (idx.indexprs IS NOT NULL) OR (idx.indkey::int[] @> array[0]) AS is_functional,
147 idx.indpred IS NOT NULL AS is_partial
148 FROM
149 pg_index AS idx
150 JOIN pg_class AS i ON i.oid = idx.indexrelid
151 JOIN pg_am AS am ON i.relam = am.oid
152 JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
153 JOIN pg_user AS U ON i.relowner = U.usesysid
154 WHERE
155 NOT nspname LIKE 'pg%'
156 AND NOT idx.indisprimary
157 AND idx.indisunique;~;
158 }
159 }
160 {
161  
162 4 package Geoffrey::Converter::Pg::Function;
163 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 0 ▶ sub add { return q~CREATE FUNCTION {0}({1}) RETURNS {2} AS ' {3} ' LANGUAGE {4} VOLATILE COST {5}~; }
Subroutine add: not called CC=1 SCAR=6.9
165 0 ▶ sub drop { return 'DROP FUNCTION {0} ({1})'; }
Subroutine drop: not called CC=1 SCAR=6.9
166  
167 sub list {
168 0 ▶ list => q~SELECT n.nspname as "Schema",
Subroutine list: not called CC=1 SCAR=6.9
169 p.proname as "Name",
170 p.prosrc,
171 p.procost,
172 pg_catalog.pg_get_function_result(p.oid) as result_data_type,
173 pg_catalog.pg_get_function_arguments(p.oid) as argument_data_types,
174 CASE
175 WHEN p.proisagg THEN 'agg'
176 WHEN p.proiswindow THEN 'window'
177 WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
178 ELSE
179 'normal'
180 END as
181 function_type
182 FROM
183 pg_catalog.pg_proc p
184 LEFT JOIN pg_catalog.pg_namespace n
185 ON ( n.oid = p.pronamespace )
186 WHERE
187 pg_catalog.pg_function_is_visible( p.oid )
188 AND n.nspname <> 'pg_catalog'
189 AND n.nspname <> 'information_schema'~;
190 }
191 }
192 {
193  
194 4 package Geoffrey::Converter::Pg::Trigger;
195 4 ▶ use parent 'Geoffrey::Role::ConverterType';
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 sub add {
198 0 ▶ my ($self, $options) = @_;
Subroutine add: not called CC=2 SCAR=17.9
199 0 my $s_sql_standard = <<'EOF';
200 CREATE TRIGGER {0} UPDATE OF {1} ON {2}
201 BEGIN
202 {4}
203 END
204 EOF
205 0 my $s_sql_view = <<'EOF';
206 CREATE TRIGGER {0} INSTEAD OF UPDATE OF {1} ON {2}
207 BEGIN
208 {4}
209 END
210 EOF
211 0 ▶ return $options->{for_view} ? $s_sql_view : $s_sql_standard;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'for_view'} ? : 0 0 0
212 }
213  
214 0 ▶ sub drop { return 'DROP TRIGGER IF EXISTS {1}'; }
Subroutine drop: not called CC=1 SCAR=6.9
215 }
216  
217 4 sub new {
218 2 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
219 2 my $self = $class->SUPER::new(@_);
220 2 $self->{min_version} = '9.1';
221 2 return bless $self, $class;
222 }
223  
224 sub defaults {
225 0 ▶ return {current_timestamp => 'CURRENT_TIMESTAMP', autoincrement => 'SERIAL',};
Subroutine defaults: not called CC=1 SCAR=6.9
Pod documented
226 }
227  
228 sub type {
229 0 ▶ my ($self, $hr_column_params) = @_;
Subroutine type: not called CC=4 SCAR=30.0
Pod documented
230 0 ▶ if ($hr_column_params->{default} eq 'autoincrement') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hr_column_params->{"default"} eq "autoincrement") 0 0 0
231 $hr_column_params->{type}
232 = lc $hr_column_params->{type} eq 'bigint' ? 'bigserial'
233 0 ▶ : lc $hr_column_params->{type} eq 'smallint' ? 'smallserial'
Branch: 2 branches
BranchTrueFalseTotal
lc $hr_column_params->{'type'} eq 'bigint' ? : 0 0 0
lc $hr_column_params->{'type'} eq 'smallint' ? : 0 0 0
234 : 'serial';
235 0 delete $hr_column_params->{default};
236 }
237 0 return $self->SUPER::type($hr_column_params);
238 }
239  
240 sub types {
241 return {
242 0 ▶ abstime => 'abstime',
Subroutine types: not called CC=1 SCAR=6.9
Pod documented
243 aclitem => 'aclitem',
244 bigint => 'bigint',
245 bigserial => 'bigserial',
246 bit => 'bit',
247 var_bit => 'bit varying',
248 bool => 'boolean',
249 box => 'box',
250 bytea => 'bytea',
251 char => '"char"',
252 character => 'character',
253 varchar => 'character varying',
254 cid => 'cid',
255 cidr => 'cidr',
256 circle => 'circle',
257 date => 'date',
258 daterange => 'daterange',
259 decimal => 'decimal',
260 double_precision => 'double precision',
261 gtsvector => 'gtsvector',
262 inet => 'inet',
263 int2vector => 'int2vector',
264 int4range => 'int4range',
265 int8range => 'int8range',
266 integer => 'integer',
267 interval => 'interval',
268 json => 'json',
269 line => 'line',
270 lseg => 'lseg',
271 macaddr => 'macaddr',
272 money => 'money',
273 name => 'name',
274 numeric => 'numeric',
275 numrange => 'numrange',
276 oid => 'oid',
277 oidvector => 'oidvector',
278 path => 'path',
279 pg_node_tree => 'pg_node_tree',
280 point => 'point',
281 polygon => 'polygon',
282 real => 'real',
283 refcursor => 'refcursor',
284 regclass => 'regclass',
285 regconfig => 'regconfig',
286 regdictionary => 'regdictionary',
287 regoper => 'regoper',
288 regoperator => 'regoperator',
289 regproc => 'regproc',
290 regprocedure => 'regprocedure',
291 regtype => 'regtype',
292 reltime => 'reltime',
293 serial => 'serial',
294 smallint => 'smallint',
295 smallserial => 'smallserial',
296 smgr => 'smgr',
297 text => 'text',
298 tid => 'tid',
299 timestamp => 'timestamp without time zone',
300 timestamp_tz => 'timestamp with time zone',
301 time => 'time without time zone',
302 time_tz => 'time with time zone',
303 tinterval => 'tinterval',
304 tsquery => 'tsquery',
305 tsrange => 'tsrange',
306 tstzrange => 'tstzrange',
307 tsvector => 'tsvector',
308 txid_snapshot => 'txid_snapshot',
309 uuid => 'uuid',
310 xid => 'xid',
311 xml => 'xml',
312 };
313 }
314  
315 sub select_get_table {
316 return
317 0 ▶ q~SELECT t.table_name AS table_name FROM information_schema.tables t WHERE t.table_type = 'BASE TABLE' AND t.table_schema = ? AND t.table_name = ?~;
Subroutine select_get_table: not called CC=1 SCAR=6.9
Pod documented
318 }
319  
320 sub convert_defaults {
321 0 ▶ my ($self, $params) = @_;
Subroutine convert_defaults: not called CC=2 SCAR=17.9
Pod documented
322 0 $params->{default} =~ s/^'(.*)'$/$1/;
323 0 ▶ if ($params->{type} eq 'bit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"type"} eq "bit") 0 0 0
324 0 return qq~$params->{default}::bit~;
325 }
326 0 return $params->{default};
327 }
328  
329 sub parse_default {
330 0 ▶ my ($self, $default_value) = @_;
Subroutine parse_default: not called CC=2 SCAR=17.9
Pod documented
331 0 ▶ return $1 * 1 if ($default_value =~ m/\w'(\d+)'::"\w+"/);
Branch: 1 branch
BranchTrueFalseTotal
if $default_value =~ /\w'(\d+)'::"\w+"/u 0 0 0
332 0 return $default_value;
333 }
334  
335 0 ▶sub can_create_empty_table { return 0 }
Subroutine can_create_empty_table: not called CC=1 SCAR=6.9
Pod documented
336  
337 sub colums_information {
338 0 ▶ my ($self, $ar_raw_data) = @_;
Subroutine colums_information: not called CC=9 SCAR=45.0
Pod documented
339 0 ▶ return [] if scalar @{$ar_raw_data} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$ar_raw_data;} == 0 0 0 0
340 0 my $table_row = shift @{$ar_raw_data};
341 0 $table_row->{sql} =~ s/^.*(CREATE|create) .*\(//g;
342 0 my $columns = [];
343 0 for (split m/,/, $table_row->{sql}) {
344 0 s/^\s*(.*)\s*$/$1/g;
345 0 my $rx_not_null = 'NOT NULL';
346 0 my $rx_primary_key = 'PRIMARY KEY';
347 0 my $rx_default = 'SERIAL|DEFAULT';
348 0 my $rx_column_values = qr/($rx_not_null)*\s($rx_primary_key)*.*($rx_default \w{1,})*/;
349 0 my @column = m/^(\w+)\s([[:upper:]]+)(\(\d*\))*\s$rx_column_values$/;
350 0 ▶ next if scalar @column == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @column == 0 0 0 0
351 0 ▶ $column[$I_CONST_LENGTH_VALUE] =~ s/([\(\)])//g if $column[$I_CONST_LENGTH_VALUE];
Branch: 1 branch
BranchTrueFalseTotal
if $column[$I_CONST_LENGTH_VALUE] 0 0 0
352 0 ▶ push @{$columns},
Branch: 4 branches
BranchTrueFalseTotal
$column[$I_CONST_LENGTH_VALUE] ? : 0 0 0
$column[$I_CONST_NOT_NULL_VALUE] ? : 0 0 0
$column[$I_CONST_PRIMARY_KEY_VALUE] ? : 0 0 0
$column[$I_CONST_DEFAULT_VALUE] ? : 0 0 0
353 {
354 name => $column[0],
355 type => $column[1],
356 ($column[$I_CONST_LENGTH_VALUE] ? (length => $column[$I_CONST_LENGTH_VALUE]) : ()),
357 ($column[$I_CONST_NOT_NULL_VALUE] ? (not_null => $column[$I_CONST_NOT_NULL_VALUE]) : ()),
358 ($column[$I_CONST_PRIMARY_KEY_VALUE] ? (primary_key => $column[$I_CONST_PRIMARY_KEY_VALUE]) : ()),
359 ($column[$I_CONST_DEFAULT_VALUE] ? (default => $column[$I_CONST_DEFAULT_VALUE]) : ()),
360 };
361 }
362 0 return $columns;
363 }
364  
365 sub index_information {
366 0 ▶ my ($self, $ar_raw_data) = @_;
Subroutine index_information: not called CC=4 SCAR=30.0
Pod documented
367 0 my @mapped = ();
368 0 for (@{$ar_raw_data}) {
369 0 ▶ next if !$_->{sql};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"sql"} 0 0 0
370 0 my ($s_columns) = $_->{sql} =~ m/\((.*)\)$/;
371 0 my @columns = split m/,/, $s_columns;
372 0 s/^\s+|\s+$//g for @columns;
373 0 push @mapped, {name => $_->{name}, table => $_->{tbl_name}, columns => \@columns};
374 }
375 0 return \@mapped;
376 }
377  
378 sub view_information {
379 0 ▶ my ($self, $ar_raw_data) = @_;
Subroutine view_information: not called CC=2 SCAR=17.9
Pod documented
380 0 ▶ return [] unless $ar_raw_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $ar_raw_data 0 0 0
381 0 return [map { {name => $_->{name}, sql => $_->{sql}} } @{$ar_raw_data}];
382 }
383  
384 sub constraints {
385 0 ▶ return shift->_get_value('constraints', 'Geoffrey::Converter::Pg::Constraints', 1);
Subroutine constraints: not called CC=1 SCAR=6.9
Pod documented
386 }
387  
388 sub index {
389 0 ▶ my ($self, $new_value) = @_;
Subroutine index: not called CC=2 SCAR=17.9
Pod documented
390 0 ▶ $self->{index} = $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 0 0 0
391 0 return $self->_get_value('index', 'Geoffrey::Converter::Pg::Index');
392 }
393  
394 sub table {
395 0 ▶ return shift->_get_value('table', 'Geoffrey::Converter::Pg::Tables');
Subroutine table: not called CC=1 SCAR=6.9
Pod documented
396 }
397  
398 sub view {
399 0 ▶ return shift->_get_value('view', 'Geoffrey::Converter::Pg::View', 1);
Subroutine view: not called CC=1 SCAR=6.9
Pod documented
400 }
401  
402 sub foreign_key {
403 0 ▶ my ($self, $new_value) = @_;
Subroutine foreign_key: not called CC=2 SCAR=17.9
Pod documented
404 0 ▶ $self->{foreign_key} = $new_value if defined $new_value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_value 0 0 0
405 0 return $self->_get_value('foreign_key', 'Geoffrey::Converter::Pg::ForeignKey', 1);
406 }
407  
408 sub trigger {
409 0 ▶ return shift->_get_value('trigger', 'Geoffrey::Converter::Pg::Trigger', 1);
Subroutine trigger: not called CC=1 SCAR=6.9
Pod documented
410 }
411  
412 sub primary_key {
413 0 ▶ return shift->_get_value('primary_key', 'Geoffrey::Converter::Pg::PrimaryKey', 1);
Subroutine primary_key: not called CC=1 SCAR=6.9
Pod documented
414 }
415  
416 sub unique {
417 0 ▶ return shift->_get_value('unique', 'Geoffrey::Converter::Pg::UniqueIndex', 1);
Subroutine unique: not called CC=1 SCAR=6.9
Pod documented
418 }
419  
420 sub sequence {
421 0 ▶ return shift->_get_value('sequence', 'Geoffrey::Converter::Pg::Sequence', 1);
Subroutine sequence: not called CC=1 SCAR=6.9
Pod documented
422 }
423  
424 sub _get_value {
425 0 ▶ my ($self, $key, $s_package_name, $b_ignore_require) = @_;
Subroutine _get_value: not called CC=2 SCAR=17.9
426 0 ▶ $self->{$key} //= $self->_set_value($key, $s_package_name, $b_ignore_require);
Condition0%0/3
$self->{$key} //= $self->_set_value($key, $s_package_name, $b_ignore_require)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$key} //= $self->_set_value($key, $s_package_name, $b_ignore_require)
$self->{$key} $self->_set_value($key, $s_package_name, $b_ignore_require)
Truth table0%0/3
A //= B
A= $self->{$key}B= $self->_set_value($key, $s_package_name, $b_ignore_require)
ABresult
1X1
011
000
427 0 return $self->{$key};
428 }
429  
430 sub _set_value {
431 0 ▶ my ($self, $key, $s_package_name, $b_ignore_require) = @_;
Subroutine _set_value: not called CC=2 SCAR=17.9
432 0 require Geoffrey::Utils;
433 0 ▶ $self->{$key} = $b_ignore_require ? $s_package_name->new(@_) : Geoffrey::Utils::obj_from_name($s_package_name);
Branch: 1 branch
BranchTrueFalseTotal
$b_ignore_require ? : 0 0 0
434 0 return $self->{$key};
435  
436 }
437  
438 4 1; # End of Geoffrey::Converter::Pg
439  
440 __END__
↑ Summary Pg/Index.pm »