| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::MySQL::Table::Struct; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use base 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::MySQL::Table::Struct | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | Synopsis | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Description | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | sub init | ||
| 24 | { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | $self->{list_expr} = 'SHOW COLUMNS FROM `%s`'; | ||
| 28 | $self->{get_expr} = 'SHOW FULL COLUMNS FROM `%s` LIKE "%s"'; | ||
| 29 | $self->{get_create_expr} = 'SHOW CREATE TABLE `%s`'; | ||
| 30 | |||
| 31 | $self->{create_expr} = 'ALTER TABLE `%s` ADD COLUMN `%s` INT NOT NULL DEFAULT 0'; | ||
| 32 | $self->{rename_expr} = 'ALTER TABLE `%s` CHANGE COLUMN `%s` `%s` %s'; | ||
| 33 | $self->{store_expr} = 'ALTER TABLE `%s` MODIFY COLUMN `%s` %s'; | ||
| 34 | $self->{drop_expr} = 'ALTER TABLE `%s` DROP COLUMN `%s`'; | ||
| 35 | $self->{default_expr} = 'ALTER TABLE `%s` ALTER COLUMN `%s` %s DEFAULT %s'; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub build_column_def | ||
| 39 | { | ||
| 40 | my $data = shift; | ||
| 41 | return sprintf('%s %s %s NULL %s %s', | ||
| 42 | $data->{type}, | ||
| 43 | $data->{collation}? 'COLLATE '.$data->{collation}: '', | ||
| 44 | $data->{null}? '': 'NOT', | ||
| 45 | #defined $data->{default}? $data->{default}: 'NULL', | ||
| 46 | $data->{extra}, | ||
| 47 | $data->{comment}? "COMMENT '$data->{comment}'": ''); | ||
| 48 | } | ||
| 49 | |||
| 50 | =begin testing list | ||
| 51 | |||
| 52 | is $_tobj->list('unknown'), undef, 'Unknown table'; | ||
| 53 | cmp_set $_tobj->list('fusqlfs_table'), ['id', 'create.sql'], 'Test table listable'; | ||
| 54 | |||
| 55 | =end testing | ||
| 56 | =cut | ||
| 57 | sub list | ||
| 58 | { | ||
| 59 | my $self = shift; | ||
| 60 | my ($table) = @_; | ||
| 61 | my $list = $self->all_col($self->{list_expr}, [$table]); | ||
| 62 | return unless $list && @$list; | ||
| 63 | |||
| 64 | push @$list, 'create.sql'; | ||
| 65 | return $list | ||
| 66 | } | ||
| 67 | |||
| 68 | =begin testing get | ||
| 69 | |||
| 70 | is $_tobj->get('fusqlfs_table', 'unknown'), undef, 'Unknown field'; | ||
| 71 | is_deeply $_tobj->get('fusqlfs_table', 'id'), { | ||
| 72 | collation => undef, | ||
| 73 | comment => '', | ||
| 74 | default => undef, | ||
| 75 | extra => 'auto_increment', | ||
| 76 | key => 'PRI', | ||
| 77 | null => 0, | ||
| 78 | privileges => [ | ||
| 79 | 'select', | ||
| 80 | 'insert', | ||
| 81 | 'update', | ||
| 82 | 'references', | ||
| 83 | ], | ||
| 84 | type => 'int(11)', | ||
| 85 | }, 'Known field'; | ||
| 86 | |||
| 87 | =end testing | ||
| 88 | =cut | ||
| 89 | sub get | ||
| 90 | { | ||
| 91 | my $self = shift; | ||
| 92 | my ($table, $name) = @_; | ||
| 93 | |||
| 94 | if ($name eq 'create.sql') | ||
| 95 | { | ||
| 96 | return $self->one_row($self->{get_create_expr}, [$table])->{'Create Table'}; | ||
| 97 | } | ||
| 98 | |||
| 99 | my $result = $self->one_row($self->{get_expr}, [$table, $name]); | ||
| 100 | if ($result) | ||
| 101 | { | ||
| 102 | $result = { map { lc($_) => $result->{$_} } grep { $_ ne 'Field' } keys %$result }; | ||
| 103 | $result->{privileges} = [ split /,/, $result->{privileges} ]; | ||
| 104 | $result->{null} = $result->{null} eq 'YES'? 1: 0; | ||
| 105 | return $self->dump($result); | ||
| 106 | } | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | =begin testing drop after rename | ||
| 111 | |||
| 112 | isnt $_tobj->drop('fusqlfs_table', 'new_field'), undef, 'Field is dropped'; | ||
| 113 | is $_tobj->get('fusqlfs_table', 'new_field'), undef, 'Field is not gettable'; | ||
| 114 | is_deeply $_tobj->list('fusqlfs_table'), ['id', 'create.sql'], 'Field is not listable'; | ||
| 115 | |||
| 116 | =end testing | ||
| 117 | =cut | ||
| 118 | sub drop | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my ($table, $name) = @_; | ||
| 122 | $self->do($self->{drop_expr}, [$table, $name]); | ||
| 123 | } | ||
| 124 | |||
| 125 | =begin testing create after get list | ||
| 126 | |||
| 127 | isnt $_tobj->create('fusqlfs_table', 'field'), undef, 'Create field'; | ||
| 128 | is_deeply $_tobj->get('fusqlfs_table', 'field'), $new_field, 'New field exists'; | ||
| 129 | is_deeply $_tobj->list('fusqlfs_table'), ['id', 'field', 'create.sql'], 'New field is listable'; | ||
| 130 | |||
| 131 | =end testing | ||
| 132 | =cut | ||
| 133 | sub create | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | my ($table, $name) = @_; | ||
| 137 | $self->do($self->{create_expr}, [$table, $name]); | ||
| 138 | } | ||
| 139 | |||
| 140 | =begin testing rename after store | ||
| 141 | |||
| 142 | isnt $_tobj->rename('fusqlfs_table', 'field', 'new_field'), undef, 'Field renamed'; | ||
| 143 | is $_tobj->get('fusqlfs_table', 'field'), undef, 'New field is unaccessible by old name'; | ||
| 144 | is_deeply $_tobj->get('fusqlfs_table', 'new_field'), $new_field, 'New field exists'; | ||
| 145 | is_deeply $_tobj->list('fusqlfs_table'), ['id', 'new_field', 'create.sql'], 'New field is listable'; | ||
| 146 | |||
| 147 | =end testing | ||
| 148 | =cut | ||
| 149 | sub rename | ||
| 150 | { | ||
| 151 | my $self = shift; | ||
| 152 | my ($table, $name, $newname) = @_; | ||
| 153 | my $field = $self->one_row($self->{get_expr}, [$table, $name]); | ||
| 154 | return unless $field; | ||
| 155 | |||
| 156 | my $fielddef = build_column_def({ map { lc($_) => $field->{$_} } keys %$field }); | ||
| 157 | $self->do($self->{rename_expr}, [$table, $name, $newname, $fielddef]); | ||
| 158 | $self->do($self->{default_expr}, [$table, $newname, | ||
| 159 | defined $field->{Default}? 'SET': 'DROP', $field->{Default}]); | ||
| 160 | } | ||
| 161 | |||
| 162 | =begin testing store after create | ||
| 163 | |||
| 164 | $new_field->{type} = 'varchar(255)'; | ||
| 165 | $new_field->{default} = undef; | ||
| 166 | $new_field->{collation} = 'utf8_general_ci'; | ||
| 167 | $new_field->{null} = 1; | ||
| 168 | isnt $_tobj->store('fusqlfs_table', 'field', $new_field), undef, 'Field changed'; | ||
| 169 | is_deeply $_tobj->get('fusqlfs_table', 'field'), $new_field, 'Field changed correctly'; | ||
| 170 | |||
| 171 | =end testing | ||
| 172 | =cut | ||
| 173 | sub store | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | my ($table, $name, $data) = @_; | ||
| 177 | $data = $self->validate($data, { | ||
| 178 | type => '', | ||
| 179 | null => qr/^\d$/, | ||
| 180 | extra => '', | ||
| 181 | collation => '', | ||
| 182 | default => '', | ||
| 183 | comment => '', | ||
| 184 | }) or return; | ||
| 185 | my $fielddef = build_column_def($data); | ||
| 186 | $self->do($self->{store_expr}, [$table, $name, $fielddef]); | ||
| 187 | $self->do($self->{default_expr}, [$table, $name, | ||
| 188 | defined $data->{default}? 'SET': 'DROP', $data->{default}]); | ||
| 189 | } | ||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |