[pypy-svn] r69613 - pypy/trunk/pypy/module/oracle

afa at codespeak.net afa at codespeak.net
Tue Nov 24 19:34:23 CET 2009


Author: afa
Date: Tue Nov 24 19:34:23 2009
New Revision: 69613

Modified:
   pypy/trunk/pypy/module/oracle/interp_variable.py
   pypy/trunk/pypy/module/oracle/roci.py
Log:
A bit of refactoring


Modified: pypy/trunk/pypy/module/oracle/interp_variable.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/interp_variable.py	(original)
+++ pypy/trunk/pypy/module/oracle/interp_variable.py	Tue Nov 24 19:34:23 2009
@@ -798,44 +798,45 @@
             pos)
         return transform.OracleDateToPythonDate(self.environment, dataptr)
 
-class VT_Timestamp(W_Variable):
-    oracleType = roci.SQLT_TIMESTAMP
-    size = rffi.sizeof(roci.OCIDateTime_p)
+class W_VariableWithDescriptor(W_Variable):
+    size = rffi.sizeof(roci.dvoidp)
 
     def initialize(self, space, cursor):
-        # initialize the Timestamp descriptors
+        # initialize the descriptors
         for i in range(self.allocatedElements):
             dataptr = rffi.ptradd(
-                rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
+                rffi.cast(roci.Ptr(roci.dvoidp), self.data),
                 i)
             status = roci.OCIDescriptorAlloc(
                 self.environment.handle,
                 dataptr,
-                roci.OCI_DTYPE_TIMESTAMP,
+                self.descriptorType,
                 0, None)
             self.environment.checkForError(
-                status, "TimestampVar_Initialize()")
+                status, self.descriptionText + "_Initialize()")
 
     def finalize(self):
-        dataptr = rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data)
+        dataptr = rffi.cast(roci.Ptr(roci.dvoidp), self.data)
         for i in range(self.allocatedElements):
             if dataptr[i]:
                 roci.OCIDescriptorFree(
-                    dataptr[i], roci.OCI_DTYPE_TIMESTAMP)
+                    dataptr[i], self.descriptorType)
 
-    def getValueProc(self, space, pos):
-        dataptr = rffi.ptradd(
-            rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
+    def getDataptr(self, pos):
+        return rffi.ptradd(
+            rffi.cast(roci.Ptr(roci.dvoidp), self.data),
             pos)
 
+class VT_Timestamp(W_VariableWithDescriptor):
+    oracleType = roci.SQLT_TIMESTAMP
+    descriptorType = roci.OCI_DTYPE_TIMESTAMP
+    descriptionText = "TimestampVar"
+
+    def getValueProc(self, space, pos):
         return transform.OracleTimestampToPythonDate(
-            self.environment, dataptr)
+            self.environment, self.getDataptr(pos))
 
     def setValueProc(self, space, pos, w_value):
-        dataptr = rffi.ptradd(
-            rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
-            pos)
-
         # make sure a timestamp is being bound
         if not space.is_true(space.isinstance(w_value, get(space).w_DateTimeType)):
             raise OperationError(
@@ -853,7 +854,7 @@
         status = roci.OCIDateTimeConstruct(
             self.environment.handle,
             self.environment.errorHandle,
-            dataptr[0],
+            self.getDataptr(pos)[0],
             year, month, day, hour, minute, second, microsecond * 1000,
             None, 0)
 
@@ -865,7 +866,7 @@
             status = roci.OCIDateTimeCheck(
                 self.environment.handle,
                 self.environment.errorHandle,
-                dataptr[0],
+                self.getDataptr(pos)[0],
                 validptr);
             self.environment.checkForError(
                 status,
@@ -879,44 +880,16 @@
                 get(space).w_DataError,
                 space.wrap("invalid date"))
 
-class VT_Interval(W_Variable):
+class VT_Interval(W_VariableWithDescriptor):
     oracleType = roci.SQLT_INTERVAL_DS
-    size = rffi.sizeof(roci.OCIInterval_p)
-
-    def initialize(self, space, cursor):
-        # initialize the Timestamp descriptors
-        for i in range(self.allocatedElements):
-            dataptr = rffi.ptradd(
-                rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
-                i)
-            status = roci.OCIDescriptorAlloc(
-                self.environment.handle,
-                dataptr,
-                roci.OCI_DTYPE_INTERVAL_DS,
-                0, None)
-            self.environment.checkForError(
-                status, "TimestampVar_Initialize()")
-
-    def finalize(self):
-        dataptr = rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data)
-        for i in range(self.allocatedElements):
-            if dataptr[i]:
-                roci.OCIDescriptorFree(
-                    dataptr[i], roci.OCI_DTYPE_INTERVAL_DS)
+    descriptorType = roci.OCI_DTYPE_INTERVAL_DS
+    descriptionText = "TimestampVar"
 
     def getValueProc(self, space, pos):
