3 * our standard code relies on links.ini files for the relationships in mysql.
5 * as we use 'loose' relationships - eg. we allow '0' as a missing link mysql FORIEGN KEYS do not really work.
7 * There are a couple of ideas behind this code.
9 * a) put the relationships in the table comments FK(col=table:col,col=table:col)
10 * -- we can not put it in the column comments as there is no clean way to update column comments.
11 * -- This can be used by external programs to extract the Relationships.
13 * b) generate triggers? to protect against updates to the database..
15 * -- stored procedures are named
16 * {tablename}_before_{insert|delete|update}
19 * initial code will auto generate triggers
20 * -- how to add User defined modifications to triggers?
21 * -- we can CALL a stored procedure..?
22 * -- {tablename}_trigger_before_delete_{column_name}(NEW.column)
23 * -- {tablename}_trigger_before_update_{column_name}(OLD.column, NEW.column}
24 * -- {tablename}_trigger_before_insert_{column_name}(OLD.column}
30 class Pman_Core_UpdateDatabase_MysqlLinks {
36 function __construct()
39 $this->loadIniFiles();
40 $this->updateTableComments();
41 $ff = HTML_FlexyFramework::get();
42 if (!empty($ff->Pman['enable_trigger_tests'])) {
44 // note we may want to override some of these... - to do special triggers..
45 // as you can only have one trigger per table for each action.
47 $this->createDeleteTriggers();
48 $this->createInsertTriggers();
49 $this->createUpdateTriggers();
55 function loadIniFiles()
57 // will create the combined ini cache file for the running user.
59 $ff = HTML_FlexyFramework::get();
60 $ff->generateDataobjectsCache(true);
61 $this->dburl = parse_url($ff->database);
63 $dbini = 'ini_'. basename($this->dburl['path']);
66 $iniCache = $ff->DB_DataObject[$dbini];
68 $this->schema = parse_ini_file($iniCache, true);
69 $this->links = parse_ini_file(preg_replace('/\.ini$/', '.links.ini', $iniCache), true);
74 function updateTableComments()
76 foreach($this->links as $tbl =>$map) {
77 $this->updateTableComment($tbl, $map);
84 function updateTableComment($tbl, $map)
88 if (!isset($this->schema[$tbl])) {
94 $q = DB_DAtaObject::factory('core_enum');
98 information_schema.TABLES
100 TABLE_SCHEMA = '{$q->escape($q->database())}'
102 TABLE_NAME = '{$q->escape($tbl)}'
105 $tc = $q->TABLE_COMMENT;
106 //echo "$tbl: $tc\n\n";
107 if (!empty($q->TABLE_COMMENT)) {
109 $tc = trim(preg_replace('/FK\([^)]+\)/', '' , $q->TABLE_COMMENT));
110 //var_dump($tc);exit;
111 // strip out the old FC(....)
115 foreach($map as $k=>$v) {
118 $fkstr = $tc . ' FK(' . implode("\n", $fks) .')';
119 if ($q->TABLE_COMMENT == $fkstr) {
123 $q = DB_DAtaObject::factory('core_enum');
124 $q->query("ALTER TABLE $tbl COMMENT = '{$q->escape($fkstr)}'");
130 function createDeleteTriggers()
133 // this should only be enabled if the project settings are configured..
137 // delete triggers on targets -
138 // if you delete a company, and a person points to it, then it should fire an error...
143 // create a list of source/targets from $this->links
146 foreach($this->links as $tbl => $map) {
147 if (!isset($this->schema[$tbl])) {
150 foreach($map as $k =>$v) {
151 list ($tname, $tcol) = explode(':', $v);
154 if (!isset($revmap[$tname])) {
155 $revmap[$tname] = array();
157 $revmap[$tname]["$tbl:$k"] = "$tname:$tcol";
162 foreach($revmap as $target_table => $sources) {
165 // throw example.. UPDATE `Error: invalid_id_test` SET x=1;
167 $q = DB_DataObject::factory('core_enum');
169 DROP TRIGGER IF EXISTS `{$target_table}_before_delete` ;
174 CREATE TRIGGER `{$target_table}_before_delete`
175 BEFORE DELETE ON `{$target_table}`
182 foreach($sources as $source=>$target) {
183 list($source_table , $source_col) = explode(':', $source);
184 list($target_table , $target_col) = explode(':', $target);
185 $err = substr("Failed Delete {$target_table} refs {$source_table}:{$source_col}", 0, 64);
188 SELECT count(*) into mid FROM {$source_table} WHERE {$source_col} = OLD.{$target_col};
191 UPDATE `$err` SET x = 1;
201 //DB_DAtaObject::debugLevel(1);
202 $q = DB_DataObject::factory('core_enum');
208 // inserting - row should not point to a reference that does not exist...
214 function createInsertTriggers()
216 foreach($this->links as $tbl => $map) {
217 if (!isset($this->schema[$tbl])) {
221 $q = DB_DataObject::factory('core_enum');
223 DROP TRIGGER IF EXISTS `{$tbl}_before_insert` ;
228 CREATE TRIGGER `{$tbl}_before_insert`
229 BEFORE INSERT ON `{$tbl}`
236 foreach($map as $source_col=>$target) {
237 // check that source_col exists in schema.
238 if (!isset($this->schema[$tbl][$source_col])) {
244 list($target_table , $target_col) = explode(':', $target);
245 $err = substr("Fail: INSERT referenced {$tbl}:{$source_col}", 0, 64);
248 if NEW.{$source_col} > 0 THEN
249 SELECT {$target_col} into mid FROM {$target_table} WHERE {$target_col} = NEW.{$source_col};
251 UPDATE `$err` SET x = 1;
261 //echo $trigger; exit;
262 //DB_DAtaObject::debugLevel(1);
263 $q = DB_DataObject::factory('core_enum');
278 function createUpdateTriggers()
280 foreach($this->links as $tbl => $map) {
281 if (!isset($this->schema[$tbl])) {
285 $q = DB_DataObject::factory('core_enum');
287 DROP TRIGGER IF EXISTS `{$tbl}_before_update` ;
292 CREATE TRIGGER `{$tbl}_before_update`
293 BEFORE UPDATE ON `{$tbl}`
300 foreach($map as $source_col=>$target) {
301 // check that source_col exists in schema.
302 if (!isset($this->schema[$tbl][$source_col])) {
308 list($target_table , $target_col) = explode(':', $target);
309 $err = substr("Fail: UPDATE referenced {$tbl}:$source_col", 0, 64);
312 if NEW.{$source_col} > 0 THEN
313 SELECT {$target_col} into mid FROM {$target_table} WHERE {$target_col} = NEW.{$source_col};
315 UPDATE `$err` SET x = 1;
325 //echo $trigger; exit;
326 //DB_DAtaObject::debugLevel(1);
327 $q = DB_DataObject::factory('core_enum');