| 1 | =head1 NAME | ||
| 2 | |||
| 3 | XAO::DO::FS::Glue::Base_MySQL - MySQL specific overrides - base for MySQL_DBI and MySQL drivers | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | Should not be used directly. | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | This module implements some functionality required by FS::Glue | ||
| 12 | in MySQL specific way. | ||
| 13 | |||
| 14 | =head1 METHODS | ||
| 15 | |||
| 16 | =over | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | ############################################################################### | ||
| 21 | package XAO::DO::FS::Glue::Base_MySQL; | ||
| 22 | use strict; | ||
| 23 | use Error qw(:try); | ||
| 24 | use XAO::Utils qw(:debug :args :keys); | ||
| 25 | use XAO::Objects; | ||
| 26 | |||
| 27 | use base XAO::Objects->load(objname => 'FS::Glue::Base'); | ||
| 28 | |||
| 29 | use vars qw($VERSION); | ||
| 30 | $VERSION=(0+sprintf('%u.%03u',(q$Id: Base_MySQL.pm,v 2.11 2008/12/10 05:33:26 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; | ||
| 31 | |||
| 32 | ############################################################################### | ||
| 33 | |||
| 34 | =item new ($%) | ||
| 35 | |||
| 36 | Creates new instance of the driver connected to the given database using | ||
| 37 | DSN, user and password. | ||
| 38 | |||
| 39 | Example: | ||
| 40 | |||
| 41 | my $driver=XAO::Objects->new(objname => 'FS::Glue::MySQL', | ||
| 42 | dsn => 'OS:MySQL_DBI:dbname', | ||
| 43 | user => 'username', | ||
| 44 | password => '123123123'); | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub new ($%) { | ||
| 49 | my $proto=shift; | ||
| 50 | my $args=get_args(\@_); | ||
| 51 | |||
| 52 | my $self=$proto->SUPER::new($args); | ||
| 53 | |||
| 54 | my $dsn=$args->{'dsn'}; | ||
| 55 | $dsn || $self->throw("- required parameter missed 'dsn'"); | ||
| 56 | $dsn=~/^OS:(\w+):(\w+)(;.*)?$/ || $self->throw("- bad format of 'dsn' ($dsn)"); | ||
| 57 | my $driver=$1; | ||
| 58 | my $dbname=$2; | ||
| 59 | my $options=$3 || ''; | ||
| 60 | |||
| 61 | # Parsing dbopts, separating what we know about from what is passed | ||
| 62 | # directly to the driver. | ||
| 63 | # | ||
| 64 | my $dbopts=''; | ||
| 65 | foreach my $pair (split(/[,;]/,$options)) { | ||
| 66 | next unless length($pair); | ||
| 67 | if($pair =~ /^table_type\s*=\s*(.*?)\s*$/) { | ||
| 68 | $self->{'table_type'}=lc($1); | ||
| 69 | } | ||
| 70 | elsif($pair =~ /^dbi(?:_driver)?\s*=\s*(.*?)\s*$/) { | ||
| 71 | $self->{'dbi_driver'}=$1; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | $dbopts.=';' . $pair; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | $driver =~ '^MySQL' || | ||
| 79 | throw $self "- wrong driver type ($driver)"; | ||
| 80 | |||
| 81 | # What specific DBI driver to use? Supporting DBD::mysql and | ||
| 82 | # DBD::MariaDB | ||
| 83 | # | ||
| 84 | my $dbi_driver=lc($self->{'dbi_driver'} || 'mysql'); | ||
| 85 | $dbi_driver='MariaDB' if $dbi_driver eq 'mariadb' || $dbi_driver eq 'dbd::mariadb'; | ||
| 86 | $dbi_driver='mysql' if $dbi_driver eq 'mysql' || $dbi_driver eq 'dbd::mysql'; | ||
| 87 | |||
| 88 | $self->connector->sql_connect( | ||
| 89 | dsn => "DBI:$dbi_driver:$dbname$dbopts", | ||
| 90 | user => $args->{'user'}, | ||
| 91 | password => $args->{'password'}, | ||
| 92 | ); | ||
| 93 | |||
| 94 | # Without this option we risk getting weird results -- for instance | ||
| 95 | # a 'koi8r' column might be returned encoded in 'utf8' otherwise | ||
| 96 | # depending on the server config. | ||
| 97 | # | ||
| 98 | # SET NAMES is the same as assigning the value to each of | ||
| 99 | # character-set-{client,connection,results} separately. | ||
| 100 | # | ||
| 101 | my $cn=$self->connector; | ||
| 102 | $cn->sql_do("SET NAMES 'binary'"); | ||
| 103 | |||
| 104 | # Getting DB version | ||
| 105 | # | ||
| 106 | my $sth=$cn->sql_execute(q(SHOW VARIABLES LIKE 'version')); | ||
| 107 | my $row=$cn->sql_first_row($sth); | ||
| 108 | my $version=($row && $row->[0] eq 'version') ? $row->[1] : '4.0-fake'; | ||
| 109 | my $vnum=($version=~/^(\d+)\.(\d+)(?:\.(\d+))?/) ? sprintf('%u.%03u%03u',$1,$2,$3||0) : 4.0; | ||
| 110 | |||
| 111 | $self->{'mysql_version'}=$vnum; | ||
| 112 | $self->{'mysql_version_full'}=$version; | ||
| 113 | |||
| 114 | ### dprint "MySQL version $version ($vnum)"; | ||
| 115 | |||
| 116 | # Just to make sure that even if the server has some different | ||
| 117 | # encoding set, we enforce transparency. | ||
| 118 | # | ||
| 119 | if($vnum>=5.006) { | ||
| 120 | $cn->sql_do("SET character_set_client='binary'"); | ||
| 121 | $cn->sql_do("SET character_set_connection='binary'"); | ||
| 122 | $cn->sql_do("SET character_set_results='binary'"); | ||
| 123 | } | ||
| 124 | |||
| 125 | # Done preparing | ||
| 126 | # | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | ############################################################################### | ||
| 131 | |||
| 132 | =item add_field_integer ($$$$) | ||
| 133 | |||
| 134 | Adds new integer field to the given table. First parameter is table | ||
| 135 | name, then field name, then index flag, then unique flag, then minimal | ||
| 136 | value and then maximum value and default value. | ||
| 137 | |||
| 138 | B<Note:> Indexes only work with MySQL 3.23 and later. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub add_field_integer ($$$$$) { | ||
| 143 | my $self=shift; | ||
| 144 | my ($table,$name,$index,$unique,$min,$max,$default,$connected)=@_; | ||
| 145 | $name.='_'; | ||
| 146 | |||
| 147 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 148 | throw $self "add_field_integer - modifying structure in transaction scope is not supported"; | ||
| 149 | } | ||
| 150 | |||
| 151 | $min=-0x80000000 unless defined $min; | ||
| 152 | $min=int($min); | ||
| 153 | if(!defined($max)) { | ||
| 154 | $max=($min<0) ? 0x7FFFFFFF : 0xFFFFFFFF; | ||
| 155 | } | ||
| 156 | |||
| 157 | my $sql; | ||
| 158 | if($min<0) { | ||
| 159 | if($min>=-0x80 && $max<=0x7F) { | ||
| 160 | $sql='TINYINT'; | ||
| 161 | } | ||
| 162 | elsif($min>=-0x8000 && $max<=0x7FFF) { | ||
| 163 | $sql='SMALLINT'; | ||
| 164 | } | ||
| 165 | elsif($min>=-0x800000 && $max<=0x7FFFFF) { | ||
| 166 | $sql='MEDIUMINT'; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | $sql='INT'; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | if($max<=0xFF) { | ||
| 174 | $sql='TINYINT UNSIGNED'; | ||
| 175 | } | ||
| 176 | elsif($max<=0xFFFF) { | ||
| 177 | $sql='SMALLINT UNSIGNED'; | ||
| 178 | } | ||
| 179 | elsif($max<=0xFFFFFF) { | ||
| 180 | $sql='MEDIUMINT UNSIGNED'; | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | $sql='INT UNSIGNED'; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | $sql.=" NOT NULL DEFAULT $default"; | ||
| 188 | |||
| 189 | $sql="ALTER TABLE $table ADD $name $sql"; | ||
| 190 | |||
| 191 | my $cn=$self->connector; | ||
| 192 | $cn->sql_do($sql); | ||
| 193 | |||
| 194 | if(($index || $unique) && (!$unique || !$connected)) { | ||
| 195 | my $usql=$unique ? " UNIQUE" : ""; | ||
| 196 | $sql="ALTER TABLE $table ADD$usql INDEX fsi__$name ($name)"; | ||
| 197 | #dprint ">>>$sql<<<"; | ||
| 198 | $cn->sql_do($sql); | ||
| 199 | } | ||
| 200 | |||
| 201 | if($unique && $connected) { | ||
| 202 | $sql="ALTER TABLE $table ADD UNIQUE INDEX fsu__$name (parent_unique_id_,$name)"; | ||
| 203 | #dprint ">>>$sql<<<"; | ||
| 204 | $cn->sql_do($sql); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | ############################################################################### | ||
| 209 | |||
| 210 | =item add_field_real ($$;$$) | ||
| 211 | |||
| 212 | Adds new real field to the given table. First parameter is table name, | ||
| 213 | then field name, then index flag, then unique flag, then optional | ||
| 214 | minimal value and then optional maximum value and default value. | ||
| 215 | |||
| 216 | B<Note:> Indexes only work with MySQL 3.23 and later. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub add_field_real ($$$;$$) { | ||
| 221 | my $self=shift; | ||
| 222 | my ($table,$name,$index,$unique,$min,$max,$scale,$default,$connected)=@_; | ||
| 223 | $name.='_'; | ||
| 224 | |||
| 225 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 226 | throw $self "add_field_real - modifying structure in transaction scope is not supported"; | ||
| 227 | } | ||
| 228 | |||
| 229 | my $sql= | ||
| 230 | "ALTER TABLE $table ADD $name ". | ||
| 231 | $self->real_field_definition($min,$max,$scale); | ||
| 232 | |||
| 233 | ### dprint ">>>$sql<<<"; | ||
| 234 | |||
| 235 | my $cn=$self->connector; | ||
| 236 | |||
| 237 | $cn->sql_do($sql,[$default || 0]); | ||
| 238 | |||
| 239 | if(($index || $unique) && (!$unique || !$connected)) { | ||
| 240 | my $usql=$unique ? " UNIQUE" : ""; | ||
| 241 | $sql="ALTER TABLE $table ADD$usql INDEX fsi__$name ($name)"; | ||
| 242 | ### dprint ">>>$sql<<<"; | ||
| 243 | $cn->sql_do($sql); | ||
| 244 | } | ||
| 245 | |||
| 246 | if($unique && $connected) { | ||
| 247 | $sql="ALTER TABLE $table ADD UNIQUE INDEX fsu__$name (parent_unique_id_,$name)"; | ||
| 248 | ### dprint ">>>$sql<<<"; | ||
| 249 | $cn->sql_do($sql); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | ############################################################################### | ||
| 254 | |||
| 255 | =item add_field_text ($$$$$) | ||
| 256 | |||
| 257 | Adds new text field to the given table. First is table name, then field | ||
| 258 | name, then index flag, then unique flag, maximum length, default value | ||
| 259 | and 'connected' flag. Depending on maximum length it will create CHAR, | ||
| 260 | TEXT, MEDIUMTEXT or LONGTEXT. | ||
| 261 | |||
| 262 | 'Connected' flag must be set if that table holds elements deeper into | ||
| 263 | the tree then the top level. | ||
| 264 | |||
| 265 | B<Note:> Modifiers 'index' and 'unique' only work with MySQL 3.23 and | ||
| 266 | later. | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub add_field_text ($$$$$$) { | ||
| 271 | my $self=shift; | ||
| 272 | my ($table,$name,$index,$unique,$maxlength,$default,$charset,$connected)=@_; | ||
| 273 | $name.='_'; | ||
| 274 | |||
| 275 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 276 | throw $self "add_field_text - modifying structure in transaction scope is not supported"; | ||
| 277 | } | ||
| 278 | |||
| 279 | $charset || | ||
| 280 | throw $self "add_field_text - 'charset' is required for text fields"; | ||
| 281 | |||
| 282 | my $def=$self->text_field_definition($charset,$maxlength); | ||
| 283 | |||
| 284 | my $cn=$self->connector; | ||
| 285 | $cn->sql_do("ALTER TABLE $table ADD $name $def",$default); | ||
| 286 | |||
| 287 | !$unique || $maxlength<=255 || | ||
| 288 | throw $self "add_field_text - property is too long to make it unique ($maxlength)"; | ||
| 289 | !$index || $maxlength<=255 || | ||
| 290 | throw $self "add_field_text - property is too long for an index ($maxlength)"; | ||
| 291 | |||
| 292 | if(($index || $unique) && (!$unique || !$connected)) { | ||
| 293 | my $usql=$unique ? " UNIQUE" : ""; | ||
| 294 | my $sql="ALTER TABLE $table ADD$usql INDEX fsi__$name ($name)"; | ||
| 295 | ### dprint ">>>$sql<<<"; | ||
| 296 | $cn->sql_do($sql); | ||
| 297 | } | ||
| 298 | |||
| 299 | if($unique && $connected) { | ||
| 300 | my $sql="ALTER TABLE $table ADD UNIQUE INDEX fsu__$name (parent_unique_id_,$name)"; | ||
| 301 | ### dprint ">>>$sql<<<"; | ||
| 302 | $cn->sql_do($sql); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | ############################################################################### | ||
| 307 | |||
| 308 | =item add_table ($$$) | ||
| 309 | |||
| 310 | Creates new empty table with unique_id, key and optionally connector | ||
| 311 | fields. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub add_table ($$$$$) { | ||
| 316 | my $self=shift; | ||
| 317 | my ($table,$key,$key_length,$key_charset,$connector)=@_; | ||
| 318 | $key.='_'; | ||
| 319 | $connector.='_' if $connector; | ||
| 320 | |||
| 321 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 322 | throw $self "add_table - modifying structure in transaction scope is not supported"; | ||
| 323 | } | ||
| 324 | |||
| 325 | my $def=$self->text_field_definition($key_charset,$key_length); | ||
| 326 | |||
| 327 | my $sql="CREATE TABLE $table (" . | ||
| 328 | " unique_id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY," . | ||
| 329 | " $key $def," . | ||
| 330 | (defined($connector) ? " $connector INT UNSIGNED NOT NULL," . | ||
| 331 | " INDEX $key($key)," . | ||
| 332 | " INDEX $connector($connector)" | ||
| 333 | : " UNIQUE INDEX $key($key)") . | ||
| 334 | ")"; | ||
| 335 | |||
| 336 | $sql.=" ENGINE=".$self->{'table_type'} | ||
| 337 | if $self->{'table_type'} && $self->{'table_type'} ne 'mixed'; | ||
| 338 | |||
| 339 | $self->connector->sql_do($sql,''); | ||
| 340 | } | ||
| 341 | |||
| 342 | ############################################################################### | ||
| 343 | |||
| 344 | =item charset_change_prepare ($) | ||
| 345 | |||
| 346 | Prepares an internal structure for a later one-shot modification of | ||
| 347 | charsets in a table. | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | sub charset_change_prepare ($$) { | ||
| 352 | my ($self,$table)=@_; | ||
| 353 | |||
| 354 | $table || throw $self "charset_change_prepare - no 'table' given"; | ||
| 355 | |||
| 356 | return { | ||
| 357 | sql => '', | ||
| 358 | table => $table, | ||
| 359 | defaults=> [ ], | ||
| 360 | }; | ||
| 361 | } | ||
| 362 | |||
| 363 | ############################################################################### | ||
| 364 | |||
| 365 | =item charset_change_field | ||
| 366 | |||
| 367 | Adds a field to the list of things to alter on charset_change_execute() | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub charset_change_field ($$$$$$) { | ||
| 372 | my ($self,$csh,$name,$charset,$maxlength,$default)=@_; | ||
| 373 | |||
| 374 | my $def=$self->text_field_definition($charset,$maxlength); | ||
| 375 | |||
| 376 | $csh->{'sql'}.=', ' if $csh->{'sql'}; | ||
| 377 | $csh->{'sql'}.="CHANGE ". | ||
| 378 | $self->mangle_field_name($name)." ". | ||
| 379 | $self->mangle_field_name($name)." ".$def; | ||
| 380 | |||
| 381 | push(@{$csh->{'defaults'}},$default); | ||
| 382 | } | ||
| 383 | |||
| 384 | ############################################################################### | ||
| 385 | |||
| 386 | =item charset_change_execute | ||
| 387 | |||
| 388 | Executes actual SQL collected from charset_change_field() | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | sub charset_change_execute ($$) { | ||
| 393 | my ($self,$csh)=@_; | ||
| 394 | |||
| 395 | my $sql="ALTER TABLE ".$csh->{'table'}." ".$csh->{'sql'}; | ||
| 396 | $self->connector->sql_do($sql,$csh->{'defaults'}); | ||
| 397 | } | ||
| 398 | |||
| 399 | ############################################################################### | ||
| 400 | |||
| 401 | =item maxlength_change_prepare ($) | ||
| 402 | |||
| 403 | Prepares an internal structure for a later one-shot modification of | ||
| 404 | maxlength in a table. | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | sub maxlength_change_prepare ($$) { | ||
| 409 | my ($self,$table)=@_; | ||
| 410 | |||
| 411 | $table || throw $self "- no 'table' given"; | ||
| 412 | |||
| 413 | return { | ||
| 414 | sql => '', | ||
| 415 | table => $table, | ||
| 416 | defaults=> [ ], | ||
| 417 | }; | ||
| 418 | } | ||
| 419 | |||
| 420 | ############################################################################### | ||
| 421 | |||
| 422 | =item maxlength_change_field | ||
| 423 | |||
| 424 | Adds a field to the list of things to alter on maxlength_change_execute() | ||
| 425 | |||
| 426 | =cut | ||
| 427 | |||
| 428 | sub maxlength_change_field ($$$$$$) { | ||
| 429 | my ($self,$csh,$name,$maxlength,$fdesc)=@_; | ||
| 430 | |||
| 431 | my $def=$self->text_field_definition($fdesc->{'charset'} || 'binary',$maxlength); | ||
| 432 | |||
| 433 | $csh->{'sql'}.=', ' if $csh->{'sql'}; | ||
| 434 | $csh->{'sql'}.="CHANGE ". | ||
| 435 | $self->mangle_field_name($name)." ". | ||
| 436 | $self->mangle_field_name($name)." ".$def; | ||
| 437 | |||
| 438 | push(@{$csh->{'defaults'}},$fdesc->{'default'}); | ||
| 439 | } | ||
| 440 | |||
| 441 | ############################################################################### | ||
| 442 | |||
| 443 | =item maxlength_change_execute | ||
| 444 | |||
| 445 | Executes actual SQL collected from maxlength_change_field() | ||
| 446 | |||
| 447 | =cut | ||
| 448 | |||
| 449 | sub maxlength_change_execute ($$) { | ||
| 450 | my ($self,$csh)=@_; | ||
| 451 | |||
| 452 | my $sql="ALTER TABLE ".$csh->{'table'}." ".$csh->{'sql'}; | ||
| 453 | $self->connector->sql_do($sql,$csh->{'defaults'}); | ||
| 454 | } | ||
| 455 | |||
| 456 | ############################################################################### | ||
| 457 | |||
| 458 | =item scale_change_prepare ($) | ||
| 459 | |||
| 460 | Prepares an internal structure for a later one-shot modification of | ||
| 461 | scale values in a table. | ||
| 462 | |||
| 463 | =cut | ||
| 464 | |||
| 465 | sub scale_change_prepare ($$) { | ||
| 466 | my ($self,$table)=@_; | ||
| 467 | |||
| 468 | $table || throw $self "- no 'table' given"; | ||
| 469 | |||
| 470 | return { | ||
| 471 | sql => '', | ||
| 472 | table => $table, | ||
| 473 | defaults=> [ ], | ||
| 474 | }; | ||
| 475 | } | ||
| 476 | |||
| 477 | ############################################################################### | ||
| 478 | |||
| 479 | =item scale_change_field | ||
| 480 | |||
| 481 | Adds a field to the list of things to alter on scale_change_execute() | ||
| 482 | |||
| 483 | =cut | ||
| 484 | |||
| 485 | sub scale_change_field ($$$$$$) { | ||
| 486 | my ($self,$csh,$name,$scale,$minvalue,$maxvalue,$default)=@_; | ||
| 487 | |||
| 488 | my $def=$self->real_field_definition($minvalue,$maxvalue,$scale); | ||
| 489 | |||
| 490 | $csh->{'sql'}.=', ' if $csh->{'sql'}; | ||
| 491 | $csh->{'sql'}.="CHANGE ". | ||
| 492 | $self->mangle_field_name($name)." ". | ||
| 493 | $self->mangle_field_name($name)." ".$def; | ||
| 494 | |||
| 495 | push(@{$csh->{'defaults'}},$default); | ||
| 496 | } | ||
| 497 | |||
| 498 | ############################################################################### | ||
| 499 | |||
| 500 | =item scale_change_execute | ||
| 501 | |||
| 502 | Executes actual SQL collected from scale_change_field() | ||
| 503 | |||
| 504 | =cut | ||
| 505 | |||
| 506 | sub scale_change_execute ($$) { | ||
| 507 | my ($self,$csh)=@_; | ||
| 508 | |||
| 509 | my $sql="ALTER TABLE ".$csh->{'table'}." ".$csh->{'sql'}; | ||
| 510 | |||
| 511 | ### dprint "...SQL: $sql"; | ||
| 512 | |||
| 513 | $self->connector->sql_do($sql,$csh->{'defaults'}); | ||
| 514 | } | ||
| 515 | |||
| 516 | ############################################################################### | ||
| 517 | |||
| 518 | =item consistency_checked | ||
| 519 | |||
| 520 | Returns true if load_structure() was asked to and performed meta-data | ||
| 521 | consistency checks (SHOW TABLE STATUS, compare requested table_type to | ||
| 522 | on-disk type, table & db version compatibility, etc). | ||
| 523 | |||
| 524 | =cut | ||
| 525 | |||
| 526 | sub consistency_checked ($) { | ||
| 527 | my $self=shift; | ||
| 528 | return $self->{'consistency_checked'}; | ||
| 529 | } | ||
| 530 | |||
| 531 | ############################################################################### | ||
| 532 | |||
| 533 | =item consistency_check_set | ||
| 534 | |||
| 535 | If called before load_structure() with a true argument then the | ||
| 536 | load_structure will also check meta-data consistency. | ||
| 537 | |||
| 538 | =cut | ||
| 539 | |||
| 540 | sub consistency_check_set ($$) { | ||
| 541 | my ($self,$cc)=@_; | ||
| 542 | if($cc && $self->{'structure_loaded'}) { | ||
| 543 | eprint "consistency_check_set($cc) must be called before load_structure(), ignoring it"; | ||
| 544 | return; | ||
| 545 | } | ||
| 546 | $self->{'consistency_check_requested'}=($cc ? 1 : undef); | ||
| 547 | } | ||
| 548 | |||
| 549 | ############################################################################### | ||
| 550 | |||
| 551 | =item delete_row ($$) | ||
| 552 | |||
| 553 | Deletes a row from the given name and unique_id. | ||
| 554 | |||
| 555 | =cut | ||
| 556 | |||
| 557 | sub delete_row ($$$) { | ||
| 558 | my $self=shift; | ||
| 559 | my ($table,$uid)=@_; | ||
| 560 | |||
| 561 | $self->tr_loc_begin; | ||
| 562 | $self->connector->sql_do("DELETE FROM $table WHERE unique_id=?",$uid); | ||
| 563 | $self->tr_loc_commit; | ||
| 564 | } | ||
| 565 | |||
| 566 | ############################################################################### | ||
| 567 | |||
| 568 | =item disconnect () | ||
| 569 | |||
| 570 | Permanently disconnects driver from database. Normally perl's garbage collector | ||
| 571 | will do that for you. | ||
| 572 | |||
| 573 | =cut | ||
| 574 | |||
| 575 | sub disconnect ($) { | ||
| 576 | my $self=shift; | ||
| 577 | if($self->{'table_type'} eq 'innodb') { | ||
| 578 | $self->tr_ext_rollback if $self->tr_ext_active; | ||
| 579 | $self->tr_loc_rollback if $self->tr_loc_active; | ||
| 580 | } | ||
| 581 | else { | ||
| 582 | $self->unlock_tables; | ||
| 583 | } | ||
| 584 | $self->connector->sql_disconnect; | ||
| 585 | } | ||
| 586 | |||
| 587 | ############################################################################### | ||
| 588 | |||
| 589 | =item drop_field ($$$$$) | ||
| 590 | |||
| 591 | Drops the given field from the given table in the database. Whatever | ||
| 592 | content was in that field is lost irrevocably. | ||
| 593 | |||
| 594 | If index, unique and connected flags are given then it first will drop | ||
| 595 | the appropriate index. | ||
| 596 | |||
| 597 | =cut | ||
| 598 | |||
| 599 | sub drop_field ($$$$$$) { | ||
| 600 | my $self=shift; | ||
| 601 | my ($table,$name,$index,$unique,$connected)=@_; | ||
| 602 | |||
| 603 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 604 | throw $self "drop_field - modifying structure in transaction scope is not supported"; | ||
| 605 | } | ||
| 606 | |||
| 607 | $name.='_'; | ||
| 608 | |||
| 609 | my $cn=$self->connector; | ||
| 610 | if($index && (!$unique || !$connected)) { | ||
| 611 | my $sql="ALTER TABLE $table DROP INDEX fsi__$name"; | ||
| 612 | # dprint ">>>$sql<<<"; | ||
| 613 | $cn->sql_do($sql); | ||
| 614 | } | ||
| 615 | |||
| 616 | if($unique && $connected) { | ||
| 617 | my $sql="ALTER TABLE $table DROP INDEX fsu__$name"; | ||
| 618 | # dprint ">>>$sql<<<"; | ||
| 619 | $cn->sql_do($sql); | ||
| 620 | } | ||
| 621 | |||
| 622 | $cn->sql_do("ALTER TABLE $table DROP $name"); | ||
| 623 | } | ||
| 624 | |||
| 625 | ############################################################################### | ||
| 626 | |||
| 627 | =item drop_table ($) | ||
| 628 | |||
| 629 | Drops the given table with all its data. Whatever content was in that | ||
| 630 | table before is lost irrevocably. | ||
| 631 | |||
| 632 | =cut | ||
| 633 | |||
| 634 | sub drop_table ($$) { | ||
| 635 | my $self=shift; | ||
| 636 | my $table=shift; | ||
| 637 | |||
| 638 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 639 | throw $self "drop_table - modifying structure in transaction scope is not supported"; | ||
| 640 | } | ||
| 641 | |||
| 642 | $self->connector->sql_do("DROP TABLE $table"); | ||
| 643 | } | ||
| 644 | |||
| 645 | ############################################################################### | ||
| 646 | |||
| 647 | =item increment_key_seq ($) | ||
| 648 | |||
| 649 | Increments the value of key_seq in Global_Fields table identified by the | ||
| 650 | given row unique ID. Returns previous value. | ||
| 651 | |||
| 652 | B<Note:> Always executed as a part of some outer level transaction. Does | ||
| 653 | not create any locks or starts transactions. | ||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | sub increment_key_seq ($$) { | ||
| 658 | my $self=shift; | ||
| 659 | my $uid=shift; | ||
| 660 | |||
| 661 | my $cn=$self->connector; | ||
| 662 | |||
| 663 | $cn->sql_do('UPDATE Global_Fields SET key_seq_=key_seq_+1 WHERE unique_id=?',$uid); | ||
| 664 | |||
| 665 | my $sth=$cn->sql_execute('SELECT key_seq_ FROM Global_Fields WHERE unique_id=?',$uid); | ||
| 666 | my $seq=$cn->sql_first_row($sth)->[0]; | ||
| 667 | if($seq==1) { | ||
| 668 | $cn->sql_do('UPDATE Global_Fields SET key_seq_=key_seq_+1 WHERE unique_id=?',$uid); | ||
| 669 | $sth=$cn->sql_execute('SELECT key_seq_ FROM Global_Fields WHERE unique_id=?',$uid); | ||
| 670 | $seq=$cn->sql_first_row($sth)->[0]; | ||
| 671 | } | ||
| 672 | |||
| 673 | return $seq-1; | ||
| 674 | } | ||
| 675 | |||
| 676 | ############################################################################### | ||
| 677 | |||
| 678 | =item initialize_database ($) | ||
| 679 | |||
| 680 | Removes all data from all tables and creates minimal tables that support | ||
| 681 | objects database. | ||
| 682 | |||
| 683 | =cut | ||
| 684 | |||
| 685 | sub initialize_database ($) { | ||
| 686 | my $self=shift; | ||
| 687 | |||
| 688 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 689 | throw $self "initialize_database - modifying structure in transaction scope is not supported"; | ||
| 690 | } | ||
| 691 | |||
| 692 | my $cn=$self->connector; | ||
| 693 | my $sth=$cn->sql_execute('SHOW TABLE STATUS'); | ||
| 694 | my $table_type=$self->{'table_type'}; | ||
| 695 | while(my $row=$self->connector->sql_fetch_row($sth)) { | ||
| 696 | my ($name,$type)=@$row; | ||
| 697 | $table_type||=lc($type); | ||
| 698 | $cn->sql_do("DROP TABLE $name"); | ||
| 699 | } | ||
| 700 | $cn->sql_finish($sth); | ||
| 701 | |||
| 702 | my @initseq=( | ||
| 703 | <<'END_OF_SQL', | ||
| 704 | CREATE TABLE Global_Fields ( | ||
| 705 | unique_id INT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
| 706 | table_name_ CHAR(30) CHARACTER SET utf8 NOT NULL DEFAULT '', | ||
| 707 | field_name_ CHAR(30) CHARACTER SET utf8 NOT NULL DEFAULT '', | ||
| 708 | type_ CHAR(20) CHARACTER SET latin1 NOT NULL DEFAULT '', | ||
| 709 | refers_ CHAR(30) CHARACTER SET latin1 DEFAULT NULL, | ||
| 710 | key_format_ CHAR(100) CHARACTER SET utf8 DEFAULT NULL, | ||
| 711 | key_seq_ INT UNSIGNED DEFAULT NULL, | ||
| 712 | index_ TINYINT DEFAULT NULL, | ||
| 713 | default_ VARBINARY(30) DEFAULT NULL, | ||
| 714 | charset_ CHAR(30) CHARACTER SET latin1 DEFAULT NULL, | ||
| 715 | maxlength_ INT UNSIGNED DEFAULT NULL, | ||
| 716 | maxvalue_ DOUBLE DEFAULT NULL, | ||
| 717 | minvalue_ DOUBLE DEFAULT NULL, | ||
| 718 | PRIMARY KEY (table_name_,field_name_), | ||
| 719 | UNIQUE KEY unique_id (unique_id) | ||
| 720 | ) | ||
| 721 | END_OF_SQL | ||
| 722 | <<'END_OF_SQL', | ||
| 723 | INSERT INTO Global_Fields VALUES (1,'Global_Data','project', | ||
| 724 | 'text','',NULL,NULL,0,'','utf8',40,NULL,NULL) | ||
| 725 | END_OF_SQL | ||
| 726 | <<'END_OF_SQL', | ||
| 727 | CREATE TABLE Global_Data ( | ||
| 728 | unique_id INT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
| 729 | project_ char(40) CHARACTER SET utf8 NOT NULL DEFAULT '', | ||
| 730 | PRIMARY KEY (unique_id) | ||
| 731 | ) | ||
| 732 | END_OF_SQL | ||
| 733 | <<'END_OF_SQL', | ||
| 734 | INSERT INTO Global_Data VALUES (1,'XAO::FS New Database') | ||
| 735 | END_OF_SQL | ||
| 736 | <<'END_OF_SQL', | ||
| 737 | CREATE TABLE Global_Classes ( | ||
| 738 | unique_id int(10) unsigned NOT NULL AUTO_INCREMENT, | ||
| 739 | class_name_ char(100) NOT NULL DEFAULT '', | ||
| 740 | table_name_ char(30) NOT NULL DEFAULT '', | ||
| 741 | PRIMARY KEY (unique_id), | ||
| 742 | UNIQUE KEY (class_name_) | ||
| 743 | ) | ||
| 744 | END_OF_SQL | ||
| 745 | <<'END_OF_SQL', | ||
| 746 | INSERT INTO Global_Classes VALUES (1,'FS::Global','Global_Data') | ||
| 747 | END_OF_SQL | ||
| 748 | ); | ||
| 749 | |||
| 750 | foreach my $sql (@initseq) { | ||
| 751 | $sql.=" ENGINE=$table_type" if $table_type && $sql =~ /^CREATE/; | ||
| 752 | $cn->sql_do($sql); | ||
| 753 | } | ||
| 754 | } | ||
| 755 | |||
| 756 | ############################################################################### | ||
| 757 | |||
| 758 | =item list_keys ($$$$) | ||
| 759 | |||
| 760 | Returns a reference to an array containing all possible values of a | ||
| 761 | given field (list key) in the given table. If connector is given - then | ||
| 762 | it is used in select too. | ||
| 763 | |||
| 764 | =cut | ||
| 765 | |||
| 766 | sub list_keys ($$$$$) { | ||
| 767 | my $self=shift; | ||
| 768 | my ($table,$key,$conn_name,$conn_value)=@_; | ||
| 769 | $key.='_' unless $key eq 'unique_id'; | ||
| 770 | $conn_name.='_' if $conn_name; | ||
| 771 | |||
| 772 | my $cn=$self->connector; | ||
| 773 | |||
| 774 | my $sth; | ||
| 775 | if($conn_name) { | ||
| 776 | $sth=$cn->sql_execute("SELECT $key FROM $table WHERE $conn_name=?", | ||
| 777 | $conn_value); | ||
| 778 | } | ||
| 779 | else { | ||
| 780 | $sth=$cn->sql_execute("SELECT $key FROM $table"); | ||
| 781 | } | ||
| 782 | |||
| 783 | return $cn->sql_first_column($sth); | ||
| 784 | } | ||
| 785 | |||
| 786 | ############################################################################### | ||
| 787 | |||
| 788 | =item load_structure () | ||
| 789 | |||
| 790 | Loads Global_Fields and Global_Classes tables into internal hash for | ||
| 791 | use in Glue. | ||
| 792 | |||
| 793 | Returns the hash reference. | ||
| 794 | |||
| 795 | B<TODO:> This should be changed so that data types would not be | ||
| 796 | hard-coded here. Probably a reference to a subroutine that will parse | ||
| 797 | and store them would do the job? | ||
| 798 | |||
| 799 | =cut | ||
| 800 | |||
| 801 | sub load_structure ($) { | ||
| 802 | my $self=shift; | ||
| 803 | |||
| 804 | if($self->tr_loc_active || $self->tr_ext_active) { | ||
| 805 | throw $self "load_structure - modifying structure in transaction scope is not supported"; | ||
| 806 | } | ||
| 807 | |||
| 808 | my $cn=$self->connector; | ||
| 809 | my $sth; | ||
| 810 | |||
| 811 | # Checking table types, if requested | ||
| 812 | # | ||
| 813 | my %table_types; | ||
| 814 | if($self->{'consistency_check_requested'}) { | ||
| 815 | ### dprint "Meta-data consistency check requested, loading 'table status', etc"; | ||
| 816 | |||
| 817 | my %type_counts; | ||
| 818 | my $table_count=0; | ||
| 819 | |||
| 820 | $sth=$cn->sql_execute("SHOW TABLE STATUS"); | ||
| 821 | while(my $row=$cn->sql_fetch_row($sth)) { | ||
| 822 | my ($name,$type,$row_format,$rows)=@$row; | ||
| 823 | $type=lc($type); | ||
| 824 | $type_counts{$type}++; | ||
| 825 | $table_types{$name}=$type; | ||
| 826 | $table_count++; | ||
| 827 | # dprint "Table '$name', type=$type, row_format=$row_format, rows=$rows"; | ||
| 828 | } | ||
| 829 | |||
| 830 | if($self->{'table_type'}) { | ||
| 831 | my $table_type=$self->{'table_type'}; | ||
| 832 | if($table_type eq 'innodb' || $table_type eq 'myisam') { | ||
| 833 | my $warning_shown; | ||
| 834 | foreach my $table_name (keys %table_types) { | ||
| 835 | next if $table_types{$table_name} eq $table_type; | ||
| 836 | |||
| 837 | my $debug_status=XAO::Utils::get_debug(); | ||
| 838 | XAO::Utils::set_debug(1); | ||
| 839 | |||
| 840 | unless($warning_shown) { | ||
| 841 | dprint "Some tables in your database differ from the requested table type ($table_type)"; | ||
| 842 | dprint "In 5 seconds they will be converted to the new table type, interrupt to abort.."; | ||
| 843 | sleep 5; | ||
| 844 | dprint "Converting..."; | ||
| 845 | $warning_shown=1; | ||
| 846 | } | ||
| 847 | |||
| 848 | dprint "...table '$table_name' type from '$table_types{$table_name}' to '$table_type'"; | ||
| 849 | $cn->sql_do("ALTER TABLE $table_name ENGINE=$table_type"); | ||
| 850 | |||
| 851 | XAO::Utils::set_debug($debug_status); | ||
| 852 | } | ||
| 853 | } | ||
| 854 | else { | ||
| 855 | throw $self "Unsupported table_type '$table_type' in DSN options"; | ||
| 856 | } | ||
| 857 | } | ||
| 858 | elsif($type_counts{'innodb'} && $type_counts{'innodb'}==$table_count) { | ||
| 859 | $self->{'table_type'}='innodb'; | ||
| 860 | } | ||
| 861 | elsif($type_counts{'myisam'} && $type_counts{'myisam'}==$table_count) { | ||
| 862 | $self->{'table_type'}='myisam'; | ||
| 863 | } | ||
| 864 | else { | ||
| 865 | $self->{'table_type'}='mixed'; | ||
| 866 | eprint "You have mixed table types in the database (" . | ||
| 867 | join(',',map { $_ . '=' . $table_types{$_} } sort keys %table_types) . | ||
| 868 | ")"; | ||
| 869 | } | ||
| 870 | |||
| 871 | # Checking if Global_Fields table has key_format_ and key_seq_ | ||
| 872 | # fields. Adding them if it does not. | ||
| 873 | # | ||
| 874 | $sth=$cn->sql_execute("DESC Global_Fields"); | ||
| 875 | my $flist=$cn->sql_first_column($sth); | ||
| 876 | if(! grep { $_ eq 'key_format_' } @$flist) { | ||
| 877 | dprint "Old database detected, adding Global_Fields.key_format_"; | ||
| 878 | $cn->sql_do('ALTER TABLE Global_Fields ADD key_format_ CHAR(100) CHARACTER SET latin1 DEFAULT NULL'); | ||
| 879 | } | ||
| 880 | if(! grep { $_ eq 'key_seq_' } @$flist) { | ||
| 881 | dprint "Old database detected, adding Global_Fields.key_seq_"; | ||
| 882 | $cn->sql_do('ALTER TABLE Global_Fields ADD key_seq_ INT UNSIGNED DEFAULT NULL'); | ||
| 883 | } | ||
| 884 | |||
| 885 | ## | ||
| 886 | # Checking for charset field, adding it if needed. | ||
| 887 | # | ||
| 888 | if(! grep { $_ eq 'charset_' } @$flist) { | ||
| 889 | dprint "Old database detected, adding Global_Fields.charset_"; | ||
| 890 | $cn->sql_do(q{ALTER TABLE Global_Fields ADD charset_ CHAR(30) CHARACTER SET latin1 DEFAULT NULL}); | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | # Since we don't do 'show table status' by default any more it is | ||
| 895 | # possible that we won't know the table type. Warning about it and | ||
| 896 | # assuming transactional without checking. | ||
| 897 | # | ||
| 898 | if(!$self->{'table_type'}) { | ||
| 899 | eprint "It is recommended to always have '...;table_type=TYPE' as part of DSN, assuming 'innodb'"; | ||
| 900 | $self->{'table_type'}='innodb'; | ||
| 901 | } | ||
| 902 | |||
| 903 | # Loading fields descriptions from the database. | ||
| 904 | # | ||
| 905 | my %fields; | ||
| 906 | my %tkeys; | ||
| 907 | my %ckeys; | ||
| 908 | my %tbdesc; | ||
| 909 | $sth=$cn->sql_execute( | ||
| 910 | "SELECT unique_id,table_name_,field_name_," . | ||
| 911 | "type_,refers_,key_format_," . | ||
| 912 | "index_,default_,charset_," . | ||
| 913 | "maxlength_,minvalue_,maxvalue_" . | ||
| 914 | " FROM Global_Fields"); | ||
| 915 | |||
| 916 | while(my $row=$cn->sql_fetch_row($sth)) { | ||
| 917 | my ($uid,$table,$field,$type,$refers,$key_format, | ||
| 918 | $index,$default,$charset,$maxlength,$minvalue,$maxvalue)=@$row; | ||
| 919 | |||
| 920 | ## | ||
| 921 | # Getting table description from the database. Comparing default | ||
| 922 | # values later. If they contain spaces the database is probably | ||
| 923 | # a badly converted 4.1 to 5.0, needs some rebuilding. | ||
| 924 | # | ||
| 925 | if(!$tbdesc{$table}) { | ||
| 926 | my $tbsth=$cn->sql_execute("DESC $table"); | ||
| 927 | while(my $tbrow=$cn->sql_fetch_row($tbsth)) { | ||
| 928 | my ($tb_field,$tb_type,$tb_null,$tb_key,$tb_default)=@$tbrow; | ||
| 929 | $tbdesc{$table}->{$tb_field}={ | ||
| 930 | type => $tb_type, | ||
| 931 | null => $tb_null, | ||
| 932 | key => $tb_key, | ||
| 933 | default => $tb_default, | ||
| 934 | }; | ||
| 935 | } | ||
| 936 | } | ||
| 937 | |||
| 938 | my $data; | ||
| 939 | if($type eq 'list') { | ||
| 940 | $refers || $self->throw("load_structure - no class name at Global_Fields($table,$field,..)"); | ||
| 941 | $data={ | ||
| 942 | type => $type, | ||
| 943 | class => $refers, | ||
| 944 | }; | ||
| 945 | $ckeys{$refers}=$field; | ||
| 946 | } | ||
| 947 | elsif($type eq 'key') { | ||
| 948 | $refers || $self->throw("load_structure - no class name at Global_Fields($table,$field,..)"); | ||
| 949 | $data={ | ||
| 950 | type => $type, | ||
| 951 | refers => $refers, | ||
| 952 | key_format => $key_format || '<$RANDOM$>', | ||
| 953 | key_unique_id => $uid, | ||
| 954 | key_length => $maxlength || 30, | ||
| 955 | key_charset => $charset, | ||
| 956 | }; | ||
| 957 | $tkeys{$table}=$field; | ||
| 958 | } | ||
| 959 | elsif($type eq 'connector') { | ||
| 960 | $refers || $self->throw("load_structure - no class name at Global_Fields($table,$field,..)"); | ||
| 961 | $data={ | ||
| 962 | type => $type, | ||
| 963 | refers => $refers | ||
| 964 | }; | ||
| 965 | } | ||
| 966 | elsif($type eq 'blob') { | ||
| 967 | $data={ | ||
| 968 | type => $type, | ||
| 969 | index => $index ? 1 : 0, | ||
| 970 | unique => $index==2 ? 1 : 0, | ||
| 971 | default => $default, | ||
| 972 | maxlength => $maxlength, | ||
| 973 | }; | ||
| 974 | } | ||
| 975 | elsif($type eq 'text' || $type eq 'words') { | ||
| 976 | $data={ | ||
| 977 | type => $type, | ||
| 978 | index => $index ? 1 : 0, | ||
| 979 | unique => $index==2 ? 1 : 0, | ||
| 980 | default => $default, | ||
| 981 | maxlength => $maxlength || 100, | ||
| 982 | charset => $charset, | ||
| 983 | }; | ||
| 984 | } | ||
| 985 | elsif($type eq 'real' || $type eq 'integer') { | ||
| 986 | $data={ | ||
| 987 | type => $type, | ||
| 988 | index => $index ? 1 : 0, | ||
| 989 | unique => $index==2 ? 1 : 0, | ||
| 990 | default => $default, | ||
| 991 | minvalue => defined($minvalue) ? 0+$minvalue : undef, | ||
| 992 | maxvalue => defined($maxvalue) ? 0+$maxvalue : undef, | ||
| 993 | scale => ($type eq 'real' ? $maxlength : 0), | ||
| 994 | }; | ||
| 995 | } | ||
| 996 | else { | ||
| 997 | $self->throw("load_structure - unknown type ($type) for table=$table, field=$field"); | ||
| 998 | } | ||
| 999 | $fields{$table}->{$field}=$data; | ||
| 1000 | } | ||
| 1001 | $cn->sql_finish($sth); | ||
| 1002 | |||
| 1003 | # Doing some meta-data consistency checks & automatic upgrades if we are asked to. | ||
| 1004 | # | ||
| 1005 | if($self->{'consistency_check_requested'}) { | ||
| 1006 | |||
| 1007 | # Checking if this is a 4.1 database running in a 5.0 engine. In | ||
| 1008 | # this situation all our text & binary fields will get unstripped | ||
| 1009 | # (and unstrippable!) spaces in the end. | ||
| 1010 | # | ||
| 1011 | my $need_50_upgrade=$ENV{'XAO_MYSQL_50_UPGRADE'}; | ||
| 1012 | if(!$need_50_upgrade && !$ENV{'XAO_MYSQL_50_SKIP_UPGRADE'}) { | ||
| 1013 | TABLE_LOOP: | ||
| 1014 | foreach my $table (keys %fields) { | ||
| 1015 | my $flist=$fields{$table}; | ||
| 1016 | foreach my $fname (keys %$flist) { | ||
| 1017 | my $type=$flist->{$fname}->{'type'}; | ||
| 1018 | next if ($type eq 'list' || $type eq 'key' || $type eq 'connector'); | ||
| 1019 | |||
| 1020 | my $tbdata=$tbdesc{$table}->{$fname.'_'} || | ||
| 1021 | throw $self "load_structure - no table definition for $fname in $table (how could it be?)"; | ||
| 1022 | |||
| 1023 | if(defined($tbdata->{'default'}) && $tbdata->{'default'} =~ /^\s+$/) { | ||
| 1024 | if(defined($flist->{$fname}->{'default'}) && $tbdata->{'default'} eq $flist->{$fname}->{'default'}) { | ||
| 1025 | eprint "Very suspicious spaces in field defaults, is it a 4.1 database running in 5.0 engine?"; | ||
| 1026 | eprint "Set XAO_MYSQL_50_UPGRADE if you want to force an upgrade"; | ||
| 1027 | next; | ||
| 1028 | } | ||
| 1029 | else { | ||
| 1030 | $need_50_upgrade=1; | ||
| 1031 | last TABLE_LOOP; | ||
| 1032 | } | ||
| 1033 | } | ||
| 1034 | elsif($flist->{$fname}->{'default'} =~ m/^\s{30}$/) { | ||
| 1035 | $need_50_upgrade=1; | ||
| 1036 | last TABLE_LOOP; | ||
| 1037 | } | ||
| 1038 | } | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | } | ||
| 1042 | if($need_50_upgrade) { | ||
| 1043 | my $debug_status=XAO::Utils::get_debug(); | ||
| 1044 | XAO::Utils::set_debug(1); | ||
| 1045 | dprint "This database seems to be a 4.1 MySQL database running in a 5.0 MySQL engine."; | ||
| 1046 | dprint "Difference in treating trailing spaces will prevent the database from working properly."; | ||
| 1047 | dprint "If you believe this to be incorrect please set XAO_MYSQL_50_SKIP_UPGRADE env. variable."; | ||
| 1048 | dprint "**"; | ||
| 1049 | dprint "** A fully automatic upgrade will be attempted in 15 seconds, interrupt to abort."; | ||
| 1050 | dprint "** It MAY BE dangerous, a restore from a textual backup made in 4.1 is always better!"; | ||
| 1051 | dprint "** The update may take a long time and it's better to let it run all the way through"; | ||
| 1052 | dprint "** once it is started"; | ||
| 1053 | dprint "**"; | ||
| 1054 | sleep 15; | ||
| 1055 | |||
| 1056 | dprint "Converting meta-data (Global_Fields & Global_Classes) to new format"; | ||
| 1057 | dprint "-"; | ||
| 1058 | my %to_convert; | ||
| 1059 | my %metatables=( | ||
| 1060 | Global_Fields => [ | ||
| 1061 | ['table_name_','latin1',30,''], | ||
| 1062 | ['field_name_','latin1',30,''], | ||
| 1063 | ['type_','latin1',20,undef], | ||
| 1064 | ['refers_','latin1',30,undef], | ||
| 1065 | ['key_format_','latin1',100,undef], | ||
| 1066 | ['charset_','latin1',30,undef], | ||
| 1067 | ['default_','binary',30,undef], | ||
| 1068 | ], | ||
| 1069 | Global_Classes => [ | ||
| 1070 | ['class_name_','latin1',100,''], | ||
| 1071 | ['table_name_','latin1',30,''], | ||
| 1072 | ], | ||
| 1073 | ); | ||
| 1074 | foreach my $table (keys %metatables) { | ||
| 1075 | my $sql=''; | ||
| 1076 | my @def; | ||
| 1077 | foreach my $fd (@{$metatables{$table}}) { | ||
| 1078 | my ($fname,$charset,$maxlength,$default)=@$fd; | ||
| 1079 | my $tdef=$self->text_field_definition($charset,$maxlength,defined $default ? undef : 1); | ||
| 1080 | $sql.=', ' if $sql; | ||
| 1081 | $sql.="CHANGE $fname $fname $tdef"; | ||
| 1082 | push(@def,$default) if defined $default; | ||
| 1083 | $to_convert{$table}->{$fname}=1; | ||
| 1084 | } | ||
| 1085 | $sql="ALTER TABLE $table $sql"; | ||
| 1086 | dprint "-- $sql"; | ||
| 1087 | $cn->sql_do($sql,@def); | ||
| 1088 | } | ||
| 1089 | dprint "-"; | ||
| 1090 | |||
| 1091 | dprint "Converting BINARY fields to VARBINARY as this is the only way to store unpadded"; | ||
| 1092 | dprint "strings of varying length"; | ||
| 1093 | dprint "-"; | ||
| 1094 | foreach my $table (keys %tbdesc) { | ||
| 1095 | my $tbdata=$tbdesc{$table}; | ||
| 1096 | my $sql=''; | ||
| 1097 | my @def; | ||
| 1098 | foreach my $fname (keys %$tbdata) { | ||
| 1099 | next if $fname eq 'unique_id'; | ||
| 1100 | |||
| 1101 | my $sfname=substr($fname,0,-1); | ||
| 1102 | my $fdata=$fields{$table}->{$sfname} || | ||
| 1103 | throw $self "load_structure - can't find field description $table/$fname/$sfname"; | ||
| 1104 | |||
| 1105 | my $charset; | ||
| 1106 | my $maxlength; | ||
| 1107 | my $default; | ||
| 1108 | if($fdata->{'type'} eq 'blob') { | ||
| 1109 | $charset='binary'; | ||
| 1110 | $maxlength=$fdata->{'maxlength'}; | ||
| 1111 | $default=$fdata->{'default'}; | ||
| 1112 | } | ||
| 1113 | elsif($fdata->{'type'} eq 'text') { | ||
| 1114 | $charset=$fdata->{'charset'} || throw $self "load_structure - no charset in '$fname'"; | ||
| 1115 | $maxlength=$fdata->{'maxlength'}; | ||
| 1116 | $default=$fdata->{'default'}; | ||
| 1117 | } | ||
| 1118 | elsif($fdata->{'type'} eq 'key') { | ||
| 1119 | $charset=$fdata->{'key_charset'} || throw $self "load_structure - no key_charset in '$fname'"; | ||
| 1120 | $maxlength=$fdata->{'key_length'}; | ||
| 1121 | $default=''; | ||
| 1122 | } | ||
| 1123 | else { | ||
| 1124 | next; # nothing to do for other types | ||
| 1125 | } | ||
| 1126 | ### dprint "fname=$fname, charset=$charset, ml=$maxlength, def=$default"; | ||
| 1127 | |||
| 1128 | # Multiple spaces in defaults are probably there because | ||
| 1129 | # the table is in the old format. Stripping them to the | ||
| 1130 | # empty string. | ||
| 1131 | # | ||
| 1132 | if($default =~ /^\s+$/) { | ||
| 1133 | $default=''; | ||
| 1134 | $fdata->{'default'}=''; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | # Converting all strings, not just BINARY columns to update the DEFAULT value | ||
| 1138 | # | ||
| 1139 | my $tdef=$self->text_field_definition($charset,$maxlength); | ||
| 1140 | $sql.=', ' if $sql; | ||
| 1141 | $sql.="CHANGE $fname $fname $tdef"; | ||
| 1142 | push(@def,$default); | ||
| 1143 | |||
| 1144 | $to_convert{$table}->{$fname}=1; | ||
| 1145 | } | ||
| 1146 | if($sql) { | ||
| 1147 | $sql="ALTER TABLE $table $sql"; | ||
| 1148 | dprint "-- $sql"; | ||
| 1149 | $cn->sql_do($sql,@def); | ||
| 1150 | } | ||
| 1151 | } | ||
| 1152 | dprint "-"; | ||
| 1153 | |||
| 1154 | if(!$ENV{'XAO_MYSQL_50_SKIP_TABLE_TYPE'}) { | ||
| 1155 | my @uptables=('Global_Fields','Global_Classes',keys %tbdesc); | ||
| 1156 | dprint "Upgrading tables to the new binary format."; | ||
| 1157 | dprint "The following tables will be upgraded:"; | ||
| 1158 | dprint join(',',@uptables); | ||
| 1159 | dprint "-"; | ||
| 1160 | foreach my $table (@uptables) { | ||
| 1161 | my $sql="ALTER TABLE $table ENGINE=$table_types{$table}"; | ||
| 1162 | dprint "-- $sql"; | ||
| 1163 | $cn->sql_do($sql); | ||
| 1164 | } | ||
| 1165 | dprint "-"; | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | dprint "Trimming trailing spaces on all text & binary fields."; | ||
| 1169 | dprint "Since spaces were always trimmed in MySQL up to 4.1 supposedly it is safe."; | ||
| 1170 | dprint "Use XAO_MYSQL_50_BROKEN_BINARY=1 if some fields got zero-padded (unsafe)."; | ||
| 1171 | dprint "-"; | ||
| 1172 | foreach my $table (keys %to_convert) { | ||
| 1173 | foreach my $fname (keys %{$to_convert{$table}}) { | ||
| 1174 | my $sql; | ||
| 1175 | if($ENV{'XAO_MYSQL_50_BROKEN_BINARY'}) { | ||
| 1176 | $sql="UPDATE $table SET $fname=LEFT($fname,LENGTH(RTRIM(REPLACE($fname,'\0',' '))))"; | ||
| 1177 | } | ||
| 1178 | else { | ||
| 1179 | $sql="UPDATE $table SET $fname=RTRIM($fname)"; | ||
| 1180 | } | ||
| 1181 | dprint "-- $sql"; | ||
| 1182 | $cn->sql_do($sql); | ||
| 1183 | } | ||
| 1184 | } | ||
| 1185 | dprint "-"; | ||
| 1186 | dprint "All done"; | ||
| 1187 | |||
| 1188 | XAO::Utils::set_debug($debug_status); | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | # Checking that we have a charset for every text field. Altering | ||
| 1192 | # tables to binary for compatibility if the charset is not defined. | ||
| 1193 | # | ||
| 1194 | my $warning_shown; | ||
| 1195 | foreach my $table (keys %fields) { | ||
| 1196 | my $debug_status=XAO::Utils::get_debug(); | ||
| 1197 | XAO::Utils::set_debug(1); | ||
| 1198 | |||
| 1199 | my $sql=''; | ||
| 1200 | my $flist=$fields{$table}; | ||
| 1201 | my @deflist; | ||
| 1202 | my @altered_fields; | ||
| 1203 | foreach my $fname (keys %$flist) { | ||
| 1204 | my $fdata=$flist->{$fname}; | ||
| 1205 | |||
| 1206 | next unless ($fdata->{'type'} eq 'text' && !$fdata->{'charset'}) || | ||
| 1207 | ($fdata->{'type'} eq 'key' && !$fdata->{'key_charset'}); | ||
| 1208 | |||
| 1209 | unless($warning_shown) { | ||
| 1210 | dprint "Some key and text fields in your database have no 'charset' value"; | ||
| 1211 | dprint "In 10 seconds we will start converting them to 'binary' for behavior compatibility."; | ||
| 1212 | dprint "For large datasets this may require a lot of time, interrupt to abort."; | ||
| 1213 | sleep 10; | ||
| 1214 | $warning_shown=1; | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | dprint "...table '$table' has empty charset for text field '$fname', altering to 'binary'"; | ||
| 1218 | |||
| 1219 | my $default; | ||
| 1220 | my $maxlength; | ||
| 1221 | if($fdata->{'type'} eq 'text') { | ||
| 1222 | $default=$fdata->{'default'}; | ||
| 1223 | $maxlength=$fdata->{'maxlength'}; | ||
| 1224 | } | ||
| 1225 | else { | ||
| 1226 | $default=''; | ||
| 1227 | $maxlength=$fdata->{'key_length'}; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | my $def=$self->text_field_definition('binary',$maxlength); | ||
| 1231 | |||
| 1232 | $sql.=', ' if $sql; | ||
| 1233 | $sql.="CHANGE ".$fname."_ ".$fname."_ ".$def; | ||
| 1234 | |||
| 1235 | push(@deflist,$default); | ||
| 1236 | push(@altered_fields,$fname); | ||
| 1237 | |||
| 1238 | if($fdata->{'type'} eq 'text') { | ||
| 1239 | $fdata->{'charset'}='binary'; | ||
| 1240 | } | ||
| 1241 | else { | ||
| 1242 | $fdata->{'key_charset'}='binary'; | ||
| 1243 | } | ||
| 1244 | } | ||
| 1245 | if($sql) { | ||
| 1246 | dprint "....preparing to execute, LAST CHANCE TO ABORT"; | ||
| 1247 | sleep 3; | ||
| 1248 | dprint ".....executing SQL instructions, do not interrupt"; | ||
| 1249 | $sql="ALTER TABLE $table $sql"; | ||
| 1250 | ### dprint $sql; | ||
| 1251 | $cn->sql_do($sql,@deflist); | ||
| 1252 | foreach my $fname (@altered_fields) { | ||
| 1253 | $sql="UPDATE Global_Fields SET charset_='binary' WHERE table_name_='$table' AND field_name_='$fname'"; | ||
| 1254 | ### dprint $sql; | ||
| 1255 | $cn->sql_do($sql); | ||
| 1256 | } | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | XAO::Utils::set_debug($debug_status); | ||
| 1260 | } | ||
| 1261 | } | ||
| 1262 | |||
| 1263 | # Now loading classes translation table and putting fields | ||
| 1264 | # descriptions inside of it as well. | ||
| 1265 | # | ||
| 1266 | $sth=$cn->sql_execute("SELECT class_name_,table_name_ FROM Global_Classes"); | ||
| 1267 | my %classes; | ||
| 1268 | while(my $row=$cn->sql_fetch_row($sth)) { | ||
| 1269 | my ($class,$table)=@$row; | ||
| 1270 | my $f=$fields{$table}; | ||
| 1271 | $f || $self->throw("load_structure - no description for $table table (class $class)"); | ||
| 1272 | $classes{$class}={ | ||
| 1273 | table => $table, | ||
| 1274 | fields => $f, | ||
| 1275 | }; | ||
| 1276 | } | ||
| 1277 | $cn->sql_finish($sth); | ||
| 1278 | |||
| 1279 | # Copying key related stuff to list description which is very | ||
| 1280 | # helpful for build_structure | ||
| 1281 | # | ||
| 1282 | foreach my $class (keys %ckeys) { | ||
| 1283 | my $upper_key_name=$ckeys{$class}; | ||
| 1284 | my ($data,$table)=@{$classes{$class}}{'fields','table'}; | ||
| 1285 | my $key_name=$tkeys{$table}; | ||
| 1286 | my $key_data=$data->{$key_name}; | ||
| 1287 | my $upper_data=$classes{$key_data->{'refers'}}->{'fields'}->{$upper_key_name}; | ||
| 1288 | @{$upper_data}{qw(key key_format key_length key_charset)}= | ||
| 1289 | ($key_name,@{$key_data}{qw(key_format key_length key_charset)}); | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | # Marking that we loaded structure, and optionally that we checked meta-data consistency | ||
| 1293 | # | ||
| 1294 | $self->{'structure_loaded'}=1; | ||
| 1295 | $self->{'consistency_checked'}=$self->{'consistency_check_requested'}; | ||
| 1296 | |||
| 1297 | # Resulting structure | ||
| 1298 | # | ||
| 1299 | return \%classes; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | ############################################################################### | ||
| 1303 | |||
| 1304 | =item mangle_field_name ($) | ||
| 1305 | |||
| 1306 | Adds underscore to the end of field name to avoid problems with reserved | ||
| 1307 | words. Could do something else in other drivers, do not count on the | ||
| 1308 | fact that there would be underscore at the end. | ||
| 1309 | |||
| 1310 | =cut | ||
| 1311 | |||
| 1312 | sub mangle_field_name ($$) { | ||
| 1313 | my $self=shift; | ||
| 1314 | my $name=shift; | ||
| 1315 | defined($name) ? $name . '_' : undef; | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | ############################################################################### | ||
| 1319 | |||
| 1320 | =item reset () | ||
| 1321 | |||
| 1322 | Brings driver to usable state. Unlocks tables if they were somehow left | ||
| 1323 | in locked state. Reconnects to the database if the connection expired. | ||
| 1324 | |||
| 1325 | =cut | ||
| 1326 | |||
| 1327 | sub reset () { | ||
| 1328 | my $self=shift; | ||
| 1329 | |||
| 1330 | if($self->connector->sql_connected) { | ||
| 1331 | if($self->{'table_type'} eq 'innodb') { | ||
| 1332 | $self->tr_loc_rollback(); | ||
| 1333 | } | ||
| 1334 | else { | ||
| 1335 | $self->unlock_tables(); | ||
| 1336 | } | ||
| 1337 | } | ||
| 1338 | else { # no point unlocking tables if we just reconnected | ||
| 1339 | dprint "Database connection expired, re-connecting"; | ||
| 1340 | $self->connector->sql_connect; | ||
| 1341 | } | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | ############################################################################### | ||
| 1345 | |||
| 1346 | =item retrieve_fields ($$$@) | ||
| 1347 | |||
| 1348 | Retrieves individual fields from the given table by unique ID of the | ||
| 1349 | row. Always returns array reference even if there is just one field in | ||
| 1350 | it. | ||
| 1351 | |||
| 1352 | =cut | ||
| 1353 | |||
| 1354 | sub retrieve_fields ($$$@) { | ||
| 1355 | my $self=shift; | ||
| 1356 | my $table=shift; | ||
| 1357 | my $unique_id=shift; | ||
| 1358 | |||
| 1359 | $unique_id || | ||
| 1360 | $self->throw("retrieve_field($table,...) - no unique_id given"); | ||
| 1361 | |||
| 1362 | my @names=map { $_ . '_' } @_; | ||
| 1363 | |||
| 1364 | my $sql=join(',',@names); | ||
| 1365 | $sql="SELECT $sql FROM $table WHERE unique_id=?"; | ||
| 1366 | |||
| 1367 | my $cn=$self->connector; | ||
| 1368 | my $sth=$cn->sql_execute($sql,$unique_id); | ||
| 1369 | return $cn->sql_first_row($sth); | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | ############################################################################### | ||
| 1373 | |||
| 1374 | =item search (\%query) | ||
| 1375 | |||
| 1376 | performs a search on the given query and returns a reference to an array | ||
| 1377 | of arrays containing search results. Query hash is as prepared by | ||
| 1378 | _build_search_query() in the Glue. | ||
| 1379 | |||
| 1380 | =cut | ||
| 1381 | |||
| 1382 | sub search ($%) { | ||
| 1383 | my $self=shift; | ||
| 1384 | my $query=get_args(\@_); | ||
| 1385 | |||
| 1386 | my $sql=$query->{'sql'}; | ||
| 1387 | |||
| 1388 | if($query->{'options'}) { | ||
| 1389 | my $limit=int($query->{'options'}->{'limit'} || 0); | ||
| 1390 | my $offset=int($query->{'options'}->{'offset'} || 0); | ||
| 1391 | |||
| 1392 | # MySQL has no syntax to specify offset without a non-zero | ||
| 1393 | # limit. Using a very large number per recommendation of mysql | ||
| 1394 | # docs. | ||
| 1395 | # | ||
| 1396 | if($limit>0 || $offset>0) { | ||
| 1397 | $sql.=' LIMIT '.($limit || '18446744073709551615'); | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | if($offset>0) { | ||
| 1401 | $sql.=' OFFSET '.$offset; | ||
| 1402 | } | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | # dprint "SQL: $sql"; | ||
| 1406 | |||
| 1407 | my $cn=$self->connector; | ||
| 1408 | my $sth=$cn->sql_execute($sql,$query->{'values'}); | ||
| 1409 | |||
| 1410 | if(scalar(@{$query->{'fields_list'}})>1) { | ||
| 1411 | my @results; | ||
| 1412 | |||
| 1413 | ## | ||
| 1414 | # We need to copy the array we get here to avoid replicating the | ||
| 1415 | # last row into all rows by using reference to the same array. | ||
| 1416 | # | ||
| 1417 | while(my $row=$cn->sql_fetch_row($sth)) { | ||
| 1418 | push @results,[ @$row ]; | ||
| 1419 | } | ||
| 1420 | $cn->sql_finish($sth); | ||
| 1421 | return \@results; | ||
| 1422 | } | ||
| 1423 | else { | ||
| 1424 | return $cn->sql_first_column($sth); | ||
| 1425 | } | ||
| 1426 | |||
| 1427 | } | ||
| 1428 | |||
| 1429 | ############################################################################### | ||
| 1430 | |||
| 1431 | =item search_clause_wq ($field $string) | ||
| 1432 | |||
| 1433 | Returns database specific syntax for REGEX matching a complete word | ||
| 1434 | if database supports it or undef otherwise. For MySQL returns REGEXP | ||
| 1435 | clause. | ||
| 1436 | |||
| 1437 | =cut | ||
| 1438 | |||
| 1439 | sub search_clause_wq ($$$) { | ||
| 1440 | my $self=shift; | ||
| 1441 | my ($field,$rha)=@_; | ||
| 1442 | $rha=~s/([\\'\[\]\|\{\}\(\)\.\*\?\$\^])/\\$1/g; | ||
| 1443 | ("$field REGEXP ?","[[:<:]]" . $rha . "[[:>:]]"); | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | ############################################################################### | ||
| 1447 | |||
| 1448 | =item search_clause_ws ($field $string) | ||
| 1449 | |||
| 1450 | Returns database specific syntax for REGEX matching the beginning of | ||
| 1451 | a word if database supports it or undef otherwise. For MySQL returns | ||
| 1452 | REGEXP clause. | ||
| 1453 | |||
| 1454 | =cut | ||
| 1455 | |||
| 1456 | sub search_clause_ws ($$$) { | ||
| 1457 | my $self=shift; | ||
| 1458 | my ($field,$rha)=@_; | ||
| 1459 | $rha=~s/([\\'\[\]])/\\$1/g; | ||
| 1460 | ("$field REGEXP ?","[[:<:]]$rha"); | ||
| 1461 | } | ||
| 1462 | |||
| 1463 | ############################################################################### | ||
| 1464 | |||
| 1465 | =item store_row ($$$$$$$) | ||
| 1466 | |||
| 1467 | Stores complete row of data into the given table. New name is generated | ||
| 1468 | in the given key field if there is no name given. | ||
| 1469 | |||
| 1470 | Example: | ||
| 1471 | |||
| 1472 | $self->_driver->store_row($table, | ||
| 1473 | $key_name,$key_value, | ||
| 1474 | $conn_name,$conn_value, | ||
| 1475 | \%row); | ||
| 1476 | |||
| 1477 | Connector name and connector value are optional if this list is directly | ||
| 1478 | underneath of Global. | ||
| 1479 | |||
| 1480 | =cut | ||
| 1481 | |||
| 1482 | sub store_row ($$$$$$$) { | ||
| 1483 | my $self=shift; | ||
| 1484 | my ($table,$key_name,$key_value,$conn_name,$conn_value,$row)=@_; | ||
| 1485 | $key_name.='_'; | ||
| 1486 | $conn_name.='_' if $conn_name; | ||
| 1487 | |||
| 1488 | # If we have no transaction support we need to lock Global_Fields | ||
| 1489 | # too as it might be used in AUTOINC key formats. | ||
| 1490 | # | ||
| 1491 | my @ltab; | ||
| 1492 | if($self->{'table_type'} eq 'innodb') { | ||
| 1493 | ### dprint "store_row: transaction begin"; | ||
| 1494 | $self->tr_loc_begin; | ||
| 1495 | } | ||
| 1496 | else { | ||
| 1497 | ### dprint "store_row: locking tables"; | ||
| 1498 | #@ltab=($table eq 'Global_Fields' || $table eq 'Global_Classes') ? ($table) : ($table,'Global_Fields','Global_Classes'); | ||
| 1499 | @ltab=($table eq 'Global_Fields') ? ($table) : ($table,'Global_Fields'); | ||
| 1500 | $self->lock_tables(@ltab); | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | my $uid; | ||
| 1504 | if(ref($key_value) eq 'CODE') { | ||
| 1505 | my $kv; | ||
| 1506 | while(1) { | ||
| 1507 | $kv=&{$key_value}; | ||
| 1508 | last unless $self->unique_id($table, | ||
| 1509 | $key_name,$kv, | ||
| 1510 | $conn_name,$conn_value, | ||
| 1511 | 1); | ||
| 1512 | } | ||
| 1513 | $key_value=$kv; | ||
| 1514 | } | ||
| 1515 | elsif($key_value) { | ||
| 1516 | $uid=$self->unique_id($table, | ||
| 1517 | $key_name,$key_value, | ||
| 1518 | $conn_name,$conn_value, | ||
| 1519 | 1); | ||
| 1520 | } | ||
| 1521 | else { | ||
| 1522 | throw $self "store_row - no key_value given (old usage??)"; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | # Trapping for errors. | ||
| 1526 | # | ||
| 1527 | # This seems to be only needed for MySQL < 5.0.38 with MyISAM, newer versions | ||
| 1528 | # auto-unlock and auto-rollback apparently. This code adds a pretty significant | ||
| 1529 | # penalty (about 10% based on bench.pl - probably less on DB bound cases). | ||
| 1530 | # So avoiding it where we can. | ||
| 1531 | # | ||
| 1532 | if(1) { | ||
| 1533 | my $connector=$self->connector; | ||
| 1534 | |||
| 1535 | local($SIG{'__DIE__'})=sub { | ||
| 1536 | my $msg=shift; | ||
| 1537 | if($self->{'table_type'} eq 'innodb') { | ||
| 1538 | $self->tr_loc_rollback; | ||
| 1539 | } | ||
| 1540 | else { | ||
| 1541 | $self->unlock_tables(@ltab); | ||
| 1542 | } | ||
| 1543 | die $msg; | ||
| 1544 | } if ($self->{'mysql_version'} < 5.00038 || $connector->need_unlock_on_error); | ||
| 1545 | |||
| 1546 | # Updating or inserting | ||
| 1547 | # | ||
| 1548 | if($uid) { | ||
| 1549 | # TODO: Needs to be split into local version that is called from | ||
| 1550 | # underneath transactional cover and "public" one. | ||
| 1551 | # | ||
| 1552 | $self->update_fields($table,$uid,$row,0); | ||
| 1553 | } | ||
| 1554 | else { | ||
| 1555 | my @fn=($key_name, map { $_.'_' } keys %{$row}); | ||
| 1556 | my @fv=($key_value, values %{$row}); | ||
| 1557 | if($conn_name && $conn_value) { | ||
| 1558 | unshift @fn,$conn_name; | ||
| 1559 | unshift @fv,$conn_value; | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | my $sql="INSERT INTO $table ("; | ||
| 1563 | $sql.=join(',',@fn); | ||
| 1564 | $sql.=') VALUES ('; | ||
| 1565 | $sql.=join(',',('?') x scalar(@fn)); | ||
| 1566 | $sql.=')'; | ||
| 1567 | $connector->sql_do($sql,\@fv); | ||
| 1568 | } | ||
| 1569 | } | ||
| 1570 | |||
| 1571 | if($self->{'table_type'} eq 'innodb') { | ||
| 1572 | $self->tr_loc_commit; | ||
| 1573 | } | ||
| 1574 | else { | ||
| 1575 | $self->unlock_tables(@ltab); | ||
| 1576 | } | ||
| 1577 | |||
| 1578 | return $key_value; | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | ############################################################################### | ||
| 1582 | |||
| 1583 | sub real_field_definition ($$$$) { | ||
| 1584 | my ($self,$min,$max,$scale)=@_; | ||
| 1585 | |||
| 1586 | # Real translates to two different field types depending on whether | ||
| 1587 | # the scale is set or not. | ||
| 1588 | # | ||
| 1589 | my $def; | ||
| 1590 | if($scale) { | ||
| 1591 | my $pmin=defined $min ? length(abs(int($min))) : 20; | ||
| 1592 | my $pmax=defined $max ? length(abs(int($max))) : 20; | ||
| 1593 | my $precision=$pmin>$pmax ? $pmin : $pmax; | ||
| 1594 | $precision+=$scale; | ||
| 1595 | $def="DECIMAL($precision,$scale)"; | ||
| 1596 | } | ||
| 1597 | else { | ||
| 1598 | $def="DOUBLE"; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | $def.=" NOT NULL DEFAULT ?"; | ||
| 1602 | |||
| 1603 | ### dprint "definition for real(",$min,",",$max,",",$scale,"): $def"; | ||
| 1604 | |||
| 1605 | return $def; | ||
| 1606 | } | ||
| 1607 | |||
| 1608 | ############################################################################### | ||
| 1609 | |||
| 1610 | =item text_field_definition | ||
| 1611 | |||
| 1612 | Returns a MySQL definition for the field suitable for use in CREATE | ||
| 1613 | TABLE or ALTER TABLE. | ||
| 1614 | |||
| 1615 | Something like "TEXT CHARACTER SET 'charset' NOT NULL DEFAULT ?". | ||
| 1616 | |||
| 1617 | Note that the default needs to be substituted in later! | ||
| 1618 | |||
| 1619 | =cut | ||
| 1620 | |||
| 1621 | sub text_field_definition ($$$;$) { | ||
| 1622 | my ($self,$charset,$maxlength,$null)=@_; | ||
| 1623 | |||
| 1624 | my $def; | ||
| 1625 | if($charset eq 'binary') { | ||
| 1626 | if($maxlength<255) { | ||
| 1627 | $def="VARBINARY($maxlength)"; | ||
| 1628 | } elsif($maxlength<65535) { | ||
| 1629 | $def="BLOB"; | ||
| 1630 | } elsif($maxlength<16777215) { | ||
| 1631 | $def="MEDIUMBLOB"; | ||
| 1632 | } else { | ||
| 1633 | $def="LONGBLOB"; | ||
| 1634 | } | ||
| 1635 | } | ||
| 1636 | else { | ||
| 1637 | if($maxlength<255) { | ||
| 1638 | $def="CHAR($maxlength)"; | ||
| 1639 | } elsif($maxlength<65535) { | ||
| 1640 | $def="TEXT"; | ||
| 1641 | } elsif($maxlength<16777215) { | ||
| 1642 | $def="MEDIUMTEXT"; | ||
| 1643 | } else { | ||
| 1644 | $def="LONGTEXT"; | ||
| 1645 | } | ||
| 1646 | $def.=" CHARACTER SET '$charset'"; | ||
| 1647 | } | ||
| 1648 | |||
| 1649 | return $null ? $def : "$def NOT NULL DEFAULT ?"; | ||
| 1650 | } | ||
| 1651 | |||
| 1652 | ############################################################################### | ||
| 1653 | |||
| 1654 | =item unique_id ($$$$$) | ||
| 1655 | |||
| 1656 | Looks up row unique ID by given key name and value (required) and | ||
| 1657 | connector name and value (optional for top level lists). | ||
| 1658 | |||
| 1659 | =cut | ||
| 1660 | |||
| 1661 | sub unique_id ($$$$$$$) { | ||
| 1662 | my $self=shift; | ||
| 1663 | my ($table,$key_name,$key_value,$conn_name,$conn_value,$translated)=@_; | ||
| 1664 | $key_name.='_' unless $translated; | ||
| 1665 | $conn_name.='_' unless $translated || !$conn_name; | ||
| 1666 | |||
| 1667 | my $cn=$self->connector; | ||
| 1668 | my $sth; | ||
| 1669 | if(defined($conn_name) && defined($conn_value)) { | ||
| 1670 | $sth=$cn->sql_execute("SELECT unique_id FROM $table WHERE $conn_name=? AND $key_name=?", | ||
| 1671 | ''.$conn_value,''.$key_value); | ||
| 1672 | } | ||
| 1673 | else { | ||
| 1674 | $sth=$cn->sql_execute("SELECT unique_id FROM $table WHERE $key_name=?", | ||
| 1675 | ''.$key_value); | ||
| 1676 | } | ||
| 1677 | |||
| 1678 | my $row=$cn->sql_first_row($sth); | ||
| 1679 | return $row ? $row->[0] : undef; | ||
| 1680 | } | ||
| 1681 | |||
| 1682 | ############################################################################### | ||
| 1683 | |||
| 1684 | =item update_fields ($$$;$) { | ||
| 1685 | |||
| 1686 | Stores new values. Example: | ||
| 1687 | |||
| 1688 | $self->_driver->update_field($table,$unique_id,{ name => 'value' }); | ||
| 1689 | |||
| 1690 | Optional last argument can be used to disable transactional wrapping if | ||
| 1691 | set to a non-zero value. | ||
| 1692 | |||
| 1693 | =cut | ||
| 1694 | |||
| 1695 | sub update_fields ($$$$;$) { | ||
| 1696 | my ($self,$table,$unique_id,$data,$internal)=@_; | ||
| 1697 | |||
| 1698 | ### dprint "update_fields($table,$unique_id,...)"; | ||
| 1699 | |||
| 1700 | $unique_id || | ||
| 1701 | throw $self "update_field($table,..) - no unique_id given"; | ||
| 1702 | |||
| 1703 | my @names=keys %$data; | ||
| 1704 | return unless @names; | ||
| 1705 | |||
| 1706 | my $sql="UPDATE $table SET "; | ||
| 1707 | $sql.=join(',',map { "${_}_=?" } @names); | ||
| 1708 | $sql.=' WHERE unique_id=?'; | ||
| 1709 | |||
| 1710 | if(!$internal && $self->{'table_type'} eq 'innodb') { | ||
| 1711 | #dprint "store_row: transaction begin"; | ||
| 1712 | $self->tr_loc_begin; | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | $self->connector->sql_do($sql,values %$data,$unique_id); | ||
| 1716 | |||
| 1717 | if(!$internal && $self->{'table_type'} eq 'innodb') { | ||
| 1718 | #dprint "store_row: transaction commit"; | ||
| 1719 | $self->tr_loc_commit; | ||
| 1720 | } | ||
| 1721 | } | ||
| 1722 | |||
| 1723 | ############################################################################### | ||
| 1724 | |||
| 1725 | =item tr_loc_active () | ||
| 1726 | |||
| 1727 | Checks if we currently have active local or external transaction. | ||
| 1728 | |||
| 1729 | =cut | ||
| 1730 | |||
| 1731 | sub tr_loc_active ($) { | ||
| 1732 | my $self=shift; | ||
| 1733 | return $self->{'tr_loc_active'} || $self->{'tr_ext_active'}; | ||
| 1734 | } | ||
| 1735 | |||
| 1736 | ############################################################################### | ||
| 1737 | |||
| 1738 | =item tr_loc_begin () | ||
| 1739 | |||
| 1740 | Starts new local transaction. Will only really start it if we do not | ||
| 1741 | have currently active external transaction. Does nothing for MyISAM. | ||
| 1742 | |||
| 1743 | =cut | ||
| 1744 | |||
| 1745 | sub tr_loc_begin ($) { | ||
| 1746 | my $self=shift; | ||
| 1747 | return if $self->{'table_type'} ne 'innodb' || | ||
| 1748 | $self->{'tr_ext_active'} || | ||
| 1749 | $self->{'tr_loc_active'}; | ||
| 1750 | $self->connector->sql_do('START TRANSACTION'); | ||
| 1751 | $self->{'tr_loc_active'}=1; | ||
| 1752 | } | ||
| 1753 | |||
| 1754 | ############################################################################### | ||
| 1755 | |||
| 1756 | =item tr_loc_commit () | ||
| 1757 | |||
| 1758 | Commits changes for local transaction if it is active. | ||
| 1759 | |||
| 1760 | =cut | ||
| 1761 | |||
| 1762 | sub tr_loc_commit ($) { | ||
| 1763 | my $self=shift; | ||
| 1764 | return unless $self->{'tr_loc_active'}; | ||
| 1765 | $self->connector->sql_do('COMMIT'); | ||
| 1766 | $self->{'tr_loc_active'}=0; | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | ############################################################################### | ||
| 1770 | |||
| 1771 | =item tr_loc_rollback () | ||
| 1772 | |||
| 1773 | Rolls back changes for local transaction if it is active. Called | ||
| 1774 | automatically on errors. | ||
| 1775 | |||
| 1776 | =cut | ||
| 1777 | |||
| 1778 | sub tr_loc_rollback ($) { | ||
| 1779 | my $self=shift; | ||
| 1780 | return unless $self->{'tr_loc_active'}; | ||
| 1781 | $self->connector->sql_do('ROLLBACK'); | ||
| 1782 | $self->{'tr_loc_active'}=0; | ||
| 1783 | } | ||
| 1784 | |||
| 1785 | ############################################################################### | ||
| 1786 | |||
| 1787 | =item tr_ext_active () | ||
| 1788 | |||
| 1789 | Checks if an external transaction is currently active. | ||
| 1790 | |||
| 1791 | =cut | ||
| 1792 | |||
| 1793 | sub tr_ext_active ($) { | ||
| 1794 | my $self=shift; | ||
| 1795 | return $self->{'tr_ext_active'}; | ||
| 1796 | } | ||
| 1797 | |||
| 1798 | ############################################################################### | ||
| 1799 | |||
| 1800 | sub tr_ext_begin ($) { | ||
| 1801 | my $self=shift; | ||
| 1802 | $self->{'tr_ext_active'} && | ||
| 1803 | throw $self "tr_ext_begin - attempt to nest transactions"; | ||
| 1804 | $self->{'tr_loc_active'} && | ||
| 1805 | throw $self "tr_ext_begin - internal error, still in local transaction"; | ||
| 1806 | if($self->{'table_type'} eq 'innodb') { | ||
| 1807 | $self->connector->sql_do('START TRANSACTION'); | ||
| 1808 | } | ||
| 1809 | $self->{'tr_ext_active'}=1; | ||
| 1810 | } | ||
| 1811 | |||
| 1812 | ############################################################################### | ||
| 1813 | |||
| 1814 | sub tr_ext_can ($) { | ||
| 1815 | my $self=shift; | ||
| 1816 | return $self->{'table_type'} eq 'innodb' ? 1 : 0; | ||
| 1817 | } | ||
| 1818 | |||
| 1819 | ############################################################################### | ||
| 1820 | |||
| 1821 | sub tr_ext_commit ($) { | ||
| 1822 | my $self=shift; | ||
| 1823 | |||
| 1824 | $self->{'tr_ext_active'} || | ||
| 1825 | throw $self "tr_ext_commit - no active transaction"; | ||
| 1826 | |||
| 1827 | if($self->{'table_type'} eq 'innodb') { | ||
| 1828 | $self->connector->sql_do('COMMIT'); | ||
| 1829 | } | ||
| 1830 | $self->{'tr_ext_active'}=0; | ||
| 1831 | } | ||
| 1832 | |||
| 1833 | ############################################################################### | ||
| 1834 | |||
| 1835 | sub tr_ext_rollback ($) { | ||
| 1836 | my $self=shift; | ||
| 1837 | |||
| 1838 | $self->{'tr_ext_active'} || | ||
| 1839 | throw $self "tr_ext_rollback - no active transaction"; | ||
| 1840 | |||
| 1841 | if($self->{'table_type'} eq 'innodb') { | ||
| 1842 | $self->connector->sql_do('ROLLBACK'); | ||
| 1843 | } | ||
| 1844 | $self->{'tr_ext_active'}=0; | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | ###################################################################### PRIVATE | ||
| 1848 | |||
| 1849 | sub lock_tables ($@) { | ||
| 1850 | my $self=shift; | ||
| 1851 | my $sql='LOCK TABLES '; | ||
| 1852 | $sql.=join(',',map { "$_ WRITE" } @_); | ||
| 1853 | ### dprint "lock_tables: caller=".((caller(1))[3])." sql=$sql"; | ||
| 1854 | $self->connector->sql_do($sql); | ||
| 1855 | } | ||
| 1856 | |||
| 1857 | sub unlock_tables ($) { | ||
| 1858 | my $self=shift; | ||
| 1859 | ### return unless $self->connector->sql_connected; | ||
| 1860 | ### dprint "unlock_tables: caller=".((caller(1))[3])." sql=$sql"; | ||
| 1861 | ### eprint "unlock_tables2: caller=".((caller(2))[3]); | ||
| 1862 | ### eprint "unlock_tables1: caller=".((caller(1))[3]); | ||
| 1863 | ### eprint "unlock_tables0: caller=".((caller(0))[3]); | ||
| 1864 | $self->connector->sql_do_no_error('UNLOCK TABLES'); | ||
| 1865 | ### eprint "unlock_tables:DONE"; | ||
| 1866 | } | ||
| 1867 | |||
| 1868 | sub throw ($@) { | ||
| 1869 | my $self=shift; | ||
| 1870 | if($self->{'table_type'} eq 'innodb') { | ||
| 1871 | $self->tr_loc_rollback(); | ||
| 1872 | } | ||
| 1873 | else { | ||
| 1874 | $self->unlock_tables(); | ||
| 1875 | } | ||
| 1876 | |||
| 1877 | $self->SUPER::throw(@_); | ||
| 1878 | } | ||
| 1879 | |||
| 1880 | ############################################################################### | ||
| 1881 | 1; | ||
| 1882 | __END__ |