[IMP] Use INSERT INTO RETURNING for the creation of a record and use some tables
instead of a string for the generation of the SQL query. bzr revid: stw@openerp.com-20140110162731-ma7wk61zof6h9vyr
This commit is contained in:
parent
a097785c67
commit
ec35576af3
|
@ -4412,7 +4412,9 @@ class BaseModel(object):
|
||||||
tocreate[v] = {}
|
tocreate[v] = {}
|
||||||
else:
|
else:
|
||||||
tocreate[v] = {'id': vals[self._inherits[v]]}
|
tocreate[v] = {'id': vals[self._inherits[v]]}
|
||||||
(upd0, upd1, upd2) = ('', '', [])
|
|
||||||
|
query_fields, query_formats, query_values = [], [], []
|
||||||
|
|
||||||
upd_todo = []
|
upd_todo = []
|
||||||
unknown_fields = []
|
unknown_fields = []
|
||||||
for v in vals.keys():
|
for v in vals.keys():
|
||||||
|
@ -4429,15 +4431,12 @@ class BaseModel(object):
|
||||||
'No such field(s) in model %s: %s.',
|
'No such field(s) in model %s: %s.',
|
||||||
self._name, ', '.join(unknown_fields))
|
self._name, ', '.join(unknown_fields))
|
||||||
|
|
||||||
# Try-except added to filter the creation of those records whose filds are readonly.
|
if not self._sequence:
|
||||||
# Example : any dashboard which has all the fields readonly.(due to Views(database views))
|
raise except_orm(
|
||||||
try:
|
_('UserError'),
|
||||||
cr.execute("SELECT nextval('"+self._sequence+"')")
|
_('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.')
|
||||||
except:
|
)
|
||||||
raise except_orm(_('UserError'),
|
|
||||||
_('You cannot perform this operation. New Record Creation is not allowed for this object as this object is for reporting purpose.'))
|
|
||||||
|
|
||||||
id_new = cr.fetchone()[0]
|
|
||||||
for table in tocreate:
|
for table in tocreate:
|
||||||
if self._inherits[table] in vals:
|
if self._inherits[table] in vals:
|
||||||
del vals[self._inherits[table]]
|
del vals[self._inherits[table]]
|
||||||
|
@ -4454,9 +4453,9 @@ class BaseModel(object):
|
||||||
else:
|
else:
|
||||||
self.pool[table].write(cr, user, [record_id], tocreate[table], context=parent_context)
|
self.pool[table].write(cr, user, [record_id], tocreate[table], context=parent_context)
|
||||||
|
|
||||||
upd0 += ',' + self._inherits[table]
|
query_fields.append(self._inherits[table])
|
||||||
upd1 += ',%s'
|
query_formats.append('%s')
|
||||||
upd2.append(record_id)
|
query_values.append(record_id)
|
||||||
|
|
||||||
#Start : Set bool fields to be False if they are not touched(to make search more powerful)
|
#Start : Set bool fields to be False if they are not touched(to make search more powerful)
|
||||||
bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
|
bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean']
|
||||||
|
@ -4494,9 +4493,10 @@ class BaseModel(object):
|
||||||
vals.pop(field)
|
vals.pop(field)
|
||||||
for field in vals:
|
for field in vals:
|
||||||
if self._columns[field]._classic_write:
|
if self._columns[field]._classic_write:
|
||||||
upd0 = upd0 + ',"' + field + '"'
|
query_fields.append('"%s"' % field)
|
||||||
upd1 = upd1 + ',' + self._columns[field]._symbol_set[0]
|
query_formats.append(self._columns[field]._symbol_set[0])
|
||||||
upd2.append(self._columns[field]._symbol_set[1](vals[field]))
|
query_values.append(self._columns[field]._symbol_set[1](vals[field]))
|
||||||
|
|
||||||
#for the function fields that receive a value, we set them directly in the database
|
#for the function fields that receive a value, we set them directly in the database
|
||||||
#(they may be required), but we also need to trigger the _fct_inv()
|
#(they may be required), but we also need to trigger the _fct_inv()
|
||||||
if (hasattr(self._columns[field], '_fnct_inv')) and not isinstance(self._columns[field], fields.related):
|
if (hasattr(self._columns[field], '_fnct_inv')) and not isinstance(self._columns[field], fields.related):
|
||||||
|
@ -4518,10 +4518,20 @@ class BaseModel(object):
|
||||||
and vals[field]:
|
and vals[field]:
|
||||||
self._check_selection_field_value(cr, user, field, vals[field], context=context)
|
self._check_selection_field_value(cr, user, field, vals[field], context=context)
|
||||||
if self._log_access:
|
if self._log_access:
|
||||||
upd0 += ',create_uid,create_date,write_uid,write_date'
|
query_fields.extend(['create_uid', 'create_date', 'write_uid', 'write_date'])
|
||||||
upd1 += ",%s,(now() at time zone 'UTC'),%s,(now() at time zone 'UTC')"
|
query_formats.extend(['%s', "(now() at time zone 'UTC')", '%s', "(now() at time zone 'UTC')"])
|
||||||
upd2.extend((user, user))
|
query_values.extend((user, user))
|
||||||
cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2))
|
|
||||||
|
cr.execute(
|
||||||
|
"""INSERT INTO "%s" (%s) VALUES(%s) RETURNING id""" % (
|
||||||
|
self._table,
|
||||||
|
','.join(query_fields),
|
||||||
|
','.join(query_formats)
|
||||||
|
),
|
||||||
|
tuple(query_values)
|
||||||
|
)
|
||||||
|
|
||||||
|
id_new, = cr.fetchone()
|
||||||
upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
|
upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority)
|
||||||
|
|
||||||
if self._parent_store and not context.get('defer_parent_store_computation'):
|
if self._parent_store and not context.get('defer_parent_store_computation'):
|
||||||
|
|
Loading…
Reference in New Issue