updated web2py db definition, thanks to Boris Manojlovic

--HG--
extra : convert_revision : svn%3Ab267cdba-c1da-11dd-874b-8bacd04a0a74/trunk%4052
This commit is contained in:
ondrej.zara
2009-09-15 11:20:00 +00:00
parent 8cb1958fdc
commit 9ccff587b7
2 changed files with 181 additions and 173 deletions
+3 -7
View File
@@ -4,23 +4,19 @@
<type label="Integer" length="1" sql="integer" re="INTEGER" quote=""/>
<type label="Double precision" length="1" sql="double" re="DOUBLE" quote=""/>
</group>
<group label="Character" color="rgb(255,200,200)">
<type label="String" length="1" sql="string" quote="'"/>
<type label="Text" length="1" sql="text" quote="'"/>
<type label="BLOB" length="1" sql="blob" quote="'"/>
</group>
<group label="Date &amp; Time" color="rgb(200,255,200)">
<type label="Date" length="0" sql="date" quote="'"/>
<type label="Time" length="0" sql="time" quote="'"/>
<type label="Date" length="0" sql="date" quote="'"/>
<type label="Datetime" length="0" sql="datetime" quote="'"/>
<type label="Timestamp" length="0" sql="timestamp" quote="'"/>
</group>
<group label="Miscellaneous" color="rgb(200,200,255)">
<type label="Boolean" length="0" sql="boolean" quote=""/>
<type label="Upload" length="0" sql="upload" quote="'"/>
<type label="Password" length="0" sql="password" quote="'"/>
<type label="Upload" length="0" sql="upload" quote=""/>
<type label="Password" length="0" sql="password" quote=""/>
</group>
</datatypes>
+58 -46
View File
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="us-ascii"?>
<!-- License public domain -->
<!-- Created by Boris Manojlovic <boris DOT manojlovic AT steki DOT net> -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<!-- this code is not used in template but anyway it can be used someday so i left it to be ;)-->
<xsl:template name="replace-substring">
<xsl:param name="value"/>
<xsl:param name="from"/>
@@ -31,36 +32,21 @@
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/sql">
<xsl:text>"""
just copy paste this code into your model and replace dbOBJECT to something
you prefer or what is used in your code, another thing that should be known is that
this code still cannot distinguish what exactly should be in reference title - name
when you use generic appadmin so please remove all fiealds you don't need not require
"""
"""
database class object creation
"""
dbOBJECT = SQLDB("sqlite://dbOBJECT.db")
</xsl:text>
<xsl:for-each select="table">
<xsl:text>
"""
Table definition
"""
dbOBJECT.define_table("</xsl:text>
<!-- return web2py python representation of table -->
<xsl:template name="tableparser">
<xsl:param name="tabledata"/>
<xsl:text>dbOBJECT.define_table("</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>",</xsl:text>
<xsl:for-each select="row">
<xsl:choose>
<!-- ignore id fields as web2py needs it for building relations and will create them automatically -->
<xsl:when test="not(@name = 'id')">
<xsl:text>
SQLField("</xsl:text>
<xsl:text>&#xa; Field("</xsl:text>
<xsl:choose>
<xsl:when test="not (relation)">
<xsl:value-of select="@name"/>
<xsl:text>", </xsl:text>
<!--<xsl:value-of select="datatype"/>-->
<xsl:choose>
<xsl:when test="contains(datatype,'(')">
<xsl:text>"</xsl:text>
@@ -82,9 +68,15 @@ dbOBJECT.define_table("</xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="default">
<xsl:text>default='</xsl:text>
<xsl:text>default=</xsl:text>
<xsl:choose>
<xsl:when test="default = 'NULL'">
<xsl:text>None</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="default"/>
<xsl:text>'</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text>default=None</xsl:text>
@@ -118,45 +110,65 @@ dbOBJECT.define_table("</xsl:text>
</xsl:choose>
</xsl:for-each>
<xsl:text>)</xsl:text>
<xsl:text>
</xsl:text>
<xsl:text>&#xa;&#xa;</xsl:text>
</xsl:template>
<!-- parsing db xml file -->
<xsl:template match="/sql">
<xsl:text>""" database class object creation (initialization) """&#xa;</xsl:text>
<xsl:text>if request.env.web2py_runtime_gae: # if running on Google App Engine &#xa;</xsl:text>
<xsl:text> dbOBJECT = DAL('gae') # connect to Google BigTable &#xa;</xsl:text>
<xsl:text> session.connect(request, response, db=dbOBJECT) # and store sessions and tickets there &#xa;</xsl:text>
<xsl:text>else: # else use a normal relational database &#xa;</xsl:text>
<xsl:text> dbOBJECT = SQLDB("sqlite://dbOBJECT.db")&#xa;&#xa;</xsl:text>
<!-- doing two pass: first ignore tables with relations as they will raise exception if table referenced still does not exist (not instantiated) -->
<!-- this is not bullet proff but should be sufficient for small projects will be in TODO :) -->
<xsl:for-each select="table">
<xsl:if test="not (row/relation)">
<xsl:call-template name="tableparser">
<xsl:with-param name="tabledata" select="table"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<!-- calling second pass ignore non relation tables -->
<xsl:for-each select="table">
<xsl:if test="row/relation">
<xsl:call-template name="tableparser">
<xsl:with-param name="tabledata" select="table"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<!-- end of for-each select="table" -->
<!-- fk -->
<xsl:text>
"""
Relations between tables (remove fields you don't need from requires)
"""
</xsl:text>
<!--
dbOBJECT.druga.prva_id.requires=IS_IN_DB(dbOBJECT, 'prva.id')
-->
<xsl:text>""" Relations between tables (remove fields you don't need from requires) """&#xa;</xsl:text>
<!-- dbOBJECT.druga.prva_id.requires=IS_IN_DB(db, 'auth_user.id','%(id)s: %(first_name)s %(last_name)s') -->
<xsl:for-each select="table">
<xsl:for-each select="row">
<xsl:for-each select="relation">
<xsl:variable name="tablename"><xsl:value-of select="@table"/></xsl:variable>
<xsl:variable name="tablename">
<xsl:value-of select="@table"/>
</xsl:variable>
<xsl:text>dbOBJECT.</xsl:text>
<xsl:value-of select="../../@name"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="../@name"/>
<xsl:text>.requires=IS_IN_DB(</xsl:text>
<xsl:text>dbOBJECT, '</xsl:text>
<xsl:text>.requires=IS_IN_DB( dbOBJECT, '</xsl:text>
<xsl:value-of select="@table"/>
<xsl:text>.id</xsl:text><!-- hardcoded as this is expected from web2py every table to have anyway :) -->
<xsl:for-each select="//table"><!-- have to do this way to find our table->row->names-->
<xsl:text>.id', '</xsl:text>
<!-- hardcoded as this is expected from web2py every table to have anyway :) -->
<xsl:for-each select="//table">
<!-- have to do this way to find our table->row->names-->
<xsl:if test="@name = $tablename">
<xsl:for-each select="row">
<xsl:if test="not(@name = 'id')">
<xsl:text>','</xsl:text>
<xsl:value-of select="$tablename"/>
<xsl:text>.</xsl:text>
<xsl:if test="not(@name = 'id') and not(datatype = 'password')">
<!-- do not show password and id fields by default in select box -->
<xsl:text> %(</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>)s</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
<xsl:text>')
</xsl:text>
<xsl:text>')&#xa;</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>