From c99dd60c347848e7103fcc88c30208933a280c40 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 3 Feb 2009 18:08:35 +0000 Subject: [PATCH] *make enum known to DB abstraction *implement some basic functions of table wrappers in woc git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@263 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- wob/magicsmoke.wolf | 2 +- woc/phpout.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++--- woc/processor.cpp | 8 +++--- woc/processor.h | 4 +- www/inc/db/db.php | 13 ++++++++--- www/inc/db/db_mysql.php | 8 +++--- www/inc/wbase/schema.php | 2 +- 7 files changed, 67 insertions(+), 20 deletions(-) diff --git a/wob/magicsmoke.wolf b/wob/magicsmoke.wolf index f53a27b..4641462 100644 --- a/wob/magicsmoke.wolf +++ b/wob/magicsmoke.wolf @@ -10,7 +10,7 @@ - + diff --git a/woc/phpout.cpp b/woc/phpout.cpp index 6ea8a2f..f48915f 100644 --- a/woc/phpout.cpp +++ b/woc/phpout.cpp @@ -72,6 +72,7 @@ void WocPHPServerOut::newClass(const WocClass&){} 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); @@ -82,12 +83,54 @@ void WocPHPServerOut::newTable(const WocTable&tbl) 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;idbInst(); + code+="){\n\tglobal "+dbi+";\n\t$res="+dbi+"->select(\""+tbl.name()+"\",\"*\",\""; + for(int i=0;iescapeColumn(\""+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;iescapeColumn(\""; + code+=foreign[0]+"\",\""+foreign[1]+"\",$this->"+local+"));\n}\n\n"; + } + //create enum constants + QList >ens=tbl.getEnums(); + for(int i=0;i(n,nxval)); nxval++; } diff --git a/woc/processor.h b/woc/processor.h index 74ebcb6..c522b22 100644 --- a/woc/processor.h +++ b/woc/processor.h @@ -104,7 +104,7 @@ class WocProcessor:public QObject 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; @@ -116,7 +116,7 @@ class WocProcessor:public QObject 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 m_tables; QList m_classes; diff --git a/www/inc/db/db.php b/www/inc/db/db.php index ca628ee..9db7399 100644 --- a/www/inc/db/db.php +++ b/www/inc/db/db.php @@ -82,8 +82,8 @@ abstract class DbEngine 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"){ @@ -97,7 +97,7 @@ abstract class DbEngine /**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"; @@ -125,6 +125,11 @@ abstract class DbEngine 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*/ @@ -146,7 +151,7 @@ abstract class DbEngine $ret.=$this->dataType($def[0])." "; //get flags for($i=0;$icolumnFlag($def[$i],$col)." "; + $ret.=$this->columnFlag($def[$i],$col,$tablename)." "; } } $ret.=$this->createTableExtras($tablename,$table); diff --git a/www/inc/db/db_mysql.php b/www/inc/db/db_mysql.php index 316fa05..fac5f50 100644 --- a/www/inc/db/db_mysql.php +++ b/www/inc/db/db_mysql.php @@ -154,8 +154,8 @@ class MysqlEngine extends DbEngine 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"; @@ -171,7 +171,7 @@ class MysqlEngine extends DbEngine 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"){ @@ -187,7 +187,7 @@ class MysqlEngine extends DbEngine return ""; } //fallback to SQL standard - return parent::columnFlag($flag,$col); + return parent::columnFlag($flag,$col,$table); } public function insert($table,array $values) diff --git a/www/inc/wbase/schema.php b/www/inc/wbase/schema.php index 3903b05..7e74ee3 100644 --- a/www/inc/wbase/schema.php +++ b/www/inc/wbase/schema.php @@ -83,7 +83,7 @@ class WobSchemaBase { 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; -- 1.7.2.5