/** * delete * @todo unset from $this->_results * @todo unset from $this->_has_many_for_save */ public function delete() { if ($this->_behave('pre_delete') === false) { //echo 'pre_delete exit'; //return false; // is FALSE right response in case of skiping delete??? } $this->preDelete(); if (empty($this->_primaryKeys)) { echo 'no primary keys'; return false; } if (!isset($this->_query)) { $this->_query = \hatwebtech\dal\DAL::query(); } $this->_query->delete($this->_getModelName()); foreach ($this->_primaryKeys as $primary_key) { if ($this->get($this->_getHumanFieldName($primary_key) . 'Exists', array())) { $this->_query->andWhere("{$primary_key} =?", $this->get($this->_getHumanFieldName($primary_key), array())); } else { throw new \Exception('Error deleting ' . $this->_getModelName() . '; ' . $primary_key . ' not set.'); } } //print_r($this->_query->queryDebug()); exit; $r1 = $this->_query->queryStmt(); if ($r1 === false) { $r1 = $this->_query->getLastPdoErrorMessage(); //$r1 = $this->_query->getPdoErrorMessages(); throw new \Exception('Error deleting ' . $this->_getModelName() . ' with message: ' . $r1); return false; } $this->postDelete(); $this->_behave('post_delete'); return true; }
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); }
protected function _prepareQueryParts_join() { // joins $this->_additional_select = array(); foreach ($this->_query_parts['join'] as $i => $join) { if ($join['type'] == 'left') { // example: hi.HatTemplate ht $dot = \strpos($join['join'], '.'); if ($dot !== false) { $left_alias = \substr($join['join'], 0, $dot); $right_part = \substr($join['join'], $dot + 1); $left_table_name = $this->_getTableNameByAlias($left_alias); if (!$left_table_name) { $this->pdo_error_message[] = "Table object for alias {$left_alias} not found in this query."; return false; } // $has_alias = \strripos($right_part, ' as '); if ($has_alias !== false) { $assoc_name = \substr($right_part, 0, $has_alias); $assoc_alias = \substr($right_part, $has_alias + 4); } else { $has_alias = \strrpos($right_part, ' '); if ($has_alias !== false) { $assoc_name = \substr($right_part, 0, $has_alias); $assoc_alias = \substr($right_part, $has_alias + 1); } else { $assoc_name = $assoc_alias = $right_part; } } $associ_info = $this->_tables[$left_table_name]->associationInfo($assoc_name); if (!$associ_info) { $this->pdo_error_message[] = "Association {$assoc_name} not found in {$left_table_name}."; return false; } $model_name = $associ_info['association_name']; $this->_tables[$model_name] = \hatwebtech\dal\DAL::load($model_name); $primary_keys = $this->_tables[$model_name]->getPrimaryKeys(); $table_info = array('model_name' => $model_name, 'model_alias' => $assoc_alias, 'primary_keys' => $primary_keys, 'assoc_name' => $assoc_name, 'assoc_to_alias' => $left_alias, 'assoc_to_name' => $left_table_name, 'assoc_info' => $associ_info, 'join_index' => $i); //\hat\dbg::alert($right_part); if ($right_part == 'HatSystemConfigArray ca') { //\hat\dbg::alert($table_info); //\hat\dbg::alert($this->_tables[$left_table_name]->toArray(), true); } $this->_tables_in_use[] = $table_info; if ($associ_info['type'] == 'hasMany') { //$this->_has_one_to_many_assoc = true; $this->_has_one_to_many_assoc = $associ_info; } $this->_additional_select[$left_alias][] = $associ_info['keys']['local']; $this->_additional_select[$assoc_alias][] = $associ_info['keys']['foreign']; $this->_additional_select[$assoc_alias] = \array_merge($this->_additional_select[$assoc_alias], $this->_tables[$model_name]->getPrimaryKeys()); $this->_join_pairs[$left_alias . '__' . $associ_info['keys']['local']] = $assoc_alias . '__' . $associ_info['keys']['foreign']; $this->_join_node_keys['local'][$left_alias . '__' . $associ_info['keys']['local']] = true; $this->_join_node_keys['foreign'][$assoc_alias . '__' . $associ_info['keys']['foreign']] = true; $assoc_table_name = $this->_tables[$model_name]->getTableName(); $join_sql = " LEFT JOIN {$assoc_table_name} AS {$assoc_alias} ON {$left_alias}.{$associ_info['keys']['local']}={$assoc_alias}.{$associ_info['keys']['foreign']} "; if (isset($join['condition'])) { //\hat\dbg::alert($join['condition'], true); //\hat\dbg::alert(array($i => $join)); //\hat\Object::__d($join); $prepare = $this->_prepareQueryParts_where($join['condition']); //\hat\dbg::alert($prepare, true); if ($prepare) { $parse = $this->_parseQueryParts_where($prepare); if ($this->_query_debug) { //\hat\dbg::alert($parse); } if ($parse && isset($parse[0])) { $join_sql .= " AND {$parse[0]} "; // \hat\dbg::alert($parse); // \hat\dbg::alert($join_sql, true); } } } $this->_JOIN[$assoc_alias] = $join_sql; // $filename = $this->_table_path . $model_name . '.php'; // if(\file_exists($filename)){ // include_once $filename; // $model_class_name = $this->_table_namespace . $model_name; // $this->_tables[$model_name] = new $model_class_name(); // $table_info = array( // 'model_name' => $model_name, // 'model_alias' => $assoc_alias, // 'assoc_name' => $assoc_name, // 'assoc_to_alias' => $left_alias, // 'assoc_to_name' => $left_table_name, // 'assoc_info' => $associ_info // ); // $this->_tables_in_use[] = $table_info; // if($associ_info['type'] == 'hasMany'){ // //$this->_has_one_to_many_assoc = true; // $this->_has_one_to_many_assoc = $associ_info; // } // $this->_additional_select[$left_alias][] = $associ_info['keys']['local']; // $this->_additional_select[$assoc_alias][] = $associ_info['keys']['foreign']; // // $this->_join_pairs[$left_alias . '__' . $associ_info['keys']['local']] = $assoc_alias . '__' . $associ_info['keys']['foreign']; // $this->_join_node_keys['local'][$left_alias . '__' . $associ_info['keys']['local']] = true; // $this->_join_node_keys['foreign'][$assoc_alias . '__' . $associ_info['keys']['foreign']] = true; // // $assoc_table_name = $this->_tables[$model_name]->getTableName(); // $join_slq = " LEFT JOIN $assoc_table_name AS $assoc_alias ON $left_alias.{$associ_info['keys']['local']}=$assoc_alias.{$associ_info['keys']['foreign']} "; // $this->_JOIN[$assoc_alias] = $join_slq; // // }else{ // throw new \Exception("Module not found for $model_name in $this->_table_path."); // } } else { $this->pdo_error_message[] = 'invalid join sintax.'; return false; } } // end left join } // print_r($this->_JOIN); exit; // print_r($this->_additional_select); return true; }