degal/db.py
author Tero Marttila <terom@fixme.fi>
Wed, 01 Jul 2009 20:15:08 +0300
changeset 139 d3167c40e7b9
parent 47 189f331c7960
permissions -rw-r--r--
remove old scripts/cgi-bin stuff. They wouldn't work as such with the new version, and replacements can be written while referring to the history
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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    15
def execute_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    16
    c = conn.cursor()
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    17
    c.executemany(expr, iter)
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    18
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    19
    return c
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    41
def commit (cursor) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    42
    try :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    43
        cursor.execute("COMMIT")
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    44
    except sqlite3.OperationalError :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    45
        pass    # ffs. INSERT just doesn't do anything otherwise
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    48
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    49
def execute_commit (expr, *args) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    50
    return commit(execute(expr, *args))
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    51
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    52
def execute_commit_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    57
delete = execute_commit
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    58
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    59
delete_many = execute_commit_many
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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