terom@22: # DeGAL - A pretty simple web image gallery terom@22: # Copyright (C) 2007 Tero Marttila terom@22: # http://marttila.de/~terom/degal/ terom@22: # terom@22: # This program is free software; you can redistribute it and/or modify terom@22: # it under the terms of the GNU General Public License as published by terom@22: # the Free Software Foundation; either version 2 of the License, or terom@22: # (at your option) any later version. terom@22: # terom@22: # This program is distributed in the hope that it will be useful, terom@22: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@22: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@22: # GNU General Public License for more details. terom@22: # terom@22: # You should have received a copy of the GNU General Public License terom@22: # along with this program; if not, write to the terom@22: # Free Software Foundation, Inc., terom@22: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@22: # terom@22: terom@22: import sqlite3 terom@22: terom@24: conn = sqlite3.connect("db/degal.db") terom@22: terom@22: def execute (expr, *args) : terom@22: c = conn.cursor() terom@22: c.execute(expr, args) terom@22: terom@22: return c terom@22: terom@24: def execute_many (expr, iter) : terom@24: c = conn.cursor() terom@24: c.executemany(expr, iter) terom@24: terom@24: return c terom@24: terom@26: def insert (expr, *args) : terom@26: return execute_commit(expr, *args).lastrowid terom@26: terom@26: def insert_many (cb, expr, iter) : terom@26: """ terom@26: 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) terom@26: """ terom@26: terom@26: c = conn.cursor() terom@26: terom@26: c.executemany(expr, _lastrowid_adapter(c, iter, cb)) terom@26: terom@26: return commit(c) terom@26: terom@26: def _lastrowid_adapter (c, iter, cb) : terom@26: for val, args in iter : terom@26: yield args terom@26: terom@26: cb(val, c.lastrowid) terom@26: terom@24: def commit (cursor) : terom@24: try : terom@24: cursor.execute("COMMIT") terom@24: except sqlite3.OperationalError : terom@24: pass # ffs. INSERT just doesn't do anything otherwise terom@24: terom@26: return cursor terom@24: terom@24: def execute_commit (expr, *args) : terom@24: return commit(execute(expr, *args)) terom@24: terom@24: def execute_commit_many (expr, iter) : terom@24: return commit(execute_many(expr, iter)) terom@22: terom@22: select = execute terom@22: terom@24: delete = execute_commit terom@24: terom@24: delete_many = execute_commit_many terom@24: terom@22: cursor = conn.cursor terom@22: