commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r7702 - in trunk/gnue-common: doc/technotes src/datasources/drive


From: johannes
Subject: [gnue] r7702 - in trunk/gnue-common: doc/technotes src/datasources/drivers/sql/sqlite
Date: Thu, 7 Jul 2005 09:19:05 -0500 (CDT)

Author: johannes
Date: 2005-07-07 09:19:03 -0500 (Thu, 07 Jul 2005)
New Revision: 7702

Added:
   trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlite2drv.py
Modified:
   trunk/gnue-common/doc/technotes/00016.txt
   trunk/gnue-common/src/datasources/drivers/sql/sqlite/Behavior.py
   trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlitedrv.py
Log:
Added driver for SQLite 3


Modified: trunk/gnue-common/doc/technotes/00016.txt
===================================================================
--- trunk/gnue-common/doc/technotes/00016.txt   2005-07-07 13:41:47 UTC (rev 
7701)
+++ trunk/gnue-common/doc/technotes/00016.txt   2005-07-07 14:19:03 UTC (rev 
7702)
@@ -70,8 +70,8 @@
     For ASCII it's 8000, for UNICODE it's 4000
 
 
-5. SQLite 2
------------
+5. SQLite 2/3
+-------------
 
 Type            Native type                             Fractional seconds
 ------------------------------------------------------------------------------
@@ -164,8 +164,8 @@
 boolean     bool
 
 
-sqlite:
--------
+sqlite2 (SQLite 2):
+-------------------
 
 Column      Datatype             F-Read    F-Write
 ------------------------------------------------------------------------------
@@ -173,3 +173,14 @@
 time        mx.DateTimeDelta     2 digits  2 digits truncated
 datetime    mx.DateTime          No        2 digits truncated
 boolean     int
+
+
+sqlite3 (SQLite 3):
+-------------------
+
+Column      Datatype             F-Read    F-Write
+------------------------------------------------------------------------------
+date        datetime.date                  
+time        datetime.time        6 digits  6 digits
+datetime    datetime.datetime    6 digits  6 digits
+boolean     int

Modified: trunk/gnue-common/src/datasources/drivers/sql/sqlite/Behavior.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/sqlite/Behavior.py    
2005-07-07 13:41:47 UTC (rev 7701)
+++ trunk/gnue-common/src/datasources/drivers/sql/sqlite/Behavior.py    
2005-07-07 14:19:03 UTC (rev 7702)
@@ -100,7 +100,8 @@
     self._RELTYPE = {'table': {'type': 'table', 'name': _("Tables")},
                      'view' : {'type': 'view',  'name': _("Views")}}
 
-    self._type2native.update ({'boolean': 'integer'})
+    self._type2native.update ({'boolean' : 'integer',
+                               'datetime': 'timestamp'})
 
 
   # ---------------------------------------------------------------------------
@@ -244,6 +245,9 @@
       if _TEXTTYPE.match (typename.upper ()):
         attrs ['type'] = 'string'
 
+      elif typename.lower () == 'timestamp':
+        attrs ['type'] = 'datetime'
+
       elif typename.lower () in ['date', 'datetime', 'time']:
         attrs ['type'] = typename.lower ()
 

