From ddbd7b2be137e28c680346fad92ecc44cdd1520f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 3 Nov 2009 15:22:17 +0100 Subject: [PATCH] Don't use now() in SQL statement as it doesn't work on SQLite --- trytond/backend/sqlite/database.py | 12 ++++-------- trytond/ir/cron.py | 4 ++-- trytond/model/modelsql.py | 14 ++++++-------- trytond/tools/misc.py | 7 ++++--- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/trytond/backend/sqlite/database.py b/trytond/backend/sqlite/database.py index d58d2d1a0a..5790a52d27 100644 --- a/trytond/backend/sqlite/database.py +++ b/trytond/backend/sqlite/database.py @@ -70,9 +70,6 @@ def date_trunc(_type, date): def split_part(text, delimiter, count): return (text.split(delimiter) + [''] * (count - 1))[count - 1] -def now(): - return mx.DateTime.now().strftime('%Y-%m-%d %H:%M:%S') - class Database(DatabaseInterface): @@ -104,7 +101,6 @@ class Database(DatabaseInterface): self._conn.create_function('extract', 2, extract) self._conn.create_function('date_trunc', 2, date_trunc) self._conn.create_function('split_part', 3, split_part) - self._conn.create_function('now', 0, now) return self def cursor(self, autocommit=False): @@ -212,8 +208,8 @@ class Database(DatabaseInterface): cursor.execute('INSERT INTO ir_module_module ' \ '(create_uid, create_date, author, website, name, ' \ 'shortdesc, description, state) ' \ - 'VALUES (%s, now(), %s, %s, %s, %s, %s, %s)', - (0, info.get('author', ''), + 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', + (0, datetime.datetime.now(), info.get('author', ''), info.get('website', ''), i, info.get('name', False), info.get('description', ''), state)) cursor.execute('SELECT last_insert_rowid()') @@ -222,8 +218,8 @@ class Database(DatabaseInterface): for dependency in dependencies: cursor.execute('INSERT INTO ir_module_module_dependency ' \ '(create_uid, create_date, module, name) ' \ - 'VALUES (%s, now(), %s, %s) ', - (0, module_id, dependency)) + 'VALUES (%s, %s, %s, %s) ', + (0, datetime.datetime.now(), module_id, dependency)) class _Cursor(sqlite.Cursor): diff --git a/trytond/ir/cron.py b/trytond/ir/cron.py index 47aa58d74d..fcd8b40c00 100644 --- a/trytond/ir/cron.py +++ b/trytond/ir/cron.py @@ -160,9 +160,9 @@ class Cron(ModelSQL, ModelView): cursor.execute('SELECT * FROM ir_cron ' \ 'WHERE numbercall <> 0 ' \ 'AND active ' \ - 'AND nextcall <= now() ' \ + 'AND nextcall <= %s ' \ 'AND NOT running ' \ - 'ORDER BY priority') + 'ORDER BY priority', (datetime.datetime.now(),)) crons = cursor.dictfetchall() for cron in crons: diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 4217b1238b..4c8d087121 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -311,8 +311,9 @@ class ModelSQL(ModelStorage): 'is not in the selection' % \ (val, field)) upd0 += ', create_uid, create_date' - upd1 += ', %s, now()' + upd1 += ', %s, %s' upd2.append(user) + upd2.append(datetime.datetime.now()) try: id_new = cursor.nextid(self._table) if id_new: @@ -483,8 +484,6 @@ class ModelSQL(ModelStorage): if not self.table_query(context): fields_pre2 += ['(COALESCE(write_date, create_date)) ' \ 'AS _timestamp'] - else: - fields_pre2 += ['now()::timestamp AS _timestamp'] for i in range(0, len(ids), in_max): sub_ids = ids[i:i + in_max] @@ -808,8 +807,9 @@ class ModelSQL(ModelStorage): (val, field)) upd0.append(('write_uid', '%s')) - upd0.append(('write_date', 'now()')) + upd0.append(('write_date', '%s')) upd1.append(user) + upd1.append(datetime.datetime.now()) domain1, domain2 = self.pool.get('ir.rule').domain_get(cursor, user, self._name, mode='write', context=context) @@ -1121,8 +1121,8 @@ class ModelSQL(ModelStorage): if self._history: for obj_id in ids: cursor.execute('INSERT INTO "' + self._table + '__history" ' \ - '(id, write_uid, write_date) VALUES (%s, %s, now())', - (obj_id, user)) + '(id, write_uid, write_date) VALUES (%s, %s, %s)', + (obj_id, user, datetime.datetime.now())) for k in tree_ids.keys(): field = self._columns[k] @@ -1205,8 +1205,6 @@ class ModelSQL(ModelStorage): select_fields += ['(COALESCE("' + self._table + '".write_date,'\ '"' + self._table + '".create_date)) ' \ 'AS _timestamp'] - else: - select_fields += ['now()::timestamp AS _timestamp'] query_str = cursor.limit_clause( 'SELECT ' + ','.join(select_fields) + ' FROM ' + \ ' '.join(tables) + (qu1 and ' WHERE ' + qu1 or '') + \ diff --git a/trytond/tools/misc.py b/trytond/tools/misc.py index 42c5f78f30..815358477e 100644 --- a/trytond/tools/misc.py +++ b/trytond/tools/misc.py @@ -19,6 +19,7 @@ try: except ImportError: import StringIO import dis +import datetime def find_in_path(name): if os.name == "nt": @@ -323,11 +324,11 @@ class Cache(object): cursor.execute('SELECT name FROM ir_cache WHERE name = %s', (name,)) if cursor.fetchone(): - cursor.execute('UPDATE ir_cache SET "timestamp" = now() '\ - 'WHERE name = %s', (name,)) + cursor.execute('UPDATE ir_cache SET "timestamp" = %s '\ + 'WHERE name = %s', (datetime.datetime.now(), name)) else: cursor.execute('INSERT INTO ir_cache ("timestamp", "name") ' \ - 'VALUES (now(), %s)', (name,)) + 'VALUES (%s, %s)', (datetime.datetime.now(), name)) Cache._resets[dbname].clear() finally: cursor.commit()