Uncommited changes synced
[Pman.Core] / UpdateDatabase / MysqlLinks.php
index d4721b7..02db548 100644 (file)
@@ -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,45 +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;
-            }
-            //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";
-            
-        }
-    }
+    
     
 }