[SQL-CVS] r1159 - in SQLObject/trunk/sqlobject: . mssql
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-10-28 11:31:58
|
Author: phd
Date: 2005-10-28 11:31:50 +0000 (Fri, 28 Oct 2005)
New Revision: 1159
Modified:
SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
SQLObject/trunk/sqlobject/sqlbuilder.py
Log:
Applied a patch by Murat Ozsoyler <mur...@is...>.
Modified: SQLObject/trunk/sqlobject/mssql/mssqlconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-10-27 22:53:56 UTC (rev 1158)
+++ SQLObject/trunk/sqlobject/mssql/mssqlconnection.py 2005-10-28 11:31:50 UTC (rev 1159)
@@ -16,14 +16,25 @@
if not sqlmodule:
try:
import adodbapi as sqlmodule
- self.dbconnection = sqlmodule.Connection
- 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)]
- except:
+ self.dbconnection = sqlmodule.connect
+ # ADO uses unicode only (AFAIK)
+ self.usingUnicodeStrings = True
+ # MSDE does not allow SQL server login
+ if "sspi" in kw and kw["sspi"]:
+ 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)]
+ except ImportError: # raise the exceptions other than ImportError for adodbapi absence
import pymssql as sqlmodule
self.dbconnection = sqlmodule.connect
sqlmodule.Binary = lambda st: str(st)
+ # don't know whether pymssql uses unicode
+ self.usingUnicodeStrings = False
self.make_conn_str = lambda keys: \
["", keys.user, keys.password, keys.host, keys.db]
self.autoCommit=autoCommit
@@ -155,6 +166,8 @@
(tableName,
column.dbName))
+ # precision and scale is gotten from column table so that we can create
+ # decimal columns if needed
SHOW_COLUMNS = """
select
name,
@@ -163,6 +176,8 @@
from systypes
where cast(xusertype as int)= cast(sc.xtype as int)
) datatype,
+ prec,
+ scale,
isnullable,
cdefault,
m.text default_text,
@@ -181,11 +196,12 @@
colData = self.queryAll(self.SHOW_COLUMNS
% tableName)
results = []
- for field, size, t, nullAllowed, default, defaultText, is_identity in colData:
+ for field, size, t, precision, scale, nullAllowed, default, defaultText, is_identity in colData:
# Seems strange to skip the pk column? What if it's not 'id'?
if field == 'id':
continue
- colClass, kw = self.guessClass(t, size)
+ # precision is needed for decimal columns
+ colClass, kw = self.guessClass(t, size, precision, scale)
kw['name'] = soClass.sqlmeta.style.dbColumnToPythonAttr(field)
kw['notNone'] = not nullAllowed
if (defaultText):
@@ -213,21 +229,30 @@
if auto == 0:
option = "OFF"
c = conn.cursor()
- c.execute("SET AUTOCOMMIT "+option)
+ c.execute("SET AUTOCOMMIT " + option)
conn.setconnectoption(SQL.AUTOCOMMIT, option)
- def guessClass(self, t, size):
+ # precision and scale is needed for decimal columns
+ def guessClass(self, t, size, precision, scale):
"""
Here we take raw values coming out of syscolumns and map to SQLObject class types.
"""
if t.startswith('int'):
return col.IntCol, {}
elif t.startswith('varchar'):
+ if self.usingUnicodeStrings:
+ return col.UnicodeCol, {'length': size}
return col.StringCol, {'length': size}
elif t.startswith('char'):
+ if self.usingUnicodeStrings:
+ return col.UnicodeCol, {'length': size,
+ 'varchar': False}
return col.StringCol, {'length': size,
'varchar': False}
elif t.startswith('datetime'):
return col.DateTimeCol, {}
+ elif t.startswith('decimal'):
+ return col.DecimalCol, {'size': precision, # be careful for awkward naming
+ 'precision': scale}
else:
return col.Col, {}
Modified: SQLObject/trunk/sqlobject/sqlbuilder.py
===================================================================
--- SQLObject/trunk/sqlobject/sqlbuilder.py 2005-10-27 22:53:56 UTC (rev 1158)
+++ SQLObject/trunk/sqlobject/sqlbuilder.py 2005-10-28 11:31:50 UTC (rev 1159)
@@ -72,11 +72,14 @@
import re, fnmatch
import operator
import threading
+import types
+
from converters import sqlrepr, registerConverter, TRUE, FALSE
safeSQLRE = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_\.]*$')
def sqlIdentifier(obj):
- return type(obj) is type("") and not not safeSQLRE.search(obj.strip())
+ # some db drivers return unicode column names
+ return isinstance(obj, types.StringTypes) and bool(safeSQLRE.search(obj.strip()))
def execute(expr, executor):
|