[pypy-svn] r69309 - in pypy/trunk/pypy/module/oracle: . test

afa at codespeak.net afa at codespeak.net
Sun Nov 15 23:59:34 CET 2009


Author: afa
Date: Sun Nov 15 23:59:33 2009
New Revision: 69309

Modified:
   pypy/trunk/pypy/module/oracle/interp_cursor.py
   pypy/trunk/pypy/module/oracle/interp_environ.py
   pypy/trunk/pypy/module/oracle/interp_error.py
   pypy/trunk/pypy/module/oracle/test/test_cursor.py
Log:
implement cursor.executemany, at least for a list of lists


Modified: pypy/trunk/pypy/module/oracle/interp_cursor.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/interp_cursor.py	(original)
+++ pypy/trunk/pypy/module/oracle/interp_cursor.py	Sun Nov 15 23:59:33 2009
@@ -108,6 +108,45 @@
         # for all other statements, simply return None
         return space.w_None
 
+    def executemany(self, space, w_stmt, w_list_of_args):
+        if space.is_w(w_stmt, space.w_None):
+            w_stmt = None
+        if not space.is_true(space.isinstance(w_list_of_args, space.w_list)):
+            raise OperationError(
+                space.w_TypeError,
+                space.wrap("list expected"))
+
+        # make sure the cursor is open
+        self._checkOpen(space)
+
+        # prepare the statement
+        self._internalPrepare(space, w_stmt, None)
+
+        # queries are not supported as the result is undefined
+        if self.statementType == roci.OCI_STMT_SELECT:
+            raise OperationError(
+                w_NotSupportedErrorException,
+                space.wrap("queries not supported: results undefined"))
+
+        # perform binds
+        numrows = space.int_w(space.len(w_list_of_args))
+        for i, arguments in enumerate(space.viewiterable(w_list_of_args)):
+            deferred = i < numrows - 1
+            if space.is_true(space.isinstance(arguments, space.w_dict)):
+                self._setBindVariablesByName(
+                    space, arguments, numrows, i, deferred)
+            else:
+                args_w = space.viewiterable(arguments)
+                self._setBindVariablesByPos(
+                    space, args_w, numrows, i, deferred)
+        self._performBind(space)
+
+        # execute the statement, but only if the number of rows is greater than
+        # zero since Oracle raises an error otherwise
+        if numrows > 0:
+            self._internalExecute(space, numIters=numrows)
+    executemany.unwrap_spec = ['self', ObjSpace, W_Root, W_Root]
+
     def close(self, space):
         # make sure we are actually open
         self._checkOpen(space)
@@ -141,7 +180,6 @@
         self._call(space, name, None, args_w)
 
         # create the return value
-        print "AFA", self.bindList
         ret_w = [v.getValue(space, 0) for v in self.bindList]
         return space.newlist(ret_w)
 
@@ -324,7 +362,9 @@
                 get(space).w_ProgrammingErrorException,
                 space.wrap("positional and named binds cannot be intermixed"))
 
-        self.bindList = []
+        if self.bindList is None:
+            self.bindList = []
+
         for i, w_value in enumerate(vars_w):
             if i < len(self.bindList):
                 origVar = self.bindList[i]
@@ -348,7 +388,8 @@
                 get(space).w_ProgrammingErrorException,
                 space.wrap("positional and named binds cannot be intermixed"))
 
-        self.bindDict = {}
+        if self.bindDict is None:
+            self.bindDict = {}
 
         for key, w_value in vars_w.iteritems():
             origVar = self.bindDict.get(key, None)
@@ -382,7 +423,7 @@
             else:
                 newVar = None
                 try:
-                    origVar.setValue(space, arrayPos, value)
+                    origVar.setValue(space, arrayPos, w_value)
                 except OperationError, e:
                     # executemany() should simply fail after the first element
                     if arrayPos > 0:
@@ -712,6 +753,8 @@
     'Cursor',
     execute = interp2app(W_Cursor.execute,
                          unwrap_spec=W_Cursor.execute.unwrap_spec),
+    executemany = interp2app(W_Cursor.executemany,
+                             unwrap_spec=W_Cursor.executemany.unwrap_spec),
     prepare = interp2app(W_Cursor.prepare,
                          unwrap_spec=W_Cursor.prepare.unwrap_spec),
     fetchone = interp2app(W_Cursor.fetchone,

Modified: pypy/trunk/pypy/module/oracle/interp_environ.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/interp_environ.py	(original)
+++ pypy/trunk/pypy/module/oracle/interp_environ.py	Sun Nov 15 23:59:33 2009
@@ -33,7 +33,7 @@
             if not handleptr[0] or status not in (roci.OCI_SUCCESS,
                                                   roci.OCI_SUCCESS_WITH_INFO):
                 raise OperationError(
-                    w_InterfaceErrorException,
+                    get(self.space).w_InterfaceError,
                     self.space.wrap(
                         "Unable to acquire Oracle environment handle"))
 
@@ -69,7 +69,7 @@
             # environment is fully initialized
             error = W_Error(self.space, self, context, 1)
             if error.code in (1, 1400, 2290, 2291, 2292):
-                w_type = w_IntegrityErrorException
+                w_type = get(self.space).w_IntegrityError
             elif error.code in (1012, 1033, 1034, 1089, 3113, 3114,
                                 12203, 12500, 12571):
                 w_type = get(self.space).w_OperationalError
@@ -80,4 +80,5 @@
         error = W_Error(self.space, self, context, 0)
         error.code = 0
         error.message = self.space.wrap("Invalid handle!")
-        raise OperationError(w_DatabaseErrorException, self.space.wrap(error))
+        raise OperationError(get(self.space).w_DatabaseError,
+                             self.space.wrap(error))

Modified: pypy/trunk/pypy/module/oracle/interp_error.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/interp_error.py	(original)
+++ pypy/trunk/pypy/module/oracle/interp_error.py	Sun Nov 15 23:59:33 2009
@@ -15,6 +15,7 @@
         self.w_InterfaceError = get('InterfaceError')
         self.w_ProgrammingError = get('ProgrammingError')
         self.w_NotSupportedError = get('NotSupportedError')
+        self.w_IntegrityError = get('IntegrityError')
         self.w_Variable = get('Variable')
         self.w_NUMBER = get('NUMBER')
 

Modified: pypy/trunk/pypy/module/oracle/test/test_cursor.py
==============================================================================
--- pypy/trunk/pypy/module/oracle/test/test_cursor.py	(original)
+++ pypy/trunk/pypy/module/oracle/test/test_cursor.py	Sun Nov 15 23:59:33 2009
@@ -95,3 +95,18 @@
                     "end;")
         results = cur.callproc("pypy_temp_procedure", ("hi", 5, var))
         assert results == ["hi", 10, 2.0]
+
+    def test_executemanyByPosition(self):
+        cur = self.cnx.cursor()
+        try:
+            cur.execute("drop table pypy_temp_table")
+        except oracle.DatabaseError:
+            pass
+        cur.execute("create table pypy_temp_table (intcol number)")
+        rows = [ [n] for n in range(23) ]
+        cur.arraysize = 10
+        statement = "insert into TestExecuteMany (IntCol) values (:1)"
+        cur.executemany(statement, rows)
+        cur.execute("select count(*) from TestExecuteMany")
+        count, = cur.fetchone()
+        assert count == len(rows)



More information about the Pypy-commit mailing list