private function _generateTableClass($table_name) { $class_name = $this->_toClassName($table_name); $path = \hatwebtech\dal\DAL::getTablePath(); //\hat\dbg::alert($path); $namespace = \hatwebtech\dal\DAL::getTableNamespace(); $namespace = \ltrim($namespace, '\\'); $namespace = \rtrim($namespace, '\\'); //\hat\dbg::alert($namespace, true); if (\is_writable($path)) { $filename = $path . '/' . $class_name . '.php'; } else { $filename = '/tmp/' . $class_name . '.php'; } if (\file_exists($filename)) { //\hat\dbg::alert("PHP class for $class_name exists!"); return; } else { //\hat\dbg::alert("genarating PHP class for $class_name."); } $db_info = $this->__load_db_info(); //\hat\dbg::alert($db_info, true); $table_info = $this->__load_table_info($table_name); if ($table_info == false) { return false; } $ISTables[$table_name] = $table_info; $PGIndexes = $db_info['PGIndexes']; $ISColumns = $db_info['ISColumns']; $ISTableConstraints = $db_info['ISTableConstraints']; //$ISTableConstraints = $db_info['ISTableConstraints']; $ISColumnConstraints = $db_info['ISColumnConstraints']; $ISConstraints = $db_info['ISConstraints']; $ISKeyColumnUsage = $db_info['ISKeyColumnUsage']; $results = array(); foreach ($ISTables as $t_name => $t) { $columns = array(); foreach ($ISColumns as $c) { if ($c['table_name'] == $t_name) { $columns[$c['column_name']] = $c; } } $ISTables[$t_name]['Columns'] = $columns; } foreach ($ISTables as $t_name => $t) { $indexes = array(); $pkeys = array(); foreach ($PGIndexes as $index) { if ($index['tablename'] == $t_name) { $indexes[] = $index; } if ($index['indexname'] == $t_name . '_pkey') { $string = $index['indexdef']; $start = \strpos($string, '(') + 1; $stop = \strpos($string, ')'); $length = $stop - $start; $_pkeys_string = \substr($string, $start, $length); $pkeys = \array_map('trim', \explode(',', $_pkeys_string)); //\hat\dbg::alert($pkeys, true); } } $ISTables[$t_name]['Indexes'] = $indexes; if (isset($pkeys)) { $ISTables[$t_name]['PKeys'] = $pkeys; } } //return $ISTables; foreach ($ISTables as $t_name => $t) { $pkeys = array(); $fkeys = array(); $rkeys = array(); $referenced_by = array(); $used_constraints = array(); foreach ($ISTableConstraints as $tc) { if ($tc['table_name'] == $t_name) { foreach ($ISKeyColumnUsage as $v0) { if ($tc['constraint_name'] == $v0['constraint_name']) { $used_constraints[] = $tc['constraint_name']; $tc['Constraint']['v0__'] = $v0; } } foreach ($ISConstraints as $constraint) { if ($tc['constraint_name'] == $constraint['constraint_name']) { $used_constraints[] = $tc['constraint_name']; $tc['Constraint'] = $constraint; foreach ($ISKeyColumnUsage as $v2) { if ($constraint['constraint_name'] == $v2['constraint_name']) { $tc['Constraint']['v2_1'] = $v2; } if ($constraint['unique_constraint_name'] == $v2['constraint_name']) { $tc['Constraint']['v2_2'] = $v2; } } } } if ($tc['constraint_type'] == 'PRIMARY KEY') { $pkeys[] = $tc; } elseif ($tc['constraint_type'] == 'FOREIGN KEY') { $fkeys[] = $tc; } else { $rkeys[] = $tc; } $referenced_by = array(); foreach ($ISColumnConstraints as $cc) { if ($t_name == $cc['table_name']) { if (!\in_array($cc['constraint_name'], $used_constraints)) { foreach ($ISKeyColumnUsage as $v3) { if ($cc['constraint_name'] == $v3['constraint_name']) { $cc['Constraint']['v0__'] = $v3; foreach ($ISConstraints as $constraint) { if ($cc['constraint_name'] == $constraint['constraint_name']) { $used_constraints[] = $tc['constraint_name']; $cc['Constraint'] = $constraint; foreach ($ISKeyColumnUsage as $v2) { if ($constraint['constraint_name'] == $v2['constraint_name']) { $cc['Constraint']['v2_1'] = $v2; } if ($constraint['unique_constraint_name'] == $v2['constraint_name']) { $cc['Constraint']['v2_2'] = $v2; } } } } } } $referenced_by[] = $cc; } } } } } $ISTables[$t_name]['Constraints'] = array('PRIMARY_KEY' => $pkeys, 'FOREIGN_KEY' => $fkeys, 'Referenced by' => $referenced_by, 'OTHER_KEYS' => $rkeys); } /* * GENERATE PHP */ $php = "<?php\n/**\n * @author created by HDM table class generator\n */\nnamespace {$namespace};\nclass {$class_name} extends \\hatwebtech\\dal\\Table{\n public function setTableDefinition(){\n \$this->setTableName('{$table_name}');\n"; foreach ($ISTables as $t_name => $t_info) { foreach ($t_info['Columns'] as $key => $column) { if ($column['data_type'] == 'character varying') { $_type = "string"; $_size = $column['character_maximum_length']; } elseif ($column['data_type'] == 'bigint') { $_type = "integer"; $_size = '8'; } elseif ($column['data_type'] == 'boolean') { $_type = "boolean"; $_size = '1'; } elseif ($column['data_type'] == 'timestamp without time zone') { $_type = "timestamp"; $_size = 'null'; } else { $_type = $column['data_type']; $_size = '0'; } $_options = 'array('; if (isset($t_info['PKeys']) && \in_array($key, $t_info['PKeys'])) { $_options .= '\'primary\' => true'; } $_options .= ')'; $php .= " \$this->hasColumn('{$key}', '{$_type}', {$_size}, {$_options});\n"; } $php .= " }\n\n public function setUp(){\n parent::setUp();\n //\$this->actAs('');\n\n"; // //\hat\dbg::alert($t_info); // //\hat\dbg::alert($php, true); // $pkeys = array(); // $_pkeys = array(); // foreach($t_info['Constraints']['PRIMARY_KEY'] as $c){ // if(isset($c['Constraint']['v0__'])){ // $pkeys[] = $c['Constraint']['v0__']['column_name']; // $_pkeys[] = $c['constraint_name']; // } // } // if(empty($fkeys)){ // echo " Primary keys ( NONE )$nl"; // }else{ // echo " Primary keys ("; echo(\implode(', ', $pkeys)); echo ")$nl"; // } // $indexes = array(); // foreach($t_info['Indexes'] as $i){ // $_i = " -- '{$i['indexname']}' "; // if(\in_array($i['indexname'], $_pkeys)){ // $_i .= "PRIMARY KEY, "; // } // $_i .= \substr($i['indexdef'], \strpos($i['indexdef'], 'USING') + 5); // $indexes[] = $_i; // } // if(empty($indexes)){ // echo " Indexes: $nl -- NONE$nl"; // }else{ // echo " Indexes: $nl"; echo(\implode(",$nl", $indexes)); echo $nl; // } $fkeys = array(); foreach ($t_info['Constraints']['FOREIGN_KEY'] as $c) { $fk = " -- '{$c['Constraint']['constraint_name']}' FOREIGN KEY ({$c['Constraint']['v2_1']['column_name']}) REFERENCES {$c['Constraint']['v2_2']['table_name']}({$c['Constraint']['v2_2']['column_name']}) "; //$fk = " -- {$c['Constraint']['v2_1']['column_name']} REFERENCES {$c['Constraint']['v2_2']['table_name']}({$c['Constraint']['v2_2']['column_name']}) "; if ($c['Constraint']['update_rule'] != 'NO ACTION') { $fk .= ' ON UPDATE ' . $c['Constraint']['update_rule']; } if ($c['Constraint']['delete_rule'] != 'NO ACTION') { $fk .= ' ON DELETE ' . $c['Constraint']['delete_rule']; } $fkeys[] = $fk; $_c = $this->_toClassName($c['Constraint']['v2_2']['table_name']); $_l = $c['Constraint']['v2_1']['column_name']; $_f = $c['Constraint']['v2_2']['column_name']; $php .= " \$this->hasOne('{$_c}', array('local' => '{$_l}', 'foreign' => '{$_f}'));\n"; } // if(empty($fkeys)){ // echo " Foreign keys: $nl -- NONE$nl"; // }else{ // echo " Foreign keys: $nl"; echo(\implode(",$nl", $fkeys)); echo $nl; // } $ref_by = array(); foreach ($t_info['Constraints']['Referenced by'] as $c) { $rb = " -- TABLE {$c['Constraint']['v2_1']['table_name']} CONSTRAINT '{$c['Constraint']['constraint_name']}' FOREIGN KEY ({$c['Constraint']['v2_1']['column_name']}) REFERENCES {$c['Constraint']['v2_2']['table_name']}({$c['Constraint']['v2_2']['column_name']}) "; //$rb = " -- TABLE {$c['Constraint']['v2_1']['table_name']}({$c['Constraint']['v2_1']['column_name']}) REFERENCES {$c['Constraint']['v2_2']['column_name']}"; if ($c['Constraint']['update_rule'] != 'NO ACTION') { $rb .= ' ON UPDATE ' . $c['Constraint']['update_rule']; } if ($c['Constraint']['delete_rule'] != 'NO ACTION') { $rb .= ' ON DELETE ' . $c['Constraint']['delete_rule']; } $ref_by[] = $rb; $_c = $this->_toClassName($c['Constraint']['v2_1']['table_name']); $_l = $c['Constraint']['v2_1']['column_name']; $_f = $c['Constraint']['v2_2']['column_name']; $php .= " \$this->hasMany('{$_c}', array('local' => '{$_f}', 'foreign' => '{$_l}'));\n"; } // if(empty($ref_by)){ // echo " Referenced by: $nl -- NONE$nl"; // }else{ // echo " Referenced by: $nl"; echo(\implode(",$nl", $ref_by)); echo "$nl"; // } // close $php .= " }\n}\n?>"; } $fileHandle = fopen($filename, 'w+') or die("can't open file"); $b = fwrite($fileHandle, $php); fclose($fileHandle); \chmod($filename, 0666); //\hat\dbg::alert("written $b bytes in $class_name [path = $filename]."); // echo "<pre>\n\nPHP code:\n"; // echo $php; // //\hat\dbg::alert('end', true); }