1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
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 # ###################################################
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
):
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.
47 if not sqlite3
.complete_statement(command
):
48 if sqlite3
.complete_statement(command
+ ';'):
49 command
= command
+ ';'
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.
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)
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
):
78 @param name: key in self.rows, whose corresponding value is then returned.
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
)