* Better Patch for Configfile handling
[opeanno-debian-packaging.git] / game / dbreader.py
blob3428ab030e6998cc7b0fd921b202340f830acd86
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 import sqlite3
23 import re
25 class DbReader(object):
26 """Class that handles connections to sqlite databases
27 @param file: str containing the database file."""
28 def __init__(self, file):
29 self.connection = sqlite3.connect(file)
30 self.connection.isolation_level = None
31 self.connection.text_factory = str
32 def regexp(expr, item):
33 """
34 @param expr:
35 @param item:
36 """
37 r = re.compile(expr)
38 return r.match(item) is not None
39 self.connection.create_function("regexp", 2, regexp)
40 self.cur = self.connection.cursor()
42 def __call__(self, command, *args):
43 """Executes a sql command.
44 @param command: str containing the raw sql command, with ? as placeholders for values (eg. SELELCT ? FROM ?).
45 @param args: tuple containing the values to add into the command.
46 """
47 if not sqlite3.complete_statement(command):
48 if sqlite3.complete_statement(command + ';'):
49 command = command + ';'
50 else:
51 raise 'Error, no complete sql statement provided by "' + command + '".'
52 self.cur.execute(command, args)
53 return SqlResult(self.cur.fetchall(), None if self.cur.rowcount == -1 else self.cur.rowcount, self.cur.lastrowid)
55 def execute_script(self, script):
56 """Executes a multiline script.
57 @param script: multiline str containing an sql script."""
58 return self.cur.executescript(script)
60 class SqlError(object):
61 """Represents a SQL error
62 @param error: str error description.
63 """
64 def __init__(self, error):
65 self.success, self.error, self.rows, self.affected, self.id = False, error, None, None, None
67 class SqlResult(object):
68 """Represents a SQL result
69 @param rows: the rows that were returned by the sql query
70 @param affected: int number of rows affected by the sql query
71 @param id: int id of the sqlresult(lastrowid of the curser)
72 """
73 def __init__(self, rows, affected, id):
74 self.success, self.error, self.rows, self.affected, self.id = True, None, rows, affected, id
76 def __getattr__(self, name):
77 """
78 @param name: key in self.rows, whose corresponding value is then returned.
79 """
80 return getattr(self.rows, name)
81 def __add__(self, *args, **kwargs): return self.rows.__add__(*args, **kwargs)
82 def __contains__(self, *args, **kwargs): return self.rows.__contains__(*args, **kwargs)
83 def __delitem__(self, *args, **kwargs): return self.rows.__delitem__(*args, **kwargs)
84 def __delslice__(self, *args, **kwargs): return self.rows.__delslice__(*args, **kwargs)
85 def __eq__(self, *args, **kwargs): return self.rows.__eq__(*args, **kwargs)
86 def __ge__(self, *args, **kwargs): return self.rows.__ge__(*args, **kwargs)
87 def __getitem__(self, *args, **kwargs): return self.rows.__getitem__(*args, **kwargs)
88 def __getslice__(self, *args, **kwargs): return self.rows.__getslice__(*args, **kwargs)
89 def __gt__(self, *args, **kwargs): return self.rows.__gt__(*args, **kwargs)
90 def __hash__(self, *args, **kwargs): return self.rows.__hash__(*args, **kwargs)
91 def __iadd__(self, *args, **kwargs): return self.rows.__iadd__(*args, **kwargs)
92 def __imul__(self, *args, **kwargs): return self.rows.__imul__(*args, **kwargs)
93 def __iter__(self, *args, **kwargs): return self.rows.__iter__(*args, **kwargs)
94 def __le__(self, *args, **kwargs): return self.rows.__le__(*args, **kwargs)
95 def __len__(self, *args, **kwargs): return self.rows.__len__(*args, **kwargs)
96 def __lt__(self, *args, **kwargs): return self.rows.__lt__(*args, **kwargs)
97 def __mul__(self, *args, **kwargs): return self.rows.__mul__(*args, **kwargs)
98 def __ne__(self, *args, **kwargs): return self.rows.__ne__(*args, **kwargs)
99 def __repr__(self, *args, **kwargs): return self.rows.__repr__(*args, **kwargs)
100 def __reversed__(self, *args, **kwargs): return self.rows.__reversed__(*args, **kwargs)
101 def __rmul__(self, *args, **kwargs): return self.rows.__rmul__(*args, **kwargs)
102 def __setitem__(self, *args, **kwargs): return self.rows.__setitem__(*args, **kwargs)
103 def __setslice__(self, *args, **kwargs): return self.rows.__setslice__(*args, **kwargs)