Commits

clach04  committed 3aa2036

Added date support, NOTE date/datetime support is still not compatible with CPython/pep-249 as result date/time type(s) are .NET types not Python types.

  • Participants
  • Parent commits 563f0af

Comments (0)

Files changed (2)

File dbapi/dbapi/generic.py

 # 2006-09-18 mark    Explicitly set .Transaction attribute
 # 2006-09-19 orichter Iterable cursor
 # 2007-10-11 carsten Added DBNull handling
+# 2009-12-09 clach04 Added date support (date only, not just datetime)
 
 import clr
 clr.AddReference('System.Data')
                     if parameter is None: parameter = DBNull.Value
                     p = command.CreateParameter()
                     p.ParameterName = parameter_name
-                    p.Value = _from_python(parameter)
+                    p.Value, tmptype = _from_python(parameter)
+                    if tmptype:
+                        p.DbType = tmptype
                     command.Parameters.Add(p)
             else:
                 for parameter in parameters:
                     if parameter is None: parameter = DBNull.Value
                     p = command.CreateParameter()
-                    p.Value = _from_python(parameter)
+                    p.Value, tmptype = _from_python(parameter)
+                    #if isinstance(parameter, datetime.date):
+                    if tmptype:
+                        #from System.Data import DbType
+                        #p.DbType = System.Data.DbType.Date
+                        p.DbType = tmptype
                     command.Parameters.Add(p)
         
         if self._is_query(operation):
 import datetime, time
 
 _from_python_mappings = {
-    datetime.datetime: (lambda d: System.DateTime.Parse(time.strftime('%Y-%m-%d %H:%M:%S', d.timetuple())))
+    datetime.datetime: ( (lambda d: System.DateTime.Parse(time.strftime('%Y-%m-%d %H:%M:%S', d.timetuple()))), None),
+    datetime.date: ( (lambda d: System.DateTime.Parse(time.strftime('%Y-%m-%d %H:%M:%S', d.timetuple()))), System.Data.DbType.Date),
 }
 
 def _add_from_python_mapping(t, f):
 
 def _from_python(pval):
     if type(pval) in _from_python_mappings:
-        return _from_python_mappings[type(pval)](pval)
+        tmpfunction, tmptype = _from_python_mappings[type(pval)]
+        return tmpfunction(pval), tmptype
     else:
-        return pval
+        return pval, None
 
 _to_python_mappings = {
     System.DateTime: datetime.datetime,

File dbapi/demo_ingres.py

 
 """
 
+import datetime
+
 import dbapi
 
 import ingres as db
 
+def simple_select(c, sql_query, bind_params=None):
+    """where c is a cursor"""
+    print sql_query
+    if bind_params is None:
+        c.execute(sql_query)
+    else:
+        print bind_params
+        c.execute(sql_query, bind_params)
+    if c.description is not None:
+        # We have a SELECT statement
+        print c.description
+        row = c.fetchone()
+        while row:
+            print row
+            row = c.fetchone()
+    print ''
+
 
 connection_function = db.connect
 
 conn = connection_function(connectstr)
 
 c = conn.cursor()
-c.execute('select * from iidbconstants')
-print c.description
-print c.fetchall()
 
-c.execute("select ingresdate('now') from iidbconstants")
-print c.description
-print c.fetchall()
+simple_select(c, 'select * from iidbconstants')
 
+simple_select(c, "select ingresdate('now') from iidbconstants")
+
+bind_params = (datetime.date(2001, 02, 03),)
+simple_select(c, 'select ? from iidbconstants', bind_params)
+
+bind_params = (datetime.datetime(2001, 02, 03, 04, 05, 06),)
+simple_select(c, 'select ? from iidbconstants', bind_params)
+