| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
import logging |
| 6 |
|
| 7 |
|
| 8 |
from petl.compat import callable |
| 9 |
|
| 10 |
|
| 11 |
logger = logging.getLogger(__name__) |
| 12 |
debug = logger.debug |
| 13 |
|
| 14 |
|
| 15 |
def _is_dbapi_connection(dbo): |
| 16 |
return _hasmethod(dbo, 'cursor') |
| 17 |
|
| 18 |
|
| 19 |
def _is_clikchouse_dbapi_connection(dbo): |
| 20 |
return 'clickhouse_driver' in str(type(dbo)) |
| 21 |
|
| 22 |
|
| 23 |
def _is_dbapi_cursor(dbo): |
| 24 |
return _hasmethods(dbo, 'execute', 'executemany', 'fetchone', 'fetchmany', |
| 25 |
'fetchall') |
| 26 |
|
| 27 |
|
| 28 |
def _is_sqlalchemy_engine(dbo): |
| 29 |
return (_hasmethods(dbo, 'execute', 'connect', 'raw_connection') |
| 30 |
and _hasprop(dbo, 'driver')) |
| 31 |
|
| 32 |
|
| 33 |
def _is_sqlalchemy_session(dbo): |
| 34 |
return _hasmethods(dbo, 'execute', 'connection', 'get_bind') |
| 35 |
|
| 36 |
|
| 37 |
def _is_sqlalchemy_connection(dbo): |
| 38 |
# N.B., this are not completely selective conditions, this test needs |
| 39 |
# to be applied after ruling out DB-API cursor |
| 40 |
return _hasmethod(dbo, 'execute') and _hasprop(dbo, 'connection') |
| 41 |
|
| 42 |
|
| 43 |
def _hasmethod(o, n): |
| 44 |
return hasattr(o, n) and callable(getattr(o, n)) |
| 45 |
|
| 46 |
|
| 47 |
def _hasmethods(o, *l): |
| 48 |
return all(_hasmethod(o, n) for n in l) |
| 49 |
|
| 50 |
|
| 51 |
def _hasprop(o, n): |
| 52 |
return hasattr(o, n) and not callable(getattr(o, n)) |
| 53 |
|
| 54 |
|
| 55 |
# default DB quote char per SQL-92 |
| 56 |
quotechar = '"' |
| 57 |
|
| 58 |
|
| 59 |
def _quote(s): |
| 60 |
# crude way to sanitise table and field names |
| 61 |
# conform with the SQL-92 standard. See http://stackoverflow.com/a/214344 |
| 62 |
return quotechar + s.replace(quotechar, quotechar+quotechar) + quotechar |
| 63 |
|
| 64 |
|
| 65 |
def _placeholders(connection, names): |
| 66 |
# discover the paramstyle |
| 67 |
if connection is None: |
| 68 |
# default to using question mark |
| 69 |
debug('connection is None, default to using qmark paramstyle') |
| 70 |
placeholders = ', '.join(['?'] * len(names)) |
| 71 |
else: |
| 72 |
mod = __import__(connection.__class__.__module__) |
| 73 |
|
| 74 |
if not hasattr(mod, 'paramstyle'): |
| 75 |
debug('module %r from connection %r has no attribute paramstyle, ' |
| 76 |
'defaulting to qmark', mod, connection) |
| 77 |
# default to using question mark |
| 78 |
placeholders = ', '.join(['?'] * len(names)) |
| 79 |
|
| 80 |
elif mod.paramstyle == 'qmark': |
| 81 |
debug('found paramstyle qmark') |
| 82 |
placeholders = ', '.join(['?'] * len(names)) |
| 83 |
|
| 84 |
elif mod.paramstyle in ('format', 'pyformat'): |
| 85 |
debug('found paramstyle pyformat') |
| 86 |
placeholders = ', '.join(['%s'] * len(names)) |
| 87 |
|
| 88 |
elif mod.paramstyle == 'numeric': |
| 89 |
debug('found paramstyle numeric') |
| 90 |
placeholders = ', '.join([':' + str(i + 1) |
| 91 |
for i in range(len(names))]) |
| 92 |
|
| 93 |
elif mod.paramstyle == 'named': |
| 94 |
debug('found paramstyle named') |
| 95 |
placeholders = ', '.join([':%s' % name |
| 96 |
for name in names]) |
| 97 |
|
| 98 |
else: |
| 99 |
debug('found unexpected paramstyle %r, defaulting to qmark', |
| 100 |
mod.paramstyle) |
| 101 |
placeholders = ', '.join(['?'] * len(names)) |
| 102 |
|
| 103 |
return placeholders |