| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Sequences; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Sequences - FusqlFS PostgreSQL database sequences interface | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use FusqlFS::Backend::PgSQL::Sequences; | ||
| 16 | |||
| 17 | my $seqs = FusqlFS::Backend::PgSQL::Sequences->new(); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This is FusqlFS an interface to PostgreSQL database sequences. This class is | ||
| 22 | not to be used by itself. | ||
| 23 | |||
| 24 | See L<FusqlFS::Artifact> for description of interface methods, | ||
| 25 | L<FusqlFS::Backend> to learn more on backend initialization and | ||
| 26 | L<FusqlFS::Backend::Base> for more info on database backends writing. | ||
| 27 | |||
| 28 | =head1 EXPOSED STRUCTURE | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item F<./struct> | ||
| 33 | |||
| 34 | Additional sequence info. To learn more about these fields see PostgreSQL | ||
| 35 | documentation about C<CREATE SEQUENCE>. | ||
| 36 | |||
| 37 | =item F<./owner> | ||
| 38 | |||
| 39 | Symlink to sequence's owner in F<../../roles>. | ||
| 40 | |||
| 41 | =item F<./acl> | ||
| 42 | |||
| 43 | Sequence's ACL with permissions given to different roles. See | ||
| 44 | L<FusqlFS::Backend::PgSQL::Role::Acl> for details. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | use FusqlFS::Backend::PgSQL::Role::Owner; | ||
| 51 | use FusqlFS::Backend::PgSQL::Role::Acl; | ||
| 52 | use DBI qw(:sql_types); | ||
| 53 | |||
| 54 | sub init | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | $self->{list_expr} = $self->expr("SELECT relname FROM pg_catalog.pg_class WHERE relkind = 'S'"); | ||
| 59 | $self->{exists_expr} = $self->expr("SELECT 1 FROM pg_catalog.pg_class WHERE relkind = 'S' AND relname = ?"); | ||
| 60 | $self->{get_expr} = 'SELECT * FROM "%s"'; | ||
| 61 | $self->{rename_expr} = 'ALTER SEQUENCE "%s" RENAME TO "%s"'; | ||
| 62 | $self->{create_expr} = 'CREATE SEQUENCE "%s"'; | ||
| 63 | $self->{drop_expr} = 'DROP SEQUENCE "%s"'; | ||
| 64 | |||
| 65 | $self->{owner} = FusqlFS::Backend::PgSQL::Role::Owner->new('S'); | ||
| 66 | $self->{acl} = FusqlFS::Backend::PgSQL::Role::Acl->new('S'); | ||
| 67 | } | ||
| 68 | |||
| 69 | =begin testing get | ||
| 70 | |||
| 71 | is $_tobj->get('unknown'), undef; | ||
| 72 | |||
| 73 | =end testing | ||
| 74 | =cut | ||
| 75 | sub get | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | my ($name) = @_; | ||
| 79 | my $result = $self->all_col($self->{exists_expr}, $name); | ||
| 80 | return unless @$result; | ||
| 81 | return { | ||
| 82 | struct => $self->dump($self->one_row($self->{get_expr}, [$name])), | ||
| 83 | owner => $self->{owner}, | ||
| 84 | acl => $self->{acl}, | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | |||
| 88 | =begin testing list | ||
| 89 | |||
| 90 | cmp_set $_tobj->list(), []; | ||
| 91 | |||
| 92 | =end testing | ||
| 93 | =cut | ||
| 94 | sub list | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | return $self->all_col($self->{list_expr}) || []; | ||
| 98 | } | ||
| 99 | |||
| 100 | =begin testing store after create | ||
| 101 | |||
| 102 | isnt $_tobj->store('fusqlfs_sequence', $new_sequence), undef; | ||
| 103 | is_deeply $_tobj->get('fusqlfs_sequence'), $new_sequence; | ||
| 104 | |||
| 105 | =end testing | ||
| 106 | =cut | ||
| 107 | sub store | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | my ($name, $data) = @_; | ||
| 111 | return unless $data; | ||
| 112 | |||
| 113 | my $struct = $self->validate($data, { | ||
| 114 | struct => { | ||
| 115 | is_cycled => '', | ||
| 116 | increment_by => qr/^-?\d+$/, | ||
| 117 | cache_value => qr/^-?\d+$/, | ||
| 118 | last_value => qr/^-?\d+$/, | ||
| 119 | max_value => qr/^-?\d+$/, | ||
| 120 | min_value => qr/^-?\d+$/,, | ||
| 121 | } | ||
| 122 | }) or return; | ||
| 123 | $data = $struct->{struct}; | ||
| 124 | |||
| 125 | my $sql = "ALTER SEQUENCE \"$name\" "; | ||
| 126 | $sql .= $data->{is_cycled}? 'CYCLE ': 'NO CYCLE ' if $data->{is_cycled}; | ||
| 127 | |||
| 128 | my $sth = $self->build($sql, sub{ | ||
| 129 | my ($a, $b) = @_; | ||
| 130 | return unless exists $data->{$a}; | ||
| 131 | if (!defined $data->{$a}) | ||
| 132 | { | ||
| 133 | return "NO $b->[0] " if $b->[2]; | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | return "$b->[0] ? ", $data->{$a}, $b->[1]; | ||
| 137 | }, increment_by => ['INCREMENT BY', SQL_INTEGER, 0], | ||
| 138 | cache_value => ['CACHE', SQL_INTEGER, 0] , | ||
| 139 | last_value => ['RESTART WITH', SQL_INTEGER, 0], | ||
| 140 | max_value => ['MAXVALUE', SQL_INTEGER, 1] , | ||
| 141 | min_value => ['MINVALUE', SQL_INTEGER, 1] ); | ||
| 142 | |||
| 143 | $sth->execute(); | ||
| 144 | } | ||
| 145 | |||
| 146 | =begin testing rename after store | ||
| 147 | |||
| 148 | isnt $_tobj->rename('fusqlfs_sequence', 'new_fusqlfs_sequence'), undef; | ||
| 149 | is $_tobj->get('fusqlfs_sequence'), undef; | ||
| 150 | is_deeply $_tobj->get('new_fusqlfs_sequence'), $new_sequence; | ||
| 151 | is_deeply $_tobj->list(), [ 'new_fusqlfs_sequence' ]; | ||
| 152 | |||
| 153 | =end testing | ||
| 154 | =cut | ||
| 155 | sub rename | ||
| 156 | { | ||
| 157 | my $self = shift; | ||
| 158 | my ($name, $newname) = @_; | ||
| 159 | $self->do($self->{rename_expr}, [$name, $newname]); | ||
| 160 | } | ||
| 161 | |||
| 162 | =begin testing drop after rename | ||
| 163 | |||
| 164 | isnt $_tobj->drop('new_fusqlfs_sequence'), undef; | ||
| 165 | is $_tobj->get('new_fusqlfs_sequence'), undef; | ||
| 166 | is_deeply $_tobj->list(), []; | ||
| 167 | |||
| 168 | =end testing | ||
| 169 | =cut | ||
| 170 | sub drop | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | my ($name) = @_; | ||
| 174 | $self->do($self->{drop_expr}, [$name]); | ||
| 175 | } | ||
| 176 | |||
| 177 | =begin testing create after get list | ||
| 178 | |||
| 179 | isnt $_tobj->create('fusqlfs_sequence'), undef; | ||
| 180 | is_deeply $_tobj->get('fusqlfs_sequence'), { struct => { | ||
| 181 | cache_value => 1, | ||
| 182 | increment_by => 1, | ||
| 183 | is_called => 0, | ||
| 184 | is_cycled => 0, | ||
| 185 | last_value => 1, | ||
| 186 | log_cnt => 0, | ||
| 187 | max_value => 9223372036854775807, | ||
| 188 | min_value => 1, | ||
| 189 | sequence_name => 'fusqlfs_sequence', | ||
| 190 | start_value => 1, | ||
| 191 | }, owner => $_tobj->{owner}, acl => $_tobj->{acl} }; | ||
| 192 | is_deeply $_tobj->list(), [ 'fusqlfs_sequence' ]; | ||
| 193 | |||
| 194 | =end testing | ||
| 195 | =cut | ||
| 196 | sub create | ||
| 197 | { | ||
| 198 | my $self = shift; | ||
| 199 | my ($name) = @_; | ||
| 200 | $self->do($self->{create_expr}, [$name]); | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ |