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