Don't use hardcoded true/false in SQL queries to be more database independant

This commit is contained in:
Cédric Krier
2009-07-07 19:22:56 +02:00
parent a359c7c0a2
commit bd02e85cee
9 changed files with 90 additions and 71 deletions
+11 -8
View File
@@ -164,10 +164,11 @@ class Cron(ModelSQL, ModelView):
crons = cursor.dictfetchall()
for cron in crons:
cursor.execute('UPDATE ir_cron SET running = True ' \
'WHERE id = %s' % cron['id'])
nextcall = DateTime.strptime(str(cron['nextcall']),
'%Y-%m-%d %H:%M:%S')
cursor.execute('UPDATE ir_cron SET running = %s ' \
'WHERE id = %s', (True, cron['id']))
nextcall = DateTime.strptime(
cron['nextcall'].strftime('%Y-%m-%d %H:%M:%S'),
'%Y-%m-%d %H:%M:%S')
numbercall = cron['numbercall']
done = False
@@ -182,13 +183,15 @@ class Cron(ModelSQL, ModelView):
done = True
addsql = ''
addsql_param = []
if not numbercall:
addsql = ', active=False'
addsql = ', active = %s'
addsql_param = [False]
cursor.execute("UPDATE ir_cron SET nextcall = %s, " \
"running = False, numbercall = %s" + addsql + " " \
"running = %s, numbercall = %s" + addsql + " " \
"WHERE id = %s",
(nextcall.strftime('%Y-%m-%d %H:%M:%S'),
numbercall, cron['id']))
[nextcall.strftime('%Y-%m-%d %H:%M:%S'), False,
numbercall] + addsql_param + [cron['id']])
cursor.commit()
except Exception, e:
cursor.rollback()
+1 -1
View File
@@ -118,7 +118,7 @@ class Sequence(ModelSQL, ModelView):
'SET number_next = number_next + number_increment, ' \
'write_uid = %s, ' \
'write_date = NOW() ' \
'WHERE id = %s AND active = True', (user, sequence.id,))
'WHERE id = %s AND active = %s', (user, sequence.id, True))
if number_next:
return self._process(cursor, user, sequence.prefix, date=date,
context=context) + \
+10 -10
View File
@@ -151,10 +151,10 @@ class Translation(ModelSQL, ModelView, Cacheable):
'AND name = %s ' \
'AND value != \'\' ' \
'AND value IS NOT NULL ' \
'AND fuzzy = false ' \
'AND fuzzy = %s ' \
'AND res_id in (' + \
','.join([str(x) for x in sub_to_fetch]) + ')',
(lang, ttype, name))
(lang, ttype, name, False))
for res_id, value in cursor.fetchall():
self.add(cursor, (lang, ttype, name, res_id), value)
translations[res_id] = value
@@ -248,9 +248,9 @@ class Translation(ModelSQL, ModelView, Cacheable):
'AND src = %s ' \
'AND value != \'\' ' \
'AND value IS NOT NULL ' \
'AND fuzzy = false ' \
'AND fuzzy = %s ' \
'AND res_id = 0',
(lang, ttype, str(name), source))
(lang, ttype, str(name), source, False))
else:
cursor.execute('SELECT value ' \
'FROM ir_translation ' \
@@ -259,9 +259,9 @@ class Translation(ModelSQL, ModelView, Cacheable):
'AND name = %s ' \
'AND value != \'\' ' \
'AND value IS NOT NULL ' \
'AND fuzzy = false ' \
'AND fuzzy = %s ' \
'AND res_id = 0',
(lang, ttype, str(name)))
(lang, ttype, str(name), False))
res = cursor.fetchone()
if res:
self.add(cursor, (lang, ttype, name, source), res[0])
@@ -302,18 +302,18 @@ class Translation(ModelSQL, ModelView, Cacheable):
'AND src = %s ' \
'AND value != \'\' ' \
'AND value IS NOT NULL ' \
'AND fuzzy = false ' \
'AND fuzzy = %s ' \
'AND res_id = 0)',
(lang, ttype, str(name), source))]
(lang, ttype, str(name), source, False))]
else:
clause += [('(lang = %s ' \
'AND type = %s ' \
'AND name = %s ' \
'AND value != \'\' ' \
'AND value IS NOT NULL ' \
'AND fuzzy = false ' \
'AND fuzzy = %s ' \
'AND res_id = 0)',
(lang, ttype, str(name)))]
(lang, ttype, str(name), False))]
if clause:
for i in range(0, len(clause), cursor.IN_MAX):
sub_clause = clause[i:i + cursor.IN_MAX]
+2 -2
View File
@@ -131,9 +131,9 @@ class View(ModelSQL, ModelView):
if not done:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy)' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(view.model, 'en_US', 'view', string, '',
view.module))
view.module, False))
if strings:
cursor.execute('DELETE FROM ir_translation ' \
'WHERE name = %s ' \
+11 -10
View File
@@ -175,8 +175,8 @@ class Model(object):
if not trans_id:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
(name, 'en_US', 'model', src, '', module_name))
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(name, 'en_US', 'model', src, '', module_name, False))
else:
cursor.execute('UPDATE ir_translation ' \
'SET src = %s ' \
@@ -250,9 +250,9 @@ class Model(object):
'write_uid', 'write_date', 'id'):
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(trans_name, 'en_US', 'field',
field.string, '', module_name))
field.string, '', module_name, False))
elif trans_fields[trans_name]['src'] != field.string:
cursor.execute('UPDATE ir_translation ' \
'SET src = %s ' \
@@ -262,9 +262,9 @@ class Model(object):
if field.help:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(trans_name, 'en_US', 'help',
field.help, '', module_name))
field.help, '', module_name, False))
elif trans_help[trans_name]['src'] != field.help:
cursor.execute('UPDATE ir_translation ' \
'SET src = %s ' \
@@ -281,9 +281,9 @@ class Model(object):
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, ' \
'module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(trans_name, 'en_US', 'selection', val, '',
module_name))
module_name, False))
# Clean ir_model_field from field that are no more existing.
for field_name in fields:
if fields[field_name]['module'] == module_name \
@@ -308,8 +308,9 @@ class Model(object):
if error not in trans_error:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
(self._name, 'en_US', 'error', error, '', module_name))
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(self._name, 'en_US', 'error', error, '', module_name,
False))
def _get_error_messages(self):
return self._error_messages.values()
+40 -27
View File
@@ -1154,7 +1154,7 @@ class ModelSQL(ModelStorage):
if count:
cursor.execute('SELECT COUNT("%s".id) FROM ' % self._table +
' '.join(tables) + ' WHERE ' + (qu1 or 'True') +
' '.join(tables) + (qu1 and ' WHERE ' + qu1 or '') +
limit_str + offset_str, tables_args + qu2)
res = cursor.fetchall()
return res[0][0]
@@ -1165,7 +1165,7 @@ class ModelSQL(ModelStorage):
select_field += ', COALESCE("' + self._table + '".write_date, "' + \
self._table + '".create_date)'
query_str = 'SELECT ' + select_field + ' FROM ' + \
' '.join(tables) + ' WHERE ' + (qu1 or 'True') + \
' '.join(tables) + (qu1 and ' WHERE ' + qu1 or '') + \
' ORDER BY ' + order_by + limit_str + offset_str
if query_string:
return (query_str, tables_args + qu2)
@@ -1257,7 +1257,7 @@ class ModelSQL(ModelStorage):
qu1b, qu2b = self.__search_domain_oper(cursor, user, domain2,
tables, tables_args, context=context)
if not qu1b:
qu1b = 'true'
qu1b = FIELDS['boolean'].sql_format(True)
if qu1 and qu1b:
qu1 += ' ' + operator + ' ' + qu1b
elif qu1b:
@@ -1500,7 +1500,7 @@ class ModelSQL(ModelStorage):
'WHERE id IN ' + \
'(' + ','.join(['%s' for x in ids2]) + ')',
ids2)
clause = 'false '
clause = FIELDS['boolean'].sql_format(False) + ' '
for left, right in cursor.fetchall():
clause += 'OR '
clause += '( "' + field.left + '" >= ' + \
@@ -1540,8 +1540,9 @@ class ModelSQL(ModelStorage):
'AND ir_translation.res_id = 0' \
'AND ir_translation.lang = %%s ' \
'AND ir_translation.type = \'model\' ' \
'AND ir_translation.fuzzy = false)' % \
(domain[i][0],)
'AND ir_translation.fuzzy = %s)' % \
(domain[i][0],
FIELDS['boolean'].sql_format(False))
elif self._name == 'ir.model.field':
if domain[i][0] == 'field_description':
ttype = 'field'
@@ -1555,16 +1556,18 @@ class ModelSQL(ModelStorage):
'AND ir_translation.res_id = 0' \
'AND ir_translation = %%s ' \
'AND ir_translation.type = \'%s\' ' \
'AND ir_translation.fuzzy = false)' % \
(table._table, ttype)
'AND ir_translation.fuzzy = %s)' % \
(table._table, ttype,
FIELDS['boolean'].sql_format(False))
else:
table_join = 'LEFT JOIN "ir_translation" ' \
'ON (ir_translation.res_id = %s.id ' \
'AND ir_translation.name = \'%s,%s\' ' \
'AND ir_translation.lang = %%s ' \
'AND ir_translation.type = \'model\' ' \
'AND ir_translation.fuzzy = false)' % \
(table._table, table._name, domain[i][0])
'AND ir_translation.fuzzy = %s)' % \
(table._table, table._name, domain[i][0],
FIELDS['boolean'].sql_format(False))
table_join_args = [context.get('language') or 'en_US']
table_query = ''
@@ -1620,47 +1623,54 @@ class ModelSQL(ModelStorage):
if table._columns[arg[0]]._type == 'boolean':
if arg[1] == 'in':
qu1[-1] = '(' + qu1[-1] + ' OR ' \
'"%s".%s = false)' % \
'"%s"."%s" = %%s)' % \
(table._table, arg[0])
arg2.append(False)
else:
qu1[-1] = '(' + qu1[-1] + ' AND ' \
'"%s".%s != false)' % \
'"%s"."%s" != %%s)' % \
(table._table, arg[0])
arg2.append(False)
else:
if arg[1] == 'in':
qu1[-1] = '(' + qu1[-1] + ' OR ' \
'"%s".%s IS NULL)' % \
'"%s"."%s" IS NULL)' % \
(table._table, arg[0])
else:
qu1[-1] = '(' + qu1[-1] + ' AND ' \
'"%s".%s IS NOT NULL)' % \
'"%s"."%s" IS NOT NULL)' % \
(table._table, arg[0])
qu2 += arg2
elif todel:
if table._columns[arg[0]]._type == 'boolean':
if arg[1] == 'in':
qu1.append('("%s".%s = false)' % \
qu1.append('("%s"."%s" = %%s)' % \
(table._table, arg[0]))
qu2.append(False)
else:
qu1.append('("%s".%s != false)' % \
qu1.append('("%s"."%s" != %%s)' % \
(table._table, arg[0]))
qu2.append(False)
else:
if arg[1] == 'in':
qu1.append('("%s".%s IS NULL)' % \
qu1.append('("%s"."%s" IS NULL)' % \
(table._table, arg[0]))
else:
qu1.append('("%s".%s IS NOT NULL)' % \
qu1.append('("%s"."%s" IS NOT NULL)' % \
(table._table, arg[0]))
else:
if arg[1] == 'in':
qu1.append(' false')
qu1.append(' %s')
qu2.append(False)
else:
qu1.append(' true')
qu1.append(' %s')
qu2.append(True)
else:
if (arg[2] is False) and (arg[1] == '='):
if table._columns[arg[0]]._type == 'boolean':
qu1.append('("%s".%s = false)' % \
qu1.append('("%s"."%s" = %%s)' % \
(table._table, arg[0]))
qu2.append(False)
else:
qu1.append('("%s".%s IS NULL)' % \
(table._table, arg[0]))
@@ -1818,10 +1828,11 @@ class ModelSQL(ModelStorage):
'AND "%s".res_id = 0 ' \
'AND "%s".lang = %%s ' \
'AND "%s".type = \'model\' ' \
'AND "%s".fuzzy = false)' % \
'AND "%s".fuzzy = %s)' % \
(translation_table, translation_table, field_name,
translation_table, translation_table,
translation_table, translation_table)
translation_table, translation_table,
FIELDS['boolean'].sql_format(False))
elif self._name == 'ir.model.field':
if field_name == 'field_description':
ttype = 'field'
@@ -1837,10 +1848,11 @@ class ModelSQL(ModelStorage):
'AND "%s".res_id = 0 ' \
'AND "%s".lang = %%s ' \
'AND "%s".type = \'%s\' ' \
'AND "%s".fuzzy = false)' % \
'AND "%s".fuzzy = %s)' % \
(translation_table, translation_table, table_name,
translation_table, translation_table,
translation_table, ttype, translation_table)
translation_table, ttype, translation_table,
FIELDS['boolean'].sql_format(False))
else:
table_join = 'LEFT JOIN "ir_translation" ' \
'AS "%s" ON ' \
@@ -1848,11 +1860,12 @@ class ModelSQL(ModelStorage):
'AND "%s".name = \'%s,%s\' ' \
'AND "%s".lang = %%s ' \
'AND "%s".type = \'model\' ' \
'AND "%s".fuzzy = false)' % \
'AND "%s".fuzzy = %s)' % \
(translation_table, translation_table, table_name,
translation_table, self._name, field_name,
translation_table, translation_table,
translation_table)
translation_table,
FIELDS['boolean'].sql_format(False))
if table_join not in tables:
tables.append(table_join)
tables_args[table_join] = [context.get('language') or 'en_US']
+1 -1
View File
@@ -103,7 +103,7 @@ class TrytonServer(object):
if not cursor.test():
raise Exception("'%s' is not a Tryton database!" % db_name)
cursor.execute('SELECT code FROM ir_lang ' \
'WHERE translatable = True')
'WHERE translatable')
lang = [x[0] for x in cursor.fetchall()]
cursor.close()
update = bool(CONFIG['init'] or CONFIG['update'])
+6 -6
View File
@@ -632,10 +632,10 @@ class TrytondXmlHandler(sax.handler.ContentHandler):
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, res_id, ' \
'value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
(table_name + ',' + field_name,
'en_US', 'model', values[field_name],
res_id, '', module))
res_id, '', module, False))
for table in inherit_db_ids.keys():
data_id = self.modeldata_obj.search(cursor, user, [
@@ -775,10 +775,10 @@ class TrytondXmlHandler(sax.handler.ContentHandler):
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, res_id, ' \
'value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
(table_name + ',' + field_name,
'en_US', 'model', values[field_name],
res_id, '', module))
res_id, '', module, False))
if to_update:
# re-read it: this ensure that we store the real value
@@ -864,10 +864,10 @@ class TrytondXmlHandler(sax.handler.ContentHandler):
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, res_id, ' \
'value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
(table_name + ',' + field_name,
'en_US', 'model', values[field_name],
res_id, '', module))
res_id, '', module, False))
# re-read it: this ensure that we store the real value
# in the model_data table:
+8 -6
View File
@@ -51,15 +51,16 @@ class Wizard(object):
if not res:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(self._name + ',' + state + ',' + button_name,
'en_US', 'wizard_button', button_value,
'', module_name))
'', module_name, False))
elif res[0]['src'] != button_value:
cursor.execute('UPDATE ir_translation ' \
'SET src = %s, ' \
'fuzzy = True '
'WHERE id = %s', (button_value, res[0]['id']))
'fuzzy = %s '
'WHERE id = %s', (button_value, True,
res[0]['id']))
cursor.execute('SELECT id, src FROM ir_translation ' \
'WHERE lang = %s ' \
@@ -74,8 +75,9 @@ class Wizard(object):
if error not in trans_error:
cursor.execute('INSERT INTO ir_translation ' \
'(name, lang, type, src, value, module, fuzzy) ' \
'VALUES (%s, %s, %s, %s, %s, %s, false)',
(self._name, 'en_US', 'error', error, '', module_name))
'VALUES (%s, %s, %s, %s, %s, %s, %s)',
(self._name, 'en_US', 'error', error, '', module_name,
False))
def raise_user_error(self, cursor, error, error_args=None,
error_description='', error_description_args=None, context=None):