@@ -13,21 +13,30 @@ def _quote(d, t):
1313if d == None :
1414return "NULL"
1515
16- if t in ['int' ,'decimal' , ' seq' ]:
17- if d == "" :return 0
16+ if t in ['int' ,'seq' ]:
17+ if d == "" :return "NULL"
1818return "%d" % int (d )
1919
20+ if t == 'decimal' :
21+ if d == "" :return "NULL"
22+ return "%f" % float (d )
23+
2024if t == 'money' :
21- if d == "" :return '0.00'
25+ if d == "" :return "NULL"
2226return "'%.2f'" % float (d )
2327
2428if 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' ]:
2633return "'t'"
2734else :
2835return "'f'"
2936
30- if d == "" :return "null"
37+ if t == 'date' and d == '' :return "NULL"
38+ if t in ('inet' ,'cidr' )and d == '' :return "NULL"
39+
3140return "'%s'" % string .strip (re .sub ("'" ,"''" , \
3241re .sub ("\\ \\ " ,"\\ \\ \\ \\ " ,"%s" % d )))
3342
@@ -68,7 +77,11 @@ def query(self, qstr):
6877print self .debug % qstr
6978return self .db .query (qstr )
7079
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+
7285# will raise an exception if primary key doesn't exist
7386return self .__pkeys__ [cl ]
7487
@@ -115,6 +128,8 @@ def get_attnames(self, cl):
115128l [attname ]= 'date'
116129elif re .match ("^date" ,typname ):
117130l [attname ]= 'date'
131+ elif re .match ("^timestamp" ,typname ):
132+ l [attname ]= 'date'
118133elif re .match ("^bool" ,typname ):
119134l [attname ]= 'bool'
120135elif re .match ("^float" ,typname ):
@@ -129,15 +144,20 @@ def get_attnames(self, cl):
129144
130145# return a tuple from a database
131146def 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+
132152if keyname == None :# use the primary key by default
133- keyname = self .__pkeys__ [cl ]
153+ keyname = self .__pkeys__ [xcl ]
134154
135- fnames = self .get_attnames (cl )
155+ fnames = self .get_attnames (xcl )
136156
137157if type (arg )== type ({}):
138158# To allow users to work with multiple tables we munge the
139159# name when the key is "oid"
140- if keyname == 'oid' :k = arg ['oid_%s' % cl ]
160+ if keyname == 'oid' :k = arg ['oid_%s' % xcl ]
141161else :k = arg [keyname ]
142162else :
143163k = arg
@@ -151,7 +171,7 @@ def get(self, cl, arg, keyname = None, view = 0):
151171(cl ,keyname ,_quote (k ,fnames [keyname ]))
152172else :
153173q = "SELECT oid AS oid_%s, %s FROM %s WHERE %s = %s" % \
154- (cl ,string .join (fnames .keys (),',' ),\
174+ (xcl ,string .join (fnames .keys (),',' ),\
155175cl ,keyname ,_quote (k ,fnames [keyname ]))
156176
157177if self .debug != None :print self .debug % q
@@ -175,8 +195,7 @@ def insert(self, cl, a):
175195n = []
176196for f in fnames .keys ():
177197if 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 ]))
180199n .append (f )
181200
182201try :
@@ -197,44 +216,37 @@ def insert(self, cl, a):
197216# otherwise use the primary key. Fail if neither.
198217def update (self ,cl ,a ):
199218foid = 'oid_%s' % cl
200- pk = self .__pkeys__ [cl ]
201219if a .has_key (foid ):
202220where = "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 ] ])
205223else :
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
216225
217226v = []
218227k = 0
219228fnames = self .get_attnames (cl )
220229
221230for ff in fnames .keys ():
222- if a .has_key (ff )and a [ ff ] != res [ 0 ][ k ] :
231+ if a .has_key (ff ):
223232v .append ("%s = %s" % (ff ,_quote (a [ff ],fnames [ff ])))
224233
225234if v == []:
226235return None
227236
228237try :
229- q = "UPDATE %s SET %s WHEREoid = %s" % \
230- (cl ,string .join (v ,',' ),a [ foid ] )
238+ q = "UPDATE %s SET %s WHERE %s" % \
239+ (cl ,string .join (v ,',' ),where )
231240if self .debug != None :print self .debug % q
232241self .db .query (q )
233242except :
234243raise error ,"Can't update %s: %s" % (cl ,sys .exc_value )
235244
236245# 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 )
238250
239251# At some point we will need a way to get defaults from a table
240252def clear (self ,cl ,a = {}):