<?
    $dbserver
= "";
    
$dbname = "";
    
$dbuser = "";
    
$dbpass = "";
    
$table = "";
    
$class = "";

    if(
array_key_exists("dbserver", $_GET)) {
        
$dbserver = $_GET["dbserver"];
    }
    if(
array_key_exists("dbname", $_GET)) {
        
$dbname = $_GET["dbname"];
    }
    if(
array_key_exists("dbuser", $_GET)) {
        
$dbuser = $_GET["dbuser"];
    }
    if(
array_key_exists("dbpass", $_GET)) {
        
$dbpass = $_GET["dbpass"];
    }

    if(
array_key_exists("table", $_GET)) {
        
$table = $_GET["table"];
    }
    if(
array_key_exists("class", $_GET)) {
        
$class = $_GET["class"];
    }

?>
<html>
<head>
</head>
<body>

<!-- start main body -->
<b>DAO\VO Builder</b>
<p />
<table>
<form action="daovo_builder.php" method="get">
<tr><td align="right">Server</td>
    <td><input type="text" name="dbserver" size="25" maxlength="50" value="<?=$dbserver?>" /></td>
    <td></td>
    </tr>
<tr><td align="right">Database</td>
    <td><input type="text" name="dbname" size="25" maxlength="50" value="<?=$dbname?>" /></td>
    <td></td>
    </tr>
<tr><td align="right">User</td>
    <td><input type="text" name="dbuser" size="25" maxlength="50" value="<?=$dbuser?>" /></td>
    <td></td>
    </tr>
<tr><td align="right">Password</td>
    <td><input type="text" name="dbpass" size="25" maxlength="50" value="<?=$dbpass?>" /></td>
    <td></td>
    </tr>

<tr><td align="right">Table</td>
    <td><input type="text" name="table" size="25" maxlength="50" value="<?=$table?>" /></td>
    <td>The name of the table in the MySQL database</td>
    </tr>
<tr><td align="right">Class</td>
    <td><input type="text" name="class" size="25" maxlength="50" value="<?=$class?>" /></td>    
    <td>The name of the class you want to generate</td>
    </tr>
<tr><td></td>
    <td colspan="2"><input type="submit" value="Submit" /></td>
    </tr>
</form>
</table>
<?    
    
    
    
    
if(strlen($table) > 0 && strlen($class) > 0) {
        include(
"TableDefDAO.php");    
        
$dao = new TableDefDAO($dbserver, $dbname, $dbuser, $dbpass);
        echo
"getting table info from db...<br />";
        
$volist = $dao->describe($table);
    
        echo
mysql_error();
        echo
"<table border='1'>";
        echo
"<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>";
        foreach(
$volist as $vo) {
            echo
"<tr>";
            echo
"<td>$vo->field</td>";
            echo
"<td>$vo->type</td>";
            echo
"<td>$vo->isnull&nbsp;</td>";
            echo
"<td>$vo->iskey&nbsp;</td>";
            echo
"<td>$vo->default&nbsp;</td>";
            echo
"<td>$vo->extra&nbsp;</td>";
            echo
"</tr>";
        }
        echo
"</table>";
    
        
$dao->close();
    
        
// find the primary key field    
        
$pk_field = "id";
        foreach(
$volist as $vo) {
            if(
$vo->isPK()) {
                
$pk_field = $vo->field;
            }
        }
        
        echo
"building class...<br />";
    
        
$buf = "<?\n";
    
        
$buf .= "require_once (\"BaseDAO.php\");\n\n";
    
        
$buf .= "// $class Value Object\n";
        
$buf .= "class " . $class . "VO extends BaseVO {\n";
        foreach(
$volist as $vo) {
            
$buf .= "\tvar \x24" . $vo->field . ";\n";
        }
        
$buf .= "\n";
        
$buf .= "\t// create a new VO\n";
        
$buf .= "\tfunction " . $class . "VO(\n";
        
$tmp = "";
        foreach(
$volist as $vo) {        
            if(
$vo->isNumber()) {
                
$tmp .="\t\t\x24" . $vo->field . "= 0,\n";
            } else {
                
$tmp .="\t\t\x24" . $vo->field . "= \"\",\n";
            }
        }
        
//clear off the last ",\n"
        
$tmp = substr($tmp, 0, strlen($tmp)-2);
        
$buf .= $tmp . "\n";
        
$buf .= "\t) {\n";
        
        foreach(
$volist as $vo) {        
            
$buf .= "\t\t\x24this->" . $vo->field . " = \x24" . $vo->field . ";\n";
        }
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// compare to another VO\n";
        
$buf .= "\tfunction equals(\x24vo) {\n";
        
$buf .= "\t\treturn ";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\x24this->" . $vo->field . " == \x24vo->" . $vo->field . " &&\n\t\t\t";
        }
        
