File Coverage

blib/lib/Geoffrey/Converter/Pg/Index.pm
Criterion Covered Total %
statement 36 36 100.0
branch 10 10 100.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package Geoffrey::Converter::Pg::Index;
2              
3 2     2   353728 use utf8;
  2         359  
  2         16  
4 2     2   108 use 5.016;
  2         7  
5 2     2   9 use strict;
  2         3  
  2         89  
6 2     2   10 use warnings;
  2         3  
  2         224  
7              
8             $Geoffrey::Converter::Pg::Index::VERSION = '0.000204';
9              
10 2     2   39 use parent 'Geoffrey::Role::ConverterType';
  2         6  
  2         16  
11              
12             sub add {
13 6     6 1 1367 my ( $self, $params ) = @_;
14 6 100       17 if ( !$params ) {
15 1         4323 require Geoffrey::Exception::General;
16 1         2061 Geoffrey::Exception::General::throw_no_params();
17             }
18 5 100       11 if ( !$params->{table} ) {
19 1         6 require Geoffrey::Exception::RequiredValue;
20 1         4 Geoffrey::Exception::RequiredValue::throw_table_name();
21             }
22 4 100       9 if ( !$params->{column} ) {
23 1         6 require Geoffrey::Exception::RequiredValue;
24 1         50 Geoffrey::Exception::RequiredValue::throw_refcolumn_missing();
25             }
26 3         554 require Ref::Util;
27 3         3616 require Geoffrey::Utils;
28             return Geoffrey::Utils::replace_spare(
29             q~CREATE INDEX {0} ON {1} ({2})~,
30             [
31             Geoffrey::Utils::add_name(
32             {
33             prefix => 'ix',
34             name => $params->{name},
35             context => $params->{table}
36             }
37             ),
38             $params->{table},
39             (
40             join ', ', Ref::Util::is_arrayref( $params->{column} )
41 1         14 ? @{ $params->{column} }
42             : ( $params->{column} )
43 3 100       17 )
44             ]
45             );
46             }
47              
48             sub drop {
49 2     2 1 2997 my ( $self, $name ) = @_;
50 2 100       7 if ( !$name ) {
51 1         1559 require Geoffrey::Exception::RequiredValue;
52 1         4802 Geoffrey::Exception::RequiredValue::throw_index_name();
53             }
54 1         6 return qq~DROP INDEX $name~;
55             }
56              
57             sub list {
58 1     1 1 2 my ( $self, $schema ) = @_;
59 1         3157 require Geoffrey::Utils;
60 1         6826 return q~SELECT
61             U.usename AS user_name,
62             ns.nspname AS schema_name,
63             idx.indrelid :: REGCLASS AS table_name,
64             i.relname AS index_name,
65             am.amname AS index_type,
66             idx.indkey,
67             ARRAY(
68             SELECT
69             pg_get_indexdef(idx.indexrelid, k + 1, TRUE)
70             FROM
71             generate_subscripts(idx.indkey, 1) AS k
72             ORDER BY k
73             ) AS index_keys,
74             (idx.indexprs IS NOT NULL) OR (idx.indkey::int[] @> array[0]) AS is_functional,
75             idx.indpred IS NOT NULL AS is_partial
76             FROM
77             pg_index AS idx
78             JOIN pg_class AS i ON i.oid = idx.indexrelid
79             JOIN pg_am AS am ON i.relam = am.oid
80             JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
81             JOIN pg_user AS U ON i.relowner = U.usesysid
82             WHERE
83             NOT nspname LIKE 'pg%'
84             AND NOT idx.indisprimary
85             AND NOT idx.indisunique~;
86             }
87              
88             1; # End of Geoffrey::Converter::Pg::Index
89              
90             __END__