*make enum known to DB abstraction
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 3 Feb 2009 18:08:35 +0000 (18:08 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 3 Feb 2009 18:08:35 +0000 (18:08 +0000)
*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
woc/phpout.cpp
woc/processor.cpp
woc/processor.h
www/inc/db/db.php
www/inc/db/db_mysql.php
www/inc/wbase/schema.php

index f53a27b..4641462 100644 (file)
@@ -10,7 +10,7 @@
        <!-- 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"/>
index 6ea8a2f..f48915f 100644 (file)
@@ -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;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());
@@ -97,7 +140,6 @@ void WocPHPServerOut::newTable(const WocTable&tbl)
        //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+=",";
index a25ee18..abde096 100644 (file)
@@ -46,7 +46,7 @@ WocProcessor::WocProcessor()
        m_svnRev="unknown uncertain";
        m_svnExe="svn";
        m_dbInst="dbInst";
-       m_dbScheme="dbScheme";
+       m_dbSchema="dbSchema";
        
        inst=this;
 }
@@ -110,8 +110,8 @@ bool WocProcessor::processFile(QString fn)
                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
@@ -326,7 +326,7 @@ WocTable::WocTable(const QDomElement&tbl)
                                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++;
                }
index 74ebcb6..c522b22 100644 (file)
@@ -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<WocTable> m_tables;
                QList<WocClass> m_classes;
index ca628ee..9db7399 100644 (file)
@@ -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;$i<count($def);$i++)
-                                       $ret.=$this->columnFlag($def[$i],$col)." ";
+                                       $ret.=$this->columnFlag($def[$i],$col,$tablename)." ";
                        }
                }
                $ret.=$this->createTableExtras($tablename,$table);
index 316fa05..fac5f50 100644 (file)
@@ -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)
index 3903b05..7e74ee3 100644 (file)
@@ -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;