//clear off the last "&&\n"
        
$tmp = substr($tmp, 0, strlen($tmp)-7);
        
// add a ";\n"
        
$tmp .= ";\n";
        
$buf .= $tmp;
        
$buf .= "\t}\n";
    
        
$buf .= "\t// copies another VO\n";
        
$buf .= "\tfunction copy(\x24vo) {\n";    
        foreach(
$volist as $vo) {
            
$buf .= "\t\t\x24this->" . $vo->field . " = \x24vo->" . $vo->field . ";\n";
        }        
        
$buf .= "\t}\n\n";
        
        
$buf .= "\t// output as a string\n";
        
$buf .= "\tfunction toString() {\n";
        
$buf .= "\t\treturn ";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\x24this->" . $vo->field . " . \",\" . ";
        }
        
$tmp = substr($tmp, 0, strlen($tmp)-9);
        
$tmp .= ";\n";
        
$buf .= $tmp;
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// output as XML node\n";    
        
$buf .= "\tfunction toXML() {\n";
        
$buf .= "\t\treturn \"<row>\\n\" .\n";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\t\t\"<" . $vo->field . ">\x24this->" . $vo->field . "</" . $vo->field . ">\\n\" .\n";
        }
        
$buf .= $tmp;
        
$buf .= "\t\t\"</row>\\n\";\n";
        
$buf .= "\t}\n\n";
        
        
$buf .= "\t// read from an html form\n";
        
$buf .= "\tfunction readForm() {\n";
        foreach(
$volist as $vo) {
            if(
$vo->isNumber()) {
                
$buf .= "\t\t\x24this->" . $vo->field . " = \x24this->formHelper(\"" . $vo->field . "\", 0);\n";
            } else {
                
$buf .= "\t\t\x24this->" . $vo->field . " = \x24this->formHelper(\"" . $vo->field . "\", \"\");\n";
            }
        }
        
$buf .= "\t}\n\n";
        
        
$buf .= "\t// read from the query string\n";
        
$buf .= "\tfunction readQuery() {\n";
        foreach(
$volist as $vo) {
            if(
$vo->isNumber()) {
                
$buf .= "\t\t\x24this->" . $vo->field . " = \x24this->queryHelper(\"" . $vo->field . "\", 0);\n";
            } else {
                
$buf .= "\t\t\x24this->" . $vo->field . " = \x24this->queryHelper(\"" . $vo->field . "\", \"\");\n";
            }
        }
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// extra functions here\n";
        
$buf .= "\tfunction getPK() {\n";
        foreach(
$volist as $vo) {
            if(
$vo->iskey == "PRI") {
                
$buf .= "\t\treturn \x24this->" . $vo->field . ";\n";
            }
        }    
        
$buf .= "\t}\n";
    
        
        
$buf .= "}\n\n";
    
    
        
$buf .= "// $class Data Access Object\n";
        
$buf .= "class " . $class . "DAO extends BaseDAO {\n";
        
$buf .= "\tvar \x24SQL_SELECT = \"SELECT * FROM `$table` \";\n";
        
$buf .= "\tvar \x24SQL_COUNT = \"SELECT count(*) AS cnt FROM `$table` \";\n";    
        
$buf .= "\t// insert with no value for an auto_increment field.\n";
        
$buf .= "\tvar \x24SQL_INSERT = \"INSERT INTO `$table` (";
        
$tmp = "";
        foreach(
$volist as $vo) {
            if (
$vo->extra != "auto_increment") {
                
$tmp .= $vo->field . ",";
            }
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-1);
        
$buf .= ") VALUES (";
        
$cnt = 65;    
        
$tmp = "";
        foreach(
$volist as $vo) {
            
// do not insert auto_increment type fields.
            
if ($vo->extra != "auto_increment") {
                
// letter A to Z... any table with more than 26 fields
                // is not normalized and the DBA should be killed.
                
if($vo->isNumber()) {
                    
$tmp .= "%" . chr($cnt++) . ",";
                } else {
                    
$tmp .= "'%" . chr($cnt++) . "',";
                }
                if(
$cnt > 90 && $cnt < 97) { $cnt=97 ;}
            }
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-1);    
        
