commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5934 - in trunk/gnue-common/src/datasources/drivers/sqlite: Schema/Crea


From: johannes
Subject: r5934 - in trunk/gnue-common/src/datasources/drivers/sqlite: Schema/Creation sqlite
Date: Wed, 7 Jul 2004 17:14:14 -0500 (CDT)

Author: johannes
Date: 2004-07-02 11:08:12 -0500 (Fri, 02 Jul 2004)
New Revision: 5934

Added:
   trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
Modified:
   trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py
Log:
Added workaround for missing Date/Time-stuff in PySQLite and first version of 
Schema-Creation (no tablemods at the moment)


Added: 
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
===================================================================
--- 
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py    
    2004-07-02 14:40:44 UTC (rev 5933)
+++ 
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py    
    2004-07-02 16:08:12 UTC (rev 5934)
@@ -0,0 +1,172 @@
+#
+# This file is part of GNU Enterprise.
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Copyright 2001-2004 Free Software Foundation
+#
+# $Id: $
+
+import os
+import string
+from gnue.common.datasources.drivers.DBSIG2.Schema.Creation import \
+    Creation as Base
+
+
+# =============================================================================
+# Class implementing schema creation for SQLite
+# =============================================================================
+
+class Creation (Base.Creation):
+
+  MAX_NAME_LENGTH = 31
+  ALTER_MULTIPLE  = False
+
+
+  # ---------------------------------------------------------------------------
+  # Create a new database
+  # ---------------------------------------------------------------------------
+
+  def createDatabase (self):
+    """
+    """
+    dbname   = self.connection.parameters.get ('dbname', None)
+    self.connection.manager.loginToConnection (self.connection)
+
+
+  # ---------------------------------------------------------------------------
+  # Create a primary key
+  # ---------------------------------------------------------------------------
+
+  def createPrimaryKey (self, tableName, keyDefinition, codeOnly = False):
+    """
+    This function creates a primary key for the given table using the primary
+    key definition.
+
+    @param tableName: name of the table for which a key should be created
+    @param keyDefinition: a dictionary of the primary key to be created 
+    @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 = ([], [], [])
+    fields  = string.join (keyDefinition ['fields'], ", ")
+    keyName = self._shortenName (keyDefinition ['name'])
+    code = u"PRIMARY KEY (%s)" % fields
+    res [1].append (code)
+
+    return res
+
+
+  # ---------------------------------------------------------------------------
+  # Create a constraint
+  # ---------------------------------------------------------------------------
+
+  def createConstraint (self, tableName, constraintDef, codeOnly = False):
+    """
+    This function returns an empty code-tuple, since SQLite does not support
+    foreign key constraints.
+
+    @param tableName: name of the table for which an index should be created
+    @param constraintDef: a dictionary of the constraint to be created 
+    @param codeOnly: if TRUE no operation takes place, but only the code will
+        be returned.
+    @return: an tuple of empty sequences.
+    """
+    print "WARNING: SQLite doesn't support FOREIGN KEY constraints"
+    return ([], [], [])
+
+
+  # ---------------------------------------------------------------------------
+  # Handle special defaults
+  # ---------------------------------------------------------------------------
+
+  def _defaultwith (self, code, tableName, fieldDef, forAlter):
+    """
+    This function creates a sequence for 'serials' and sets the default for
+    'timestamps' to 'now ()'
+
+    @param code: code-tuple to merge the result in
+    @param tableName: name of the table
+    @param fieldDef: dictionary describing the field with the default
+    @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
+
+    elif fieldDef ['defaultwith'] == 'timestamp':
+      fieldDef ['default'] = "now()"
+
+
+
+  # ---------------------------------------------------------------------------
+  # A key is an integer
+  # ---------------------------------------------------------------------------
+
+  def key (self, fieldDefinition):
+    """
+    Native datatype for a 'key'-field is 'integer'
+
+    @param fieldDefinition: dictionary describing the field
+    @return: string with the native datatype 'integer'
+    """
+    return "integer"
+
+
+  # ---------------------------------------------------------------------------
+  # Create an apropriate type for a number
+  # ---------------------------------------------------------------------------
+
+  def number (self, fieldDefinition):
+    """
+    This function returns an apropriate type for a number according to the
+    given length and precision.
+
+    @param fieldDefinition: dictionary describing the field
+    @return: string with the native datatype
+    """
+    scale  = 0
+    length = 0
+
+    if fieldDefinition.has_key ('precision'):
+      scale = fieldDefinition ['precision']
+    if fieldDefinition.has_key ('length'):
+      length = fieldDefinition ['length']
+
+    if scale == 0:
+      return "integer"
+    else:
+      return "numeric (%s,%s)" % (length, scale)
+
+
+  # ---------------------------------------------------------------------------
+  # Native datatype for boolean is boolean
+  # ---------------------------------------------------------------------------
+
+  def boolean (self, fieldDefinition):
+    """
+    This funciton returns the native data type for a boolean, which is
+    'boolean'
+
+    @param fieldDefinition: dictionary describing the field
+    @return: 'boolean'
+    """
+    return "boolean"
+
+


Property changes on: 
trunk/gnue-common/src/datasources/drivers/sqlite/Schema/Creation/Creation.py
___________________________________________________________________
Name: svn:keywords
   + +Id

Modified: trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py       
2004-07-02 14:40:44 UTC (rev 5933)
+++ trunk/gnue-common/src/datasources/drivers/sqlite/sqlite/Connection.py       
2004-07-02 16:08:12 UTC (rev 5934)
@@ -48,11 +48,27 @@
 
 try:
   import sqlite as SIG2api
+  
+  # This is a workaround for a bug in PySQLite. All the following mebers are
+  # not imported from main.py in __init__.py 
+  if not hasattr (SIG2api, 'Timestamp') and SIG2api.main.have_datetime:
+    SIG2api.Date               = SIG2api.main.Date
+    SIG2api.Time               = SIG2api.main.Time
+    SIG2api.Timestamp          = SIG2api.main.Timestamp
+    SIG2api.DateFromTicks      = SIG2api.main.DateFromTicks
+    SIG2api.TimeFromTicks      = SIG2api.main.TimeFromTicks
+    SIG2api.TimestampFromTicks = SIG2api.main.TimestampFromTicks
 
+    SIG2api.DateTimeType       = SIG2api.main.DateTimeType
+    SIG2api.DateTimeDeltaType  = SIG2api.main.DateTimeDeltaType
+
 except ImportError:
   raise GConnections.DependencyError, ('SQLitedbapi', None)
 
-from gnue.common.datasources.drivers.sqlite.Schema.Discovery.Introspection 
import Introspection
+from gnue.common.datasources.drivers.sqlite.Schema.Discovery.Introspection \
+    import Introspection
+from gnue.common.datasources.drivers.sqlite.Schema.Creation.Creation \
+    import Creation
 
 
 ######################################################################
@@ -64,6 +80,7 @@
   _driver         = SIG2api
   _DatabaseError  = SIG2api.DatabaseError
   defaultBehavior = Introspection
+  defaultCreator  = Creation
   supportedDataObjects = {
     'object': DataObject_Object,
     'sql':    DataObject_SQL





reply via email to

[Prev in Thread] Current Thread [Next in Thread]