| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Table::Struct; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | sub init | ||
| 10 | { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | $self->{list_expr} = $self->expr("SELECT attname FROM pg_catalog.pg_attribute as a | ||
| 14 | WHERE attrelid = (SELECT oid FROM pg_catalog.pg_class as c WHERE c.relname = ? AND relkind = 'r') AND attnum > 0 | ||
| 15 | ORDER BY attnum"); | ||
| 16 | $self->{get_expr} = $self->expr("SELECT pg_catalog.format_type(atttypid, atttypmod) AS type, | ||
| 17 | NOT attnotnull as nullable, | ||
| 18 | CASE WHEN atthasdef THEN | ||
| 19 | (SELECT pg_catalog.pg_get_expr(adbin, adrelid) FROM pg_attrdef as d | ||
| 20 | WHERE adrelid = attrelid AND adnum = attnum) | ||
| 21 | ELSE NULL END AS default, | ||
| 22 | attndims AS dimensions, | ||
| 23 | attnum as order | ||
| 24 | FROM pg_catalog.pg_attribute as a | ||
| 25 | WHERE attrelid = (SELECT oid FROM pg_catalog.pg_class as c WHERE c.relname = ? AND relkind = 'r') | ||
| 26 | AND attname = ?"); | ||
| 27 | |||
| 28 | $self->{drop_expr} = 'ALTER TABLE "%s" DROP COLUMN "%s"'; | ||
| 29 | $self->{create_expr} = 'ALTER TABLE "%s" ADD COLUMN "%s" INTEGER NOT NULL DEFAULT \'0\''; | ||
| 30 | $self->{rename_expr} = 'ALTER TABLE "%s" RENAME COLUMN "%s" TO "%s"'; | ||
| 31 | |||
| 32 | $self->{store_default_expr} = 'ALTER TABLE "%s" ALTER COLUMN "%s" SET DEFAULT %s'; | ||
| 33 | $self->{drop_default_expr} = 'ALTER TABLE "%s" ALTER COLUMN "%s" DROP DEFAULT'; | ||
| 34 | $self->{set_nullable_expr} = 'ALTER TABLE "%s" ALTER COLUMN "%s" DROP NOT NULL'; | ||
| 35 | $self->{drop_nullable_expr} = 'ALTER TABLE "%s" ALTER COLUMN "%s" SET NOT NULL'; | ||
| 36 | $self->{store_type_expr} = 'ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s'; | ||
| 37 | } | ||
| 38 | |||
| 39 | =begin testing list | ||
| 40 | |||
| 41 | is $_tobj->list('unknown'), undef; | ||
| 42 | cmp_set $_tobj->list('fusqlfs_table'), [ 'id' ]; | ||
| 43 | |||
| 44 | =end testing | ||
| 45 | =cut | ||
| 46 | sub list | ||
| 47 | { | ||
| 48 | my $self = shift; | ||
| 49 | my ($table) = @_; | ||
| 50 | my $list = $self->all_col($self->{list_expr}, $table); | ||
| 51 | return unless @$list; | ||
| 52 | return $list; | ||
| 53 | } | ||
| 54 | |||
| 55 | =begin testing get | ||
| 56 | |||
| 57 | is $_tobj->get('fusqlfs_table', 'unknown'), undef; | ||
| 58 | is_deeply $_tobj->get('fusqlfs_table', 'id'), { | ||
| 59 | default => "nextval('fusqlfs_table_id_seq'::regclass)", | ||
| 60 | dimensions => 0, | ||
| 61 | nullable => 0, | ||
| 62 | order => 1, | ||
| 63 | type => 'integer', | ||
| 64 | }; | ||
| 65 | |||
| 66 | =end testing | ||
| 67 | =cut | ||
| 68 | sub get | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | my ($table, $name) = @_; | ||
| 72 | my $result = $self->one_row($self->{get_expr}, $table, $name); | ||
| 73 | return $self->dump($result); | ||
| 74 | } | ||
| 75 | |||
| 76 | =begin testing drop after rename | ||
| 77 | |||
| 78 | isnt $_tobj->drop('fusqlfs_table', 'new_field'), undef; | ||
| 79 | is $_tobj->get('fusqlfs_table', 'new_field'), undef; | ||
| 80 | is_deeply $_tobj->list('fusqlfs_table'), [ 'id', '........pg.dropped.2........' ]; | ||
| 81 | |||
| 82 | =end testing | ||
| 83 | =cut | ||
| 84 | sub drop | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | my ($table, $name) = @_; | ||
| 88 | $self->do($self->{drop_expr}, [$table, $name]); | ||
| 89 | } | ||
| 90 | |||
| 91 | =begin testing create after get list | ||
| 92 | |||
| 93 | isnt $_tobj->create('fusqlfs_table', 'field'), undef; | ||
| 94 | is_deeply $_tobj->list('fusqlfs_table'), [ 'id', 'field' ]; | ||
| 95 | is_deeply $_tobj->get('fusqlfs_table', 'field'), { | ||
| 96 | default => 0, | ||
| 97 | dimensions => 0, | ||
| 98 | nullable => 0, | ||
| 99 | order => 2, | ||
| 100 | type => 'integer', | ||
| 101 | }; | ||
| 102 | |||
| 103 | =end testing | ||
| 104 | =cut | ||
| 105 | sub create | ||
| 106 | { | ||
| 107 | my $self = shift; | ||
| 108 | my ($table, $name) = @_; | ||
| 109 | $self->do($self->{create_expr}, [$table, $name]); | ||
| 110 | } | ||
| 111 | |||
| 112 | =begin testing rename after store | ||
| 113 | |||
| 114 | isnt $_tobj->rename('fusqlfs_table', 'field', 'new_field'), undef; | ||
| 115 | is_deeply $_tobj->list('fusqlfs_table'), [ 'id', 'new_field' ]; | ||
| 116 | is $_tobj->get('fusqlfs_table', 'field'), undef; | ||
| 117 | is_deeply $_tobj->get('fusqlfs_table', 'new_field'), $new_field; | ||
| 118 | |||
| 119 | =end testing | ||
| 120 | =cut | ||
| 121 | sub rename | ||
| 122 | { | ||
| 123 | my $self = shift; | ||
| 124 | my ($table, $name, $newname) = @_; | ||
| 125 | $self->do($self->{rename_expr}, [$table, $name, $newname]); | ||
| 126 | } | ||
| 127 | |||
| 128 | =begin testing store after create | ||
| 129 | |||
| 130 | isnt $_tobj->store('fusqlfs_table', 'field', $new_field), undef; | ||
| 131 | is_deeply $_tobj->get('fusqlfs_table', 'field'), $new_field; | ||
| 132 | |||
| 133 | =end testing | ||
| 134 | =cut | ||
| 135 | sub store | ||
| 136 | { | ||
| 137 | my $self = shift; | ||
| 138 | my ($table, $name, $data) = @_; | ||
| 139 | $data = $self->validate($data, { | ||
| 140 | type => '', | ||
| 141 | dimensions => qr/^\d+$/, | ||
| 142 | default => '', | ||
| 143 | nullable => '', | ||
| 144 | }) or return; | ||
| 145 | |||
| 146 | my $newtype = $data->{'type'}; | ||
| 147 | $newtype =~ s/(\[\])+$//; | ||
| 148 | $newtype .= '[]' x $data->{'dimensions'}; | ||
| 149 | |||
| 150 | my $using = $data->{'using'} || undef; | ||
| 151 | $newtype .= " USING $using" if $using; | ||
| 152 | |||
| 153 | $self->do($self->{store_type_expr}, [$table, $name, $newtype]); | ||
| 154 | |||
| 155 | if (defined $data->{'default'}) { | ||
| 156 | $self->do($self->{store_default_expr}, [$table, $name, $data->{'default'}]); | ||
| 157 | } else { | ||
| 158 | $self->do($self->{drop_default_expr}, [$table, $name]); | ||
| 159 | } | ||
| 160 | $self->do($self->{$data->{'nullable'}? 'set_nullable_expr': 'drop_nullable_expr'}, [$table, $name]); | ||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | __END__ |