| 1 | package QBit::Application::Model::DB::mysql::Table; | ||
| 2 | $QBit::Application::Model::DB::mysql::Table::VERSION = '0.017'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use base qw(QBit::Application::Model::DB::Table); | ||
| 6 | |||
| 7 | use Exception::DB; | ||
| 8 | use Exception::DB::DuplicateEntry; | ||
| 9 | |||
| 10 | use QBit::Application::Model::DB::mysql::Field; | ||
| 11 | |||
| 12 | our $ADD_CHUNK = 1000; | ||
| 13 | |||
| 14 | sub create_sql { | ||
| 15 | my ($self) = @_; | ||
| 16 | |||
| 17 | throw gettext('Inherites does not realize') if $self->inherits; | ||
| 18 | |||
| 19 | my $collate = defined($self->collate()) ? (" COLLATE '" . $self->collate() . "'") : ''; | ||
| 20 | my $engine = defined($self->engine()) ? $self->engine() : 'InnoDB'; | ||
| 21 | |||
| 22 | return | ||
| 23 | 'CREATE TABLE ' | ||
| 24 | . $self->quote_identifier($self->name) | ||
| 25 | . " (\n " | ||
| 26 | . join( | ||
| 27 | ",\n ", | ||
| 28 | (map {$_->create_sql()} @{$self->fields}), | ||
| 29 | ( | ||
| 30 | $self->primary_key | ||
| 31 | ? 'PRIMARY KEY (' . join(', ', map {$self->quote_identifier($_)} @{$self->primary_key}) . ')' | ||
| 32 | : () | ||
| 33 | ), | ||
| 34 | (map {$self->_create_sql_index($_)} @{$self->indexes || []}), | ||
| 35 | (map {$self->_create_sql_foreign_key($_)} @{$self->foreign_keys || []}), | ||
| 36 | ) | ||
| 37 | . "\n" | ||
| 38 | . ") ENGINE='$engine' DEFAULT CHARACTER SET 'UTF8'$collate;"; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub add_multi { | ||
| 42 | my ($self, $data, %opts) = @_; | ||
| 43 | |||
| 44 | my $fields = $self->_fields_hs(); | ||
| 45 | |||
| 46 | my $field_names; | ||
| 47 | if ($opts{'fields'}) { | ||
| 48 | my @unknown_fields = grep {!exists($fields->{$_})} @{$opts{'fields'}}; | ||
| 49 | |||
| 50 | throw gettext('In table %s not found follows fields: %s', $self->name(), join(', ', @unknown_fields)) | ||
| 51 | if @unknown_fields; | ||
| 52 | |||
| 53 | $field_names = $opts{'fields'}; | ||
| 54 | } elsif (blessed($data->[0]) && $data->[0]->isa('QBit::Application::Model::DB::Query')) { | ||
| 55 | $field_names = [sort map {$fields->{$_}->name} keys %$fields]; | ||
| 56 | } else { | ||
| 57 | my $data_fields; | ||
| 58 | if ($opts{'identical_rows'}) { | ||
| 59 | $data_fields = [keys(%{$data->[0] // {}})]; | ||
| 60 | } else { | ||
| 61 | $data_fields = array_uniq(map {keys(%$_)} @$data); | ||
| 62 | } | ||
| 63 | |||
| 64 | if ($opts{'ignore_extra_fields'}) { | ||
| 65 | $field_names = arrays_intersection([map {$fields->{$_}->name} keys %$fields], $data_fields); | ||
| 66 | } else { | ||
| 67 | my @unknown_fields = grep {!exists($fields->{$_})} @$data_fields; | ||
| 68 | |||
| 69 | throw gettext('In table %s not found follows fields: %s', $self->name(), join(', ', @unknown_fields)) | ||
| 70 | if @unknown_fields; | ||
| 71 | |||
| 72 | $field_names = $data_fields; | ||
| 73 | } | ||
| 74 | |||
| 75 | $field_names = [sort @$field_names]; | ||
| 76 | } | ||
| 77 | |||
| 78 | throw Exception::BadArguments gettext('Expected fields') unless $field_names; | ||
| 79 | |||
| 80 | my @locales = sort keys(%{$self->db->get_option('locales', {})}); | ||
| 81 | @locales = (undef) unless @locales; | ||
| 82 | |||
| 83 | my $sql_header = | ||
| 84 | ($opts{'replace'} ? 'REPLACE' : 'INSERT') | ||
| 85 | . ($opts{'ignore'} ? ' IGNORE ' : '') | ||
| 86 | . ' INTO ' | ||
| 87 | . $self->quote_identifier($self->name) . ' ('; | ||
| 88 | my @real_field_names; | ||
| 89 | foreach my $name (@$field_names) { | ||
| 90 | if ($fields->{$name}{'i18n'}) { | ||
| 91 | push(@real_field_names, defined($_) ? "${name}_${_}" : $name) foreach @locales; | ||
| 92 | } else { | ||
| 93 | push(@real_field_names, $name); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | $sql_header .= join(', ', map {$self->quote_identifier($_)} @real_field_names) . ") "; | ||
| 97 | |||
| 98 | if (blessed($data->[0]) && $data->[0]->isa('QBit::Application::Model::DB::Query')) { | ||
| 99 | my ($sql, @params) = $data->[0]->get_sql_with_data(); | ||
| 100 | |||
| 101 | return $self->db->_do($sql_header . $sql, @params); | ||
| 102 | } | ||
| 103 | |||
| 104 | $sql_header .= "VALUES\n"; | ||
| 105 | |||
| 106 | my $add_rows = 0; | ||
| 107 | |||
| 108 | my $need_transact = @$data > $ADD_CHUNK; | ||
| 109 | $self->db->begin() if $need_transact; | ||
| 110 | |||
| 111 | try { | ||
| 112 | $self->db->_sub_with_connected_dbh( | ||
| 113 | sub { | ||
| 114 | my ($db, @data) = @_; | ||
| 115 | |||
| 116 | my ($sql, $values); | ||
| 117 | my $sth; | ||
| 118 | my $err_code; | ||
| 119 | |||
| 120 | while (my @add_data = splice(@data, 0, $ADD_CHUNK)) { | ||
| 121 | if (@add_data != $ADD_CHUNK) { | ||
| 122 | $sql = undef; | ||
| 123 | $values = undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | my @params = (); | ||
| 127 | foreach my $row (@add_data) { | ||
| 128 | unless ($values) { | ||
| 129 | $values = '(?' . ', ?' x (@real_field_names - 1) . ')'; | ||
| 130 | $values = $values . (",\n" . $values) x (@add_data - 1); | ||
| 131 | } | ||
| 132 | |||
| 133 | foreach my $name (@$field_names) { | ||
| 134 | if ($fields->{$name}{'i18n'}) { | ||
| 135 | if (ref($row->{$name}) eq 'HASH') { | ||
| 136 | my @missed_langs = grep {!exists($row->{$name}{$_})} @locales; | ||
| 137 | throw Exception::BadArguments gettext('Undefined languages "%s" for field "%s"', | ||
| 138 | join(', ', @missed_langs), $name) | ||
| 139 | if @missed_langs; | ||
| 140 | push(@params, $row->{$name}{$_}) foreach @locales; | ||
| 141 | } elsif (!ref($row->{$name})) { | ||
| 142 | push(@params, $row->{$name}) foreach @locales; | ||
| 143 | } else { | ||
| 144 | throw Exception::BadArguments gettext('Invalid value in table->add'); | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | push(@params, $row->{$name}); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | unless ($sql) { | ||
| 153 | $sql = $sql_header . $values; | ||
| 154 | |||
| 155 | $sth = $db->dbh->prepare($sql) | ||
| 156 | || ($err_code = $db->dbh->err()) | ||
| 157 | && throw Exception::DB $db->dbh->errstr() . " ($err_code)\n" . $db->_log_sql($sql, \@params), | ||
| 158 | errorcode => $err_code; | ||
| 159 | } | ||
| 160 | |||
| 161 | $add_rows += $sth->execute(@params) | ||
| 162 | || ($err_code = $db->dbh->err()) | ||
| 163 | && throw Exception::DB $sth->errstr() . " ($err_code)\n" . $db->_log_sql($sql, \@params), | ||
| 164 | errorcode => $err_code; | ||
| 165 | } | ||
| 166 | }, | ||
| 167 | [$self->db, @$data] | ||
| 168 | ); | ||
| 169 | } | ||
| 170 | catch Exception::DB with { | ||
| 171 | my $e = shift; | ||
| 172 | $e->{'text'} =~ /^Duplicate entry/ | ||
| 173 | ? throw Exception::DB::DuplicateEntry $e | ||
| 174 | : throw $e; | ||
| 175 | }; | ||
| 176 | |||
| 177 | $self->db->commit() if $need_transact; | ||
| 178 | |||
| 179 | return $add_rows; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub add { | ||
| 183 | my ($self, $data, %opts) = @_; | ||
| 184 | |||
| 185 | $data = {$self->primary_key->[0] => $data} if !ref($data) && @{$self->primary_key || []} == 1; | ||
| 186 | |||
| 187 | $self->add_multi([$data], %opts); | ||
| 188 | |||
| 189 | my $fields_hs = $self->_fields_hs(); | ||
| 190 | my @res = map { | ||
| 191 | !defined($data->{$_}) | ||
| 192 | && $fields_hs->{$_}{'autoincrement'} | ||
| 193 | ? $self->db->_get_all('SELECT LAST_INSERT_ID() AS `id`')->[0]{'id'} | ||
| 194 | : $data->{$_} | ||
| 195 | } @{$self->primary_key || []}; | ||
| 196 | |||
| 197 | return @res == 1 ? $res[0] : \@res; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub delete { | ||
| 201 | my ($self, $pkeys_or_filter, %opts) = @_; | ||
| 202 | |||
| 203 | my $query = $self->db->query()->select(table => $self, fields => {}); | ||
| 204 | my $filter_expr = $query->filter($self->_pkeys_or_filter_to_filter($pkeys_or_filter))->expression(); | ||
| 205 | my ($filter_sql, @filter_data) = $query->_field_to_sql(undef, $filter_expr, $query->_get_table($self)); | ||
| 206 | |||
| 207 | $self->db->_do('DELETE FROM ' . $self->quote_identifier($self->name) . "\nWHERE $filter_sql", @filter_data); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub replace { | ||
| 211 | my ($self, $data, %opts) = @_; | ||
| 212 | |||
| 213 | $self->add($data, %opts, replace => 1); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _get_field_object { | ||
| 217 | my ($self, %opts) = @_; | ||
| 218 | |||
| 219 | return QBit::Application::Model::DB::mysql::Field->new(%opts); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _convert_fk_auto_type { | ||
| 223 | my ($self, $field, $fk_field) = @_; | ||
| 224 | |||
| 225 | $field->{$_} = $fk_field->{$_} | ||
| 226 | foreach grep {exists($fk_field->{$_}) && !exists($field->{$_})} qw(type unsigned not_null length); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _create_sql_index { | ||
| 230 | my ($self, $index) = @_; | ||
| 231 | |||
| 232 | my @fields = map {ref($_) ? $_ : {name => $_}} @{$index->{'fields'}}; | ||
| 233 | |||
| 234 | if ($index->{'unique'}) { | ||
| 235 | throw gettext('Class "%s" conflict with option "unique"', $index->{'class'}) | ||
| 236 | if defined($index->{'class'}) && $index->{'class'} ne 'UNIQUE'; | ||
| 237 | |||
| 238 | $index->{'class'} = 'UNIQUE'; | ||
| 239 | } | ||
| 240 | |||
| 241 | return | ||
| 242 | ($index->{'class'} ? "$index->{'class'} " : '') | ||
| 243 | . 'INDEX ' | ||
| 244 | . $self->quote_identifier( | ||
| 245 | substr(join('_', ($index->{'unique'} ? 'uniq' : ()), $self->name, '', map {$_->{'name'}} @fields), 0, 64)) | ||
| 246 | . ' (' | ||
| 247 | . join(', ', map {$self->quote_identifier($_->{'name'}) . ($_->{'length'} ? "($_->{'length'})" : '')} @fields) | ||
| 248 | . ')'; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub _create_sql_foreign_key { | ||
| 252 | my ($self, $key) = @_; | ||
| 253 | |||
| 254 | return 'FOREIGN KEY ' | ||
| 255 | . $self->quote_identifier( | ||
| 256 | substr(join('_', 'fk', $self->name, '', @{$key->[0]}, '_', $key->[1], '', @{$key->[2]}), 0, 64)) | ||
| 257 | . ' (' | ||
| 258 | . join(', ', map {$self->quote_identifier($_)} @{$key->[0]}) . ")\n" | ||
| 259 | . ' REFERENCES ' | ||
| 260 | . $self->quote_identifier($key->[1]) . ' (' | ||
| 261 | . join(', ', map {$self->quote_identifier($_)} @{$key->[2]}) . ")\n" | ||
| 262 | . " ON UPDATE RESTRICT\n" | ||
| 263 | . " ON DELETE RESTRICT"; | ||
| 264 | } | ||
| 265 | |||
| 266 | TRUE; | ||
| 267 | |||
| 268 | __END__ |