author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 15 Jan 2010 09:36:49 +0100 | |
branch | stable |
changeset 4245 | ff8e355dbc97 |
parent 4212 | ab6573088b4a |
child 4252 | 6c4f109c2b03 |
child 4298 | 2ca56131079e |
permissions | -rw-r--r-- |
0 | 1 |
"""SQL utilities functions and classes. |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4195
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
10 |
import os |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
11 |
from os.path import exists |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
12 |
from warnings import warn |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
13 |
from datetime import datetime, date, timedelta |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
14 |
|
1783 | 15 |
import logilab.common as lgc |
0 | 16 |
from logilab.common.shellutils import ProgressBar |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
17 |
from logilab.common import db |
0 | 18 |
from logilab.common.adbh import get_adv_func_helper |
19 |
from logilab.common.sqlgen import SQLGenerator |
|
20 |
||
21 |
from indexer import get_indexer |
|
22 |
||
23 |
from cubicweb import Binary, ConfigurationError |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
24 |
from cubicweb.utils import todate, todatetime |
0 | 25 |
from cubicweb.common.uilib import remove_html_tags |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
26 |
from cubicweb.toolsutils import restrict_perms_to_user |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2512
diff
changeset
|
27 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 28 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 29 |
from cubicweb.server.utils import crypt_password |
0 | 30 |
|
31 |
||
1783 | 32 |
lgc.USE_MX_DATETIME = False |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
33 |
SQL_PREFIX = 'cw_' |
0 | 34 |
|
35 |
||
3623
9b838e2d72bb
avoid progress bar when test launched from apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
36 |
def sqlexec(sqlstmts, cursor_or_execute, withpb=not os.environ.get('APYCOT_ROOT'), |
9b838e2d72bb
avoid progress bar when test launched from apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
37 |
pbtitle='', delimiter=';'): |
0 | 38 |
"""execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
39 |
error. If a cnx is given, commit at each statement |
|
40 |
""" |
|
41 |
if hasattr(cursor_or_execute, 'execute'): |
|
42 |
execute = cursor_or_execute.execute |
|
43 |
else: |
|
44 |
execute = cursor_or_execute |
|
45 |
sqlstmts = sqlstmts.split(delimiter) |
|
46 |
if withpb: |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
47 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
0 | 48 |
for sql in sqlstmts: |
49 |
sql = sql.strip() |
|
50 |
if withpb: |
|
51 |
pb.update() |
|
52 |
if not sql: |
|
53 |
continue |
|
54 |
# some dbapi modules doesn't accept unicode for sql string |
|
55 |
execute(str(sql)) |
|
56 |
if withpb: |
|
57 |
print |
|
58 |
||
59 |
||
60 |
def sqlgrants(schema, driver, user, |
|
61 |
text_index=True, set_owner=True, |
|
62 |
skip_relations=(), skip_entities=()): |
|
63 |
"""return sql to give all access privileges to the given user on the system |
|
64 |
schema |
|
65 |
""" |
|
66 |
from yams.schema2sql import grant_schema |
|
67 |
from cubicweb.server.sources import native |
|
68 |
output = [] |
|
69 |
w = output.append |
|
70 |
w(native.grant_schema(user, set_owner)) |
|
71 |
w('') |
|
72 |
if text_index: |
|
73 |
indexer = get_indexer(driver) |
|
74 |
w(indexer.sql_grant_user(user)) |
|
75 |
w('') |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
76 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 77 |
return '\n'.join(output) |
78 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
79 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
80 |
def sqlschema(schema, driver, text_index=True, |
0 | 81 |
user=None, set_owner=False, |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2512
diff
changeset
|
82 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 83 |
"""return the system sql schema, according to the given parameters""" |
84 |
from yams.schema2sql import schema2sql |
|
85 |
from cubicweb.server.sources import native |
|
86 |
if set_owner: |
|
87 |
assert user, 'user is argument required when set_owner is true' |
|
88 |
output = [] |
|
89 |
w = output.append |
|
90 |
w(native.sql_schema(driver)) |
|
91 |
w('') |
|
92 |
if text_index: |
|
93 |
indexer = get_indexer(driver) |
|
94 |
w(indexer.sql_init_fti()) |
|
95 |
w('') |
|
96 |
dbhelper = get_adv_func_helper(driver) |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
97 |
w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
0 | 98 |
skip_entities=skip_entities, skip_relations=skip_relations)) |
99 |
if dbhelper.users_support and user: |
|
100 |
w('') |
|
101 |
w(sqlgrants(schema, driver, user, text_index, set_owner, |
|
102 |
skip_relations, skip_entities)) |
|
103 |
return '\n'.join(output) |
|
104 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
105 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
106 |
def sqldropschema(schema, driver, text_index=True, |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2512
diff
changeset
|
107 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 108 |
"""return the sql to drop the schema, according to the given parameters""" |
109 |
from yams.schema2sql import dropschema2sql |
|
110 |
from cubicweb.server.sources import native |
|
111 |
output = [] |
|
112 |
w = output.append |
|
113 |
w(native.sql_drop_schema(driver)) |
|
114 |
w('') |
|
115 |
if text_index: |
|
116 |
indexer = get_indexer(driver) |
|
117 |
w(indexer.sql_drop_fti()) |
|
118 |
w('') |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
119 |
w(dropschema2sql(schema, prefix=SQL_PREFIX, |
1954 | 120 |
skip_entities=skip_entities, |
121 |
skip_relations=skip_relations)) |
|
0 | 122 |
return '\n'.join(output) |
123 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
124 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
125 |
from mx.DateTime import DateTimeType, DateTimeDeltaType |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
126 |
except ImportError: |
1953 | 127 |
DateTimeType = DateTimeDeltaType = None |
0 | 128 |
|
129 |
class SQLAdapterMixIn(object): |
|
130 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
131 |
dictionary and handling connection locking |
|
132 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
133 |
|
0 | 134 |
def __init__(self, source_config): |
135 |
try: |
|
136 |
self.dbdriver = source_config['db-driver'].lower() |
|
137 |
self.dbname = source_config['db-name'] |
|
138 |
except KeyError: |
|
139 |
raise ConfigurationError('missing some expected entries in sources file') |
|
140 |
self.dbhost = source_config.get('db-host') |
|
141 |
port = source_config.get('db-port') |
|
142 |
self.dbport = port and int(port) or None |
|
143 |
self.dbuser = source_config.get('db-user') |
|
144 |
self.dbpasswd = source_config.get('db-password') |
|
145 |
self.encoding = source_config.get('db-encoding', 'UTF-8') |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
146 |
self.dbapi_module = db.get_dbapi_compliant_module(self.dbdriver) |
4177
f0ab2b6d3553
SqlServer: support single sign on / Windows credential authentication
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4176
diff
changeset
|
147 |
self.dbdriver_extra_args = source_config.get('db-extra-arguments') |
0 | 148 |
self.binary = self.dbapi_module.Binary |
149 |
self.dbhelper = self.dbapi_module.adv_func_helper |
|
150 |
self.sqlgen = SQLGenerator() |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
151 |
|
0 | 152 |
def get_connection(self, user=None, password=None): |
153 |
"""open and return a connection to the database""" |
|
154 |
if user or self.dbuser: |
|
155 |
self.info('connecting to %s@%s for user %s', self.dbname, |
|
156 |
self.dbhost or 'localhost', user or self.dbuser) |
|
157 |
else: |
|
158 |
self.info('connecting to %s@%s', self.dbname, |
|
159 |
self.dbhost or 'localhost') |
|
4179
3a0b2f14e17b
only give extra_args if there is something to be fed (breaks some db connectors otherwise)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
160 |
extra = {} |
3a0b2f14e17b
only give extra_args if there is something to be fed (breaks some db connectors otherwise)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
161 |
if self.dbdriver_extra_args: |
3a0b2f14e17b
only give extra_args if there is something to be fed (breaks some db connectors otherwise)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
162 |
extra = {'extra_args': self.dbdriver_extra_args} |
0 | 163 |
cnx = self.dbapi_module.connect(self.dbhost, self.dbname, |
164 |
user or self.dbuser, |
|
165 |
password or self.dbpasswd, |
|
4177
f0ab2b6d3553
SqlServer: support single sign on / Windows credential authentication
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4176
diff
changeset
|
166 |
port=self.dbport, |
4179
3a0b2f14e17b
only give extra_args if there is something to be fed (breaks some db connectors otherwise)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
167 |
**extra) |
0 | 168 |
init_cnx(self.dbdriver, cnx) |
169 |
#self.dbapi_module.type_code_test(cnx.cursor()) |
|
170 |
return cnx |
|
171 |
||
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2739
diff
changeset
|
172 |
def backup_to_file(self, backupfile): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
173 |
cmd = self.dbhelper.backup_command(self.dbname, self.dbhost, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
174 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
175 |
keepownership=False) |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
176 |
if os.system(cmd): |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2739
diff
changeset
|
177 |
raise Exception('Failed command: %s' % cmd) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
178 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
179 |
def restore_from_file(self, backupfile, confirm, drop=True): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
180 |
for cmd in self.dbhelper.restore_commands(self.dbname, self.dbhost, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
181 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
182 |
self.encoding, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
183 |
keepownership=False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
184 |
drop=drop): |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2739
diff
changeset
|
185 |
if os.system(cmd): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
186 |
print '-> Failed command: %s' % cmd |
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
187 |
if not confirm('Continue anyway?', default='n'): |
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
188 |
raise Exception('Failed command: %s' % cmd) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
189 |
|
0 | 190 |
def merge_args(self, args, query_args): |
191 |
if args is not None: |
|
192 |
args = dict(args) |
|
193 |
for key, val in args.items(): |
|
194 |
# convert cubicweb binary into db binary |
|
195 |
if isinstance(val, Binary): |
|
196 |
val = self.binary(val.getvalue()) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
197 |
# XXX <3.2 bw compat |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
198 |
elif type(val) is DateTimeType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
199 |
warn('found mx date time instance, please update to use datetime', |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
200 |
DeprecationWarning) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
201 |
val = datetime(val.year, val.month, val.day, |
1026 | 202 |
val.hour, val.minute, int(val.second)) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
203 |
elif type(val) is DateTimeDeltaType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
204 |
warn('found mx date time instance, please update to use datetime', |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
205 |
DeprecationWarning) |
1026 | 206 |
val = timedelta(0, int(val.seconds), 0) |
0 | 207 |
args[key] = val |
208 |
# should not collide |
|
209 |
args.update(query_args) |
|
210 |
return args |
|
211 |
return query_args |
|
212 |
||
213 |
def process_result(self, cursor): |
|
214 |
"""return a list of CubicWeb compliant values from data in the given cursor |
|
215 |
""" |
|
216 |
descr = cursor.description |
|
217 |
encoding = self.encoding |
|
218 |
process_value = self.dbapi_module.process_value |
|
219 |
binary = Binary |
|
220 |
results = cursor.fetchall() |
|
221 |
for i, line in enumerate(results): |
|
222 |
result = [] |
|
223 |
for col, value in enumerate(line): |
|
224 |
if value is None: |
|
225 |
result.append(value) |
|
226 |
continue |
|
227 |
result.append(process_value(value, descr[col], encoding, binary)) |
|
228 |
results[i] = result |
|
229 |
return results |
|
230 |
||
231 |
||
232 |
def preprocess_entity(self, entity): |
|
233 |
"""return a dictionary to use as extra argument to cursor.execute |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
234 |
to insert/update an entity into a SQL database |
0 | 235 |
""" |
236 |
attrs = {} |
|
237 |
eschema = entity.e_schema |
|
238 |
for attr, value in entity.items(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3623
diff
changeset
|
239 |
rschema = eschema.subjrels[attr] |
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3623
diff
changeset
|
240 |
if rschema.final: |
0 | 241 |
atype = str(entity.e_schema.destination(attr)) |
242 |
if atype == 'Boolean': |
|
243 |
value = self.dbhelper.boolean_value(value) |
|
244 |
elif atype == 'Password': |
|
245 |
# if value is a Binary instance, this mean we got it |
|
246 |
# from a query result and so it is already encrypted |
|
247 |
if isinstance(value, Binary): |
|
248 |
value = value.getvalue() |
|
249 |
else: |
|
250 |
value = crypt_password(value) |
|
4176
42247d70105b
SQL Server port: convert Password to Binary
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
251 |
value = self.binary(value) |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
252 |
# XXX needed for sqlite but I don't think it is for other backends |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
253 |
elif atype == 'Datetime' and isinstance(value, date): |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
254 |
value = todatetime(value) |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
255 |
elif atype == 'Date' and isinstance(value, datetime): |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
256 |
value = todate(value) |
0 | 257 |
elif isinstance(value, Binary): |
258 |
value = self.binary(value.getvalue()) |
|
1408
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
259 |
# XXX <3.2 bw compat |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
260 |
elif type(value) is DateTimeType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
261 |
warn('found mx date time instance, please update to use datetime', |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
262 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
263 |
value = datetime(value.year, value.month, value.day, |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
264 |
value.hour, value.minute, int(value.second)) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
265 |
elif type(value) is DateTimeDeltaType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
266 |
warn('found mx date time instance, please update to use datetime', |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
267 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
268 |
value = timedelta(0, int(value.seconds), 0) |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
269 |
attrs[SQL_PREFIX+str(attr)] = value |
0 | 270 |
return attrs |
271 |
||
272 |
||
273 |
from logging import getLogger |
|
274 |
from cubicweb import set_log_methods |
|
275 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
276 |
||
277 |
def init_sqlite_connexion(cnx): |
|
278 |
# XXX should not be publicly exposed |
|
279 |
#def comma_join(strings): |
|
280 |
# return ', '.join(strings) |
|
281 |
#cnx.create_function("COMMA_JOIN", 1, comma_join) |
|
282 |
||
283 |
class concat_strings(object): |
|
284 |
def __init__(self): |
|
285 |
self.values = [] |
|
286 |
def step(self, value): |
|
287 |
if value is not None: |
|
288 |
self.values.append(value) |
|
289 |
def finalize(self): |
|
290 |
return ', '.join(self.values) |
|
291 |
# renamed to GROUP_CONCAT in cubicweb 2.45, keep old name for bw compat for |
|
292 |
# some time |
|
293 |
cnx.create_aggregate("CONCAT_STRINGS", 1, concat_strings) |
|
294 |
cnx.create_aggregate("GROUP_CONCAT", 1, concat_strings) |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
295 |
|
0 | 296 |
def _limit_size(text, maxsize, format='text/plain'): |
297 |
if len(text) < maxsize: |
|
298 |
return text |
|
299 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
300 |
text = remove_html_tags(text) |
|
301 |
if len(text) > maxsize: |
|
302 |
text = text[:maxsize] + '...' |
|
303 |
return text |
|
304 |
||
305 |
def limit_size3(text, format, maxsize): |
|
306 |
return _limit_size(text, maxsize, format) |
|
307 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
308 |
||
309 |
def limit_size2(text, maxsize): |
|
310 |
return _limit_size(text, maxsize) |
|
311 |
cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2) |
|
312 |
import yams.constraints |
|
313 |
if hasattr(yams.constraints, 'patch_sqlite_decimal'): |
|
314 |
yams.constraints.patch_sqlite_decimal() |
|
315 |
||
316 |
||
317 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
318 |
sqlite_hooks.append(init_sqlite_connexion) |
|
319 |
||
320 |
def init_cnx(driver, cnx): |
|
321 |
for hook in SQL_CONNECT_HOOKS.get(driver, ()): |
|
322 |
hook(cnx) |