author | Tero Marttila <terom@fixme.fi> |
Wed, 03 Jun 2009 19:03:28 +0300 | |
branch | use-distutils |
changeset 41 | 3b1579a7bffb |
parent 26 | lib/db.py@81d6679d50d0 |
child 44 | 533b7e8b5d3b |
permissions | -rw-r--r-- |
22
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
1 |
# DeGAL - A pretty simple web image gallery |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
2 |
# Copyright (C) 2007 Tero Marttila |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
3 |
# http://marttila.de/~terom/degal/ |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
4 |
# |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
5 |
# This program is free software; you can redistribute it and/or modify |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
6 |
# it under the terms of the GNU General Public License as published by |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
7 |
# the Free Software Foundation; either version 2 of the License, or |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
8 |
# (at your option) any later version. |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
9 |
# |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
10 |
# This program is distributed in the hope that it will be useful, |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
13 |
# GNU General Public License for more details. |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
14 |
# |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
15 |
# You should have received a copy of the GNU General Public License |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
16 |
# along with this program; if not, write to the |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
17 |
# Free Software Foundation, Inc., |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
18 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
19 |
# |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
20 |
|
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
21 |
import sqlite3 |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
22 |
|
24 | 23 |
conn = sqlite3.connect("db/degal.db") |
22
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
24 |
|
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
25 |
def execute (expr, *args) : |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
26 |
c = conn.cursor() |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
27 |
c.execute(expr, args) |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
28 |
|
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
29 |
return c |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
30 |
|
24 | 31 |
def execute_many (expr, iter) : |
32 |
c = conn.cursor() |
|
33 |
c.executemany(expr, iter) |
|
34 |
||
35 |
return c |
|
36 |
||
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
37 |
def insert (expr, *args) : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
38 |
return execute_commit(expr, *args).lastrowid |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
39 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
40 |
def insert_many (cb, expr, iter) : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
41 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
42 |
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) |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
43 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
44 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
45 |
c = conn.cursor() |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
46 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
47 |
c.executemany(expr, _lastrowid_adapter(c, iter, cb)) |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
48 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
49 |
return commit(c) |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
50 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
51 |
def _lastrowid_adapter (c, iter, cb) : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
52 |
for val, args in iter : |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
53 |
yield args |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
54 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
55 |
cb(val, c.lastrowid) |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
56 |
|
24 | 57 |
def commit (cursor) : |
58 |
try : |
|
59 |
cursor.execute("COMMIT") |
|
60 |
except sqlite3.OperationalError : |
|
61 |
pass # ffs. INSERT just doesn't do anything otherwise |
|
62 |
||
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
63 |
return cursor |
24 | 64 |
|
65 |
def execute_commit (expr, *args) : |
|
66 |
return commit(execute(expr, *args)) |
|
67 |
||
68 |
def execute_commit_many (expr, iter) : |
|
69 |
return commit(execute_many(expr, iter)) |
|
22
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
70 |
|
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
71 |
select = execute |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
72 |
|
24 | 73 |
delete = execute_commit |
74 |
||
75 |
delete_many = execute_commit_many |
|
76 |
||
22
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
77 |
cursor = conn.cursor |
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
78 |