| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Table::Indices; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact::Table::Lazy'; | ||
| 8 | |||
| 9 | use FusqlFS::Backend::PgSQL::Table::Struct; | ||
| 10 | |||
| 11 | sub init | ||
| 12 | { | ||
| 13 | my $self = shift; | ||
| 14 | |||
| 15 | $self->{rename_expr} = 'ALTER INDEX "%s" RENAME TO "%s"'; | ||
| 16 | $self->{drop_expr} = 'DROP INDEX "%s"'; | ||
| 17 | $self->{create_expr} = 'CREATE %s INDEX "%s" ON "%s" (%s)'; | ||
| 18 | |||
| 19 | $self->{list_expr} = $self->expr("SELECT (SELECT c1.relname FROM pg_catalog.pg_class as c1 WHERE c1.oid = indexrelid) as Index_name | ||
| 20 | FROM pg_catalog.pg_index | ||
| 21 | WHERE indrelid = (SELECT oid FROM pg_catalog.pg_class as c WHERE c.relname = ? AND relkind = 'r')"); | ||
| 22 | $self->{get_expr} = $self->expr("SELECT pg_get_indexdef(indexrelid, 0, true) AS \"create.sql\", | ||
| 23 | indisunique as \".unique\", indisprimary as \".primary\", indkey as \".order\" | ||
| 24 | FROM pg_catalog.pg_index | ||
| 25 | WHERE indexrelid = (SELECT oid FROM pg_catalog.pg_class as c WHERE c.relname = ? AND relkind = 'i')"); | ||
| 26 | |||
| 27 | $self->{template} = { '.order' => [] }; | ||
| 28 | } | ||
| 29 | |||
| 30 | =begin testing get | ||
| 31 | |||
| 32 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_table_pkey'), { | ||
| 33 | '.primary' => 1, | ||
| 34 | '.unique' => 1, | ||
| 35 | '.order' => [ 'id' ], | ||
| 36 | 'id' => \'tables/fusqlfs_table/struct/id', | ||
| 37 | 'create.sql' => 'CREATE UNIQUE INDEX fusqlfs_table_pkey ON fusqlfs_table USING btree (id)', | ||
| 38 | }; | ||
| 39 | is $_tobj->get('fusqlfs_table', 'fusqlfs_index'), undef; | ||
| 40 | |||
| 41 | =end testing | ||
| 42 | =cut | ||
| 43 | sub get | ||
| 44 | { | ||
| 45 | my $self = shift; | ||
| 46 | my ($table, $name) = @_; | ||
| 47 | |||
| 48 | unless ($self->SUPER::get($table, $name)) | ||
| 49 | { | ||
| 50 | my $result = $self->one_row($self->{get_expr}, $name); | ||
| 51 | return unless $result; | ||
| 52 | if ($result->{'.order'}) | ||
| 53 | { | ||
| 54 | my @fields = @{FusqlFS::Backend::PgSQL::Table::Struct->new()->list($table)}; | ||
| 55 | $result->{'.order'} = [ map { $fields[$_-1] } split / /, $result->{'.order'} ]; | ||
| 56 | $result->{$_} = \"tables/$table/struct/$_" foreach @{$result->{'.order'}}; | ||
| 57 | } | ||
| 58 | delete $result->{'.unique'} unless $result->{'.unique'}; | ||
| 59 | delete $result->{'.primary'} unless $result->{'.primary'}; | ||
| 60 | return $result; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | =begin testing list | ||
| 65 | |||
| 66 | cmp_set $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey' ]; | ||
| 67 | |||
| 68 | =end testing | ||
| 69 | =cut | ||
| 70 | sub list | ||
| 71 | { | ||
| 72 | my $self = shift; | ||
| 73 | my ($table) = @_; | ||
| 74 | my @list = @{$self->SUPER::list($table)}; | ||
| 75 | return [ (@{$self->all_col($self->{list_expr}, $table)}, @list) ]; | ||
| 76 | } | ||
| 77 | |||
| 78 | =begin testing drop after rename | ||
| 79 | |||
| 80 | isnt $_tobj->drop('fusqlfs_table', 'new_fusqlfs_index'), undef; | ||
| 81 | is $_tobj->get('fusqlfs_table', 'new_fusqlfs_index'), undef; | ||
| 82 | is_deeply $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey' ]; | ||
| 83 | |||
| 84 | =end testing | ||
| 85 | =cut | ||
| 86 | sub drop | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | my ($table, $name) = @_; | ||
| 90 | $self->SUPER::drop($table, $name) or $self->do($self->{drop_expr}, [$name]); | ||
| 91 | } | ||
| 92 | |||
| 93 | =begin testing store after create | ||
| 94 | |||
| 95 | ok $_tobj->store('fusqlfs_table', 'fusqlfs_index', $new_index); | ||
| 96 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_index'), $new_index; | ||
| 97 | is_deeply [ sort(@{$_tobj->list('fusqlfs_table')}) ], [ sort('fusqlfs_table_pkey', 'fusqlfs_index') ]; | ||
| 98 | |||
| 99 | =end testing | ||
| 100 | =cut | ||
| 101 | sub store | ||
| 102 | { | ||
| 103 | my $self = shift; | ||
| 104 | my ($table, $name, $data) = @_; | ||
| 105 | |||
| 106 | $self->drop($table, $name); | ||
| 107 | my $fields = $self->parse_fields($data); | ||
| 108 | my $unique = defined $data->{'.unique'}? 'UNIQUE': ''; | ||
| 109 | $self->do($self->{create_expr}, [$unique, $name, $table, $fields]); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub parse_fields | ||
| 113 | { | ||
| 114 | my $self = shift; | ||
| 115 | my ($data) = @_; | ||
| 116 | my @order = grep { exists $data->{$_} } @{$data->{'.order'}}; | ||
| 117 | my @fields = grep { !/^\./ && $_ ne 'create.sql' } keys %$data; | ||
| 118 | |||
| 119 | my %order = map { $_ => 1 } @order; | ||
| 120 | foreach (@fields) | ||
| 121 | { | ||
| 122 | push @order, $_ unless exists $order{$_}; | ||
| 123 | } | ||
| 124 | my $fields = '"'.join('", "', @order).'"'; | ||
| 125 | |||
| 126 | return $fields; | ||
| 127 | } | ||
| 128 | |||
| 129 | =begin testing create after get list | ||
| 130 | |||
| 131 | ok $_tobj->create('fusqlfs_table', 'fusqlfs_index'); | ||
| 132 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_index'), { | ||
| 133 | '.order' => [], | ||
| 134 | }; | ||
| 135 | is_deeply $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey', 'fusqlfs_index' ]; | ||
| 136 | |||
| 137 | =end testing | ||
| 138 | =cut | ||
| 139 | |||
| 140 | =begin testing rename after store | ||
| 141 | |||
| 142 | isnt $_tobj->rename('fusqlfs_table', 'fusqlfs_index', 'new_fusqlfs_index'), undef; | ||
| 143 | is_deeply $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey', 'new_fusqlfs_index' ]; | ||
| 144 | is $_tobj->get('fusqlfs_table', 'fusqlfs_index'), undef; | ||
| 145 | |||
| 146 | $new_index->{'create.sql'} =~ s/INDEX fusqlfs_index ON/INDEX new_fusqlfs_index ON/; | ||
| 147 | is_deeply $_tobj->get('fusqlfs_table', 'new_fusqlfs_index'), $new_index; | ||
| 148 | |||
| 149 | =end testing | ||
| 150 | =cut | ||
| 151 | sub rename | ||
| 152 | { | ||
| 153 | my $self = shift; | ||
| 154 | my ($table, $name, $newname) = @_; | ||
| 155 | $self->SUPER::rename($table, $name, $newname) or $self->do($self->{rename_expr}, [$name, $newname]); | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | __END__ |