X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=UpdateDatabase%2FMysqlLinks.php;h=02db5481d7a7dca3fb27cba0a97106717adace5a;hb=3dfa28ee12e72e27f69a7734bef807c2628e5d53;hp=36ea2fe045b183eb6aaee80dabb7a54ff93009d6;hpb=4cd211d4af031da8a2e67409304c92af37dd74c6;p=Pman.Core diff --git a/UpdateDatabase/MysqlLinks.php b/UpdateDatabase/MysqlLinks.php index 36ea2fe0..02db5481 100644 --- a/UpdateDatabase/MysqlLinks.php +++ b/UpdateDatabase/MysqlLinks.php @@ -40,26 +40,29 @@ class Pman_Core_UpdateDatabase_MysqlLinks { var $dburl; var $schema; - var $links; + var $links = array(); function __construct() { $this->loadIniFiles(); $this->updateTableComments(); - - $this->updateCharacterSet(); - + $ff = HTML_FlexyFramework::get(); - if (!empty($ff->Pman['enable_trigger_tests'])) { + if (empty($ff->Pman['enable_trigger_tests'])) { + return; + } + if (!empty($ff->page->opts['disable-create-triggers'])) { + return; + } // note we may want to override some of these... - to do special triggers.. // as you can only have one trigger per table for each action. - $this->createDeleteTriggers(); - $this->createInsertTriggers(); - $this->createUpdateTriggers(); - } + $this->createDeleteTriggers(); + $this->createInsertTriggers(); + $this->createUpdateTriggers(); + } @@ -75,12 +78,43 @@ class Pman_Core_UpdateDatabase_MysqlLinks { $dbini = 'ini_'. basename($this->dburl['path']); - $iniCache = $ff->DB_DataObject[$dbini]; + $iniCache = isset( $ff->PDO_DataObject) ? $ff->PDO_DataObject['schema_location'] : $ff->DB_DataObject[$dbini]; + if (strpos($iniCache, DIRECTORY_SEPARATOR) !== false) { + echo "SKIP links code - cached ini file has not been created\n"; + return; + } $this->schema = parse_ini_file($iniCache, true); $this->links = parse_ini_file(preg_replace('/\.ini$/', '.links.ini', $iniCache), true); - + $lcfg = &$this->links; + $cfg = empty($ff->DB_DataObject) ? array() : $ff->DB_DataObject; + + if (!empty($cfg['table_alias'])) { + $ta = $cfg['table_alias']; + foreach($lcfg as $k=>$v) { + $kk = $k; + if (isset($ta[$k])) { + $kk = $ta[$k]; + if (!isset($lcfg[$kk])) { + $lcfg[$kk] = array(); + } + } + foreach($v as $l => $t_c) { + $bits = explode(':',$t_c); + $tt = isset($ta[$bits[0]]) ? $ta[$bits[0]] : $bits[0]; + if ($tt == $bits[0] && $kk == $k) { + continue; + } + + $lcfg[$kk][$l] = $tt .':'. $bits[1]; + + + } + + } + } + } function updateTableComments() @@ -109,7 +143,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks { FROM information_schema.TABLES WHERE - TABLE_SCHEMA = '{$q->escape($q->database())}' + TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{$q->escape($tbl)}' "); @@ -264,9 +298,12 @@ class Pman_Core_UpdateDatabase_MysqlLinks { IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN "; + $has_checks= false; + $errs = array(); foreach($map as $source_col=>$target) { // check that source_col exists in schema. if (!isset($this->schema[$tbl][$source_col])) { + $errs[] = "SOURCE MISSING: $source_col => $target"; continue; } @@ -276,6 +313,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks { if (!isset($this->schema[$target_table])) { // skip... target table does not exist + $errs[] = "TARGET MISSING: $source_col => $target"; continue; } @@ -291,15 +329,17 @@ class Pman_Core_UpdateDatabase_MysqlLinks { END IF; "; - + $has_checks= true; } + $ar = $this->listTriggerFunctions($tbl, 'insert'); foreach($ar as $fn=>$col) { $trigger .= " CALL $fn( NEW.{$col}); "; + $has_checks= true; } $trigger .= " @@ -307,6 +347,11 @@ class Pman_Core_UpdateDatabase_MysqlLinks { END "; + + if (!$has_checks) { + echo "SKIP TRIGGER {$tbl}_before_insert (missing " . implode(", ", $errs) . ")\n"; + continue; + } //echo $trigger; exit; //DB_DAtaObject::debugLevel(1); $q = DB_DataObject::factory('core_enum'); @@ -346,9 +391,12 @@ class Pman_Core_UpdateDatabase_MysqlLinks { IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN "; + $has_checks= false; + $errs = array(); foreach($map as $source_col=>$target) { // check that source_col exists in schema. if (!isset($this->schema[$tbl][$source_col])) { + $errs[] = "SOURCE MISSING: $source_col => $target"; continue; } @@ -358,6 +406,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks { if (!isset($this->schema[$target_table])) { // skip... target table does not exist + $errs[] = "TARGET MISSING: $source_col => $target"; continue; } @@ -372,12 +421,14 @@ class Pman_Core_UpdateDatabase_MysqlLinks { END IF; "; + $has_checks= true; } $ar = $this->listTriggerFunctions($tbl, 'update'); foreach($ar as $fn=>$col) { $trigger .= " CALL $fn(OLD.{$col}, NEW.{$col}); "; + $has_checks= true; } $trigger .= " @@ -385,6 +436,11 @@ class Pman_Core_UpdateDatabase_MysqlLinks { END "; + if (!$has_checks) { + echo "SKIP TRIGGER {$tbl}_before_update (missing " . implode(", ", $errs) . ")\n"; + continue; + } + //echo $trigger; exit; //DB_DAtaObject::debugLevel(1); $q = DB_DataObject::factory('core_enum'); @@ -392,11 +448,6 @@ class Pman_Core_UpdateDatabase_MysqlLinks { echo "CREATED TRIGGER {$tbl}_before_update\n"; - - - - - } @@ -447,46 +498,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks { return $ret; } - function updateCharacterSet() - { - foreach (array_keys($this->schema) as $tbl){ - - if(strpos($tbl, '__keys') !== false ){ - continue; - } - - $ce = DB_DataObject::factory('core_enum'); - - $ce->query(" - SELECT - CCSA.character_set_name csname, - CCSA.collation_name collatename - FROM - information_schema.`TABLES` T, - information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA - WHERE - CCSA.collation_name = T.table_collation - AND - T.table_schema = '{$ce->database()}' -- COLLATE utf8_general_ci - AND - T.table_name = '{$tbl}' -- COLLATE utf8_general_ci - "); - - $ce->fetch(); - - if($ce->csname == 'utf8' && $ce->collatename == 'utf8_general_ci'){ - echo "$tbl is Already utf8 \n"; - continue; - } - // this used to be utf8_unicode_ci - //as the default collation for stored procedure parameters is utf8_general_ci and you can't mix collations. - - $ce = DB_DataObject::factory('core_enum'); - $ce->query("ALTER TABLE {$tbl} CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"); - echo "FIXED utf8 on {$tbl}\n"; - - } - } + }