author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 18 Feb 2010 14:07:56 +0100 | |
branch | stable |
changeset 4638 | c7d2145abf61 |
parent 4622 | 790181bfd19c |
child 4719 | aaed3f813ef8 |
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 |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
17 |
from logilab.common import db |
0 | 18 |
from logilab.common.shellutils import ProgressBar |
19 |
from logilab.common.adbh import get_adv_func_helper |
|
20 |
from logilab.common.sqlgen import SQLGenerator |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
21 |
from logilab.common.date import todate, todatetime |
0 | 22 |
|
23 |
from indexer import get_indexer |
|
24 |
||
25 |
from cubicweb import Binary, ConfigurationError |
|
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 |
||
134 |
||
135 |
class SQLAdapterMixIn(object): |
|
136 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
137 |
dictionary and handling connection locking |
|
138 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
139 |
|
0 | 140 |
def __init__(self, source_config): |
141 |
try: |
|
142 |
self.dbdriver = source_config['db-driver'].lower() |
|
143 |
self.dbname = source_config['db-name'] |
|
144 |
except KeyError: |
|
145 |
raise ConfigurationError('missing some expected entries in sources file') |
|
146 |
self.dbhost = source_config.get('db-host') |
|
147 |
port = source_config.get('db-port') |
|
148 |
self.dbport = port and int(port) or None |
|
149 |
self.dbuser = source_config.get('db-user') |
|
150 |
self.dbpasswd = source_config.get('db-password') |
|
151 |
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
|
152 |
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
|
153 |
self.dbdriver_extra_args = source_config.get('db-extra-arguments') |
0 | 154 |
self.binary = self.dbapi_module.Binary |
155 |
self.dbhelper = self.dbapi_module.adv_func_helper |
|
156 |
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
|
157 |
|
0 | 158 |
def get_connection(self, user=None, password=None): |
159 |
"""open and return a connection to the database""" |
|
160 |
if user or self.dbuser: |
|
161 |
self.info('connecting to %s@%s for user %s', self.dbname, |
|
162 |
self.dbhost or 'localhost', user or self.dbuser) |
|
163 |
else: |
|
164 |
self.info('connecting to %s@%s', self.dbname, |
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
extra = {'extra_args': self.dbdriver_extra_args} |
0 | 169 |
cnx = self.dbapi_module.connect(self.dbhost, self.dbname, |
170 |
user or self.dbuser, |
|
171 |
password or self.dbpasswd, |
|
4177
f0ab2b6d3553
SqlServer: support single sign on / Windows credential authentication
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4176
diff
changeset
|
172 |
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
|
173 |
**extra) |
0 | 174 |
init_cnx(self.dbdriver, cnx) |
175 |
#self.dbapi_module.type_code_test(cnx.cursor()) |
|
176 |
return cnx |
|
177 |
||
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2739
diff
changeset
|
178 |
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
|
179 |
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
|
180 |
self.dbuser, backupfile, |
4622
790181bfd19c
specify db port on backup/restore commands (require forth comming lgc 0.48.1)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4477
diff
changeset
|
181 |
dbport=self.dbport, |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
187 |
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
|
188 |
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
|
189 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
190 |
self.encoding, |
4622
790181bfd19c
specify db port on backup/restore commands (require forth comming lgc 0.48.1)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4477
diff
changeset
|
191 |
dbport=self.dbport, |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
192 |
keepownership=False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
193 |
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
|
194 |
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
|
195 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
196 |
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
|
197 |
|
0 | 198 |
def merge_args(self, args, query_args): |
199 |
if args is not None: |
|
4474
55fe19813bb7
kill mx compat code (dropped since 3.2), more efficient merge_args implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
200 |
newargs = {} |
55fe19813bb7
kill mx compat code (dropped since 3.2), more efficient merge_args implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
201 |
for key, val in args.iteritems(): |
0 | 202 |
# convert cubicweb binary into db binary |
203 |
if isinstance(val, Binary): |
|
204 |
val = self.binary(val.getvalue()) |
|
4474
55fe19813bb7
kill mx compat code (dropped since 3.2), more efficient merge_args implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
205 |
newargs[key] = val |
0 | 206 |
# should not collide |
4474
55fe19813bb7
kill mx compat code (dropped since 3.2), more efficient merge_args implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
207 |
newargs.update(query_args) |
55fe19813bb7
kill mx compat code (dropped since 3.2), more efficient merge_args implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
208 |
return newargs |
0 | 209 |
return query_args |
210 |
||
211 |
def process_result(self, cursor): |
|
212 |
"""return a list of CubicWeb compliant values from data in the given cursor |
|
213 |
""" |
|
214 |
descr = cursor.description |
|
215 |
encoding = self.encoding |
|
216 |
process_value = self.dbapi_module.process_value |
|
217 |
binary = Binary |
|
218 |
results = cursor.fetchall() |
|
219 |
for i, line in enumerate(results): |
|
220 |
result = [] |
|
221 |
for col, value in enumerate(line): |
|
222 |
if value is None: |
|
223 |
result.append(value) |
|
224 |
continue |
|
225 |
result.append(process_value(value, descr[col], encoding, binary)) |
|
226 |
results[i] = result |
|
227 |
return results |
|
228 |
||
229 |
def preprocess_entity(self, entity): |
|
230 |
"""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
|
231 |
to insert/update an entity into a SQL database |
0 | 232 |
""" |
233 |
attrs = {} |
|
234 |
eschema = entity.e_schema |
|
235 |
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
|
236 |
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
|
237 |
if rschema.final: |
0 | 238 |
atype = str(entity.e_schema.destination(attr)) |
239 |
if atype == 'Boolean': |
|
240 |
value = self.dbhelper.boolean_value(value) |
|
241 |
elif atype == 'Password': |
|
242 |
# if value is a Binary instance, this mean we got it |
|
243 |
# from a query result and so it is already encrypted |
|
244 |
if isinstance(value, Binary): |
|
245 |
value = value.getvalue() |
|
246 |
else: |
|
247 |
value = crypt_password(value) |
|
4176
42247d70105b
SQL Server port: convert Password to Binary
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3689
diff
changeset
|
248 |
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
|
249 |
# 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
|
250 |
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
|
251 |
value = todatetime(value) |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
252 |
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
|
253 |
value = todate(value) |
0 | 254 |
elif isinstance(value, Binary): |
255 |
value = self.binary(value.getvalue()) |
|
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
|
256 |
attrs[SQL_PREFIX+str(attr)] = value |
0 | 257 |
return attrs |
258 |
||
259 |
||
260 |
from logging import getLogger |
|
261 |
from cubicweb import set_log_methods |
|
262 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
263 |
||
264 |
def init_sqlite_connexion(cnx): |
|
265 |
# XXX should not be publicly exposed |
|
266 |
#def comma_join(strings): |
|
267 |
# return ', '.join(strings) |
|
268 |
#cnx.create_function("COMMA_JOIN", 1, comma_join) |
|
269 |
||
270 |
class concat_strings(object): |
|
271 |
def __init__(self): |
|
272 |
self.values = [] |
|
273 |
def step(self, value): |
|
274 |
if value is not None: |
|
275 |
self.values.append(value) |
|
276 |
def finalize(self): |
|
277 |
return ', '.join(self.values) |
|
278 |
# renamed to GROUP_CONCAT in cubicweb 2.45, keep old name for bw compat for |
|
279 |
# some time |
|
280 |
cnx.create_aggregate("CONCAT_STRINGS", 1, concat_strings) |
|
281 |
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
|
282 |
|
0 | 283 |
def _limit_size(text, maxsize, format='text/plain'): |
284 |
if len(text) < maxsize: |
|
285 |
return text |
|
286 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
287 |
text = remove_html_tags(text) |
|
288 |
if len(text) > maxsize: |
|
289 |
text = text[:maxsize] + '...' |
|
290 |
return text |
|
291 |
||
292 |
def limit_size3(text, format, maxsize): |
|
293 |
return _limit_size(text, maxsize, format) |
|
294 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
295 |
||
296 |
def limit_size2(text, maxsize): |
|
297 |
return _limit_size(text, maxsize) |
|
298 |
cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2) |
|
299 |
import yams.constraints |
|
300 |
if hasattr(yams.constraints, 'patch_sqlite_decimal'): |
|
301 |
yams.constraints.patch_sqlite_decimal() |
|
302 |
||
4322
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
303 |
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
|
304 |
try: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
305 |
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
|
306 |
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
|
307 |
'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
|
308 |
{'eid': eid}) |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
309 |
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
|
310 |
except: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
311 |
import traceback |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
312 |
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
|
313 |
raise |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
314 |
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
|
315 |
|
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
316 |
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
|
317 |
if fspath: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
318 |
try: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
319 |
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
|
320 |
except: |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
321 |
import traceback |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
322 |
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
|
323 |
raise |
f65743cc53e4
first draft for a simple hooks based custom attribute storage,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4314
diff
changeset
|
324 |
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
|
325 |
|
0 | 326 |
|
327 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
328 |
sqlite_hooks.append(init_sqlite_connexion) |
|
329 |
||
330 |
def init_cnx(driver, cnx): |
|
331 |
for hook in SQL_CONNECT_HOOKS.get(driver, ()): |
|
332 |
hook(cnx) |