$buf .= ")\";\n";
/*
        $buf .= "\t// insert using LAST_INSERT_ID() for an auto_increment field.\n";
        $buf .= "\tvar \x24SQL_INSERT_WITH_ID = \"INSERT INTO `$table` (";
        $tmp = "";
        foreach($volist as $vo) {
            $tmp .= $vo->field . ",";
        }
        $buf .= substr($tmp, 0, strlen($tmp)-1);
        $buf .= ") VALUES (";
        $cnt = 65;    
        $tmp = "";
        foreach($volist as $vo) {
            // do not insert auto_increment type fields.
            if ($vo->extra == "auto_increment") {
                $tmp .= "LAST_INSERT_ID(),";
            } else {
                // letter A to Z... any table with more than 26 fields
                // is not normalized and the DBA should be killed.
                if($vo->isNumber()) {
                    $tmp .= "%" . chr($cnt++) . ",";
                } else {
                    $tmp .= "'%" . chr($cnt++) . "',";
                }
                if($cnt > 90 && $cnt < 97) { $cnt=97 ;}
            }
        }
        $buf .= substr($tmp, 0, strlen($tmp)-1);    
        $buf .= ")\";\n";
*/

        
$buf .= "\tvar \x24SQL_UPDATE = \"UPDATE `$table` SET \";\n";
        
$buf .= "\tvar \x24SQL_DELETE = \"DELETE FROM `$table` ";
        
$tmp = "";
        foreach(
$volist as $vo) {
            if(
$vo->isPK()) {
                if(
$vo->isNumber()) {
                    
$tmp = "WHERE " . $vo->field . "=%A\";\n";
                } else {
                    
$tmp = "WHERE " . $vo->field . "='%A'\";\n";
                }
            }
        }
        
$buf .= $tmp . "\n";
        
        
$buf .= "\t// default constructor\n";
        
$buf .= "\tfunction " . $class . "DAO(\x24dbserver=\"\", \x24dbname=\"\", \x24dbuser=\"\", \x24dbpass=\"\") {\n";
        
$buf .= "\t\t// calls the parent constructor which\n";
        
$buf .= "\t\t// makes the database connection.\n";
        
$buf .= "\t\tparent::BaseDAO(\x24dbserver, \x24dbname, \x24dbuser, \x24dbpass);\n";
        
$buf .= "\t}\n\n";
        
        
$buf .= "\t// returns a single " . $class . "VO obeject\n";
        
$buf .= "\tfunction findByPK(\x24" . $pk_field . ") {    \n";
        
$buf .= "\t\x24this->sql = \x24this->SQL_SELECT . \"WHERE (" . $pk_field . "='\x24" . $pk_field . "')\";\n";
        
$buf .= "\t\t\x24this->exec(\x24this->sql);\n";
        
$buf .= "\t\tif(\x24this->numRows() > 0 ) {\n";
        
$buf .= "\t\t\t\x24row = \x24this->getObject();\n";
        
$buf .= "\t\t\t\x24vo = new " . $class . "VO(\n";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\t\t\t\t\x24row->" . $vo->field . ", \n";
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-3);
        
$buf .= "\n\t\t\t);\n";
        
$buf .= "\t\t\treturn \x24vo;\n";
        
$buf .= "\t\t} else {\n";
        
$buf .= "\t\t\treturn null;    \n";
        
$buf .= "\t\t}\n";    
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// returns an array of " . $class . "VO objects\n";
        
$buf .= "\t// given a complete SQL query \n";
        
$buf .= "\tfunction findBySQL(\x24sql) {\n";
        
$buf .= "\t\t\x24voList = array();\n";
        
$buf .= "\t\t\x24this->sql = \x24sql;\n";
        
$buf .= "\t\t\x24this->exec(\x24this->sql);\n";
        
$buf .= "\t\twhile(\x24row = \x24this->getObject()) {\n";
        
$buf .= "\t\t\t\x24vo = new " . $class . "VO(\n";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\t\t\t\t\x24row->" . $vo->field . ", \n";
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-3);
        
$buf .= "\n\t\t\t);\n";
        
$buf .= "\t\t\tarray_push(\x24voList, \x24vo);\n";
        
$buf .= "\t\t}\n";
        
$buf .= "\t\treturn \x24voList;\n";
        
$buf .= "\t}\n\n";
        
        
$buf .= "\t// returns an array of " . $class . "VO objects\n";
        
