var $dburl;
var $schema;
- var $links;
+ var $links = array();
function __construct()
{
$this->updateTableComments();
$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();
+
}
$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, PATH_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()
FROM
information_schema.TABLES
WHERE
- TABLE_SCHEMA = '{$q->escape($q->database())}'
+ TABLE_SCHEMA = DATABASE()
AND
TABLE_NAME = '{$q->escape($tbl)}'
");
}
$q = DB_DAtaObject::factory('core_enum');
- $q->query("ALTER TABLE $tbl COMMENT = '{$q->escape($fkstr)}'");
+ $q->query("ALTER TABLE `$tbl` COMMENT = '{$q->escape($fkstr)}'");
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;
}
if (!isset($this->schema[$target_table])) {
// skip... target table does not exist
+ $errs[] = "TARGET MISSING: $source_col => $target";
continue;
}
END IF;
";
-
+ $has_checks= true;
}
+
$ar = $this->listTriggerFunctions($tbl, 'insert');
foreach($ar as $fn=>$col) {
$trigger .= "
CALL $fn( NEW.{$col});
";
+ $has_checks= true;
}
$trigger .= "
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');
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;
}
if (!isset($this->schema[$target_table])) {
// skip... target table does not exist
+ $errs[] = "TARGET MISSING: $source_col => $target";
continue;
}
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 .= "
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');
echo "CREATED TRIGGER {$tbl}_before_update\n";
-
-
-
-
-
}