lib/db.py
changeset 26 81d6679d50d0
parent 24 001f52cd057e
--- a/lib/db.py	Sun Jan 20 01:52:00 2008 +0000
+++ b/lib/db.py	Thu Jan 31 17:28:02 2008 +0000
@@ -34,13 +34,33 @@
 
     return c
 
+def insert (expr, *args) :
+    return execute_commit(expr, *args).lastrowid
+
+def insert_many (cb, expr, iter) :
+    """
+        Perform an executemany with the given iterator (which must yield (cb_val, args) tuples), calling the given callback with the args (cb_val, row_id)
+    """
+
+    c = conn.cursor()
+
+    c.executemany(expr, _lastrowid_adapter(c, iter, cb))
+
+    return commit(c)
+
+def _lastrowid_adapter (c, iter, cb) :
+    for val, args in iter :
+        yield args
+
+        cb(val, c.lastrowid)
+
 def commit (cursor) :
     try :
         cursor.execute("COMMIT")
     except sqlite3.OperationalError :
         pass    # ffs. INSERT just doesn't do anything otherwise
 
-    return cursor.rowcount
+    return cursor
 
 def execute_commit (expr, *args) :
     return commit(execute(expr, *args))
@@ -51,10 +71,8 @@
 select = execute
 
 delete = execute_commit
-insert = execute_commit
 
 delete_many = execute_commit_many
-insert_many = execute_commit_many
 
 cursor = conn.cursor