[SQL-CVS] r1313 - in SQLObject/trunk/sqlobject: . mssql
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-11-21 18:29:27
|
Author: phd
Date: 2005-11-21 18:29:12 +0000 (Mon, 21 Nov 2005)
New Revision: 1313
Modified:
SQLObject/trunk/sqlobject/col.py
SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
Log:
A patch by Murat Ozsoyler <mur...@is...>: DecimalValidator.
Modified: SQLObject/trunk/sqlobject/col.py
===================================================================
--- SQLObject/trunk/sqlobject/col.py 2005-11-21 00:18:34 UTC (rev 1312)
+++ SQLObject/trunk/sqlobject/col.py 2005-11-21 18:29:12 UTC (rev 1313)
@@ -1116,6 +1116,47 @@
baseClass = SOTimeCol
+try:
+ from decimal import Decimal
+except ImportError:
+ Decimal = float
+
+class DecimalValidator(validators.Validator):
+ def to_python(self, value, state):
+ if value is None:
+ return None
+ if isinstance(value, (int, long, Decimal, sqlbuilder.SQLExpression)):
+ return value
+ if isinstance(value, float):
+ value = str(value)
+ connection = state.soObject._connection
+ if hasattr(connection, "decimalSeparator"):
+ value = value.replace(connection.decimalSeparator, ".")
+ try:
+ return Decimal(value)
+ except:
+ raise validators.Invalid("expected a Decimal in the DecimalCol '%s', got %s %r instead" % \
+ (self.name, type(value), value), value, state)
+
+ def from_python(self, value, state):
+ if value is None:
+ return None
+ if isinstance(value, float):
+ value = str(value)
+ if isinstance(value, (str, unicode)):
+ connection = state.soObject._connection
+ if hasattr(connection, "decimalSeparator"):
+ value = value.replace(connection.decimalSeparator, ".")
+ try:
+ return Decimal(value)
+ except:
+ raise validators.Invalid("can not parse Decimal value '%s' in the DecimalCol '%s'" %
+ (value, self.name), value, state)
+ if not isinstance(value, (int, long, Decimal, sqlbuilder.SQLExpression)):
+ raise validators.Invalid("expected a decimal in the DecimalCol '%s', got %s %r instead" % \
+ (self.name, type(value), value), value, state)
+ return value
+
class SODecimalCol(SOCol):
def __init__(self, **kw):
@@ -1130,6 +1171,10 @@
def _sqlType(self):
return 'DECIMAL(%i, %i)' % (self.size, self.precision)
+ def createValidators(self):
+ return [DecimalValidator()] + \
+ super(SODecimalCol, self).createValidators()
+
class DecimalCol(Col):
baseClass = SODecimalCol
Modified: SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-11-21 00:18:34 UTC (rev 1312)
+++ SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-11-21 18:29:12 UTC (rev 1313)
@@ -24,11 +24,12 @@
self.make_conn_str = lambda keys: \
["Provider=SQLOLEDB;Data Source=%s;Initial Catalog=%s;Integrated Security=SSPI;Persist Security Info=False" % (
keys.host, keys.db)]
- del kw["sspi"]
else:
self.make_conn_str = lambda keys: \
["Provider=SQLOLEDB;Data Source=%s;User Id=%s;Password=%s;Initial Catalog=%s" % (
keys.host, keys.user, keys.password, keys.db)]
+ if "sspi" in kw:
+ del kw["sspi"]
except ImportError: # raise the exceptions other than ImportError for adodbapi absence
import pymssql as sqlmodule
self.dbconnection = sqlmodule.connect
@@ -68,6 +69,8 @@
con = self.dbconnection( *self.make_conn_str(self) )
cur = con.cursor()
cur.execute('SET ANSI_NULLS ON')
+ cur.execute("SELECT CAST('12345.21' AS DECIMAL(10, 2))")
+ self.decimalSeparator = cur.fetchone()[0][-3]
cur.close()
return con
|