-        dataptr = rffi.ptradd(
-            rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
-            pos)
-
         return transform.OracleIntervalToPythonDelta(
-            self.environment, dataptr)
+            self.environment, self.getDataptr(pos))
 
     def setValueProc(self, space, pos, w_value):
-        dataptr = rffi.ptradd(
-            rffi.cast(roci.Ptr(roci.OCIDateTime_p), self.data),
-            pos)
-
         if not space.is_true(space.isinstance(w_value,
                                               get(space).w_TimedeltaType)):
             raise OperationError(
@@ -936,7 +909,7 @@
             self.environment.handle,
             self.environment.errorHandle,
             days, hours, minutes, seconds, microseconds,
-            dataptr[0])
+            self.getDataptr(pos)[0])
 
         self.environment.checkForError(
             status, "IntervalVar_SetValue()")

Modified: pypy/trunk/pypy/module/oracle/roci.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/roci.py	(original)
+++ pypy/trunk/pypy/module/oracle/roci.py	Tue Nov 24 19:34:23 2009
@@ -45,9 +45,6 @@
                                  ('OCIDateTime', OCITime),
                                  ])
 
-    OCIDateTime_p = platform.SimpleType('OCIDateTime*', rffi.VOIDP)
-    OCIInterval_p = platform.SimpleType('OCIInterval*', rffi.VOIDP)
-
     constants = '''
     OCI_DEFAULT OCI_OBJECT OCI_THREADED OCI_EVENTS
     OCI_SUCCESS OCI_SUCCESS_WITH_INFO OCI_INVALID_HANDLE OCI_NO_DATA
@@ -94,6 +91,8 @@
 OCIBind = rffi.VOIDP
 OCIDefine = rffi.VOIDP
 OCISnapshot = rffi.VOIDP
+OCIDateTime = rffi.VOIDP
+OCIInterval = rffi.VOIDP
 
 void = lltype.Void
 dvoidp = rffi.VOIDP
@@ -354,7 +353,7 @@
     'OCIDateTimeCheck',
     [dvoidp,        # hndl
      OCIError,      # err
-     OCIDateTime_p, # date
+     OCIDateTime,   # date
      Ptr(ub4)],     # valid
     sword)
 
@@ -362,7 +361,7 @@
     'OCIDateTimeConstruct',
     [dvoidp,        # hndl
      OCIError,      # errhp
-     OCIDateTime_p, # datetime
+     OCIDateTime,   # datetime
      sb2,           # year
      ub1,           # month
      ub1,           # day
@@ -378,7 +377,7 @@
     'OCIDateTimeGetDate',
     [dvoidp,        # hndl
      OCIError,      # errhp
-     OCIDateTime_p, # datetime
+     OCIDateTime,   # datetime
      Ptr(sb2),      # year
      Ptr(ub1),      # month
      Ptr(ub1)],     # day
@@ -388,7 +387,7 @@
     'OCIDateTimeGetTime',
     [dvoidp,        # hndl
      OCIError,      # errhp
-     OCIDateTime_p, # datetime
+     OCIDateTime,   # datetime
      Ptr(ub1),      # hour
      Ptr(ub1),      # minute
      Ptr(ub1),      # second
@@ -404,7 +403,7 @@
      Ptr(sb4),       # mm
      Ptr(sb4),       # ss
      Ptr(sb4),       # fsec
-     OCIInterval_p], # result
+     OCIInterval],   # result
     sword)
 
 OCIIntervalSetDaySecond = external(
@@ -416,7 +415,7 @@
      sb4,            # mm
      sb4,            # ss
      sb4,            # fsec
-     OCIInterval_p], # result
+     OCIInterval],   # result
     sword)
 
 # OCI Number Functions



More information about the Pypy-commit mailing list