| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Tables; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Tables - FusqlFS PostgreSQL database tables interface | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use FusqlFS::Backend::PgSQL::Tables; | ||
| 16 | |||
| 17 | my $tables = FusqlFS::Backend::PgSQL::Tables->new(); | ||
| 18 | my $list = $tables->list(); | ||
| 19 | $tables->create('sometable'); | ||
| 20 | my $table = $tables->get('sometable'); | ||
| 21 | $tables->drop('sometable'); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This is FusqlFS an interface to PostgreSQL database tables. This class is not | ||
| 26 | to be used by itself. | ||
| 27 | |||
| 28 | This class provides a view of a set of different table's artifacts like | ||
| 29 | indices, struct (fields description), data rows/records, constraints, triggers | ||
| 30 | etc. | ||
| 31 | |||
| 32 | See L<FusqlFS::Artifact> for description of interface methods, | ||
| 33 | L<FusqlFS::Backend> to learn more on backend initialization and | ||
| 34 | L<FusqlFS::Backend::Base> for more info on database backends writing. | ||
| 35 | |||
| 36 | =head1 EXPOSED STRUCTURE | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =item F<./indices> | ||
| 41 | |||
| 42 | Table's indices, see L<FusqlFS::Backend::PgSQL::Table::Indices> for details. | ||
| 43 | |||
| 44 | =item F<./struct> | ||
| 45 | |||
| 46 | Table's structure, see L<FusqlFS::Backend::PgSQL::Table::Struct> for details. | ||
| 47 | |||
| 48 | =item F<./constraints> | ||
| 49 | |||
| 50 | Table's constraints, see L<FusqlFS::Backend::PgSQL::Table::Constraints> for details. | ||
| 51 | |||
| 52 | =item F<./triggers> | ||
| 53 | |||
| 54 | Table's triggers, see L<FusqlFS::Backend::PgSQL::Table::Triggers> for details. | ||
| 55 | |||
| 56 | =item F<./owner> | ||
| 57 | |||
| 58 | Symlink to table's owner role in F<../../roles>. | ||
| 59 | |||
| 60 | =item F<./acl> | ||
| 61 | |||
| 62 | Table's ACL with permissions given to different roles. See | ||
| 63 | L<FusqlFS::Backend::PgSQL::Role::Acl> for details. | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | use FusqlFS::Backend::PgSQL::Role::Owner; | ||
| 70 | use FusqlFS::Backend::PgSQL::Role::Acl; | ||
| 71 | |||
| 72 | sub init | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | $self->{rename_expr} = 'ALTER TABLE "%s" RENAME TO "%s"'; | ||
| 76 | $self->{drop_expr} = 'DROP TABLE "%s"'; | ||
| 77 | $self->{create_expr} = 'CREATE TABLE "%s" (id serial, PRIMARY KEY (id))'; | ||
| 78 | |||
| 79 | $self->{list_expr} = $self->expr("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public'"); | ||
| 80 | $self->{get_expr} = $self->expr("SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename = ?"); | ||
| 81 | |||
| 82 | $self->extend( | ||
| 83 | $self->autopackages( | ||
| 84 | 'indices', | ||
| 85 | 'struct', | ||
| 86 | 'data', | ||
| 87 | 'constraints', | ||
| 88 | 'triggers' | ||
| 89 | ), { | ||
| 90 | owner => FusqlFS::Backend::PgSQL::Role::Owner->new('r'), | ||
| 91 | acl => FusqlFS::Backend::PgSQL::Role::Acl->new('r'), | ||
| 92 | } | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | =begin testing get | ||
| 97 | |||
| 98 | is $_tobj->get('fusqlfs_table'), undef, 'Test table doesn\'t exist'; | ||
| 99 | |||
| 100 | =end testing | ||
| 101 | =cut | ||
| 102 | sub get | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | my ($name) = @_; | ||
| 106 | my $result = $self->all_col($self->{get_expr}, $name); | ||
| 107 | return unless @$result; | ||
| 108 | return $self->{subpackages}; | ||
| 109 | } | ||
| 110 | |||
| 111 | =begin testing drop after rename | ||
| 112 | |||
| 113 | isnt $_tobj->drop('new_fusqlfs_table'), undef, 'Table dropped'; | ||
| 114 | is $_tobj->get('new_fusqlfs_table'), undef, 'Table dropped correctly'; | ||
| 115 | is_deeply $_tobj->list(), [], 'Tables list is empty'; | ||
| 116 | |||
| 117 | =end testing | ||
| 118 | =cut | ||
| 119 | sub drop | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my ($name) = @_; | ||
| 123 | $self->do($self->{drop_expr}, [$name]); | ||
| 124 | } | ||
| 125 | |||
| 126 | =begin testing create after get list | ||
| 127 | |||
| 128 | isnt $_tobj->create('fusqlfs_table'), undef, 'Table created'; | ||
| 129 | is_deeply $_tobj->get('fusqlfs_table'), $_tobj->{subpackages}, 'New table is sane'; | ||
| 130 | is_deeply $_tobj->list(), [ 'fusqlfs_table' ], 'New table is listed'; | ||
| 131 | |||
| 132 | =end testing | ||
| 133 | =cut | ||
| 134 | sub create | ||
| 135 | { | ||
| 136 | my $self = shift; | ||
| 137 | my ($name) = @_; | ||
| 138 | $self->do($self->{create_expr}, [$name]); | ||
| 139 | } | ||
| 140 | |||
| 141 | =begin testing rename after create | ||
| 142 | |||
| 143 | isnt $_tobj->rename('fusqlfs_table', 'new_fusqlfs_table'), undef, 'Table renamed'; | ||
| 144 | is $_tobj->get('fusqlfs_table'), undef, 'Table is unaccessable under old name'; | ||
| 145 | is_deeply $_tobj->get('new_fusqlfs_table'), $_tobj->{subpackages}, 'Table renamed correctly'; | ||
| 146 | is_deeply $_tobj->list(), [ 'new_fusqlfs_table' ], 'Table is listed under new name'; | ||
| 147 | |||
| 148 | =end testing | ||
| 149 | =cut | ||
| 150 | sub rename | ||
| 151 | { | ||
| 152 | my $self = shift; | ||
| 153 | my ($name, $newname) = @_; | ||
| 154 | $self->do($self->{rename_expr}, [$name, $newname]); | ||
| 155 | } | ||
| 156 | |||
| 157 | =begin testing list | ||
| 158 | |||
| 159 | cmp_set $_tobj->list(), [], 'Tables list is sane'; | ||
| 160 | |||
| 161 | =end testing | ||
| 162 | =cut | ||
| 163 | sub list | ||
| 164 | { | ||
| 165 | my $self = shift; | ||
| 166 | return $self->all_col($self->{list_expr}) || []; | ||
| 167 | } | ||
| 168 | |||
| 169 | 1; | ||
| 170 | |||
| 171 | __END__ |