@@ -13,21 +13,30 @@ def _quote(d, t):
13
13
if d == None :
14
14
return "NULL"
15
15
16
- if t in ['int' , 'decimal' , ' seq' ]:
17
- if d == "" : return 0
16
+ if t in ['int' , 'seq' ]:
17
+ if d == "" : return "NULL"
18
18
return "%d" % int (d )
19
19
20
+ if t == 'decimal' :
21
+ if d == "" : return "NULL"
22
+ return "%f" % float (d )
23
+
20
24
if t == 'money' :
21
- if d == "" : return '0.00'
25
+ if d == "" : return "NULL"
22
26
return "'%.2f'" % float (d )
23
27
24
28
if t == 'bool' :
25
- if string .upper (d ) in ['T' , 'TRUE' , 'Y' , 'YES' , 1 , '1' , 'ON' ]:
29
+ # Can't run upper() on these
30
+ if d in (0 , 1 ): return ('f' , 't' )[d ]
31
+
32
+ if string .upper (d ) in ['T' , 'TRUE' , 'Y' , 'YES' , '1' , 'ON' ]:
26
33
return "'t'"
27
34
else :
28
35
return "'f'"
29
36
30
- if d == "" : return "null"
37
+ if t == 'date' and d == '' : return "NULL"
38
+ if t in ('inet' , 'cidr' ) and d == '' : return "NULL"
39
+
31
40
return "'%s'" % string .strip (re .sub ("'" , "''" , \
32
41
re .sub ("\\ \\ " , "\\ \\ \\ \\ " , "%s" % d )))
33
42
@@ -68,7 +77,11 @@ def query(self, qstr):
68
77
print self .debug % qstr
69
78
return self .db .query (qstr )
70
79
71
- def pkey (self , cl ):
80
+ # If third arg supplied set primary key to it
81
+ def pkey (self , cl , newpkey = None ):
82
+ if newpkey :
83
+ self .__pkeys__ [cl ] = newpkey
84
+
72
85
# will raise an exception if primary key doesn't exist
73
86
return self .__pkeys__ [cl ]
74
87
@@ -115,6 +128,8 @@ def get_attnames(self, cl):
115
128
l [attname ] = 'date'
116
129
elif re .match ("^date" , typname ):
117
130
l [attname ] = 'date'
131
+ elif re .match ("^timestamp" , typname ):
132
+ l [attname ] = 'date'
118
133
elif re .match ("^bool" , typname ):
119
134
l [attname ] = 'bool'
120
135
elif re .match ("^float" , typname ):
@@ -129,15 +144,20 @@ def get_attnames(self, cl):
129
144
130
145
# return a tuple from a database
131
146
def get (self , cl , arg , keyname = None , view = 0 ):
147
+ if cl [- 1 ] == '*' : # need parent table name
148
+ xcl = cl [:- 1 ]
149
+ else :
150
+ xcl = cl
151
+
132
152
if keyname == None : # use the primary key by default
133
- keyname = self .__pkeys__ [cl ]
153
+ keyname = self .__pkeys__ [xcl ]
134
154
135
- fnames = self .get_attnames (cl )
155
+ fnames = self .get_attnames (xcl )
136
156
137
157
if type (arg ) == type ({}):
138
158
# To allow users to work with multiple tables we munge the
139
159
# name when the key is "oid"
140
- if keyname == 'oid' : k = arg ['oid_%s' % cl ]
160
+ if keyname == 'oid' : k = arg ['oid_%s' % xcl ]
141
161
else : k = arg [keyname ]
142
162
else :
143
163
k = arg
@@ -151,7 +171,7 @@ def get(self, cl, arg, keyname = None, view = 0):
151
171
(cl , keyname , _quote (k , fnames [keyname ]))
152
172
else :
153
173
q = "SELECT oid AS oid_%s, %s FROM %s WHERE %s = %s" % \
154
- (cl , string .join (fnames .keys (), ',' ),\
174
+ (xcl , string .join (fnames .keys (), ',' ),\
155
175
cl , keyname , _quote (k , fnames [keyname ]))
156
176
157
177
if self .debug != None : print self .debug % q
@@ -175,8 +195,7 @@ def insert(self, cl, a):
175
195
n = []
176
196
for f in fnames .keys ():
177
197
if a .has_key (f ):
178
- if a [f ] == "" : l .append ("null" )
179
- else : l .append (_quote (a [f ], fnames [f ]))
198
+ l .append (_quote (a [f ], fnames [f ]))
180
199
n .append (f )
181
200
182
201
try :
@@ -197,44 +216,37 @@ def insert(self, cl, a):
197
216
# otherwise use the primary key. Fail if neither.
198
217
def update (self , cl , a ):
199
218
foid = 'oid_%s' % cl
200
- pk = self .__pkeys__ [cl ]
201
219
if a .has_key (foid ):
202
220
where = "oid = %s" % a [foid ]
203
- elif a .has_key (pk ):
204
- where = "%s = '%s'" % (pk , a [pk ])
221
+ elif self . __pkeys__ . has_key ( cl ) and a .has_key (self . __pkeys__ [ cl ] ):
222
+ where = "%s = '%s'" % (self . __pkeys__ [ cl ] , a [self . __pkeys__ [ cl ] ])
205
223
else :
206
- raise error , "Update needs key (%s) or oid as %s" % (pk , foid )
207
-
208
- q = "SELECT oid FROM %s WHERE %s" % (cl , where )
209
- if self .debug != None : print self .debug % q
210
- res = self .db .query (q ).getresult ()
211
-
212
- if len (res ) < 1 :
213
- raise error , "No record in %s where %s (%s)" % \
214
- (cl , where , sys .exc_value )
215
- else : a [foid ] = res [0 ][0 ]
224
+ raise error , "Update needs primary key or oid as %s" % foid
216
225
217
226
v = []
218
227
k = 0
219
228
fnames = self .get_attnames (cl )
220
229
221
230
for ff in fnames .keys ():
222
- if a .has_key (ff ) and a [ ff ] != res [ 0 ][ k ] :
231
+ if a .has_key (ff ):
223
232
v .append ("%s = %s" % (ff , _quote (a [ff ], fnames [ff ])))
224
233
225
234
if v == []:
226
235
return None
227
236
228
237
try :
229
- q = "UPDATE %s SET %s WHERE oid = %s" % \
230
- (cl , string .join (v , ',' ), a [ foid ] )
238
+ q = "UPDATE %s SET %s WHERE %s" % \
239
+ (cl , string .join (v , ',' ), where )
231
240
if self .debug != None : print self .debug % q
232
241
self .db .query (q )
233
242
except :
234
243
raise error , "Can't update %s: %s" % (cl , sys .exc_value )
235
244
236
245
# reload the dictionary to catch things modified by engine
237
- return self .get (cl , a , 'oid' )
246
+ if a .has_key (foid ):
247
+ return self .get (cl , a , 'oid' )
248
+ else :
249
+ return self .get (cl , a )
238
250
239
251
# At some point we will need a way to get defaults from a table
240
252
def clear (self , cl , a = {}):
0 commit comments