1 """SQL utilities functions and classes. |
1 """SQL utilities functions and classes. |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
|
9 from warnings import warn |
|
10 from datetime import datetime, timedelta |
|
11 |
9 from logilab.common.shellutils import ProgressBar |
12 from logilab.common.shellutils import ProgressBar |
10 from logilab.common.db import get_dbapi_compliant_module |
13 from logilab.common import db |
11 from logilab.common.adbh import get_adv_func_helper |
14 from logilab.common.adbh import get_adv_func_helper |
12 from logilab.common.sqlgen import SQLGenerator |
15 from logilab.common.sqlgen import SQLGenerator |
13 |
16 |
14 from indexer import get_indexer |
17 from indexer import get_indexer |
15 |
18 |
16 from cubicweb import Binary, ConfigurationError |
19 from cubicweb import Binary, ConfigurationError |
17 from cubicweb.common.uilib import remove_html_tags |
20 from cubicweb.common.uilib import remove_html_tags |
18 from cubicweb.server import SQL_CONNECT_HOOKS |
21 from cubicweb.server import SQL_CONNECT_HOOKS |
19 from cubicweb.server.utils import crypt_password, cartesian_product |
22 from cubicweb.server.utils import crypt_password, cartesian_product |
20 |
23 |
|
24 db.USE_MX_DATETIME = False |
21 |
25 |
22 def sqlexec(sqlstmts, cursor_or_execute, withpb=True, delimiter=';'): |
26 def sqlexec(sqlstmts, cursor_or_execute, withpb=True, delimiter=';'): |
23 """execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
27 """execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
24 error. If a cnx is given, commit at each statement |
28 error. If a cnx is given, commit at each statement |
25 """ |
29 """ |
103 w('') |
107 w('') |
104 w(dropschema2sql(schema, |
108 w(dropschema2sql(schema, |
105 skip_entities=skip_entities, skip_relations=skip_relations)) |
109 skip_entities=skip_entities, skip_relations=skip_relations)) |
106 return '\n'.join(output) |
110 return '\n'.join(output) |
107 |
111 |
108 |
112 try: |
|
113 from mx.DateTime import DateTimeType, DateTimeDeltaType |
|
114 except ImportError: |
|
115 DateTimeType, DateTimeDeltaType = None |
109 |
116 |
110 class SQLAdapterMixIn(object): |
117 class SQLAdapterMixIn(object): |
111 """Mixin for SQL data sources, getting a connection from a configuration |
118 """Mixin for SQL data sources, getting a connection from a configuration |
112 dictionary and handling connection locking |
119 dictionary and handling connection locking |
113 """ |
120 """ |
122 port = source_config.get('db-port') |
129 port = source_config.get('db-port') |
123 self.dbport = port and int(port) or None |
130 self.dbport = port and int(port) or None |
124 self.dbuser = source_config.get('db-user') |
131 self.dbuser = source_config.get('db-user') |
125 self.dbpasswd = source_config.get('db-password') |
132 self.dbpasswd = source_config.get('db-password') |
126 self.encoding = source_config.get('db-encoding', 'UTF-8') |
133 self.encoding = source_config.get('db-encoding', 'UTF-8') |
127 self.dbapi_module = get_dbapi_compliant_module(self.dbdriver) |
134 self.dbapi_module = db.get_dbapi_compliant_module(self.dbdriver) |
128 self.binary = self.dbapi_module.Binary |
135 self.binary = self.dbapi_module.Binary |
129 self.dbhelper = self.dbapi_module.adv_func_helper |
136 self.dbhelper = self.dbapi_module.adv_func_helper |
130 self.sqlgen = SQLGenerator() |
137 self.sqlgen = SQLGenerator() |
131 |
138 |
132 def get_connection(self, user=None, password=None): |
139 def get_connection(self, user=None, password=None): |
150 args = dict(args) |
157 args = dict(args) |
151 for key, val in args.items(): |
158 for key, val in args.items(): |
152 # convert cubicweb binary into db binary |
159 # convert cubicweb binary into db binary |
153 if isinstance(val, Binary): |
160 if isinstance(val, Binary): |
154 val = self.binary(val.getvalue()) |
161 val = self.binary(val.getvalue()) |
|
162 # XXX <3.2 bw compat |
|
163 elif type(val) is DateTimeType: |
|
164 warn('found mx date time instance, please update to use datetime', |
|
165 DeprecationWarning) |
|
166 val = datetime(val.year, val.month, val.day, |
|
167 val.hour, val.minute, val.second) |
|
168 elif type(val) is DateTimeDeltaType: |
|
169 warn('found mx date time instance, please update to use datetime', |
|
170 DeprecationWarning) |
|
171 val = timedelta(0, val.seconds, 0) |
155 args[key] = val |
172 args[key] = val |
156 # should not collide |
173 # should not collide |
157 args.update(query_args) |
174 args.update(query_args) |
158 return args |
175 return args |
159 return query_args |
176 return query_args |