295 """return a dictionary to use as extra argument to cursor.execute |
295 """return a dictionary to use as extra argument to cursor.execute |
296 to insert/update an entity into a SQL database |
296 to insert/update an entity into a SQL database |
297 """ |
297 """ |
298 attrs = {} |
298 attrs = {} |
299 eschema = entity.e_schema |
299 eschema = entity.e_schema |
|
300 converters = getattr(self.dbhelper, 'TYPE_CONVERTERS', {}) |
300 for attr, value in entity.cw_edited.iteritems(): |
301 for attr, value in entity.cw_edited.iteritems(): |
301 if value is not None and eschema.subjrels[attr].final: |
302 if value is not None and eschema.subjrels[attr].final: |
302 atype = str(entity.e_schema.destination(attr)) |
303 atype = str(entity.e_schema.destination(attr)) |
303 if atype == 'Boolean': |
304 if atype in converters: |
304 value = self.dbhelper.boolean_value(value) |
305 # It is easier to modify preprocess_entity rather |
305 elif atype == 'Password': |
306 # than add_entity (native) as this behavior |
|
307 # may also be used for update. |
|
308 value = converters[atype](value) |
|
309 elif atype == 'Password': # XXX could be done using a TYPE_CONVERTERS callback |
306 # if value is a Binary instance, this mean we got it |
310 # if value is a Binary instance, this mean we got it |
307 # from a query result and so it is already encrypted |
311 # from a query result and so it is already encrypted |
308 if isinstance(value, Binary): |
312 if isinstance(value, Binary): |
309 value = value.getvalue() |
313 value = value.getvalue() |
310 else: |
314 else: |
311 value = crypt_password(value) |
315 value = crypt_password(value) |
312 value = self._binary(value) |
316 value = self._binary(value) |
313 # XXX needed for sqlite but I don't think it is for other backends |
|
314 # Note: use is __class__ since issubclass(datetime, date) |
|
315 elif atype in ('Datetime', 'TZDatetime') and type(value) is date: |
|
316 value = todatetime(value) |
|
317 elif atype == 'Date' and isinstance(value, datetime): |
|
318 value = todate(value) |
|
319 elif atype == 'TZDatetime' and getattr(value, 'tzinfo', None): |
|
320 value = utcdatetime(value) |
|
321 elif atype == 'TZTime' and getattr(value, 'tzinfo', None): |
|
322 value = utctime(value) |
|
323 elif isinstance(value, Binary): |
317 elif isinstance(value, Binary): |
324 value = self._binary(value.getvalue()) |
318 value = self._binary(value.getvalue()) |
325 attrs[SQL_PREFIX+str(attr)] = value |
319 attrs[SQL_PREFIX+str(attr)] = value |
326 attrs[SQL_PREFIX+'eid'] = entity.eid |
320 attrs[SQL_PREFIX+'eid'] = entity.eid |
327 return attrs |
321 return attrs |