| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Table::Constraints; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact::Table::Lazy'; | ||
| 8 | |||
| 9 | sub init | ||
| 10 | { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | $self->{get_expr} = $self->expr('SELECT pg_catalog.pg_get_constraintdef(co.oid, true) AS "content.sql", co.contype AS ".type" FROM pg_catalog.pg_constraint co | ||
| 14 | JOIN pg_catalog.pg_class AS cl ON (cl.oid = co.conrelid) WHERE cl.relname = ? AND co.conname = ?'); | ||
| 15 | $self->{list_expr} = $self->expr('SELECT co.conname FROM pg_catalog.pg_constraint AS co | ||
| 16 | JOIN pg_catalog.pg_class AS cl ON (cl.oid = co.conrelid) WHERE cl.relname = ?'); | ||
| 17 | |||
| 18 | $self->{drop_expr} = 'ALTER TABLE "%s" DROP CONSTRAINT "%s"'; | ||
| 19 | $self->{store_expr} = 'ALTER TABLE "%s" ADD CONSTRAINT "%s" %s'; | ||
| 20 | |||
| 21 | $self->{template} = { 'content.sql' => "" }; | ||
| 22 | } | ||
| 23 | |||
| 24 | =begin testing store after create | ||
| 25 | |||
| 26 | isnt $_tobj->store('fusqlfs_table', 'fusqlfs_constraint', $new_constraint), undef; | ||
| 27 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_constraint'), $new_constraint; | ||
| 28 | |||
| 29 | =end testing | ||
| 30 | =cut | ||
| 31 | sub store | ||
| 32 | { | ||
| 33 | my $self = shift; | ||
| 34 | my ($table, $name, $data) = @_; | ||
| 35 | $self->drop($table, $name); | ||
| 36 | $self->do($self->{store_expr}, [$table, $name, $data->{'content.sql'}]); | ||
| 37 | } | ||
| 38 | |||
| 39 | =begin testing rename after store | ||
| 40 | |||
| 41 | isnt $_tobj->rename('fusqlfs_table', 'fusqlfs_constraint', 'new_fusqlfs_constraint'), undef; | ||
| 42 | is $_tobj->get('fusqlfs_table', 'fusqlfs_constraint'), undef; | ||
| 43 | is_deeply $_tobj->get('fusqlfs_table', 'new_fusqlfs_constraint'), $new_constraint; | ||
| 44 | is_deeply [ sort(@{$_tobj->list('fusqlfs_table')}) ], [ sort('fusqlfs_table_pkey', 'new_fusqlfs_constraint') ]; | ||
| 45 | |||
| 46 | =end testing | ||
| 47 | =cut | ||
| 48 | sub rename | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | my ($table, $name, $newname) = @_; | ||
| 52 | unless ($self->SUPER::rename($table, $name, $newname)) | ||
| 53 | { | ||
| 54 | my $data = $self->get($table, $name); | ||
| 55 | $self->drop($table, $name); | ||
| 56 | $self->store($table, $newname, $data); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | =begin testing create after get list | ||
| 61 | |||
| 62 | isnt $_tobj->create('fusqlfs_table', 'fusqlfs_constraint'), undef; | ||
| 63 | isnt $_tobj->get('fusqlfs_table', 'fusqlfs_constraint'), $_tobj->{template}; | ||
| 64 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_constraint'), $_tobj->{template}; | ||
| 65 | is_deeply [ sort(@{$_tobj->list('fusqlfs_table')}) ], [ sort('fusqlfs_table_pkey', 'fusqlfs_constraint') ]; | ||
| 66 | |||
| 67 | =end testing | ||
| 68 | =cut | ||
| 69 | |||
| 70 | =begin testing drop after rename | ||
| 71 | |||
| 72 | isnt $_tobj->drop('fusqlfs_table', 'new_fusqlfs_constraint'), undef; | ||
| 73 | is $_tobj->get('fusqlfs_table', 'new_fusqlfs_constraint'), undef; | ||
| 74 | is_deeply $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey' ]; | ||
| 75 | |||
| 76 | =end testing | ||
| 77 | =cut | ||
| 78 | sub drop | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | my ($table, $name) = @_; | ||
| 82 | $self->SUPER::drop($table, $name) or $self->do($self->{drop_expr}, [$table, $name]); | ||
| 83 | } | ||
| 84 | |||
| 85 | =begin testing get | ||
| 86 | |||
| 87 | is $_tobj->get('fusqlfs_table', 'unknown'), undef; | ||
| 88 | is_deeply $_tobj->get('fusqlfs_table', 'fusqlfs_table_pkey'), { 'content.sql' => 'PRIMARY KEY (id)', '.type' => 'p' }; | ||
| 89 | |||
| 90 | =end testing | ||
| 91 | =cut | ||
| 92 | sub get | ||
| 93 | { | ||
| 94 | my $self = shift; | ||
| 95 | my ($table, $name) = @_; | ||
| 96 | unless ($self->SUPER::get($table, $name)) | ||
| 97 | { | ||
| 98 | my $data = $self->one_row($self->{get_expr}, $table, $name); | ||
| 99 | return unless $data; | ||
| 100 | if ($data->{".type"} eq 'f') | ||
| 101 | { | ||
| 102 | my ($myfields, $table, $herfields) = ($data->{'content.sql'} =~ /KEY \((.+?)\) REFERENCES (.+?)\((.+?)\)/); | ||
| 103 | my @myfields = split /,/, $myfields; | ||
| 104 | my @herfields = split /,/, $herfields; | ||
| 105 | foreach (0..$#myfields) | ||
| 106 | { | ||
| 107 | $data->{$myfields[$_]} = \"tables/$table/struct/$herfields[$_]"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | return $data; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | =begin testing list | ||
| 115 | |||
| 116 | #is $_tobj->list('unknown'), undef; | ||
| 117 | cmp_set $_tobj->list('fusqlfs_table'), [ 'fusqlfs_table_pkey' ]; | ||
| 118 | |||
| 119 | =end testing | ||
| 120 | =cut | ||
| 121 | sub list | ||
| 122 | { | ||
| 123 | my $self = shift; | ||
| 124 | my ($table) = @_; | ||
| 125 | my @list = @{$self->SUPER::list($table)}; | ||
| 126 | return [ @{$self->all_col($self->{list_expr}, $table)}, @list ]; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | |||
| 131 | __END__ |