数据库配置文件:db_config.php
<?php $vhost="localhost"; $vdb="testdb"; $vuname="root"; $vupwd=""; $vcode="utf-8"; ?>
模板文件:t_source.html
<div id="phpcode"> <pre ID="codearea" style="background-color:#F0F0F0;border:1px dotted black"> /* 作者:由martixwang的自动PHP生成器生成 版本:1.0 */ require_once 'ado.php'; class <{$tablename}> extends ADO{ <{$classPropertys}> <{$propertyGeter}> <{$propertySeter}> function __construct(){ parent::__construct(); } public function query($whereExp){ $result=""; if(strlen($whereExp)!=0){ $result=$this->executeQuery("<{$selectsql}> where ".$whereExp, 0, 0); } else { $result=$this->executeQuery("<{$selectsql}>",0,0); } $returnVal=array(); $i=0; while($row=mysql_fetch_array($result)){ $vc=new <{$tablename}>(); <{$propertyAssignstring}>; array_push($returnVal,$vc); $i++; } $this->closeConnection(); return $returnVal; } public function addNew($new){ $sqls=array("insert into <{$tablename}> (<{$fieldslist}>) values (".<{$valueslist}>.")"); $result=$this->executeCommands($sqls); return $result; } public function modifyBy<{$PKfieldName}>($new){ //$tempsql=<{$modifysql}>." where <{$PKfieldName}>=".$new->get<{$PKfieldName}>(); $sqls=array(<{$modifysql}>." where <{$PKfieldName}>=".$new->get<{$PKfieldName}>()); $result=$this->executeCommands($sqls); return $result; } public function deleteBy<{$PKfieldName}>($<{$PKfieldName}>){ $sqls=array("<{$deletesql}>"." where <{$PKfieldName}>=".$<{$PKfieldName}>); $result=$this->executeCommands($sqls); return $result; } public function pageCount($whereExp, $pagesize){ return $this->getPageCount("<{$tablename}>", $whereExp, $pagesize); } public function rowCount($whereExp){ return $this->getRowCount("<{$tablename}>",$whereExp); } public function Exist($whereExp){ return $this->isExist("<{$tablename}>", $whereExp); } function toJSON() { $sResult=""; foreach($this as $key=>$value) { //根据 http://www.json.org/ $value=str_replace("\"","\\\"",$value); $value=str_replace("\\","\\\\",$value); $value=str_replace("/","\\/",$value); $value=str_replace("\b","\\b",$value); $value=str_replace("\f","\\f",$value); $value=str_replace("\n","\\n",$value); $value=str_replace("\r","\\r",$value); $value=str_replace("\t","\\t",$value); if($sResult=="") $sResult="\"$key\":\"$value\""; else $sResult.=" , \"$key\":\"$value\""; } return "{".$sResult."}"; } } class <{$tablename}>BLL extends <{$tablename}>{ public function __construct(){ parent::__construct(); } } </pre> </div>
基于mysql的通用访问类:ado.php
<?php class ADO{ private $dbhost; private $db=""; private $uname=""; private $upwd=""; private $conn; private $query; private $code; function __construct(){ include 'db_config.php'; $this->dbhost=$vhost; $this->uname=$vuname; $this->upwd=$vupwd; $this->code=$vcode; //echo "<br>db_config:".$this->dbhost.$this->uname.$this->upwd."<br>"; $this->db=$vdb; // $this->connect(); } //public function open_connect(){ // $this->connect(); //} private function connect(){ //echo "db_config".$this->dbhost.$this->uname.$this->upwd; $this->conn=mysql_connect($this->dbhost,$this->uname,$this->upwd) or die("connect fail!!!"); if(!$this->conn) echo "connection database fail!!!<br>".mysql_error(); mysql_select_db($this->db,$this->conn); mysql_query("set names '". $this->code ."'"); } protected function executeQuery($sql,$pagenumber,$pagesize){ $this->connect(); if($pagenumber!=0){ $sql=$sql." limit ".$pagenumber.",".$pagesize; } $query=mysql_query($sql,$this->conn); return $query; } //批量提交增删改命令 protected function executeCommands($sqls){ $this->connect(); mysql_query("BEGIN"); $error=1; foreach($sqls as $k=>$v){ $result=mysql_query($v,$this->conn); if(!$result){ $error=0; //echo $v." 执行失败!<br>"; break; } else { //echo $v." 执行成功!<br>"; } } if($error==0){ mysql_query("ROLLBACK"); } else { mysql_query("COMMIT"); } mysql_query("END"); $this->closeConnection(); return $error; } //取得页总数 protected function getPageCount($tablename,$whereExp,$pagesize){ $this->connect(); if(strlen($whereExp)!=0){ $sql="select ceiling(count(*)/".$pagesize.") as rowcount from ".$tablename." where ".$whereExp; } else { $sql="select ceiling(count(*)/".$pagesize.") as rowcount from ".$tablename; } $query=mysql_query($sql,$this->conn); $row=mysql_fetch_row($query); $this->closeConnection(); $rw= $row[0]; $this->closeConnection(); return $rw; } //取得行总数 protected function getRowCount($tablename,$whereExp){ $this->connect(); if(strlen($whereExp)!=0){ $sql="select count(*) as rowcount from ".$tablename." where ".$whereExp; }else { $sql="select count(*) as rowcount from ".$tablename;//." where ".$whereExp; } //echo $sql; $query=mysql_query($sql,$this->conn); $row=mysql_fetch_row($query); $rw= $row[0]; $this->closeConnection(); return $rw; } //按条判断存在性 protected function isExist($tablename,$whereExp){ $this->connect(); $c=$this->getRowCount($tablename,$whereExp); $this->closeConnection(); if($c!=0) {return 1;} else {return 0;} } //关闭链接 protected function closeConnection(){ mysql_close(); } } ?>
根据选择定的数据库表,生成dal代码:codecreate.php(这部分代码还没有优化,将就着用吧)
<?php require "./main.php"; require_once 'ado.php'; class mataRow{ private $fname=""; private $ftype=""; private $pk=""; /** * @return the $fname */ public function getFname() { return $this->fname; } /** * @return the $ftype */ public function getFtype() { return $this->ftype; } /** * @return the $pk */ public function getPk() { return $this->pk; } /** * @param string $fname */ public function setFname($fname) { $this->fname = $fname; } /** * @param string $ftype */ public function setFtype($ftype) { $this->ftype = $ftype; } /** * @param string $pk */ public function setPk($pk) { $this->pk = $pk; } } class codeCreate extends ADO{ private $tablename=""; private $fields=array(); private $updatesql=""; private $selectsql=""; private $deletesql=""; private $insertsql=""; private $fieldInfo=array(); private $pkFieldName=""; public function getPkFieldName() { return $this->pkFieldName; } public function setPkFieldName($pkFieldName) { $this->pkFieldName = $pkFieldName; } public function __construct($tabname){ parent::__construct(); $this->setTablename($tabname); $this->fieldInfo=$this->getTableInfo(); } public function getUpdatesql() { return $this->updatesql; } public function getSelectsql() { return $this->selectsql; } public function getDeletesql() { return $this->deletesql; } public function getInsertsql() { return $this->insertsql; } public function setUpdatesql($updatesql) { $this->updatesql = $updatesql; } public function setSelectsql($selectsql) { $this->selectsql = $selectsql; } public function setDeletesql($deletesql) { $this->deletesql = $deletesql; } public function setInsertsql($insertsql) { $this->insertsql = $insertsql; } public function getTablename() { return $this->tablename; } public function setTablename($tablename) { $this->tablename = $tablename; } public function getFields() { return $this->fields; } public function setFields($fields) { $this->fields = $fields; } public function getTableInfo(){ $returnVal=array(); $i=0; $result=$this->executeQuery("desc ".$this->tablename.";",0,0); if(!$result) echo "表没有找到,请检查数据库的配置文件db_config.php"; while($row=mysql_fetch_array($result)) { $mata=new mataRow(); //echo "<br>row:".$row[Field]."<br>".$row[Type]."<br>".$row[Key]."<br>"; $mata->setFname($row[Field]); $mata->setFtype($row[Type]); $mata->setPk($row[Key]); if($row[Key]=="PKI"){$this->setPkFieldName($row["Key"]);} $returnVal[$i]=$mata; $i++; } $this->closeConnection(); //print_r($returnVal); return $returnVal; } } $tablename=$_POST["selectComobo"]; echo "<input type='button' value='返回' onclick='javascript:history.go(-1);' > ". "<input type='button' value='复制' onclick=\"clipboardData.setData('Text',document.getElementById('codearea').innerHTML);\" />"; echo "<br><P style='color:red'>你选择的表是:".$_POST["selectComobo"]."的类操作代码成功生成!</p>"; if(strlen($tablename)!=0){ $gen=new codeCreate($tablename); $gen->setTablename($tablename); $rs=$gen->getTableInfo(); //echo "<br>ddddddddddddd:".$rs[0]->getFname(); //echo "<br>list:".count($rs)."<br>"; $v=new mataRow(); $n=count($rs); $i=0; $PKfieldName=""; $selectsql="select * from ".$tablename; $deletesql="delete from ".$tablename; $fieldslist=""; $valueslist=""; $fieldvaluelist=""; $propertyAssignstring=""; $classPropertys=""; $propertyGeter=""; $propertySeter=""; $tab_head=""; $tab_body="<{foreach item=v from=\$result}>\n<tr id='row<{\$v->getid()}>'>\n"."12345678". "\n<td><a href='#' onclick='addnew(<{\$v->getid()}>)'>增</a> \n". "<a href='#' id='delink' onclick='del(<{\$v->getid()}>)'>删</a> \n". "<a href='#' onclick='modify(<{\$v->getid()}>)'>改</a> \n<a href='#'>阅</a></td><tr>". "\n<{/foreach}>"; $tds=""; //"<td style='width:60px'><input type=text id='txt<{$v->getFname()}>' value='<{$v->getContentPostUserName()}>'</td>"; while($i<$n){ $v=$rs[$i]; //echo $v->getFname()." ".$v->getFtype()." ".$v->getFtype()." ".$v->getPk()."<br>"; $tab_head=$tab_head."<td>".$v->getFname()."</td>\n"; $tds=$tds."\n<td style='width:60px'><{\$v->get".$v->getFname()."()}></td>\n"; //$vc->contenttypeid=$row["id"]; $tempPA=""; $tempPA="\$vc->".$v->getFname()."=\$row[\"".$v->getFname()."\"]"; $propertyAssignstring=(strlen($propertyAssignstring)==0?$propertyAssignstring:$propertyAssignstring.";").$tempPA; $tempCP=""; $tempCP="private $".$v->getFname().";"; $classPropertys=(strlen($classPropertys)==0?$classPropertys:$classPropertys."").$tempCP; // public function getContenttypeid() {return $this->contenttypeid;} $tempPG="public function get".$v->getFname()."() {return \$this->".$v->getFname().";}"; $propertyGeter=(strlen($propertyGeter)==0?$propertyGeter:$propertyGeter."").$tempPG; $tempPS="public function set".$v->getFname()."(\$".$v->getFname()."){\$this->".$v->getFname()." = \$".$v->getFname().";}"; $propertySeter=(strlen($propertySeter)==0?$propertySeter:$propertySeter."").$tempPS; if($v->getPk()=="PRI"){ //如果是主键字段 //echo "i get the PKI"; $PKfieldName=$v->getFname(); }else{ //echo "<br>".$v->getFname()."-------".$v->getFtype()."<br>"; $temptype=$v->getFtype(); //echo "<br>the value is found?:".$v->getFname()."======================".$temptype."==============================".(stripos("_".$temptype,"int")?"found":"not found!")."<br>"; $fieldslist=(strlen($fieldslist)==0?$fieldslist:$fieldslist.",").$v->getFname(); $tempfvbegin=$v->getFname()."="; $tempfvend="\","; $tempfvstring=""; if(stripos("_".$temptype,"int")||stripos("_".$temptype,"num")||stripos("_".$temptype,"bit")||stripos("_".$temptype,"flo")||stripos("_".$temptype,"dou")||stripos("_".$temptype,"dec")){ $tempfv="\$new->get".$v->getFname()."()"; $tempfvbegin=$v->getFname()."="; $tempfvend=","; //echo "I GOT INT TYPE@".$temptype; } else { $tempfv="\"'\".\$new->get".$v->getFname()."().\"'\""; $tempfvbegin=$tempfvbegin."'"; $tempfvend="'"; } $tempfvstring=$tempfvbegin."\$new->get".$v->getFname()."()".$tempfvend; $fieldvaluelist=(strlen($fieldvaluelist)==0?$fieldvaluelist:$fieldvaluelist.",").$tempfvstring; $valueslist=(strlen($valueslist)==0?$valueslist:$valueslist.".\",\".").$tempfv; } $tempmodifysql="update ".$tablename." set ".$fieldvaluelist; $tempmodifysql="\"".str_replace("',",".\"',",str_replace("='","='\".",$tempmodifysql)); $tempmodifysql=str_replace("=\$","=\".\$",$tempmodifysql); $tempmodifysql=str_replace(",,",".\",",$tempmodifysql); if(strripos($tempmodifysql,"'")+1==strlen($tempmodifysql)){ $tempmodifysql=substr($tempmodifysql, 0,strlen($tempmodifysql)-1).".\"'\""; } if(strripos($tempmodifysql,",")+1==strlen($tempmodifysql)){ $tempmodifysql=substr($tempmodifysql, 0,strlen($tempmodifysql)-1);//.".\"'\""; } //$tempmodifysql=str_replace(",.",".",$tempmodifysql); $i++; } $tab_body=str_replace("12345678", $tds, $tab_body); // echo "<br>fieldlist and valuelist:".$fieldslist."<br>".$valueslist."<br>fvlist:".$fieldvaluelist."<br>propertyassignstring:".$propertyAssignstring. // "<br>classprorpertyes:".$classPropertys."<br>propertygeter:".$propertyGeter; // echo "<BR>propertySetter:". $propertySeter ; // echo "<br>tempmodifysql:".$tempmodifysql; $tpl->assign("tab_head",$tab_head); $tpl->assign("tab_body",$tab_body); $tpl->assign("tablename",$tablename); $tpl->assign("PKfieldName",$PKfieldName); $tpl->assign("selectsql",$selectsql); $tpl->assign("fieldslist",$fieldslist); $tpl->assign("valueslist",$valueslist); $modifysql=$tempmodifysql; $tpl->assign("modifysql",$modifysql); $tpl->assign("deletesql",$deletesql); $tpl->assign("propertyAssignstring",$propertyAssignstring); $tpl->assign("classPropertys",$classPropertys); $tpl->assign("propertyGeter",$propertyGeter); $tpl->assign("propertySeter",$propertySeter); $tpl->display("t_source.htm"); } else { echo "没有选择表,不能进行生成!!!"; } ?>
smarty调用文件:codecreateapp.php
<?php include "./main.php"; include "./dal/ado.php"; class test extends ADO{ public function __construct(){ parent::__construct(); } public function getTablesInfoOnServer(){ $result=array(); $rs=$this->executeQuery("show tables;",0,0); //print_r($rs); //echo "...."; while($t=mysql_fetch_array($rs)){ //echo $t[Tables_in_testdb]."|<br>"; array_push($result,$t[Tables_in_testdb]); } $this->closeConnection(); return $result; } } $t=new test(); $r=$t->getTablesInfoOnServer(); $tpl->assign("result",$r); $tpl->display("t_codeCreate.html"); ?>