Added: trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlite2drv.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlite2drv.py        
2005-07-07 13:41:47 UTC (rev 7701)
+++ trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlite2drv.py        
2005-07-07 14:19:03 UTC (rev 7702)
@@ -0,0 +1,191 @@
+# GNU Enterprise Common Library - SQLite3 database driver using pysqlite2
+#
+# Copyright 2000-2005 Free Software Foundation
+#
+# 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.
+#
+# $Id: $
+
+"""
+Database driver plugin for SQLite3 backends using the pysqlite2 DBSIG2 module.
+"""
+
+__all__         = ['Connection']
+__pluginalias__ = ['sqlite3']
+
+import datetime
+
+from gnue.common.datasources.drivers import DBSIG2
+from gnue.common.datasources.drivers.sql.sqlite import Behavior
+
+
+# =============================================================================
+# Test if plugin is functional
+# =============================================================================
+
+def __initplugin__ ():
+  from gnue.common.datasources import GConnections
+  try:
+    from pysqlite2 import dbapi2
+  
+  except ImportError:
+    raise GConnections.DependencyError, ('pysqlite2.dbapi2', None)
+
+
+# =============================================================================
+# Driver info
+# =============================================================================
+
+class DriverInfo:
+
+  name = "pysqlite2"
+
+  url = "http://initd.org";
+
+  doc = """
+Description
+-----------
+PySQLite is a Python extension for SQLite that conforms to the Python
+Database API Specification 2.0. The source is released under the
+Python license.
+
+Support
+-------
+Supported Platforms:
+
+  - Linux/BSD
+  - Solaris
+  - MS Windows 98/NT/2000/XP
+
+Connection Properties
+---------------------
+* dbname     -- This is the file name of the sqlite database (required)
+* timeout    -- When a database is accessed by multiple connections, and one of
+                the processes modifies the database, the SQLite database is
+                locked until that transaction is committed. The timeout
+                parameter specifies how long the connection should wait for the
+                lock to go away until raising an exception
+
+Examples
+--------
+[myconn]
+provider=sqlite2         # Use the SQLite adapter
+dbname=/usr/db/testdb    # The filename for the SQLite database
+
+Notes
+-----
+1. The database engine stores all data in string format. Many
+   SQL statements won't work.
+
+2. Other than that, this driver is fully functional without any serious
+   known problems.
+"""
+
+
+# =============================================================================
+# Connection class
+# =============================================================================
+
+class Connection (DBSIG2.Connection):
+  """
+  Connection class for SQLite backends using the pysqlite DBSIG2 module.
+  """
+
+  _drivername_ = 'pysqlite2.dbapi2'
+  _behavior_   = Behavior.Behavior
+
+  # SQLite doesn't like boolean type in SQL parameters
+  _boolean_true_  = 1
+  _boolean_false_ = 0
+  _std_datetime_  = True
+
+
+  # ---------------------------------------------------------------------------
+  # Return a sequence of required login fields
+  # ---------------------------------------------------------------------------
+
+  def _getLoginFields_ (self):
+    """
+    This function returns an empty sequence since SQLite doesn't use any user
+    authentication.
+    """
+    return []
+
+
+  # ---------------------------------------------------------------------------
+  # Get connection parameters
+  # ---------------------------------------------------------------------------
+
+  def _getConnectParams_ (self, connectData):
+
+    from pysqlite2 import dbapi2
+
+    # Register the missing converter and adpater for time values
+    dbapi2.register_adapter (datetime.time, adapt_time)
+    dbapi2.register_converter ('time', convert_time)
+    # NOTE: gnue-forms allways creates datetime values, even for dates. This is
+    # why we have to define our own converter. Please remove as soon as
+    # gnue-forms is fixed.
+    dbapi2.register_converter ('date', convert_date)
+
+    # mandatory parameters
+    kwargs = {'database'    : connectData ['dbname'],
+              'detect_types': dbapi2.PARSE_DECLTYPES}
+
+    if 'timeout' in connectData:
+      kwargs ['timeout'] = connectData ['timeout']
+
+    return ([], kwargs)
+
+
+# =============================================================================
+# The following functions should go into pysqlite2.dbapi2 !
+# =============================================================================
+
+# -----------------------------------------------------------------------------
+
+def convert_time (value):
+
+  # Be nice to datetime values passed in and take only the timepart
+  parts    = value.split (' ', 1)
+  timepart = len (parts) > 1 and parts [1] or parts [0]
+
+  timepart_full = timepart.split(".")
+  hours, minutes, seconds = map(int, timepart_full[0].split(":"))
+  if len(timepart_full) == 2:
+    microseconds = int(float("0." + timepart_full[1]) * 1000000)
+  else:
+    microseconds = 0
+
+  return datetime.time (hours, minutes, seconds, microseconds)
+
+# -----------------------------------------------------------------------------
+
+def convert_date (value):
+
+  datepart = value.split (' ', 1) [0]
+  return datetime.date (*map (int, datepart.split ('-')))
+
+# -----------------------------------------------------------------------------
+
+def adapt_time (value):
+
+  if isinstance (value, datetime.datetime):
+    value = value.time ()
+
+  return value.isoformat ()


Property changes on: 
trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlite2drv.py
___________________________________________________________________
Name: svn:keyword
   + Id

Modified: trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlitedrv.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlitedrv.py 
2005-07-07 13:41:47 UTC (rev 7701)
+++ trunk/gnue-common/src/datasources/drivers/sql/sqlite/pysqlitedrv.py 
2005-07-07 14:19:03 UTC (rev 7702)
@@ -25,7 +25,8 @@
 Database driver plugin for SQLite backends using the pysqlite DBSIG2 module.
 """
 
-__all__ = ['Connection']
+__all__         = ['Connection']
+__pluginalias__ = ['sqlite2']
 
 from gnue.common.datasources.drivers import DBSIG2
 from gnue.common.datasources.drivers.sql.sqlite import Behavior





reply via email to

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