$buf .= "\t// value for \x24where is some thing like \"(xxx = 'abc') AND (zzz='123')\"\n";
        
$buf .= "\t// value for \x24order_by is a comma separated list of columns \"company, name\"\n";
        
$buf .= "\tfunction findWhere(\x24where = \"\", \x24orderby = \"\") {\n";
        
$buf .= "\t\t\x24this->sql = \x24this->SQL_SELECT;\n";    
        
$buf .= "\t\t\x24voList = array();\n";    
        
$buf .= "\t\tif(strlen(\x24where) > 0 ) {\n";
        
$buf .= "\t\t\t\x24where = \"WHERE (\" . \x24where . \") \";\n";
        
$buf .= "\t\t}\n";
        
$buf .= "\t\tif(strlen(\x24orderby) > 0) {\n";
        
$buf .= "\t\t\t\x24orderby = \"ORDER BY \" . \x24orderby;\n";
        
$buf .= "\t\t} else {\n";
        
$buf .= "\t\t\t\x24orderby = \"ORDER BY 1\";\n";
        
$buf .= "\t\t}\n";
        
$buf .= "\t\t\x24this->sql .= \x24where . \" \" . \x24orderby;\n";        
        
$buf .= "\t\t\x24this->exec(\x24this->sql);\n";
        
$buf .= "\t\twhile(\x24row = \x24this->getObject()) {\n";
        
$buf .= "\t\t\t\x24vo = new " . $class . "VO(\n";
        
$tmp = "";
        foreach(
$volist as $vo) {
            
$tmp .= "\t\t\t\t\x24row->" . $vo->field . ", \n";
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-3);
        
$buf .= "\n\t\t\t);\n";
        
$buf .= "\t\t\tarray_push(\x24voList, \x24vo);\n";
        
$buf .= "\t\t}\n";
        
$buf .= "\t\treturn \x24voList;\n";
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// insert a record from a vo\n";
        
$buf .= "\tfunction insertVO(\x24vo) {\n";
        
$buf .= "\t\t\x24this->sql = \x24this->SQL_INSERT;\n";
        
$tmp = "";
        
$cnt = 65;
        foreach(
$volist as $vo) {
            if (
$vo->extra != "auto_increment") {
                
$tmp .="\t\t\x24this->sql = str_replace(\"%" . chr($cnt++) . "\", \x24vo->" . $vo->field . ", \x24this->sql);\n";
                if(
$cnt > 90) { $cnt=97 ;}
            }
        }
        
$buf .= $tmp;
        
$buf .= "\n\t\t\x24this->exec(\x24this->sql);\n";
        
$buf .= "\t\treturn \x24this->affectedRows();\n";
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// update a record from a vo\n";
        
$buf .= "\tfunction updateVO(\x24vo) {\n";
        
$buf .= "\t\t\x24this->sql = \x24this->SQL_UPDATE;\n";
        
$tmp = "";
        foreach(
$volist as $vo) {
            if (!
$vo->isPK()) {
                
$tmp .= "\t\t\x24this->sql .= \"" . $vo->field . " = '\" . \x24vo->" . $vo->field . " . \"', \";\n";
            }
        }
        
$buf .= substr($tmp, 0, strlen($tmp)-5) . " \";\n";
        
$buf .= "\n\t\t\x24this->sql .= \"WHERE ". $pk_field . "=\" . \x24vo->". $pk_field . ";\n";
            
        
$buf .= "\n\t\t\x24this->exec($this->sql);\n";
                    
        
$buf .= "\t\treturn \x24this->affectedRows();\n";
        
$buf .= "\t}\n\n";
    
        
$buf .= "\t// delete a record from a vo\n";
        
$buf .= "\tfunction deleteVO(\x24vo) {\n";
        
$buf .= "\t\t\x24this->sql = \x24this->SQL_DELETE;\n";
        
$buf .= "\t\t\x24this->sql = str_replace(\"%A\", \x24vo->". $pk_field . ", \x24this->sql);\n";
    
        
$buf .= "\t\t\x24this->exec(\x24this->sql);\n";
                    
        
$buf .= "\t\treturn \x24this->affectedRows();\n";
        
$buf .= "\t}\n\n";
    
        
        
$buf .= "}\n\n";
?>
<textarea id="" rows="40" cols="100">
<? echo $buf ?>
</textarea>

<? } ?>

<!-- end main body -->
</body>
</html>