I'd like to propose the attached changes to the DB-API spec. My main motivation is to make DB-API modules less cumbersome to use when used directly.

If you want to try it out live, everything except .fetchscalar() is already implemented in pysqlite. The __enter__/__exit__ thing is in pysqlite and cx_Oracle.


==> .execute(), .executemany() returning self.

Ability to write shorter code. No more:

cur.execute("select ...")
for row in cur:
  ...

instead:

for row in cur.exeucte("..."):
  ...

==> .execute(), .executemany() in connection object.

They should have been placed here anyway. *cough* Users shouldn't have to mess with cursor objects normally. Ability to write shorter code:

con = module.connect(...)
for row in con.execute("..."):
  ...

==> __enter__ and __exit__ in the connection object.

Ability to automatically wrap database code in transactions, when using Python 2.5 or higher:

with con:
    con.execute(DML1)
    con.execute(DML2)

no more:

try:
   cur = con.cursor()
   cur.execute(DML1)
   cur.execute(DML2)
   con.commit()
except:
    con.rollback()

==> fetchscalar method in cursor object

This one is low-prio for me, but it's a common use case to only query a scalar value.

-- Gerhard
*** pep-0249.txt.orig   2008-03-19 13:11:02.000000000 +0100
--- pep-0249.txt        2008-03-19 13:10:57.000000000 +0100
***************
*** 344,354 ****
              The parameters may also be specified as list of tuples to
              e.g. insert multiple rows in a single operation, but this
              kind of usage is deprecated: .executemany() should be used
              instead.
              
!             Return values are not defined.
              
          .executemany(operation,seq_of_parameters) 
            
              Prepare a database operation (query or command) and then
              execute it against all parameter sequences or mappings
--- 344,354 ----
              The parameters may also be specified as list of tuples to
              e.g. insert multiple rows in a single operation, but this
              kind of usage is deprecated: .executemany() should be used
              instead.
              
!             This method returns the cursor object.
              
          .executemany(operation,seq_of_parameters) 
            
              Prepare a database operation (query or command) and then
              execute it against all parameter sequences or mappings
***************
*** 366,376 ****
              created by an invocation of the operation.
              
              The same comments as for .execute() also apply accordingly
              to this method.
              
!             Return values are not defined.
              
          .fetchone() 
            
              Fetch the next row of a query result set, returning a
              single sequence, or None when no more data is
--- 366,376 ----
              created by an invocation of the operation.
              
              The same comments as for .execute() also apply accordingly
              to this method.
              
!             This method returns the cursor object.
              
          .fetchone() 
            
              Fetch the next row of a query result set, returning a
              single sequence, or None when no more data is
***************
*** 378,387 ****
--- 378,390 ----
              
              An Error (or subclass) exception is raised if the previous
              call to .execute*() did not produce any result set or no
              call was issued yet.
  
+         .fetchscalar()
+             This is a wrapper for .fetchone()[0].
+ 
          .fetchmany([size=cursor.arraysize])
            
              Fetch the next set of rows of a query result, returning a
              sequence of sequences (e.g. a list of tuples). An empty
              sequence is returned when no more rows are available.
***************
*** 804,813 ****
--- 807,836 ----
          executed statement modified more than one row, e.g. when
          using INSERT with .executemany().
  
          Warning Message: "DB-API extension cursor.lastrowid used"
  
+     Connection Method .execute
+         This is a shorthand for creating a cursor object by calling .cursor(),
+         then executing the new cursor object's .execute() method with the
+         parameters specified.
+ 
+         This method returns the newly created cursor object.
+ 
+     Connection Method .executemany
+         This is a shorthand for creating a cursor object by calling .cursor(),
+         then executing the new cursor object's .executemany() method with the
+         parameters specified.
+ 
+         This method returns the newly created cursor object.
+             
+     Connection Method .__enter__
+         This method returns self.
+ 
+     Connect Method .__exit__
+         This method is implemented such that it calls .rollback() if any
+         exception has been raised, and .commit() otherwise.
          
  Optional Error Handling Extensions
  
      The core DB API specification only introduces a set of exceptions
      which can be raised to report errors to the user. In some cases,
_______________________________________________
DB-SIG maillist  -  DB-SIG@python.org
http://mail.python.org/mailman/listinfo/db-sig

Reply via email to