author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 27 Jan 2010 10:17:27 +0100 | |
changeset 4397 | fbac86ca7af1 |
parent 4353 | 7db69db4913c |
child 4436 | 294e084f1263 |
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 |
4298
2ca56131079e
enable cubicweb-ctl db-dump and db-restore on Windows with SQL Server
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
11 |
import subprocess |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
12 |
from os.path import exists |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
13 |
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
|
14 |
from datetime import datetime, date, timedelta |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
15 |
|
1783 | 16 |
import logilab.common as lgc |
0 | 17 |
from logilab.common.shellutils import ProgressBar |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
18 |
from logilab.common import db |
0 | 19 |
from logilab.common.adbh import get_adv_func_helper |
20 |
from logilab.common.sqlgen import SQLGenerator |
|
21 |
||
22 |
from indexer import get_indexer |
|
23 |
||
24 |
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
|
25 |
from cubicweb.utils import todate, todatetime |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
26 |
from cubicweb.uilib import remove_html_tags |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
27 |
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
|
28 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 29 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 30 |
from cubicweb.server.utils import crypt_password |
0 | 31 |
|
32 |
||
1783 | 33 |
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
|
34 |
SQL_PREFIX = 'cw_' |
0 | 35 |
|
4353
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
36 |
def _run_command(cmd): |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
37 |
"""backup/restore command are string w/ lgc < 0.47, lists with earlier versions |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
38 |
""" |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
39 |
if isinstance(cmd, basestring): |
4353
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
40 |
print '->', cmd |
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
41 |
return subprocess.call(cmd, shell=True) |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
42 |
print ' '.join(cmd) |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
43 |
return subprocess.call(cmd) |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
44 |
|
0 | 45 |
|
3623
9b838e2d72bb
avoid progress bar when test launched from apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
46 |
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
|
47 |
pbtitle='', delimiter=';'): |
0 | 48 |
"""execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
49 |
error. If a cnx is given, commit at each statement |
|
50 |
""" |
|
51 |
if hasattr(cursor_or_execute, 'execute'): |
|
52 |
execute = cursor_or_execute.execute |
|
53 |
else: |
|
54 |
execute = cursor_or_execute |
|
55 |
sqlstmts = sqlstmts.split(delimiter) |
|
56 |
if withpb: |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
57 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
0 | 58 |
for sql in sqlstmts: |
59 |
sql = sql.strip() |
|
60 |
if withpb: |
|
61 |
pb.update() |
|
62 |
if not sql: |
|
63 |
continue |
|
64 |
# some dbapi modules doesn't accept unicode for sql string |
|
65 |
execute(str(sql)) |
|
66 |
if withpb: |
|
67 |
print |
|
68 |
||
69 |
||
70 |
def sqlgrants(schema, driver, user, |
|
71 |
text_index=True, set_owner=True, |
|
72 |
skip_relations=(), skip_entities=()): |
|
73 |
"""return sql to give all access privileges to the given user on the system |
|
74 |
schema |
|
75 |
""" |
|
76 |
from yams.schema2sql import grant_schema |
|
77 |
from cubicweb.server.sources import native |
|
78 |
output = [] |
|
79 |
w = output.append |
|
80 |
w(native.grant_schema(user, set_owner)) |
|
81 |
w('') |
|
82 |
if text_index: |
|
83 |
indexer = get_indexer(driver) |
|
84 |
w(indexer.sql_grant_user(user)) |
|
85 |
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
|
86 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 87 |
return '\n'.join(output) |
88 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
89 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
90 |
def sqlschema(schema, driver, text_index=True, |
0 | 91 |
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
|
92 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 93 |
"""return the system sql schema, according to the given parameters""" |
94 |
from yams.schema2sql import schema2sql |
|
95 |
from cubicweb.server.sources import native |
|
96 |
if set_owner: |
|
97 |
assert user, 'user is argument required when set_owner is true' |
|
98 |
output = [] |
|
99 |
w = output.append |
|
100 |
w(native.sql_schema(driver)) |
|
101 |
w('') |
|
102 |
if text_index: |
|
103 |
indexer = get_indexer(driver) |
|
104 |
w(indexer.sql_init_fti()) |
|
105 |
w('') |
|
106 |
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
|
107 |
w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
0 | 108 |
skip_entities=skip_entities, skip_relations=skip_relations)) |
109 |
if dbhelper.users_support and user: |
|
110 |
w('') |
|
111 |
w(sqlgrants(schema, driver, user, text_index, set_owner, |
|
112 |
skip_relations, skip_entities)) |
|
113 |
return '\n'.join(output) |
|
114 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
115 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
116 |
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
|
117 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 118 |
"""return the sql to drop the schema, according to the given parameters""" |
119 |
from yams.schema2sql import dropschema2sql |
|
120 |
from cubicweb.server.sources import native |
|
121 |
output = [] |
|
122 |
w = output.append |
|
123 |
w(native.sql_drop_schema(driver)) |
|
124 |
w('') |
|
125 |
if text_index: |
|
126 |
indexer = get_indexer(driver) |
|
127 |
w(indexer.sql_drop_fti()) |
|
128 |
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
|
129 |
w(dropschema2sql(schema, prefix=SQL_PREFIX, |
1954 | 130 |
skip_entities=skip_entities, |
131 |
skip_relations=skip_relations)) |
|
0 | 132 |
return '\n'.join(output) |
133 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
134 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
135 |
from mx.DateTime import DateTimeType, DateTimeDeltaType |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
136 |
except ImportError: |
1953 | 137 |
DateTimeType = DateTimeDeltaType = None |
0 | 138 |
|
139 |
class SQLAdapterMixIn(object): |
|
140 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
141 |
dictionary and handling connection locking |
|
142 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
143 |
|
0 | 144 |
def __init__(self, source_config): |
145 |
try: |
|
146 |
self.dbdriver = source_config['db-driver'].lower() |
|
147 |
self.dbname = source_config['db-name'] |
|
148 |
except KeyError: |
|
149 |
raise ConfigurationError('missing some expected entries in sources file') |
|
150 |
self.dbhost = source_config.get('db-host') |
|
151 |
port = source_config.get('db-port') |
|
152 |
self.dbport = port and int(port) or None |
|
153 |
self.dbuser = source_config.get('db-user') |
|
154 |
self.dbpasswd = source_config.get('db-password') |
|
155 |
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
|
156 |
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
|
157 |
self.dbdriver_extra_args = source_config.get('db-extra-arguments') |
0 | 158 |
self.binary = self.dbapi_module.Binary |
159 |
self.dbhelper = self.dbapi_module.adv_func_helper |
|
160 |
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
|
161 |
|
0 | 162 |
def get_connection(self, user=None, password=None): |
163 |
"""open and return a connection to the database""" |
|
164 |
if user or self.dbuser: |
|
165 |
self.info('connecting to %s@%s for user %s', self.dbname, |
|
166 |
self.dbhost or 'localhost', user or self.dbuser) |
|
167 |
else: |
|
168 |
self.info('connecting to %s@%s', self.dbname, |
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
extra = {'extra_args': self.dbdriver_extra_args} |
0 | 173 |
cnx = self.dbapi_module.connect(self.dbhost, self.dbname, |
174 |
user or self.dbuser, |
|
175 |
password or self.dbpasswd, |
|
4177
f0ab2b6d3553
SqlServer: support single sign on / Windows credential authentication
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4176
diff
changeset
|
176 |
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
|
177 |
**extra) |
0 | 178 |
init_cnx(self.dbdriver, cnx) |
179 |
#self.dbapi_module.type_code_test(cnx.cursor()) |
|
180 |
return cnx |
|
181 |
||
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2739
diff
changeset
|
182 |
def backup_to_file(self, backupfile): |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
183 |
for cmd in self.dbhelper.backup_commands(self.dbname, self.dbhost, |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
184 |
self.dbuser, backupfile, |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
185 |
keepownership=False): |
4353
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
186 |
if _run_command(cmd): |
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
187 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
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 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
193 |
self.encoding, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
194 |
keepownership=False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
195 |
drop=drop): |
4353
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
196 |
if _run_command(cmd): |
7db69db4913c
command may now officially be either a string or a list, don't make think it's for backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4342
diff
changeset
|
197 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
198 |
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
|
199 |
|
0 | 200 |
def merge_args(self, args, query_args): |
201 |
if args is not None: |
|
202 |
args = dict(args) |
|
203 |
for key, val in args.items(): |
|
204 |
# convert cubicweb binary into db binary |
|
205 |
if isinstance(val, Binary): |
|
206 |
val = self.binary(val.getvalue()) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
207 |
# XXX <3.2 bw compat |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
208 |
elif type(val) is DateTimeType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
209 |
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
|
210 |
DeprecationWarning) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
211 |
val = datetime(val.year, val.month, val.day, |
1026 | 212 |
val.hour, val.minute, int(val.second)) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
213 |
elif type(val) is DateTimeDeltaType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
214 |
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
|
215 |
DeprecationWarning) |
1026 | 216 |
val = timedelta(0, int(val.seconds), 0) |
0 | 217 |
args[key] = val |
218 |
# should not collide |
|
219 |
args.update(query_args) |
|
220 |
return args |
|
221 |
return query_args |
|
222 |
||
223 |
def process_result(self, cursor): |
|
224 |
"""return a list of CubicWeb compliant values from data in the given cursor |
|
225 |
""" |
|
226 |
descr = cursor.description |
|
227 |
encoding = self.encoding |
|
228 |
process_value = self.dbapi_module.process_value |
|
229 |
binary = Binary |
|
230 |
results = cursor.fetchall() |
|
231 |
for i, line in enumerate(results): |
|
232 |
result = [] |
|
233 |
for col, value in enumerate(line): |
|
234 |
if value is None: |
|
235 |
result.append(value) |
|
236 |
continue |
|
237 |
result.append(process_value(value, descr[col], encoding, binary)) |
|
238 |
results[i] = result |
|
239 |
return results |
|
240 |
||
241 |
def preprocess_entity(self, entity): |
|
242 |
"""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
|
243 |
to insert/update an entity into a SQL database |
0 | 244 |
""" |
245 |
attrs = {} |
|
246 |
eschema = entity.e_schema |
|
247 |
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
|
248 |
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
|
249 |
if rschema.final: |
0 | 250 |
atype = str(entity.e_schema.destination(attr)) |
251 |
if atype == 'Boolean': |
|
252 |
value = self.dbhelper.boolean_value(value) |
|
253 |
elif atype == 'Password': |
|
254 |
# if value is a Binary instance, this mean we got it |
|
255 |
# from a query result and so it is already encrypted |
|
256 |
if isinstance(value, Binary): |
|
257 |
value = value.getvalue() |
|
258 |
else: |
|
259 |
value = crypt_password(value) |
|
4176
42247d70105b
SQL Server port: convert Password to Binary
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
260 |
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
|
261 |
# 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
|
262 |
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
|
263 |
value = todatetime(value) |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
264 |
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
|
265 |
value = todate(value) |
0 | 266 |
elif isinstance(value, Binary): |
267 |
value = self.binary(value.getvalue()) |
|
1408
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
268 |
# XXX <3.2 bw compat |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
269 |
elif type(value) is DateTimeType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
270 |
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
|
271 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
272 |
value = datetime(value.year, value.month, value.day, |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
273 |
value.hour, value.minute, int(value.second)) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
274 |
elif type(value) is DateTimeDeltaType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
275 |
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
|
276 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
277 |
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
|
278 |
attrs[SQL_PREFIX+str(attr)] = value |
0 | 279 |
return attrs |
280 |
||
281 |
||
282 |
from logging import getLogger |
|
283 |
from cubicweb import set_log_methods |
|
284 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
285 |
||
286 |
def init_sqlite_connexion(cnx): |
|
287 |
# XXX should not be publicly exposed |
|
288 |
#def comma_join(strings): |
|
289 |
# return ', '.join(strings) |
|
290 |
#cnx.create_function("COMMA_JOIN", 1, comma_join) |
|
291 |
||
292 |
class concat_strings(object): |
|
293 |
def __init__(self): |
|
294 |
self.values = [] |
|
295 |
def step(self, value): |
|
296 |
if value is not None: |
|
297 |
self.values.append(value) |
|
298 |
def finalize(self): |
|
299 |
return ', '.join(self.values) |
|
300 |
# renamed to GROUP_CONCAT in cubicweb 2.45, keep old name for bw compat for |
|
301 |
# some time |
|
302 |
cnx.create_aggregate("CONCAT_STRINGS", 1, concat_strings) |
|
303 |
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
|
304 |
|
0 | 305 |
def _limit_size(text, maxsize, format='text/plain'): |
306 |
if len(text) < maxsize: |
|
307 |
return text |
|
308 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
309 |
text = remove_html_tags(text) |
|
310 |
if len(text) > maxsize: |
|
311 |
text = text[:maxsize] + '...' |
|
312 |
return text |
|
313 |
||
314 |
def limit_size3(text, format, maxsize): |
|
315 |
return _limit_size(text, maxsize, format) |
|
316 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
317 |
||
318 |
def limit_size2(text, maxsize): |
|
319 |
return _limit_size(text, maxsize) |
|
320 |
cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2) |
|
321 |
import yams.constraints |
|
322 |
if hasattr(yams.constraints, 'patch_sqlite_decimal'): |
|
323 |
yams.constraints.patch_sqlite_decimal() |
|
324 |
||
4322
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
325 |
def fspath(eid, etype, attr): |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
326 |
try: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
327 |
cu = cnx.cursor() |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
328 |
cu.execute('SELECT X.cw_%s FROM cw_%s as X ' |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
329 |
'WHERE X.cw_eid=%%(eid)s' % (attr, etype), |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
330 |
{'eid': eid}) |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
331 |
return cu.fetchone()[0] |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
332 |
except: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
333 |
import traceback |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
334 |
traceback.print_exc() |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
335 |
raise |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
336 |
cnx.create_function('fspath', 3, fspath) |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
337 |
|
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
338 |
def _fsopen(fspath): |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
339 |
if fspath: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
340 |
try: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
341 |
return buffer(file(fspath).read()) |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
342 |
except: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
343 |
import traceback |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
344 |
traceback.print_exc() |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
345 |
raise |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
346 |
cnx.create_function('_fsopen', 1, _fsopen) |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
347 |
|
0 | 348 |
|
349 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
350 |
sqlite_hooks.append(init_sqlite_connexion) |
|
351 |
||
352 |
def init_cnx(driver, cnx): |
|
353 |
for hook in SQL_CONNECT_HOOKS.get(driver, ()): |
|
354 |
hook(cnx) |