/** * Enter description here... * * @param unknown_type $ret * @return */ function generateDomainObjects($ret){ for($i=0;$i<count($ret);$i++){ if(!doesTableContainPK($ret[$i])){ continue; } $tableName = $ret[$i][0]; $clazzName = getClazzName($tableName); if($clazzName[strlen($clazzName)-1]=='s'){ $clazzName = substr($clazzName, 0, strlen($clazzName)-1); } $template = new Template('templates/Domain.tpl'); $template->set('domain_class_name', $clazzName); $template->set('table_name', $tableName); $tab = getFields($tableName); $fields = ""; $initFields = ""; $getsetMethods = "\n"; $toStrFields = "return ''"; for($j=0;$j<count($tab);$j++) { $fieldName = getVarNameWithS($tab[$j][0]); $fields .= "\tprivate $".$fieldName.";\n"; $initFields .= "\t\tif (isset(\$fields['".$fieldName."'])) \$this->".$fieldName." = \$fields['".$fieldName."'];\n"; $camelFieldName = $fieldName; $camelFieldName[0] = strtoupper($fieldName[0]); $getsetMethods .= "\tpublic function get$camelFieldName() {\n\t\treturn \$this->$fieldName;\n\t}\n"; $getsetMethods .= "\tpublic function set$camelFieldName($".$fieldName.") {\n\t\t\$this->fields['$fieldName'] = $".$fieldName.";\n\t\t\$this->$fieldName = $".$fieldName.";\n\t}\n\n"; $toStrFields .= ".':'.\$this->".$fieldName; } $toStrFields .= ";"; $template->set('variables', $fields); $template->set('init_variables', $initFields); $template->set('getset_methods', $getsetMethods); $template->set('tostr_variables', $toStrFields); $template->set('date', date("Y-m-d H:i")); $template->write('generated/'.$clazzName.'.php'); } }
function getnerateIDAOObjects($ret) { for ($i = 0; $i < count($ret); $i++) { if (!doesTableContainPK($ret[$i])) { continue; } $tableName = $ret[$i][0]; $clazzName = getClazzName($tableName); $tab = getFields($tableName); $parameterSetter = "\n"; $insertFields = ""; $updateFields = ""; $questionMarks = ""; $readRow = "\n"; $pk = ''; $pks = array(); $queryByField = ''; $deleteByField = ''; for ($j = 0; $j < count($tab); $j++) { if ($tab[$j][3] == 'PRI') { $pk = $tab[$j][0]; $c = count($pks); $pks[$c] = $tab[$j][0]; } else { $insertFields .= $tab[$j][0] . ", "; $updateFields .= $tab[$j][0] . " = ?, "; $questionMarks .= "?, "; if (isColumnTypeNumber($tab[$j][1])) { $parameterSetter .= "\t\t\$sqlQuery->setNumber(\$" . getVarName($tableName) . "->get" . camelize(getVarNameWithS($tab[$j][0])) . "());\n"; } else { $parameterSetter .= "\t\t" . '$sqlQuery->set($' . getVarName($tab[$j][0]) . ');' . "\n"; } $queryByField .= "\tpublic function queryBy" . getClazzName($tab[$j][0]) . "(\$value);\n\n"; $deleteByField .= "\tpublic function deleteBy" . getClazzName($tab[$j][0]) . "(\$value);\n\n"; } $readRow .= "\t\t\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . " = \$row['" . $tab[$j][0] . "'];\n"; } if ($pk == '') { continue; } if (count($pks) == 1) { $template = new Template('templates/IDAO.tpl'); } else { $template = new Template('templates/IDAO_with_complex_pk.tpl'); } $template->set('dao_clazz_name', $clazzName); $template->set('domain_clazz_name', getDTOName($tableName)); $template->set('dao_clazz_name_singular', plural2singular($clazzName)); $template->set('table_name', $tableName); $template->set('var_name', getVarName($tableName)); $s = ''; $s2 = ''; $s3 = ''; $s4 = ''; $insertFields2 = $insertFields; $questionMarks2 = $questionMarks; for ($z = 0; $z < count($pks); $z++) { $questionMarks2 .= ', ?'; if ($z > 0) { $s .= ', '; $s2 .= ' AND '; $s3 .= "\t\t"; } $insertFields2 .= ', ' . getVarNameWithS($pks[$z]); $s .= '$' . getVarNameWithS($pks[$z]); $s2 .= getVarNameWithS($pks[$z]) . ' = ? '; $s3 .= '$sqlQuery->setNumber(' . getVarName($pks[$z]) . ');'; $s3 .= "\n"; $s4 .= "\n\t\t"; $s4 .= '$sqlQuery->setNumber($' . getVarName($tableName) . '->' . getVarNameWithS($pks[$z]) . ');'; $s4 .= "\n"; } $template->set('question_marks2', $questionMarks2); $template->set('insert_fields2', $insertFields2); $template->set('pk_set_update', $s4); $template->set('pk_set', $s3); $template->set('pk_where', $s2); $template->set('pks', $s); $insertFields = substr($insertFields, 0, strlen($insertFields) - 2); $updateFields = substr($updateFields, 0, strlen($updateFields) - 2); $questionMarks = substr($questionMarks, 0, strlen($questionMarks) - 2); $template->set('pk', $pk); $template->set('insert_fields', $insertFields); $template->set('read_row', $readRow); $template->set('update_fields', $updateFields); $template->set('question_marks', $questionMarks); $template->set('parameter_setter', $parameterSetter); $template->set('read_row', $readRow); $template->set('date', date("Y-m-d H:i")); $template->set('queryByFieldFunctions', $queryByField); $template->set('deleteByFieldFunctions', $deleteByField); $template->write('generated/class/dao/' . $clazzName . 'DAO.class.php'); } }
function generateIDAOObjects($ret) { for ($i = 0; $i < count($ret); $i++) { if (!doesTableContainPK($ret[$i])) { continue; } $tableName = $ret[$i][0]; $clazzName = getClazzName($tableName); $tab = getFields($tableName); $nullReplacer = "\n"; $parameterSetter = "\n"; $insertFields = ""; $updateFields = ""; $questionMarks = ""; $readRow = "\n"; $pk = ''; $pks = array(); $queryByField = ''; $deleteByField = ''; //RJG - Default for no primary key the first column - so that views and tables without a primary key appear //Assume the first field is the primary key $primary_found = false; for ($j = 0; $j < count($tab); $j++) { if ($tab[$j][3] == 'PRI') { $primary_found = true; } } if (!$primary_found) { $tab[0][3] = 'PRI'; } //RJG End for ($j = 0; $j < count($tab); $j++) { if ($tab[$j][3] == 'PRI') { $pk = $tab[$j][0]; $c = count($pks); $pks[$c] = $tab[$j][0]; } else { $insertFields .= $tab[$j][0] . ", "; $updateFields .= $tab[$j][0] . " = ?, "; $questionMarks .= "?, "; //RJG - to handle insert and update with nulls $nullReplacer .= "\t\t\$qpos = strpos(\$sql, '?', \$qpos + 1);\n\t\tif ((!isset(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ")) || is_null(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . "))\n\t\t\t\$sql = substr_replace(\$sql, 'NULL', \$qpos, 1);\n"; if (isColumnTypeNumber($tab[$j][1])) { $parameterSetter .= "\t\tif ((isset(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ")) && (!is_null(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ")))\n\t\t\t\$sqlQuery->setNumber(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ");\n"; } else { $parameterSetter .= "\t\tif ((isset(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ")) && (!is_null(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ")))\n\t\t\t\$sqlQuery->set(\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . ");\n"; } //RJG End $queryByField .= "\tpublic function queryBy" . getClazzName($tab[$j][0]) . "(\$value);\n\n"; $deleteByField .= "\tpublic function deleteBy" . getClazzName($tab[$j][0]) . "(\$value);\n\n"; } $readRow .= "\t\t\$" . getVarName($tableName) . "->" . getVarNameWithS($tab[$j][0]) . " = \$row['" . $tab[$j][0] . "'];\n"; } if ($pk == '') { continue; } if (count($pks) == 1) { $template = new Template('templates/IDAO.tpl'); } else { $template = new Template('templates/IDAO_with_complex_pk.tpl'); } $template->set('dao_clazz_name', $clazzName); $template->set('table_name', $tableName); $template->set('var_name', getVarName($tableName)); $s = ''; $s2 = ''; $s3 = ''; $s4 = ''; $insertFields2 = $insertFields; $questionMarks2 = $questionMarks; for ($z = 0; $z < count($pks); $z++) { $questionMarks2 .= ', ?'; if ($z > 0) { $s .= ', '; $s2 .= ' AND '; $s3 .= "\t\t"; } $insertFields2 .= ', ' . getVarNameWithS($pks[$z]); $s .= '$' . getVarNameWithS($pks[$z]); $s2 .= getVarNameWithS($pks[$z]) . ' = ? '; $s3 .= '$sqlQuery->setNumber(' . getVarName($pks[$z]) . ');'; $s3 .= "\n"; $s4 .= "\n\t\t"; $s4 .= '$sqlQuery->setNumber($' . getVarName($tableName) . '->' . getVarNameWithS($pks[$z]) . ');'; $s4 .= "\n"; } $template->set('question_marks2', $questionMarks2); $template->set('insert_fields2', $insertFields2); $template->set('pk_set_update', $s4); $template->set('pk_set', $s3); $template->set('pk_where', $s2); $template->set('pks', $s); $insertFields = substr($insertFields, 0, strlen($insertFields) - 2); $updateFields = substr($updateFields, 0, strlen($updateFields) - 2); $questionMarks = substr($questionMarks, 0, strlen($questionMarks) - 2); $template->set('pk', $pk); $template->set('insert_fields', $insertFields); $template->set('read_row', $readRow); $template->set('update_fields', $updateFields); $template->set('question_marks', $questionMarks); $template->set('null_replacer', $nullReplacer); $template->set('parameter_setter', $parameterSetter); $template->set('read_row', $readRow); $template->set('date', date("Y-m-d H:i")); $template->set('queryByFieldFunctions', $queryByField); $template->set('deleteByFieldFunctions', $deleteByField); $template->write('modelo/class/dao/' . $clazzName . 'DAO.class.php'); } }