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:
@@ -4,23 +4,19 @@
|
|||||||
<type label="Integer" length="1" sql="integer" re="INTEGER" quote=""/>
|
<type label="Integer" length="1" sql="integer" re="INTEGER" quote=""/>
|
||||||
<type label="Double precision" length="1" sql="double" re="DOUBLE" quote=""/>
|
<type label="Double precision" length="1" sql="double" re="DOUBLE" quote=""/>
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group label="Character" color="rgb(255,200,200)">
|
<group label="Character" color="rgb(255,200,200)">
|
||||||
<type label="String" length="1" sql="string" quote="'"/>
|
<type label="String" length="1" sql="string" quote="'"/>
|
||||||
<type label="Text" length="1" sql="text" quote="'"/>
|
<type label="Text" length="1" sql="text" quote="'"/>
|
||||||
<type label="BLOB" length="1" sql="blob" quote="'"/>
|
<type label="BLOB" length="1" sql="blob" quote="'"/>
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group label="Date & Time" color="rgb(200,255,200)">
|
<group label="Date & Time" color="rgb(200,255,200)">
|
||||||
<type label="Date" length="0" sql="date" quote="'"/>
|
|
||||||
<type label="Time" length="0" sql="time" 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="Datetime" length="0" sql="datetime" quote="'"/>
|
||||||
<type label="Timestamp" length="0" sql="timestamp" quote="'"/>
|
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group label="Miscellaneous" color="rgb(200,200,255)">
|
<group label="Miscellaneous" color="rgb(200,200,255)">
|
||||||
<type label="Boolean" length="0" sql="boolean" quote=""/>
|
<type label="Boolean" length="0" sql="boolean" quote=""/>
|
||||||
<type label="Upload" length="0" sql="upload" quote="'"/>
|
<type label="Upload" length="0" sql="upload" quote=""/>
|
||||||
<type label="Password" length="0" sql="password" quote="'"/>
|
<type label="Password" length="0" sql="password" quote=""/>
|
||||||
</group>
|
</group>
|
||||||
</datatypes>
|
</datatypes>
|
||||||
+58
-46
@@ -1,7 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="us-ascii"?>
|
<?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:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
|
||||||
<xsl:output method="text"/>
|
<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:template name="replace-substring">
|
||||||
<xsl:param name="value"/>
|
<xsl:param name="value"/>
|
||||||
<xsl:param name="from"/>
|
<xsl:param name="from"/>
|
||||||
@@ -31,36 +32,21 @@
|
|||||||
</xsl:when>
|
</xsl:when>
|
||||||
</xsl:choose>
|
</xsl:choose>
|
||||||
</xsl:template>
|
</xsl:template>
|
||||||
<xsl:template match="/sql">
|
<!-- return web2py python representation of table -->
|
||||||
<xsl:text>"""
|
<xsl:template name="tableparser">
|
||||||
just copy paste this code into your model and replace dbOBJECT to something
|
<xsl:param name="tabledata"/>
|
||||||
you prefer or what is used in your code, another thing that should be known is that
|
<xsl:text>dbOBJECT.define_table("</xsl:text>
|
||||||
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>
|
|
||||||
<xsl:value-of select="@name"/>
|
<xsl:value-of select="@name"/>
|
||||||
<xsl:text>",</xsl:text>
|
<xsl:text>",</xsl:text>
|
||||||
<xsl:for-each select="row">
|
<xsl:for-each select="row">
|
||||||
<xsl:choose>
|
<xsl:choose>
|
||||||
|
<!-- ignore id fields as web2py needs it for building relations and will create them automatically -->
|
||||||
<xsl:when test="not(@name = 'id')">
|
<xsl:when test="not(@name = 'id')">
|
||||||
<xsl:text>
|
<xsl:text>
 Field("</xsl:text>
|
||||||
SQLField("</xsl:text>
|
|
||||||
<xsl:choose>
|
<xsl:choose>
|
||||||
<xsl:when test="not (relation)">
|
<xsl:when test="not (relation)">
|
||||||
<xsl:value-of select="@name"/>
|
<xsl:value-of select="@name"/>
|
||||||
<xsl:text>", </xsl:text>
|
<xsl:text>", </xsl:text>
|
||||||
<!--<xsl:value-of select="datatype"/>-->
|
|
||||||
<xsl:choose>
|
<xsl:choose>
|
||||||
<xsl:when test="contains(datatype,'(')">
|
<xsl:when test="contains(datatype,'(')">
|
||||||
<xsl:text>"</xsl:text>
|
<xsl:text>"</xsl:text>
|
||||||
@@ -82,9 +68,15 @@ dbOBJECT.define_table("</xsl:text>
|
|||||||
</xsl:if>
|
</xsl:if>
|
||||||
<xsl:choose>
|
<xsl:choose>
|
||||||
<xsl:when test="default">
|
<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:value-of select="default"/>
|
||||||
<xsl:text>'</xsl:text>
|
</xsl:otherwise>
|
||||||
|
</xsl:choose>
|
||||||
</xsl:when>
|
</xsl:when>
|
||||||
<xsl:otherwise>
|
<xsl:otherwise>
|
||||||
<xsl:text>default=None</xsl:text>
|
<xsl:text>default=None</xsl:text>
|
||||||
@@ -118,45 +110,65 @@ dbOBJECT.define_table("</xsl:text>
|
|||||||
</xsl:choose>
|
</xsl:choose>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
<xsl:text>)</xsl:text>
|
<xsl:text>)</xsl:text>
|
||||||
<xsl:text>
|
<xsl:text>

</xsl:text>
|
||||||
|
</xsl:template>
|
||||||
</xsl:text>
|
<!-- parsing db xml file -->
|
||||||
|
<xsl:template match="/sql">
|
||||||
|
<xsl:text>""" database class object creation (initialization) """
</xsl:text>
|
||||||
|
<xsl:text>if request.env.web2py_runtime_gae: # if running on Google App Engine 
</xsl:text>
|
||||||
|
<xsl:text> dbOBJECT = DAL('gae') # connect to Google BigTable 
</xsl:text>
|
||||||
|
<xsl:text> session.connect(request, response, db=dbOBJECT) # and store sessions and tickets there 
</xsl:text>
|
||||||
|
<xsl:text>else: # else use a normal relational database 
</xsl:text>
|
||||||
|
<xsl:text> dbOBJECT = SQLDB("sqlite://dbOBJECT.db")

</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>
|
</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 -->
|
<!-- fk -->
|
||||||
<xsl:text>
|
<xsl:text>""" Relations between tables (remove fields you don't need from requires) """
</xsl:text>
|
||||||
"""
|
<!-- dbOBJECT.druga.prva_id.requires=IS_IN_DB(db, 'auth_user.id','%(id)s: %(first_name)s %(last_name)s') -->
|
||||||
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:for-each select="table">
|
<xsl:for-each select="table">
|
||||||
<xsl:for-each select="row">
|
<xsl:for-each select="row">
|
||||||
<xsl:for-each select="relation">
|
<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:text>dbOBJECT.</xsl:text>
|
||||||
<xsl:value-of select="../../@name"/>
|
<xsl:value-of select="../../@name"/>
|
||||||
<xsl:text>.</xsl:text>
|
<xsl:text>.</xsl:text>
|
||||||
<xsl:value-of select="../@name"/>
|
<xsl:value-of select="../@name"/>
|
||||||
<xsl:text>.requires=IS_IN_DB(</xsl:text>
|
<xsl:text>.requires=IS_IN_DB( dbOBJECT, '</xsl:text>
|
||||||
<xsl:text>dbOBJECT, '</xsl:text>
|
|
||||||
<xsl:value-of select="@table"/>
|
<xsl:value-of select="@table"/>
|
||||||
<xsl:text>.id</xsl:text><!-- hardcoded as this is expected from web2py every table to have anyway :) -->
|
<xsl:text>.id', '</xsl:text>
|
||||||
<xsl:for-each select="//table"><!-- have to do this way to find our table->row->names-->
|
<!-- 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:if test="@name = $tablename">
|
||||||
<xsl:for-each select="row">
|
<xsl:for-each select="row">
|
||||||
<xsl:if test="not(@name = 'id')">
|
<xsl:if test="not(@name = 'id') and not(datatype = 'password')">
|
||||||
<xsl:text>','</xsl:text>
|
<!-- do not show password and id fields by default in select box -->
|
||||||
<xsl:value-of select="$tablename"/>
|
<xsl:text> %(</xsl:text>
|
||||||
<xsl:text>.</xsl:text>
|
|
||||||
<xsl:value-of select="@name"/>
|
<xsl:value-of select="@name"/>
|
||||||
|
<xsl:text>)s</xsl:text>
|
||||||
</xsl:if>
|
</xsl:if>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
</xsl:if>
|
</xsl:if>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
<xsl:text>')
|
<xsl:text>')
</xsl:text>
|
||||||
</xsl:text>
|
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
</xsl:for-each>
|
</xsl:for-each>
|
||||||
|
|||||||
Reference in New Issue
Block a user