author | Tero Marttila <terom@fixme.fi> |
Sat, 13 Jun 2009 20:59:53 +0300 | |
branch | new-exif |
changeset 105 | effae6f38749 |
parent 47 | 189f331c7960 |
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 |
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
|
2 |
|
47
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
3 |
try : |
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
4 |
conn = sqlite3.connect("db/degal.db") |
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
5 |
|
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
6 |
except sqlite3.OperationalError : |
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
7 |
conn = None |
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
|
8 |
|
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 |
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
|
10 |
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
|
11 |
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
|
12 |
|
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 |
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
|
14 |
|
24 | 15 |
def execute_many (expr, iter) : |
16 |
c = conn.cursor() |
|
17 |
c.executemany(expr, iter) |
|
18 |
||
19 |
return c |
|
20 |
||
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
21 |
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
|
22 |
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
|
23 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
24 |
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
|
25 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
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) |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
27 |
""" |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
28 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
29 |
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
|
30 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
31 |
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
|
32 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
33 |
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
|
34 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
yield args |
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
38 |
|
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
39 |
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
|
40 |
|
24 | 41 |
def commit (cursor) : |
42 |
try : |
|
43 |
cursor.execute("COMMIT") |
|
44 |
except sqlite3.OperationalError : |
|
45 |
pass # ffs. INSERT just doesn't do anything otherwise |
|
46 |
||
26
81d6679d50d0
updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents:
24
diff
changeset
|
47 |
return cursor |
24 | 48 |
|
49 |
def execute_commit (expr, *args) : |
|
50 |
return commit(execute(expr, *args)) |
|
51 |
||
52 |
def execute_commit_many (expr, iter) : |
|
53 |
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
|
54 |
|
72696ca68c34
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff
changeset
|
55 |
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
|
56 |
|
24 | 57 |
delete = execute_commit |
58 |
||
59 |
delete_many = execute_commit_many |
|
60 |
||
47
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
61 |
if conn : |
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
62 |
cursor = conn.cursor |
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
|
63 |
|
47
189f331c7960
fix template to use pkg_resources, bin/degal does now run
Tero Marttila <terom@fixme.fi>
parents:
44
diff
changeset
|
64 |