| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::MySQL::Tables; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::MySQL::Tables - FusqlFS MySQL database tables interface | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use FusqlFS::Backend::MySQL::Tables; | ||
| 16 | |||
| 17 | my $tables = FusqlFS::Backend::MySQL::Tables->new(); | ||
| 18 | my $list = $table->list(); | ||
| 19 | $tables->create('sometable'); | ||
| 20 | my $table = $tables->get('sometable'); | ||
| 21 | $tables->drop('sometable'); | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This is FusqlFS interface to MySQL database tables. This class is not | ||
| 26 | to be used by itself. | ||
| 27 | |||
| 28 | See L<FusqlFS::Backend::PgSQL::Tables> for details. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | sub init | ||
| 33 | { | ||
| 34 | my $self = shift; | ||
| 35 | $self->{rename_expr} = 'ALTER TABLE `%s` RENAME TO `%s`'; | ||
| 36 | $self->{drop_expr} = 'DROP TABLE `%s`'; | ||
| 37 | $self->{create_expr} = 'CREATE TABLE `%s` (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id))'; | ||
| 38 | |||
| 39 | $self->{list_expr} = 'SHOW TABLES'; | ||
| 40 | $self->{get_expr} = 'SHOW TABLES LIKE "%s"'; | ||
| 41 | |||
| 42 | $self->autopackages( | ||
| 43 | 'struct', | ||
| 44 | 'data', | ||
| 45 | 'indices', | ||
| 46 | 'triggers'); | ||
| 47 | } | ||
| 48 | |||
| 49 | =begin testing get | ||
| 50 | |||
| 51 | is $_tobj->get('fusqlfs_table'), undef, 'Test table doesn\'t exist'; | ||
| 52 | |||
| 53 | =end testing | ||
| 54 | =cut | ||
| 55 | sub get | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | my ($name) = @_; | ||
| 59 | my $result = $self->all_col($self->{get_expr}, [$name]); | ||
| 60 | return unless @$result; | ||
| 61 | return $self->{subpackages}; | ||
| 62 | } | ||
| 63 | |||
| 64 | =begin testing drop after rename | ||
| 65 | |||
| 66 | isnt $_tobj->drop('new_fusqlfs_table'), undef, 'Table dropped'; | ||
| 67 | is $_tobj->get('new_fusqlfs_table'), undef, 'Table dropped correctly'; | ||
| 68 | is_deeply $_tobj->list(), [], 'Tables list is empty'; | ||
| 69 | |||
| 70 | =end testing | ||
| 71 | =cut | ||
| 72 | sub drop | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | my ($name) = @_; | ||
| 76 | $self->do($self->{drop_expr}, [$name]); | ||
| 77 | } | ||
| 78 | |||
| 79 | =begin testing create after get list | ||
| 80 | |||
| 81 | isnt $_tobj->create('fusqlfs_table'), undef, 'Table created'; | ||
| 82 | is_deeply $_tobj->get('fusqlfs_table'), $_tobj->{subpackages}, 'New table is sane'; | ||
| 83 | is_deeply $_tobj->list(), ['fusqlfs_table'], 'New table is listed'; | ||
| 84 | |||
| 85 | =end testing | ||
| 86 | =cut | ||
| 87 | sub create | ||
| 88 | { | ||
| 89 | my $self = shift; | ||
| 90 | my ($name) = @_; | ||
| 91 | $self->do($self->{create_expr}, [$name]); | ||
| 92 | } | ||
| 93 | |||
| 94 | =begin testing rename after create | ||
| 95 | |||
| 96 | isnt $_tobj->rename('fusqlfs_table', 'new_fusqlfs_table'), undef, 'Table renamed'; | ||
| 97 | is $_tobj->get('fusqlfs_table'), undef, 'Table is unaccessible under old name'; | ||
| 98 | is_deeply $_tobj->get('new_fusqlfs_table'), $_tobj->{subpackages}, 'Table renamed correctly'; | ||
| 99 | is_deeply $_tobj->list(), ['new_fusqlfs_table'], 'Table is listed under new name'; | ||
| 100 | |||
| 101 | =end testing | ||
| 102 | =cut | ||
| 103 | sub rename | ||
| 104 | { | ||
| 105 | my $self = shift; | ||
| 106 | my ($name, $newname) = @_; | ||
| 107 | $self->do($self->{rename_expr}, [$name, $newname]); | ||
| 108 | } | ||
| 109 | |||
| 110 | =begin testing list | ||
| 111 | |||
| 112 | is_deeply $_tobj->list(), [], 'Tables list is sane'; | ||
| 113 | |||
| 114 | =end testing | ||
| 115 | =cut | ||
| 116 | sub list | ||
| 117 | { | ||
| 118 | my $self = shift; | ||
| 119 | return $self->all_col($self->{list_expr}) || []; | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |