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   1857 use utf8;
  2         17  
  2         11  
4 2     2   82 use 5.016;
  2         7  
5 2     2   11 use strict;
  2         4  
  2         38  
6 2     2   9 use warnings;
  2         4  
  2         136  
7              
8             $Geoffrey::Converter::Pg::Index::VERSION = '0.000203';
9              
10 2     2   416 use parent 'Geoffrey::Role::ConverterType';
  2         318  
  2         13  
11              
12             sub add {
13 6     6 1 1417 my ( $self, $params ) = @_;
14 6 100       18 if ( !$params ) {
15 1         476 require Geoffrey::Exception::General;
16 1         2399 Geoffrey::Exception::General::throw_no_params();
17             }
18 5 100       15 if ( !$params->{table} ) {
19 1         8 require Geoffrey::Exception::RequiredValue;
20 1         4 Geoffrey::Exception::RequiredValue::throw_table_name();
21             }
22 4 100       23 if ( !$params->{column} ) {
23 1         12 require Geoffrey::Exception::RequiredValue;
24 1         4 Geoffrey::Exception::RequiredValue::throw_refcolumn_missing();
25             }
26 3         534 require Ref::Util;
27 3         1805 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         11 ? @{ $params->{column} }
42             : ( $params->{column} )
43 3 100       26 )
44             ]
45             );
46             }
47              
48             sub drop {
49 2     2 1 2946 my ( $self, $name ) = @_;
50 2 100       8 if ( !$name ) {
51 1         520 require Geoffrey::Exception::RequiredValue;
52 1         5367 Geoffrey::Exception::RequiredValue::throw_index_name();
53             }
54 1         9 return qq~DROP INDEX $name~;
55             }
56              
57             sub list {
58 1     1 1 3 my ( $self, $schema ) = @_;
59 1         525 require Geoffrey::Utils;
60 1         5475 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__