[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
r5947 - in trunk/gnue-common/src/datasources/drivers/sqlite/Schema: Crea
From: |
johannes |
Subject: |
r5947 - in trunk/gnue-common/src/datasources/drivers/sqlite/Schema: Creation Discovery |
Date: |
Wed, 7 Jul 2004 17:14:24 -0500 (CDT) |
Author: johannes
Date: 2004-07-07 03:26:13 -0500 (Wed, 07 Jul 2004)
New Revision: 5947
Modified:
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Discovery/Introspection.py
Log:
sqlite schema creation now support table modifications
Modified:
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
===================================================================
---
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
2004-07-06 14:53:09 UTC (rev 5946)
+++
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
2004-07-07 08:26:13 UTC (rev 5947)
@@ -21,10 +21,15 @@
# $Id: $
import os
+import re
import string
from gnue.common.datasources.drivers.DBSIG2.Schema.Creation import \
Creation as Base
+class MissingTableError (gException):
+ def __init__ (self, table):
+ msg = u_("Cannot find table '%s' anymore") % table
+ gException.__init__ (self, msg)
# =============================================================================
# Class implementing schema creation for SQLite
@@ -35,7 +40,9 @@
MAX_NAME_LENGTH = 31
ALTER_MULTIPLE = False
+ _CMD = re.compile ('(.*?)\((.*)\)(.*)')
+
# ---------------------------------------------------------------------------
# Create a new database
# ---------------------------------------------------------------------------
@@ -92,6 +99,66 @@
# ---------------------------------------------------------------------------
+ # Modify a table
+ # ---------------------------------------------------------------------------
+
+ def modifyTable (self, tableDefinition, codeOnly = False):
+ """
+ This function modifies a table according to the given definition.
+
+ @param tableDefinition: a dictionary of the table to be modified
+ @param codeOnly: if TRUE no operation takes place, but only the code will
+ be returned.
+ @return: a tuple of sequences (prologue, body, epliogue) containing the
+ code to perform the action.
+ """
+
+ res = ([], [], [])
+ body = res [1]
+
+ tSchema = self.introspector.find (name = tableDefinition ['name'])
+ if tSchema is None:
+ raise MissingTableError, tableDefinition ['name']
+
+ parts = self._CMD.match (tSchema [0].sql)
+ if not parts:
+ raise gException, u_("Cannot split SQL command: '%s'") % tSchema [0].sql
+
+ fields = [f.name for f in tSchema [0].fields ()]
+ nfields = ["NULL" for f in tableDefinition ['fields']]
+ nfields.extend (fields)
+
+ body.append (u"CREATE TEMPORARY TABLE t1_backup (%s)" \
+ % string.join (fields, ","))
+ body.append (u"INSERT INTO t1_backup SELECT %(fields)s FROM %(table)s" \
+ % {'fields': string.join (fields, ","),
+ 'table' : tableDefinition ['name']})
+ body.append (u"DROP TABLE %s" % tableDefinition ['name'])
+
+ fCode = self.createFields (tableDefinition ['name'],
+ tableDefinition ['fields'])
+ self.mergeTuple (res, (fCode [0], [], fCode [2]))
+
+ oldSQL = parts.groups ()
+ newBody = [string.join (fCode [1], ", ")]
+ if len (oldSQL [1]):
+ newBody.append (oldSQL [1])
+
+ cmd = u"%s (%s)%s" % (oldSQL [0], string.join (newBody, ", "), oldSQL [2])
+
+ body.append (cmd)
+ body.append (u"INSERT INTO %(table)s SELECT %(fields)s FROM t1_backup" \
+ % {'table' : tableDefinition ['name'],
+ 'fields': string.join (nfields, ",")})
+ body.append (u"DROP TABLE t1_backup")
+
+ if not codeOnly:
+ self._executeCodeTuple (res)
+
+ return res
+
+
+ # ---------------------------------------------------------------------------
# Handle special defaults
# ---------------------------------------------------------------------------
@@ -106,15 +173,12 @@
@param forAlter: TRUE if the definition is used in a table modification
"""
if fieldDef ['defaultwith'] == 'serial':
- seq = self._getSequenceName (tableName, fieldDef)
- code [0].append (u"CREATE SEQUENCE %s%s" % (seq, self.END_COMMAND))
- fieldDef ['default'] = "nextval ('%s')" % seq
+ print "WARNING: SQLite has no serials"
elif fieldDef ['defaultwith'] == 'timestamp':
- fieldDef ['default'] = "now()"
+ print "WARNING: SQLite has no default timestamp"
-
# ---------------------------------------------------------------------------
# A key is an integer
# ---------------------------------------------------------------------------
Modified:
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Discovery/Introspection.py
===================================================================
---
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Discovery/Introspection.py
2004-07-06 14:53:09 UTC (rev 5946)
+++
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Discovery/Introspection.py
2004-07-07 08:26:13 UTC (rev 5947)
@@ -145,12 +145,11 @@
if self._TEXTTYPE.match (typename.upper ()):
attrs ['datatype'] = 'text'
- elif typename.lower () in ['date', 'datetime']:
+ elif typename.lower () in ['date', 'datetime', 'time']:
attrs ['datatype'] = 'date'
else:
attrs ['datatype'] = 'number'
- print "ATTR:", attrs
result.append (GIntrospection.Schema (attrs))
finally:
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- r5947 - in trunk/gnue-common/src/datasources/drivers/sqlite/Schema: Creation Discovery,
johannes <=