<!-- generic settings -->
<Project baseDir=".." wobDir="wob"/>
<Version comm="0100" needcomm="0100" humanReadable="1.1 alpha" svnTarget="."/>
- <DataBase instance="db" scheme="dbScheme" version="00.03"/>
+ <DataBase instance="db" schema="dbScheme" version="00.03"/>
<!-- configure output -->
<QtClientOutput sourceDir="src" subDir="wob" priInclude="wob.pri"/>
void WocPHPServerOut::newTable(const WocTable&tbl)
{
if(!m_loader.isOpen())return;
+ WocProcessor *woc=WocProcessor::instance();
//create table file
QString fn=m_subdir+"/wt_"+tbl.name()+m_fileext;
QFile tf(m_basedir+"/"+fn);
tf.write(PHPSTART);
QString code="class WT"+tbl.name()+" extends WobTable\n{\n";
//initializer
- code+="protected function __construct(array $data,$isfromdb){parent::__construct($data,$isfromdb,\""+tbl.name()+"\");}\n";
+ code+="protected function __construct(array $data,$isfromdb){parent::__construct($data,$isfromdb,\""+tbl.name()+"\");}\n\n";
+
//static get instance
- code+="public static function getFromDB(...){global $db...;}\n";
- //automatic resolution of internal foreign keys
+ QStringList pcols=tbl.primaryColumns();
+ code+="public static function getFromDB(";
+ for(int i=0;i<pcols.size();i++){
+ if(i)code+=",";
+ code+="$"+pcols[i];
+ }
+ QString dbi="$"+woc->dbInst();
+ code+="){\n\tglobal "+dbi+";\n\t$res="+dbi+"->select(\""+tbl.name()+"\",\"*\",\"";
+ for(int i=0;i<pcols.size();i++){
+ if(i)code+=" AND ";
+ code+=pcols[i]+"=\"."+dbi+"->escapeColumn(\""+tbl.name()+"\",\""+pcols[i]+"\",$"+pcols[i]+").\"";
+ }
+ code+="\");\n\tif($res!==false && count($res)<1)return false;\n\telse return new WT"+tbl.name()+"($res[0],true);\n}\n\n";
+ //static get selection
+ code+="public static function selectFromDB($where){\n\tglobal "+dbi+";\n\t$res="+dbi+"->select(\""+tbl.name()+"\",\"*\",$where);\n\tif($res===false || count($res)<1)return array();\n\t";
+ code+="$r=array();\n\tforeach($res as $row)\n\t\t$r[]=new WT"+tbl.name()+"($row,true);\n\treturn $r;\n}\n\n";
+
+ //TODO:automatic resolution of internal foreign keys
+
//reverse resolution of configured foreign keys
+ QStringList fs=tbl.foreigns();
+ for(int i=0;i<fs.size();i++){
+ QString via=tbl.foreignQuery(fs[i]);
+ //parse via
+ QStringList v1=via.split("=");
+ if(v1.size()!=2){
+ qDebug("Warning: Foreign clause %s of table %s has illegal syntax. Should be foreigntable:column=localcolumn.",fs[i].toAscii().data(),tbl.name().toAscii().data());
+ continue;
+ }
+ QString local=v1[1].trimmed();
+ QStringList foreign=v1[0].split(":");
+ if(foreign.size()!=2){
+ qDebug("Warning: Foreign clause %s of table %s has illegal syntax. Should be foreigntable:column=localcolumn.",fs[i].toAscii().data(),tbl.name().toAscii().data());
+ continue;
+ }
+ code+="public function "+fs[i]+"(){\n\treturn WT"+foreign[0]+"::selectFromDB(\""+foreign[1]+"=\"."+dbi+"->escapeColumn(\"";
+ code+=foreign[0]+"\",\""+foreign[1]+"\",$this->"+local+"));\n}\n\n";
+ }
+
//create enum constants
+ QList<QPair<QString,int> >ens=tbl.getEnums();
+ for(int i=0;i<ens.size();i++){
+ code+="const "+ens[i].first+"="+QString::number(ens[i].second)+";\n";
+ }
+
//write table class
code+="};\n";
tf.write(code.toAscii());
//extend schema file
//column definitions
QStringList cols=tbl.columns();
- QStringList pcols=tbl.primaryColumns();
code="\t$this->scheme[\""+tbl.name()+"\"]=array(";
for(int i=0;i<cols.size();i++){
if(i)code+=",";
m_svnRev="unknown uncertain";
m_svnExe="svn";
m_dbInst="dbInst";
- m_dbScheme="dbScheme";
+ m_dbSchema="dbSchema";
inst=this;
}
if(tn=="DataBase"){
if(el.hasAttribute("instance"))
m_dbInst=el.attribute("instance");
- if(el.hasAttribute("scheme"))
- m_dbScheme=el.attribute("scheme");
+ if(el.hasAttribute("schema"))
+ m_dbSchema=el.attribute("schema");
if(el.hasAttribute("version"))
m_dbVer=el.attribute("version");
}else
qDebug("Warning: anonymous enum value in table %s column %s. Ignoring it.",m_name.toAscii().data(),cl.name.toAscii().data());
continue;
}
- nxval=el2.attribute("value",QString::number(nxval)).toInt();
+ nxval=el2.attribute("value",QString::number(nxval)).toInt(0,0);
cl.enumvals.append(QPair<QString,int>(n,nxval));
nxval++;
}
QString verHR()const{return m_verHR;}
QString svnRevision()const{return m_svnRev;}
QString dbInst()const{return m_dbInst;}
- QString dbScheme()const{return m_dbScheme;}
+ QString dbSchema()const{return m_dbSchema;}
QString dbVersion()const{return m_dbVer;}
bool haveTable(QString)const;
private:
QString m_baseDir,m_wobDir,m_verComm,m_verNeedComm,m_verHR;
- QString m_svnTarget,m_svnRev,m_svnExe,m_dbInst,m_dbScheme,m_dbVer;
+ QString m_svnTarget,m_svnRev,m_svnExe,m_dbInst,m_dbSchema,m_dbVer;
QList<WocTable> m_tables;
QList<WocClass> m_classes;
types that must be understood are: int32 (INTEGER), int64 (LONG INTEGER), seq32 (auto-incrementing int), seq64, bool (boolean), string:$length (text up to 255 chars, length is optional, default is 255; VARCHAR($length)), text (unlimited text)*/
protected function dataType($type)
{
- if($type=="int32")return "INTEGER";
- if($type=="int64")return "LONG INTEGER";
+ if($type=="int32"||$type=="enum"||$type=="enum32")return "INTEGER";
+ if($type=="int64"||$type=="enum64")return "LONG INTEGER";
if($type=="bool")return "BOOLEAN";
$tpa=explode(":",$type);
if($tpa[0]=="string"){
/**returns the correct name/coding of a flag:
primarykey, notnull, unique (implies notnull), foreignkey:$table:$col, defaultint:$val, defaultstr:$val, index*/
- protected function columnFlag($flag,$col)
+ protected function columnFlag($flag,$col,$table)
{
if($flag=="primarykey")return "PRIMARY KEY";
if($flag=="null")return "NULL";
return "DEFAULT NULL";
return "DEFAULT ".$this->escapeBool($tpa[1]);
}
+ if($tpa[0]=="default"){
+ if(count($tpa)<2)
+ return "DEFAULT NULL";
+ return "DEFAULT ".$this->escapeColumn($table,$col,$tpa[1]);
+ }
}
/**creates a SQL92 statement for creating a table; overwrite this to implement DB specific syntax*/
$ret.=$this->dataType($def[0])." ";
//get flags
for($i=0;$i<count($def);$i++)
- $ret.=$this->columnFlag($def[$i],$col)." ";
+ $ret.=$this->columnFlag($def[$i],$col,$tablename)." ";
}
}
$ret.=$this->createTableExtras($tablename,$table);
protected function dataType($type)
{
- if($type=="int32")return "INT";
- if($type=="int64")return "BIGINT";
+ if($type=="int32"||$type=="enum"||$type=="enum32")return "INT";
+ if($type=="int64"||$type=="enum64")return "BIGINT";
if($type=="seq32")return "INT AUTO_INCREMENT";
if($type=="seq64")return "BIGINT AUTO_INCREMENT";
if($type=="text")return "TEXT";
return parent::dataType($type);
}
- protected function columnFlag($flag,$col)
+ protected function columnFlag($flag,$col,$table)
{
//FIXME: currently MySQL does not mark columns for indexing, since the syntax is somewhat different --> this needs to be fixed!
if($flag=="index"){
return "";
}
//fallback to SQL standard
- return parent::columnFlag($flag,$col);
+ return parent::columnFlag($flag,$col,$table);
}
public function insert($table,array $values)
return false;
$tpa=explode(":",$this->scheme[$tab][$col][0]);
switch($tpa[0]){
- case "int32":case "seq32":case "int64":case "seq64":
+ case "int32":case "seq32":case "int64":case "seq64":case "enum":case "enum32":case "enum64":
return true;
default:
return false;