- Timestamp:
- 2013/08/02 13:22:57 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/MDB2/Driver/Manager/mysql.php
r20764 r23022 43 43 // +----------------------------------------------------------------------+ 44 44 // 45 // $Id: mysql.php ,v 1.113 2008/11/23 20:30:29 quipo Exp$45 // $Id: mysql.php 327310 2012-08-27 15:16:18Z danielc $ 46 46 // 47 47 … … 72 72 function createDatabase($name, $options = array()) 73 73 { 74 $db = &$this->getDBInstance();75 if ( PEAR::isError($db)) {74 $db = $this->getDBInstance(); 75 if (MDB2::isError($db)) { 76 76 return $db; 77 77 } … … 102 102 function alterDatabase($name, $options = array()) 103 103 { 104 $db = &$this->getDBInstance();105 if ( PEAR::isError($db)) {104 $db = $this->getDBInstance(); 105 if (MDB2::isError($db)) { 106 106 return $db; 107 107 } … … 129 129 function dropDatabase($name) 130 130 { 131 $db = &$this->getDBInstance();132 if ( PEAR::isError($db)) {131 $db = $this->getDBInstance(); 132 if (MDB2::isError($db)) { 133 133 return $db; 134 134 } … … 205 205 function createTable($name, $fields, $options = array()) 206 206 { 207 $db = &$this->getDBInstance();208 if ( PEAR::isError($db)) {207 $db = $this->getDBInstance(); 208 if (MDB2::isError($db)) { 209 209 return $db; 210 210 } … … 223 223 } 224 224 } 225 if ( !is_null($autoincrement) && count($pk_fields) > 1) {225 if ((null !== $autoincrement) && count($pk_fields) > 1) { 226 226 $options['primary'] = $pk_fields; 227 227 } else { … … 232 232 233 233 $query = $this->_getCreateTableQuery($name, $fields, $options); 234 if ( PEAR::isError($query)) {234 if (MDB2::isError($query)) { 235 235 return $query; 236 236 } 237 237 238 if ( !is_null($autoincrement)) {238 if (null !== $autoincrement) { 239 239 // we have to remove the PK clause added by _getIntegerDeclaration() 240 240 $query = str_replace('AUTO_INCREMENT PRIMARY KEY', 'AUTO_INCREMENT', $query); … … 268 268 } 269 269 $result = $db->exec($query); 270 if ( PEAR::isError($result)) {270 if (MDB2::isError($result)) { 271 271 return $result; 272 272 } … … 286 286 function dropTable($name) 287 287 { 288 $db = &$this->getDBInstance();289 if ( PEAR::isError($db)) {288 $db = $this->getDBInstance(); 289 if (MDB2::isError($db)) { 290 290 return $db; 291 291 } … … 293 293 //delete the triggers associated to existing FK constraints 294 294 $constraints = $this->listTableConstraints($name); 295 if (! PEAR::isError($constraints) && !empty($constraints)) {295 if (!MDB2::isError($constraints) && !empty($constraints)) { 296 296 $db->loadModule('Reverse', null, true); 297 297 foreach ($constraints as $constraint) { 298 298 $definition = $db->reverse->getTableConstraintDefinition($name, $constraint); 299 if (! PEAR::isError($definition) && !empty($definition['foreign'])) {299 if (!MDB2::isError($definition) && !empty($definition['foreign'])) { 300 300 $result = $this->_dropFKTriggers($name, $constraint, $definition['references']['table']); 301 if ( PEAR::isError($result)) {301 if (MDB2::isError($result)) { 302 302 return $result; 303 303 } … … 322 322 function truncateTable($name) 323 323 { 324 $db = &$this->getDBInstance();325 if ( PEAR::isError($db)) {324 $db = $this->getDBInstance(); 325 if (MDB2::isError($db)) { 326 326 return $db; 327 327 } 328 328 329 329 $name = $db->quoteIdentifier($name, true); 330 return $db->exec("TRUNCATE TABLE $name"); 330 $result = $db->exec("TRUNCATE TABLE $name"); 331 if (MDB2::isError($result)) { 332 return $result; 333 } 334 return MDB2_OK; 331 335 } 332 336 … … 350 354 function vacuum($table = null, $options = array()) 351 355 { 352 $db = &$this->getDBInstance();353 if ( PEAR::isError($db)) {356 $db = $this->getDBInstance(); 357 if (MDB2::isError($db)) { 354 358 return $db; 355 359 } … … 357 361 if (empty($table)) { 358 362 $table = $this->listTables(); 359 if ( PEAR::isError($table)) {363 if (MDB2::isError($table)) { 360 364 return $table; 361 365 } … … 371 375 372 376 $result = $db->exec('OPTIMIZE TABLE '.$table); 373 if ( PEAR::isError($result)) {377 if (MDB2::isError($result)) { 374 378 return $result; 375 379 } 376 380 if (!empty($options['analyze'])) { 377 return $db->exec('ANALYZE TABLE '.$table); 381 $result = $db->exec('ANALYZE TABLE '.$table); 382 if (MDB2::isError($result)) { 383 return $result; 384 } 378 385 } 379 386 return MDB2_OK; … … 475 482 function alterTable($name, $changes, $check) 476 483 { 477 $db = &$this->getDBInstance();478 if ( PEAR::isError($db)) {484 $db = $this->getDBInstance(); 485 if (MDB2::isError($db)) { 479 486 return $db; 480 487 } … … 562 569 563 570 $name = $db->quoteIdentifier($name, true); 564 return $db->exec("ALTER TABLE $name $query"); 571 $result = $db->exec("ALTER TABLE $name $query"); 572 if (MDB2::isError($result)) { 573 return $result; 574 } 575 return MDB2_OK; 565 576 } 566 577 … … 576 587 function listDatabases() 577 588 { 578 $db = &$this->getDBInstance();579 if ( PEAR::isError($db)) {589 $db = $this->getDBInstance(); 590 if (MDB2::isError($db)) { 580 591 return $db; 581 592 } 582 593 583 594 $result = $db->queryCol('SHOW DATABASES'); 584 if ( PEAR::isError($result)) {595 if (MDB2::isError($result)) { 585 596 return $result; 586 597 } … … 602 613 function listUsers() 603 614 { 604 $db = &$this->getDBInstance();605 if ( PEAR::isError($db)) {615 $db = $this->getDBInstance(); 616 if (MDB2::isError($db)) { 606 617 return $db; 607 618 } … … 621 632 function listFunctions() 622 633 { 623 $db = &$this->getDBInstance();624 if ( PEAR::isError($db)) {634 $db = $this->getDBInstance(); 635 if (MDB2::isError($db)) { 625 636 return $db; 626 637 } … … 633 644 */ 634 645 $result = $db->queryCol($query); 635 if ( PEAR::isError($result)) {646 if (MDB2::isError($result)) { 636 647 return $result; 637 648 } … … 654 665 function listTableTriggers($table = null) 655 666 { 656 $db = &$this->getDBInstance();657 if ( PEAR::isError($db)) {667 $db = $this->getDBInstance(); 668 if (MDB2::isError($db)) { 658 669 return $db; 659 670 } 660 671 661 672 $query = 'SHOW TRIGGERS'; 662 if ( !is_null($table)) {673 if (null !== $table) { 663 674 $table = $db->quote($table, 'text'); 664 675 $query .= " LIKE $table"; 665 676 } 666 677 $result = $db->queryCol($query); 667 if ( PEAR::isError($result)) {678 if (MDB2::isError($result)) { 668 679 return $result; 669 680 } … … 686 697 function listTables($database = null) 687 698 { 688 $db = &$this->getDBInstance();689 if ( PEAR::isError($db)) {699 $db = $this->getDBInstance(); 700 if (MDB2::isError($db)) { 690 701 return $db; 691 702 } 692 703 693 704 $query = "SHOW /*!50002 FULL*/ TABLES"; 694 if ( !is_null($database)) {705 if (null !== $database) { 695 706 $query .= " FROM $database"; 696 707 } … … 698 709 699 710 $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED); 700 if ( PEAR::isError($table_names)) {711 if (MDB2::isError($table_names)) { 701 712 return $table_names; 702 713 } … … 726 737 function listViews($database = null) 727 738 { 728 $db = &$this->getDBInstance();729 if ( PEAR::isError($db)) {739 $db = $this->getDBInstance(); 740 if (MDB2::isError($db)) { 730 741 return $db; 731 742 } 732 743 733 744 $query = 'SHOW FULL TABLES'; 734 if ( !is_null($database)) {745 if (null !== $database) { 735 746 $query.= " FROM $database"; 736 747 } … … 738 749 739 750 $result = $db->queryCol($query); 740 if ( PEAR::isError($result)) {751 if (MDB2::isError($result)) { 741 752 return $result; 742 753 } … … 760 771 function listTableFields($table) 761 772 { 762 $db = &$this->getDBInstance();763 if ( PEAR::isError($db)) {773 $db = $this->getDBInstance(); 774 if (MDB2::isError($db)) { 764 775 return $db; 765 776 } … … 767 778 $table = $db->quoteIdentifier($table, true); 768 779 $result = $db->queryCol("SHOW COLUMNS FROM $table"); 769 if ( PEAR::isError($result)) {780 if (MDB2::isError($result)) { 770 781 return $result; 771 782 } … … 816 827 function createIndex($table, $name, $definition) 817 828 { 818 $db = &$this->getDBInstance();819 if ( PEAR::isError($db)) {829 $db = $this->getDBInstance(); 830 if (MDB2::isError($db)) { 820 831 return $db; 821 832 } … … 833 844 } 834 845 $query .= ' ('. implode(', ', $fields) . ')'; 835 return $db->exec($query); 846 $result = $db->exec($query); 847 if (MDB2::isError($result)) { 848 return $result; 849 } 850 return MDB2_OK; 836 851 } 837 852 … … 849 864 function dropIndex($table, $name) 850 865 { 851 $db = &$this->getDBInstance();852 if ( PEAR::isError($db)) {866 $db = $this->getDBInstance(); 867 if (MDB2::isError($db)) { 853 868 return $db; 854 869 } … … 856 871 $table = $db->quoteIdentifier($table, true); 857 872 $name = $db->quoteIdentifier($db->getIndexName($name), true); 858 return $db->exec("DROP INDEX $name ON $table"); 873 $result = $db->exec("DROP INDEX $name ON $table"); 874 if (MDB2::isError($result)) { 875 return $result; 876 } 877 return MDB2_OK; 859 878 } 860 879 … … 871 890 function listTableIndexes($table) 872 891 { 873 $db = &$this->getDBInstance();874 if ( PEAR::isError($db)) {892 $db = $this->getDBInstance(); 893 if (MDB2::isError($db)) { 875 894 return $db; 876 895 } … … 891 910 $query = "SHOW INDEX FROM $table"; 892 911 $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC); 893 if ( PEAR::isError($indexes)) {912 if (MDB2::isError($indexes)) { 894 913 return $indexes; 895 914 } … … 935 954 function createConstraint($table, $name, $definition) 936 955 { 937 $db = &$this->getDBInstance();938 if ( PEAR::isError($db)) {956 $db = $this->getDBInstance(); 957 if (MDB2::isError($db)) { 939 958 return $db; 940 959 } … … 981 1000 // @see http://forums.mysql.com/read.php?22,19755,226009 982 1001 $result = $this->createIndex($table, $name.'_fkidx', $definition); 983 if ( PEAR::isError($result)) {1002 if (MDB2::isError($result)) { 984 1003 return $result; 985 1004 } 986 1005 } 987 1006 $res = $db->exec($query); 988 if ( PEAR::isError($res)) {1007 if (MDB2::isError($res)) { 989 1008 return $res; 990 1009 } … … 1009 1028 function dropConstraint($table, $name, $primary = false) 1010 1029 { 1011 $db = &$this->getDBInstance();1012 if ( PEAR::isError($db)) {1030 $db = $this->getDBInstance(); 1031 if (MDB2::isError($db)) { 1013 1032 return $db; 1014 1033 } … … 1016 1035 if ($primary || strtolower($name) == 'primary') { 1017 1036 $query = 'ALTER TABLE '. $db->quoteIdentifier($table, true) .' DROP PRIMARY KEY'; 1018 return $db->exec($query); 1037 $result = $db->exec($query); 1038 if (MDB2::isError($result)) { 1039 return $result; 1040 } 1041 return MDB2_OK; 1019 1042 } 1020 1043 … … 1022 1045 $db->loadModule('Reverse', null, true); 1023 1046 $definition = $db->reverse->getTableConstraintDefinition($table, $name); 1024 if (! PEAR::isError($definition) && !empty($definition['foreign'])) {1047 if (!MDB2::isError($definition) && !empty($definition['foreign'])) { 1025 1048 //first drop the FK enforcing triggers 1026 1049 $result = $this->_dropFKTriggers($table, $name, $definition['references']['table']); 1027 if ( PEAR::isError($result)) {1050 if (MDB2::isError($result)) { 1028 1051 return $result; 1029 1052 } … … 1032 1055 $name = $db->quoteIdentifier($db->getIndexName($name), true); 1033 1056 $query = "ALTER TABLE $table DROP FOREIGN KEY $name"; 1034 return $db->exec($query); 1057 $result = $db->exec($query); 1058 if (MDB2::isError($result)) { 1059 return $result; 1060 } 1061 return MDB2_OK; 1035 1062 } 1036 1063 … … 1038 1065 $name = $db->quoteIdentifier($db->getIndexName($name), true); 1039 1066 $query = "ALTER TABLE $table DROP INDEX $name"; 1040 return $db->exec($query); 1067 $result = $db->exec($query); 1068 if (MDB2::isError($result)) { 1069 return $result; 1070 } 1071 return MDB2_OK; 1041 1072 } 1042 1073 … … 1059 1090 function _createFKTriggers($table, $foreign_keys) 1060 1091 { 1061 $db = &$this->getDBInstance();1062 if ( PEAR::isError($db)) {1092 $db = $this->getDBInstance(); 1093 if (MDB2::isError($db)) { 1063 1094 return $db; 1064 1095 } … … 1104 1135 $conditions2[] = 'NEW.'.$referenced_fields[$i] .' <> OLD.'.$referenced_fields[$i]; 1105 1136 } 1106 $restrict_action .= implode(' AND ', $conditions).') IS NOT NULL' 1107 .' AND (' .implode(' OR ', $conditions2) .')' 1108 .' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();' 1109 .' END IF;'; 1137 1138 $restrict_action .= implode(' AND ', $conditions).') IS NOT NULL'; 1139 $restrict_action2 = empty($conditions2) ? '' : ' AND (' .implode(' OR ', $conditions2) .')'; 1140 $restrict_action3 = ' THEN CALL %s_ON_TABLE_'.$table.'_VIOLATES_FOREIGN_KEY_CONSTRAINT();' 1141 .' END IF;'; 1142 1143 $restrict_action_update = $restrict_action . $restrict_action2 . $restrict_action3; 1144 $restrict_action_delete = $restrict_action . $restrict_action3; // There is no NEW row in on DELETE trigger 1110 1145 1111 1146 $cascade_action_update = 'UPDATE '.$table_quoted.' SET '.implode(', ', $new_values) .' WHERE '.implode(' AND ', $conditions). ';'; … … 1118 1153 foreach ($table_fields as $table_field) { 1119 1154 $field_definition = $db->reverse->getTableFieldDefinition($table, $field); 1120 if ( PEAR::isError($field_definition)) {1155 if (MDB2::isError($field_definition)) { 1121 1156 return $field_definition; 1122 1157 } … … 1138 1173 $sql_update = sprintf($query, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update') . $setdefault_action; 1139 1174 } elseif ('NO ACTION' == $fkdef['onupdate']) { 1140 $sql_update = sprintf($query.$restrict_action , $trigger_names['pk_update'], 'AFTER UPDATE', 'update');1175 $sql_update = sprintf($query.$restrict_action_update, $trigger_names['pk_update'], 'AFTER UPDATE', 'update'); 1141 1176 } elseif ('RESTRICT' == $fkdef['onupdate']) { 1142 $sql_update = sprintf($query.$restrict_action , $trigger_names['pk_update'], 'BEFORE UPDATE', 'update');1177 $sql_update = sprintf($query.$restrict_action_update, $trigger_names['pk_update'], 'BEFORE UPDATE', 'update'); 1143 1178 } 1144 1179 if ('CASCADE' == $fkdef['ondelete']) { … … 1149 1184 $sql_delete = sprintf($query, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete') . $setdefault_action; 1150 1185 } elseif ('NO ACTION' == $fkdef['ondelete']) { 1151 $sql_delete = sprintf($query.$restrict_action , $trigger_names['pk_delete'], 'AFTER DELETE', 'delete');1186 $sql_delete = sprintf($query.$restrict_action_delete, $trigger_names['pk_delete'], 'AFTER DELETE', 'delete'); 1152 1187 } elseif ('RESTRICT' == $fkdef['ondelete']) { 1153 $sql_delete = sprintf($query.$restrict_action , $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete');1188 $sql_delete = sprintf($query.$restrict_action_delete, $trigger_names['pk_delete'], 'BEFORE DELETE', 'delete'); 1154 1189 } 1155 1190 $sql_update .= ' SET FOREIGN_KEY_CHECKS = 1; END;'; … … 1162 1197 $db->popExpect(); 1163 1198 $db->popErrorHandling(); 1164 if ( PEAR::isError($result)) {1199 if (MDB2::isError($result)) { 1165 1200 if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) { 1166 1201 return $result; … … 1173 1208 $db->popExpect(); 1174 1209 $db->popErrorHandling(); 1175 if ( PEAR::isError($result) && $result->getCode() != MDB2_ERROR_CANNOT_CREATE) {1210 if (MDB2::isError($result) && $result->getCode() != MDB2_ERROR_CANNOT_CREATE) { 1176 1211 if ($result->getCode() != MDB2_ERROR_CANNOT_CREATE) { 1177 1212 return $result; … … 1199 1234 function _dropFKTriggers($table, $fkname, $referenced_table) 1200 1235 { 1201 $db = &$this->getDBInstance();1202 if ( PEAR::isError($db)) {1236 $db = $this->getDBInstance(); 1237 if (MDB2::isError($db)) { 1203 1238 return $db; 1204 1239 } … … 1206 1241 $triggers = $this->listTableTriggers($table); 1207 1242 $triggers2 = $this->listTableTriggers($referenced_table); 1208 if (! PEAR::isError($triggers2) && !PEAR::isError($triggers)) {1243 if (!MDB2::isError($triggers2) && !MDB2::isError($triggers)) { 1209 1244 $triggers = array_merge($triggers, $triggers2); 1210 1245 $pattern = '/^'.$fkname.'(_pk)?_(insert|update|delete)_trg$/i'; … … 1212 1247 if (preg_match($pattern, $trigger)) { 1213 1248 $result = $db->exec('DROP TRIGGER '.$trigger); 1214 if ( PEAR::isError($result)) {1249 if (MDB2::isError($result)) { 1215 1250 return $result; 1216 1251 } … … 1233 1268 function listTableConstraints($table) 1234 1269 { 1235 $db = &$this->getDBInstance();1236 if ( PEAR::isError($db)) {1270 $db = $this->getDBInstance(); 1271 if (MDB2::isError($db)) { 1237 1272 return $db; 1238 1273 } … … 1252 1287 $query = 'SHOW INDEX FROM ' . $db->quoteIdentifier($table, true); 1253 1288 $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC); 1254 if ( PEAR::isError($indexes)) {1289 if (MDB2::isError($indexes)) { 1255 1290 return $indexes; 1256 1291 } … … 1273 1308 $query = 'SHOW CREATE TABLE '. $db->escape($table); 1274 1309 $definition = $db->queryOne($query, 'text', 1); 1275 if (! PEAR::isError($definition) && !empty($definition)) {1310 if (!MDB2::isError($definition) && !empty($definition)) { 1276 1311 $pattern = '/\bCONSTRAINT\b\s+([^\s]+)\s+\bFOREIGN KEY\b/Uims'; 1277 1312 if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 0) { … … 1308 1343 function createSequence($seq_name, $start = 1, $options = array()) 1309 1344 { 1310 $db = &$this->getDBInstance();1311 if ( PEAR::isError($db)) {1345 $db = $this->getDBInstance(); 1346 if (MDB2::isError($db)) { 1312 1347 return $db; 1313 1348 } … … 1344 1379 } 1345 1380 $res = $db->exec($query); 1346 if ( PEAR::isError($res)) {1381 if (MDB2::isError($res)) { 1347 1382 return $res; 1348 1383 } … … 1354 1389 $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')'; 1355 1390 $res = $db->exec($query); 1356 if (! PEAR::isError($res)) {1391 if (!MDB2::isError($res)) { 1357 1392 return MDB2_OK; 1358 1393 } … … 1360 1395 // Handle error 1361 1396 $result = $db->exec("DROP TABLE $sequence_name"); 1362 if ( PEAR::isError($result)) {1397 if (MDB2::isError($result)) { 1363 1398 return $db->raiseError($result, null, null, 1364 1399 'could not drop inconsistent sequence table', __FUNCTION__); … … 1381 1416 function dropSequence($seq_name) 1382 1417 { 1383 $db = &$this->getDBInstance();1384 if ( PEAR::isError($db)) {1418 $db = $this->getDBInstance(); 1419 if (MDB2::isError($db)) { 1385 1420 return $db; 1386 1421 } 1387 1422 1388 1423 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true); 1389 return $db->exec("DROP TABLE $sequence_name"); 1424 $result = $db->exec("DROP TABLE $sequence_name"); 1425 if (MDB2::isError($result)) { 1426 return $result; 1427 } 1428 return MDB2_OK; 1390 1429 } 1391 1430 … … 1402 1441 function listSequences($database = null) 1403 1442 { 1404 $db = &$this->getDBInstance();1405 if ( PEAR::isError($db)) {1443 $db = $this->getDBInstance(); 1444 if (MDB2::isError($db)) { 1406 1445 return $db; 1407 1446 } 1408 1447 1409 1448 $query = "SHOW TABLES"; 1410 if ( !is_null($database)) {1449 if (null !== $database) { 1411 1450 $query .= " FROM $database"; 1412 1451 } 1413 1452 $table_names = $db->queryCol($query); 1414 if ( PEAR::isError($table_names)) {1453 if (MDB2::isError($table_names)) { 1415 1454 return $table_names; 1416 1455 }
Note: See TracChangeset
for help on using the changeset viewer.