degal/db.py
author Tero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 19:23:10 +0300
branchuse-distutils
changeset 44 533b7e8b5d3b
parent 41 3b1579a7bffb
child 47 189f331c7960
permissions -rw-r--r--
strip copyright/license boilerplate from modules, except dexif and formatbytes
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
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
     3
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
     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
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
     6
    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
     7
    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
     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
    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
    10
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    11
def execute_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    12
    c = conn.cursor()
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    13
    c.executemany(expr, iter)
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    14
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    15
    return c
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    16
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    17
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
    18
    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
    19
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    20
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
    21
    """
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    22
        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
    23
    """
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    24
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    25
    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
    26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    27
    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
    28
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    29
    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
    30
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    31
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
    32
    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
    33
        yield args
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
        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
    36
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    37
def commit (cursor) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    38
    try :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    39
        cursor.execute("COMMIT")
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    40
    except sqlite3.OperationalError :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    41
        pass    # ffs. INSERT just doesn't do anything otherwise
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    42
26
81d6679d50d0 updated shorturls.py to write new shorturls to the db, also adding support for dir-shorturls
terom
parents: 24
diff changeset
    43
    return cursor
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    44
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    45
def execute_commit (expr, *args) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    46
    return commit(execute(expr, *args))
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    47
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    48
def execute_commit_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    49
    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
    50
72696ca68c34 use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
terom
parents:
diff changeset
    51
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
    52
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    53
delete = execute_commit
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    54
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    55
delete_many = execute_commit_many
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    56
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
    57
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
    58