Author: phd
Date: 2005-06-14 11:13:08 +0000 (Tue, 14 Jun 2005)
New Revision: 815
Modified:
trunk/SQLObject/sqlobject/col.py
trunk/SQLObject/sqlobject/converters.py
trunk/SQLObject/sqlobject/dbconnection.py
trunk/SQLObject/sqlobject/sqlite/sqliteconnection.py
trunk/SQLObject/sqlobject/tests/test_indexes.py
Log:
Added a patch for PySQLite2 made by Kevin Dangoor <da...@gm...>
and a lot of tweaks for it.
Modified: trunk/SQLObject/sqlobject/col.py
===================================================================
--- trunk/SQLObject/sqlobject/col.py 2005-06-10 13:23:37 UTC (rev 814)
+++ trunk/SQLObject/sqlobject/col.py 2005-06-14 11:13:08 UTC (rev 815)
@@ -373,6 +373,24 @@
def withClass(self, soClass):
return self.baseClass(soClass=soClass, name=self._name, **self.kw)
+class StringValidator(validators.Validator):
+
+ def toPython(self, value, state):
+ if value is None:
+ return None
+ if isinstance(value, unicode):
+ return value.encode("ascii")
+ return value
+
+ def fromPython(self, value, state):
+ if value is None:
+ return None
+ if isinstance(value, str):
+ return value
+ if isinstance(value, unicode):
+ return value.encode("ascii")
+ return value
+
class SOStringCol(SOCol):
# 3-03 @@: What about BLOB?
@@ -390,6 +408,10 @@
SOCol.__init__(self, **kw)
+ def createValidators(self):
+ return [StringValidator()] + \
+ super(SOStringCol, self).createValidators()
+
def autoConstraints(self):
constraints = [consts.isString]
if self.length is not None:
@@ -851,6 +873,17 @@
% DATETIME_IMPLEMENTATION)
now = staticmethod(now)
+
+if datetime_available:
+ class DateValidator(DateTimeValidator):
+ def toPython(self, value, state):
+ value = super(DateValidator, self).toPython(value, state)
+ if isinstance(value, datetime.datetime):
+ value = datetime.date(value.year, value.month, value.day)
+ return value
+
+ fromPython = toPython
+
class SODateCol(SOCol):
dateFormat = '%Y-%m-%d'
@@ -863,7 +896,7 @@
"""Create a validator for the column. Can be overriden in descendants."""
_validators = super(SODateCol, self).createValidators()
if default_datetime_implementation == DATETIME_IMPLEMENTATION:
- validatorClass = DateTimeValidator
+ validatorClass = DateValidator
elif default_datetime_implementation == MXDATETIME_IMPLEMENTATION:
validatorClass = MXDateTimeValidator
if default_datetime_implementation:
@@ -937,7 +970,9 @@
value = module.decode(value)
return value
if isinstance(value, state.soObject._connection._binaryType):
- return self._origValue
+ if hasattr(self, "_binaryValue") and (value == self._binaryValue):
+ return self._origValue
+ return str(value)
raise validators.InvalidField("expected a string in the BLOBCol '%s', got %s instead" % \
(self.name, type(value)), value, state)
@@ -945,7 +980,8 @@
if value is None:
return None
self._origValue = value # store the original value
- return state.soObject._connection.createBinary(value)
+ self._binaryValue = state.soObject._connection.createBinary(value)
+ return self._binaryValue
class SOBLOBCol(SOStringCol):
def createValidators(self):
@@ -983,6 +1019,8 @@
def toPython(self, value, state):
if value is None:
return None
+ if isinstance(value, unicode):
+ value = value.encode("ascii")
if isinstance(value, str):
return pickle.loads(value)
raise validators.InvalidField("expected a pickle string in the PickleCol '%s', got %s instead" % \
Modified: trunk/SQLObject/sqlobject/converters.py
===================================================================
--- trunk/SQLObject/sqlobject/converters.py 2005-06-10 13:23:37 UTC (rev 814)
+++ trunk/SQLObject/sqlobject/converters.py 2005-06-14 11:13:08 UTC (rev 815)
@@ -1,5 +1,4 @@
import array
-array_type = type(array.array('c', '')) # In Python 2.2 array.array is a function
try:
import mx.DateTime.ISO
@@ -91,12 +90,17 @@
registerConverter = converters.registerConverter
lookupConverter = converters.lookupConverter
+array_type = type(array.array('c', '')) # In Python 2.2 array.array and buffer
+buffer_type = type(buffer('')) # are functions, not classes
+
def StringLikeConverter(value, db):
if isinstance(value, array_type):
try:
value = value.tounicode()
except ValueError:
value = value.tostring()
+ elif isinstance(value, buffer_type):
+ value = str(value)
if db in ('mysql', 'postgres'):
for orig, repl in sqlStringReplace:
@@ -110,6 +114,7 @@
registerConverter(type(""), StringLikeConverter)
registerConverter(type(u""), StringLikeConverter)
registerConverter(array_type, StringLikeConverter)
+registerConverter(buffer_type, StringLikeConverter)
def IntConverter(value, db):
return repr(int(value))
Modified: trunk/SQLObject/sqlobject/dbconnection.py
===================================================================
--- trunk/SQLObject/sqlobject/dbconnection.py 2005-06-10 13:23:37 UTC (rev 814)
+++ trunk/SQLObject/sqlobject/dbconnection.py 2005-06-14 11:13:08 UTC (rev 815)
@@ -510,7 +510,7 @@
self.query(self.createTableSQL(soClass))
def createTableSQL(self, soClass):
- return ('CREATE TABLE %s (\n%s\n)' %
+ return ('CREATE TABLE %s (\n%s\n)' %
(soClass.sqlmeta.table, self.createColumns(soClass)))
def createColumns(self, soClass):
@@ -822,7 +822,7 @@
_connection = hub
hub.threadConnection = connectionFromURI('...')
-
+
"""
def __init__(self):
Modified: trunk/SQLObject/sqlobject/sqlite/sqliteconnection.py
===================================================================
--- trunk/SQLObject/sqlobject/sqlite/sqliteconnection.py 2005-06-10 13:23:37 UTC (rev 814)
+++ trunk/SQLObject/sqlobject/sqlite/sqliteconnection.py 2005-06-14 11:13:08 UTC (rev 815)
@@ -1,6 +1,7 @@
from sqlobject.dbconnection import DBAPI
from sqlobject.col import popKey
sqlite = None
+using_sqlite2 = False
class SQLiteConnection(DBAPI):
@@ -10,19 +11,33 @@
def __init__(self, filename, autoCommit=1, **kw):
global sqlite
+ global using_sqlite2
if sqlite is None:
- import sqlite
+ try:
+ from pysqlite2 import dbapi2 as sqlite
+ using_sqlite2 = True
+ except ImportError:
+ import sqlite
+ using_sqlite2 = False
self.module = sqlite
self.filename = filename # full path to sqlite-db-file
if not autoCommit and not kw.has_key('pool'):
# Pooling doesn't work with transactions...
kw['pool'] = 0
# connection options
- opts = {'autocommit': autoCommit}
- if 'encoding' in kw:
- opts['encoding'] = popKey(kw, 'encoding')
- if 'mode' in kw:
- opts['mode'] = int(popKey(kw, 'mode'), 0)
+ opts = {}
+ if using_sqlite2:
+ if autoCommit:
+ opts["isolation_level"] = None
+ if 'encoding' in kw:
+ import warnings
+ warnings.warn(DeprecationWarning("pysqlite2 does not support the encoding option"))
+ else:
+ opts['autocommit'] = autoCommit
+ if 'encoding' in kw:
+ opts['encoding'] = popKey(kw, 'encoding')
+ if 'mode' in kw:
+ opts['mode'] = int(popKey(kw, 'mode'), 0)
if 'timeout' in kw:
opts['timeout'] = float(popKey(kw, 'timeout'))
# use only one connection for sqlite - supports multiple)
@@ -46,8 +61,19 @@
return 'sqlite:///%s' % self.filename
def _setAutoCommit(self, conn, auto):
- conn.autocommit = auto
+ if using_sqlite2:
+ if auto:
+ conn.isolation_level = None
+ else:
+ conn.isolation_level = ""
+ else:
+ conn.autocommit = auto
+ def _setIsolationLevel(self, conn, level):
+ if not using_sqlite2:
+ return
+ conn.isolation_level = level
+
def makeConnection(self):
return self._conn
Modified: trunk/SQLObject/sqlobject/tests/test_indexes.py
===================================================================
--- trunk/SQLObject/sqlobject/tests/test_indexes.py 2005-06-10 13:23:37 UTC (rev 814)
+++ trunk/SQLObject/sqlobject/tests/test_indexes.py 2005-06-14 11:13:08 UTC (rev 815)
@@ -29,12 +29,12 @@
mod = SOIndex1._connection.module
try:
SOIndex1(name='blah', number=0)
- except (mod.ProgrammingError, mod.IntegrityError):
+ except (mod.ProgrammingError, mod.IntegrityError, mod.OperationalError):
# expected
pass
else:
assert 0, "Exception expected."
-
+
def test_2():
if not supports('expressionIndex'):
return
|