dibbler/sqlalchemy/dialects/oracle/zxjdbc.py

236 lines
7.9 KiB
Python
Raw Normal View History

2017-04-15 18:33:29 +02:00
# oracle/zxjdbc.py
# Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
# <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
2010-05-07 19:33:49 +02:00
2017-04-15 18:33:29 +02:00
"""
.. dialect:: oracle+zxjdbc
:name: zxJDBC for Jython
:dbapi: zxjdbc
:connectstring: oracle+zxjdbc://user:pass@host/dbname
:driverurl: http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
2010-05-07 19:33:49 +02:00
2017-04-15 18:33:29 +02:00
.. note:: Jython is not supported by current versions of SQLAlchemy. The
zxjdbc dialect should be considered as experimental.
2010-05-07 19:33:49 +02:00
"""
import decimal
import re
from sqlalchemy import sql, types as sqltypes, util
from sqlalchemy.connectors.zxJDBC import ZxJDBCConnector
2017-04-15 18:33:29 +02:00
from sqlalchemy.dialects.oracle.base import (OracleCompiler,
OracleDialect,
OracleExecutionContext)
from sqlalchemy.engine import result as _result
2010-05-07 19:33:49 +02:00
from sqlalchemy.sql import expression
2017-04-15 18:33:29 +02:00
import collections
2010-05-07 19:33:49 +02:00
SQLException = zxJDBC = None
2017-04-15 18:33:29 +02:00
2010-05-07 19:33:49 +02:00
class _ZxJDBCDate(sqltypes.Date):
def result_processor(self, dialect, coltype):
def process(value):
if value is None:
return None
else:
return value.date()
return process
class _ZxJDBCNumeric(sqltypes.Numeric):
def result_processor(self, dialect, coltype):
2017-04-15 18:33:29 +02:00
# XXX: does the dialect return Decimal or not???
2010-05-07 19:33:49 +02:00
# if it does (in all cases), we could use a None processor as well as
# the to_float generic processor
if self.asdecimal:
def process(value):
if isinstance(value, decimal.Decimal):
return value
else:
return decimal.Decimal(str(value))
else:
def process(value):
if isinstance(value, decimal.Decimal):
return float(value)
else:
return value
return process
class OracleCompiler_zxjdbc(OracleCompiler):
def returning_clause(self, stmt, returning_cols):
2017-04-15 18:33:29 +02:00
self.returning_cols = list(
expression._select_iterables(returning_cols))
2010-05-07 19:33:49 +02:00
# within_columns_clause=False so that labels (foo AS bar) don't render
2017-04-15 18:33:29 +02:00
columns = [self.process(c, within_columns_clause=False)
2010-05-07 19:33:49 +02:00
for c in self.returning_cols]
if not hasattr(self, 'returning_parameters'):
self.returning_parameters = []
binds = []
for i, col in enumerate(self.returning_cols):
2017-04-15 18:33:29 +02:00
dbtype = col.type.dialect_impl(
self.dialect).get_dbapi_type(self.dialect.dbapi)
2010-05-07 19:33:49 +02:00
self.returning_parameters.append((i + 1, dbtype))
2017-04-15 18:33:29 +02:00
bindparam = sql.bindparam(
"ret_%d" % i, value=ReturningParam(dbtype))
2010-05-07 19:33:49 +02:00
self.binds[bindparam.key] = bindparam
2017-04-15 18:33:29 +02:00
binds.append(
self.bindparam_string(self._truncate_bindparam(bindparam)))
2010-05-07 19:33:49 +02:00
2017-04-15 18:33:29 +02:00
return 'RETURNING ' + ', '.join(columns) + " INTO " + ", ".join(binds)
2010-05-07 19:33:49 +02:00
class OracleExecutionContext_zxjdbc(OracleExecutionContext):
def pre_exec(self):
if hasattr(self.compiled, 'returning_parameters'):
# prepare a zxJDBC statement so we can grab its underlying
# OraclePreparedStatement's getReturnResultSet later
self.statement = self.cursor.prepare(self.statement)
def get_result_proxy(self):
if hasattr(self.compiled, 'returning_parameters'):
rrs = None
try:
try:
rrs = self.statement.__statement__.getReturnResultSet()
2017-04-15 18:33:29 +02:00
next(rrs)
except SQLException as sqle:
msg = '%s [SQLCode: %d]' % (
sqle.getMessage(), sqle.getErrorCode())
2010-05-07 19:33:49 +02:00
if sqle.getSQLState() is not None:
msg += ' [SQLState: %s]' % sqle.getSQLState()
raise zxJDBC.Error(msg)
else:
2017-04-15 18:33:29 +02:00
row = tuple(
self.cursor.datahandler.getPyObject(
rrs, index, dbtype)
for index, dbtype in
self.compiled.returning_parameters)
2010-05-07 19:33:49 +02:00
return ReturningResultProxy(self, row)
finally:
if rrs is not None:
try:
rrs.close()
except SQLException:
pass
self.statement.close()
2017-04-15 18:33:29 +02:00
return _result.ResultProxy(self)
2010-05-07 19:33:49 +02:00
def create_cursor(self):
2017-04-15 18:33:29 +02:00
cursor = self._dbapi_connection.cursor()
2010-05-07 19:33:49 +02:00
cursor.datahandler = self.dialect.DataHandler(cursor.datahandler)
return cursor
2017-04-15 18:33:29 +02:00
class ReturningResultProxy(_result.FullyBufferedResultProxy):
2010-05-07 19:33:49 +02:00
"""ResultProxy backed by the RETURNING ResultSet results."""
def __init__(self, context, returning_row):
self._returning_row = returning_row
super(ReturningResultProxy, self).__init__(context)
def _cursor_description(self):
ret = []
for c in self.context.compiled.returning_cols:
if hasattr(c, 'name'):
ret.append((c.name, c.type))
else:
ret.append((c.anon_label, c.type))
return ret
def _buffer_rows(self):
2017-04-15 18:33:29 +02:00
return collections.deque([self._returning_row])
2010-05-07 19:33:49 +02:00
class ReturningParam(object):
"""A bindparam value representing a RETURNING parameter.
Specially handled by OracleReturningDataHandler.
"""
def __init__(self, type):
self.type = type
def __eq__(self, other):
if isinstance(other, ReturningParam):
return self.type == other.type
return NotImplemented
def __ne__(self, other):
if isinstance(other, ReturningParam):
return self.type != other.type
return NotImplemented
def __repr__(self):
kls = self.__class__
2017-04-15 18:33:29 +02:00
return '<%s.%s object at 0x%x type=%s>' % (
kls.__module__, kls.__name__, id(self), self.type)
2010-05-07 19:33:49 +02:00
class OracleDialect_zxjdbc(ZxJDBCConnector, OracleDialect):
jdbc_db_name = 'oracle'
jdbc_driver_name = 'oracle.jdbc.OracleDriver'
statement_compiler = OracleCompiler_zxjdbc
execution_ctx_cls = OracleExecutionContext_zxjdbc
colspecs = util.update_copy(
OracleDialect.colspecs,
{
2017-04-15 18:33:29 +02:00
sqltypes.Date: _ZxJDBCDate,
2010-05-07 19:33:49 +02:00
sqltypes.Numeric: _ZxJDBCNumeric
}
)
def __init__(self, *args, **kwargs):
super(OracleDialect_zxjdbc, self).__init__(*args, **kwargs)
global SQLException, zxJDBC
from java.sql import SQLException
from com.ziclix.python.sql import zxJDBC
from com.ziclix.python.sql.handler import OracleDataHandler
2017-04-15 18:33:29 +02:00
class OracleReturningDataHandler(OracleDataHandler):
2010-05-07 19:33:49 +02:00
"""zxJDBC DataHandler that specially handles ReturningParam."""
def setJDBCObject(self, statement, index, object, dbtype=None):
if type(object) is ReturningParam:
statement.registerReturnParameter(index, object.type)
elif dbtype is None:
2017-04-15 18:33:29 +02:00
OracleDataHandler.setJDBCObject(
self, statement, index, object)
2010-05-07 19:33:49 +02:00
else:
2017-04-15 18:33:29 +02:00
OracleDataHandler.setJDBCObject(
self, statement, index, object, dbtype)
2010-05-07 19:33:49 +02:00
self.DataHandler = OracleReturningDataHandler
def initialize(self, connection):
super(OracleDialect_zxjdbc, self).initialize(connection)
2017-04-15 18:33:29 +02:00
self.implicit_returning = \
connection.connection.driverversion >= '10.2'
2010-05-07 19:33:49 +02:00
def _create_jdbc_url(self, url):
2017-04-15 18:33:29 +02:00
return 'jdbc:oracle:thin:@%s:%s:%s' % (
url.host, url.port or 1521, url.database)
2010-05-07 19:33:49 +02:00
def _get_server_version_info(self, connection):
2017-04-15 18:33:29 +02:00
version = re.search(
r'Release ([\d\.]+)', connection.connection.dbversion).group(1)
2010-05-07 19:33:49 +02:00
return tuple(int(x) for x in version.split('.'))
dialect = OracleDialect_zxjdbc