From 74eee1cd207517d51b216a5194a429ce5d215aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 31 Oct 2009 19:33:45 +0100 Subject: [PATCH] Refactor backend fields --- trytond/backend/fields.py | 125 +++++++++++++++++++++++++++ trytond/backend/postgresql/fields.py | 106 +++++------------------ trytond/backend/sqlite/fields.py | 111 +++++------------------- 3 files changed, 166 insertions(+), 176 deletions(-) create mode 100644 trytond/backend/fields.py diff --git a/trytond/backend/fields.py b/trytond/backend/fields.py new file mode 100644 index 0000000000..bc0febeabe --- /dev/null +++ b/trytond/backend/fields.py @@ -0,0 +1,125 @@ +#This file is part of Tryton. The COPYRIGHT file at the top level of +#this repository contains the full copyright notices and license terms. +try: + import hashlib +except ImportError: + hashlib = None + import sha + + +class Field(object): + + @staticmethod + def sql_format(value): + if value is None or value == False: + return None + elif isinstance(value, str): + return unicode(value, 'utf-8') + elif isinstance(value, unicode): + return value + return unicode(value) + + @staticmethod + def sql_type(field): + return None + + +class Boolean(Field): + + @staticmethod + def sql_format(value): + return value and 'True' or 'False' + + +class Integer(Field): + + @staticmethod + def sql_format(value): + return int(value or 0) + + +class BigInteger(Integer): + pass + + +class Char(Field): + pass + + +class Sha(Field): + + @staticmethod + def sql_format(value): + if isinstance(value, basestring): + if isinstance(value, unicode): + value = value.encode('utf-8') + if hashlib: + value = hashlib.sha1(value).hexdigest() + else: + value = sha.new(value).hexdigest() + return Field.sql_format(value) + + +class Text(Field): + pass + + +class Float(Field): + + @staticmethod + def sql_format(value): + return float(value or 0.0) + + +class Numeric(Float): + pass + + +class Date(Field): + pass + + +class DateTime(Field): + pass + + +class Time(Field): + pass + + +class Binary(Field): + + @staticmethod + def sql_format(value): + return value or None + + +class Selection(Char): + pass + + +class Reference(Field): + pass + + +class Many2One(Field): + + @staticmethod + def sql_format(value): + return value and int(value) or None + + +class One2Many(Field): + pass + + +class Many2Many(Field): + pass + + +class Function(Field): + pass + + +class Property(Function): + pass diff --git a/trytond/backend/postgresql/fields.py b/trytond/backend/postgresql/fields.py index 5814bee0f8..3689de740e 100644 --- a/trytond/backend/postgresql/fields.py +++ b/trytond/backend/postgresql/fields.py @@ -1,61 +1,31 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -try: - import hashlib -except ImportError: - hashlib = None - import sha import psycopg2 +from trytond.backend import fields -class Field(object): - - @staticmethod - def sql_format(value): - if value is None or value == False: - return None - elif isinstance(value, str): - return unicode(value, 'utf-8') - elif isinstance(value, unicode): - return value - return unicode(value) - - @staticmethod - def sql_type(field): - return None - - -class Boolean(Field): - - @staticmethod - def sql_format(value): - return value and 'True' or 'False' +class Boolean(fields.Boolean): @staticmethod def sql_type(field): return ('bool', 'bool') -class Integer(Field): - - @staticmethod - def sql_format(value): - return int(value or 0) +class Integer(fields.Integer): @staticmethod def sql_type(field): return ('int4', 'int4') -class BigInteger(Integer): +class BigInteger(fields.BigInteger): @staticmethod def sql_type(field): return ('int8', 'int8') -class Char(Field): - +class Char(fields.Char): @staticmethod def sql_type(field): @@ -64,71 +34,56 @@ class Char(Field): return ('varchar', 'varchar') -class Sha(Field): - - @staticmethod - def sql_format(value): - if isinstance(value, basestring): - if isinstance(value, unicode): - value = value.encode('utf-8') - if hashlib: - value = hashlib.sha1(value).hexdigest() - else: - value = sha.new(value).hexdigest() - return Field.sql_format(value) +class Sha(fields.Sha): @staticmethod def sql_type(field): return ('varchar', 'varchar(40)') -class Text(Field): +class Text(fields.Text): @staticmethod def sql_type(field): return ('text', 'text') -class Float(Field): - - @staticmethod - def sql_format(value): - return float(value or 0.0) +class Float(fields.Float): @staticmethod def sql_type(field): return ('float8', 'float8') -class Numeric(Float): +class Numeric(fields.Numeric): @staticmethod def sql_type(field): return ('numeric', 'numeric') -class Date(Field): +class Date(fields.Date): @staticmethod def sql_type(field): return ('date', 'date') -class DateTime(Field): +class DateTime(fields.DateTime): @staticmethod def sql_type(field): return ('timestamp', 'timestamp(0)') -class Time(Field): +class Time(fields.Time): @staticmethod def sql_type(field): return ('time', 'time') -class Binary(Field): +class Binary(fields.Binary): @staticmethod def sql_format(value): @@ -139,47 +94,26 @@ class Binary(Field): return ('bytea', 'bytea') -class Selection(Char): +class Selection(fields.Selection): @staticmethod def sql_type(field): return ('varchar', 'varchar') -class Reference(Field): +class Reference(fields.Reference): @staticmethod def sql_type(field): return ('varchar', 'varchar') -class Many2One(Field): - - @staticmethod - def sql_format(value): - return value and int(value) or None +class Many2One(fields.Many2One): @staticmethod def sql_type(field): return ('int4', 'int4') - -class One2Many(Field): - pass - - -class Many2Many(Field): - pass - - -class Function(Field): - pass - - -class Property(Function): - pass - - FIELDS = { 'boolean': Boolean, 'integer': Integer, @@ -196,8 +130,8 @@ FIELDS = { 'selection': Selection, 'reference': Reference, 'many2one': Many2One, - 'one2many': One2Many, - 'many2many': Many2Many, - 'function': Function, - 'property': Property, + 'one2many': fields.One2Many, + 'many2many': fields.Many2Many, + 'function': fields.Function, + 'property': fields.Property, } diff --git a/trytond/backend/sqlite/fields.py b/trytond/backend/sqlite/fields.py index 188c7b7c45..aa39721d48 100644 --- a/trytond/backend/sqlite/fields.py +++ b/trytond/backend/sqlite/fields.py @@ -1,30 +1,9 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -try: - import hashlib -except ImportError: - hashlib = None - import sha +from trytond.backend import fields -class Field(object): - - @staticmethod - def sql_format(value): - if value is None or value == False: - return None - elif isinstance(value, str): - return unicode(value, 'utf-8') - elif isinstance(value, unicode): - return value - return unicode(value) - - @staticmethod - def sql_type(field): - return None - - -class Boolean(Field): +class Boolean(fields.Boolean): @staticmethod def sql_format(value): @@ -35,148 +14,100 @@ class Boolean(Field): return ('BOOLEAN', 'BOOLEAN') -class Integer(Field): - - @staticmethod - def sql_format(value): - return int(value or 0) +class Integer(fields.Integer): @staticmethod def sql_type(field): return ('INTEGER', 'INTEGER') -class BigInteger(Integer): - - @staticmethod - def sql_type(field): - return ('INTEGER', 'INTEGER') +class BigInteger(fields.BigInteger, Integer): + pass -class Char(Field): - +class Char(fields.Char): @staticmethod def sql_type(field): return ('VARCHAR', 'VARCHAR') -class Sha(Field): - - @staticmethod - def sql_format(value): - if isinstance(value, basestring): - if isinstance(value, unicode): - value = value.encode('utf-8') - if hashlib: - value = hashlib.sha1(value).hexdigest() - else: - value = sha.new(value).hexdigest() - return Field.sql_format(value) +class Sha(fields.Sha): @staticmethod def sql_type(field): return ('VARCHAR', 'VARCHAR(40)') -class Text(Field): +class Text(fields.Text): @staticmethod def sql_type(field): return ('TEXT', 'TEXT') -class Float(Field): - - @staticmethod - def sql_format(value): - return float(value or 0.0) +class Float(fields.Float): @staticmethod def sql_type(field): return ('FLOAT', 'FLOAT') -class Numeric(Float): +class Numeric(fields.Numeric): @staticmethod def sql_type(field): return ('NUMERIC', 'NUMERIC') -class Date(Field): +class Date(fields.Date): @staticmethod def sql_type(field): return ('DATE', 'DATE') -class DateTime(Field): +class DateTime(fields.DateTime): @staticmethod def sql_type(field): return ('TIMESTAMP', 'TIMESTAMP') -class Time(Field): +class Time(fields.Time): @staticmethod def sql_type(field): return ('TIME', 'TIME') -class Binary(Field): - - @staticmethod - def sql_format(value): - return value or None +class Binary(fields.Binary): @staticmethod def sql_type(field): return ('BLOB', 'BLOB') -class Selection(Char): +class Selection(fields.Selection): @staticmethod def sql_type(field): return ('VARCHAR', 'VARCHAR') -class Reference(Field): +class Reference(fields.Reference): @staticmethod def sql_type(field): return ('VARCHAR', 'VARCHAR') -class Many2One(Field): - - @staticmethod - def sql_format(value): - return value and int(value) or None +class Many2One(fields.Many2One): @staticmethod def sql_type(field): return ('INTEGER', 'INTEGER') - -class One2Many(Field): - pass - - -class Many2Many(Field): - pass - - -class Function(Field): - pass - - -class Property(Function): - pass - - FIELDS = { 'boolean': Boolean, 'integer': Integer, @@ -193,8 +124,8 @@ FIELDS = { 'selection': Selection, 'reference': Reference, 'many2one': Many2One, - 'one2many': One2Many, - 'many2many': Many2Many, - 'function': Function, - 'property': Property, + 'one2many': fields.One2Many, + 'many2many': fields.Many2Many, + 'function': fields.Function, + 'property': fields.Property, }