diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-08-09 23:46:06 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-08-09 23:46:06 +0000 |
| commit | d564baf593311ca5bc62dbc401e57986bf9169ba (patch) | |
| tree | 11a278cb89481ca6d775971ff6ee07c2701f8008 /lib/sqlalchemy | |
| parent | f0d2e599b60317fc27f64f0abe7d2af65fba7e7b (diff) | |
| download | sqlalchemy-d564baf593311ca5bc62dbc401e57986bf9169ba.tar.gz | |
- the Oracle dialect now features NUMBER which intends
to act justlike Oracle's NUMBER type. It is the primary
numeric type returned by table reflection and attempts
to return Decimal()/float/int based on the precision/scale
parameters. [ticket:885]
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/dialects/oracle/base.py | 51 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/oracle/cx_oracle.py | 1 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 2 |
3 files changed, 35 insertions, 19 deletions
diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index 17b09e79c..a5ced0738 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -132,14 +132,26 @@ class NCLOB(sqltypes.Text): VARCHAR2 = VARCHAR NVARCHAR2 = NVARCHAR -class NUMBER(sqltypes.Numeric): + +class NUMBER(sqltypes.Numeric, sqltypes.Integer): __visit_name__ = 'NUMBER' -class BFILE(sqltypes.Binary): - __visit_name__ = 'BFILE' - + def __init__(self, precision=None, scale=None, asdecimal=None): + if asdecimal is None: + asdecimal = bool(scale and scale > 0) + + super(NUMBER, self).__init__(precision=precision, scale=scale, asdecimal=asdecimal) + class DOUBLE_PRECISION(sqltypes.Numeric): __visit_name__ = 'DOUBLE_PRECISION' + def __init__(self, precision=None, scale=None, asdecimal=None): + if asdecimal is None: + asdecimal = False + + super(DOUBLE_PRECISION, self).__init__(precision=precision, scale=scale, asdecimal=asdecimal) + +class BFILE(sqltypes.Binary): + __visit_name__ = 'BFILE' class LONG(sqltypes.Text): __visit_name__ = 'LONG' @@ -200,13 +212,24 @@ class OracleTypeCompiler(compiler.GenericTypeCompiler): return self.visit_DATE(type_) def visit_float(self, type_): - if type_.precision is None: - return "NUMERIC" - else: - return "NUMERIC(%(precision)s, %(scale)s)" % {'precision': type_.precision, 'scale' : 2} + return self.visit_FLOAT(type_) def visit_unicode(self, type_): return self.visit_NVARCHAR(type_) + + def visit_DOUBLE_PRECISION(self, type_): + return self._generate_numeric(type_, "DOUBLE PRECISION") + + def visit_NUMBER(self, type_): + return self._generate_numeric(type_, "NUMBER") + + def _generate_numeric(self, type_, name): + if type_.precision is None: + return name + elif type_.scale is None: + return "%(name)s(%(precision)s)" % {'name':name,'precision': type_.precision} + else: + return "%(name)s(%(precision)s, %(scale)s)" % {'name':name,'precision': type_.precision, 'scale' : type_.scale} def visit_VARCHAR(self, type_): return "VARCHAR(%(length)s)" % {'length' : type_.length} @@ -658,18 +681,8 @@ class OracleDialect(default.DefaultDialect): (colname, coltype, length, precision, scale, nullable, default) = \ (self.normalize_name(row[0]), row[1], row[2], row[3], row[4], row[5]=='Y', row[6]) - # INTEGER if the scale is 0 and precision is null - # NUMBER if the scale and precision are both null - # NUMBER(9,2) if the precision is 9 and the scale is 2 - # NUMBER(3) if the precision is 3 and scale is 0 - #length is ignored except for CHAR and VARCHAR2 if coltype == 'NUMBER' : - if precision is None and scale is None: - coltype = sqltypes.NUMERIC - elif precision is None and scale == 0: - coltype = sqltypes.INTEGER - else : - coltype = sqltypes.NUMERIC(precision, scale) + coltype = NUMBER(precision, scale) elif coltype=='CHAR' or coltype=='VARCHAR2': coltype = self.ischema_names.get(coltype)(length) else: diff --git a/lib/sqlalchemy/dialects/oracle/cx_oracle.py b/lib/sqlalchemy/dialects/oracle/cx_oracle.py index 475d6559a..f40923591 100644 --- a/lib/sqlalchemy/dialects/oracle/cx_oracle.py +++ b/lib/sqlalchemy/dialects/oracle/cx_oracle.py @@ -178,6 +178,7 @@ colspecs = { sqltypes.TIMESTAMP : _OracleTimestamp, sqltypes.Integer : _OracleInteger, # this is only needed for OUT parameters. # it would be nice if we could not use it otherwise. + oracle.NUMBER : oracle.NUMBER, # don't let this get converted oracle.RAW: _OracleRaw, } diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index d6187bcde..403ec968b 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1149,6 +1149,8 @@ class GenericTypeCompiler(engine.TypeCompiler): def visit_NUMERIC(self, type_): if type_.precision is None: return "NUMERIC" + elif type_.scale is None: + return "NUMERIC(%(precision)s)" % {'precision': type_.precision} else: return "NUMERIC(%(precision)s, %(scale)s)" % {'precision': type_.precision, 'scale' : type_.scale} |
