1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 """
62 This datasouce module uses the Python-only, filesystem based SQL
63 backend called gadfly by Aaron Watters, currently maintained by
64 Richard Jones <richard@users.sf.net>. It is available at
65 U{http://gadfly.sourceforge.net}.
66 """
67
68 __author__ = "Diedrich Vorberg <diedrich@tux4web.de>"
69 __version__ = "$Revision: 1.8 $"[11:-2]
70
71
72 from types import *
73 from sets import Set
74 import re
75
76
77 from orm2.datasource import datasource_base
78 from orm2.debug import sqllog, debug
79 from orm2.exceptions import *
80 from orm2.datatypes import common_serial
81 from orm2 import sql
82 from orm2.util import stupid_dict
83
84 import orm2.datasource
85
86 from gadfly import gadfly
87
89 """
90 An orm database adapter for gadfly.
91 """
92 no_fetchone = True
93
94 - def __init__(self, dbname="tmp", directory="/tmp",
95 encoding = "iso-8859-1"):
96 """
97 The default values will create a database called tmp in your
98 /tmp directory. Gadfly will create a number of files called dbname.*
99 there.
100 """
101 orm2.datasource.datasource_base.__init__(self)
102
103 self._conn = gadfly()
104 self._conn.startup(dbname, directory)
105 self.encoding = encoding
106
107 self._update_cursor = self.cursor()
108
110 database_name = params.get("dbname", "tmp")
111 database_directory = params.get("directory", "/tmp")
112 encoding = params.get("encoding", "iso-8859-1")
113
114 return datasource(database_name, database_directory, encoding)
115
116 from_params = staticmethod(_from_params)
117
119 return self.encoding
120
121 - def insert(self, dbobj, dont_select=False):
122 """
123 The gadfly backend does not provide a mechanism to create unique
124 keys for new rows. Values for the common_serial() datatype must be
125 determined by the insert() function. It will query the maximum value
126 of the id column and increment it.
127 """
128
129 query_id = False
130 for property in dbobj.__dbproperties__():
131 if isinstance(property, common_serial):
132 common_serial_property = property
133 query_id = True
134 break
135
136 if query_id:
137 query = "SELECT COUNT(*) FROM %s" % dbobj.__relation__
138 cursor = self.execute(query)
139 tpl = cursor.fetchone()
140 count = tpl[0]
141
142 if count == 0:
143 new_id = 1
144 else:
145 query = "SELECT MAX(id) FROM %s" % dbobj.__relation__
146 cursor = self.execute(query)
147 tpl = cursor.fetchone()
148 max_id = tpl[0]
149
150 new_id = max_id + 1
151
152 common_serial_property.__set_from_result__(self, dbobj, new_id)
153
154 datasource_base.insert(self, dbobj, dont_select)
155
157 """
158 Gadfly doesn't support the LIMIT clause.
159 """
160 result = self.select(dbclass, *clauses)
161 result = list(result)
162
163 if len(result) == 0:
164 return None
165 else:
166 return result[0]
167
168
170 """
171 The gadfly backend neither supports default values for columns
172 not owns a mechanism to provide unique keys for new rows. So the
173 select_after_insert() mechanism is useless.
174 """
175 pass
176
178 """
179 See select_after_insert() above.
180 """
181 raise NotImplemented()
182