lib/db.py
author terom
Thu, 31 Jan 2008 19:19:05 +0000
changeset 30 b1d5c32ab771
parent 26 81d6679d50d0
permissions -rw-r--r--
moar tweaks/small bugfixes
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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    31
def execute_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    32
    c = conn.cursor()
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    33
    c.executemany(expr, iter)
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    34
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    35
    return c
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    57
def commit (cursor) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    58
    try :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    59
        cursor.execute("COMMIT")
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    60
    except sqlite3.OperationalError :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    61
        pass    # ffs. INSERT just doesn't do anything otherwise
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    64
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    65
def execute_commit (expr, *args) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    66
    return commit(execute(expr, *args))
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    67
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    68
def execute_commit_many (expr, iter) :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    73
delete = execute_commit
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    74
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    75
delete_many = execute_commit_many
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 22
diff changeset
    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