author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 May 2010 14:24:04 +0200 | |
branch | stable |
changeset 5535 | b32b8f56192a |
parent 5424 | 8ecbcbff9777 |
child 5605 | 2604545d7dd9 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5214
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""SQL utilities functions and classes. |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
23 |
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
|
24 |
import subprocess |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4622
diff
changeset
|
25 |
from datetime import datetime, date |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
26 |
|
4849
3827b9ee77ac
missing rename
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
27 |
from logilab import database as db, common as lgc |
0 | 28 |
from logilab.common.shellutils import ProgressBar |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
29 |
from logilab.common.date import todate, todatetime |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
30 |
from logilab.database.sqlgen import SQLGenerator |
0 | 31 |
|
32 |
from cubicweb import Binary, ConfigurationError |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
33 |
from cubicweb.uilib import remove_html_tags |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2512
diff
changeset
|
34 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 35 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 36 |
from cubicweb.server.utils import crypt_password |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
37 |
from rql.utils import RQL_FUNCTIONS_REGISTRY |
0 | 38 |
|
1783 | 39 |
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
|
40 |
SQL_PREFIX = 'cw_' |
0 | 41 |
|
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
|
42 |
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
|
43 |
"""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
|
44 |
""" |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
|
0 | 51 |
|
3623
9b838e2d72bb
avoid progress bar when test launched from apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
52 |
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
|
53 |
pbtitle='', delimiter=';'): |
0 | 54 |
"""execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
55 |
error. If a cnx is given, commit at each statement |
|
56 |
""" |
|
57 |
if hasattr(cursor_or_execute, 'execute'): |
|
58 |
execute = cursor_or_execute.execute |
|
59 |
else: |
|
60 |
execute = cursor_or_execute |
|
61 |
sqlstmts = sqlstmts.split(delimiter) |
|
62 |
if withpb: |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
63 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
0 | 64 |
for sql in sqlstmts: |
65 |
sql = sql.strip() |
|
66 |
if withpb: |
|
67 |
pb.update() |
|
68 |
if not sql: |
|
69 |
continue |
|
70 |
# some dbapi modules doesn't accept unicode for sql string |
|
71 |
execute(str(sql)) |
|
72 |
if withpb: |
|
73 |
print |
|
74 |
||
75 |
||
76 |
def sqlgrants(schema, driver, user, |
|
77 |
text_index=True, set_owner=True, |
|
78 |
skip_relations=(), skip_entities=()): |
|
79 |
"""return sql to give all access privileges to the given user on the system |
|
80 |
schema |
|
81 |
""" |
|
82 |
from yams.schema2sql import grant_schema |
|
83 |
from cubicweb.server.sources import native |
|
84 |
output = [] |
|
85 |
w = output.append |
|
86 |
w(native.grant_schema(user, set_owner)) |
|
87 |
w('') |
|
88 |
if text_index: |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
89 |
dbhelper = db.get_db_helper(driver) |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
90 |
w(dbhelper.sql_grant_user_on_fti(user)) |
0 | 91 |
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
|
92 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 93 |
return '\n'.join(output) |
94 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
95 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
96 |
def sqlschema(schema, driver, text_index=True, |
0 | 97 |
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
|
98 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 99 |
"""return the system sql schema, according to the given parameters""" |
100 |
from yams.schema2sql import schema2sql |
|
101 |
from cubicweb.server.sources import native |
|
102 |
if set_owner: |
|
103 |
assert user, 'user is argument required when set_owner is true' |
|
104 |
output = [] |
|
105 |
w = output.append |
|
106 |
w(native.sql_schema(driver)) |
|
107 |
w('') |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
108 |
dbhelper = db.get_db_helper(driver) |
0 | 109 |
if text_index: |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
110 |
w(dbhelper.sql_init_fti().replace(';', ';;')) |
0 | 111 |
w('') |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
112 |
w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
113 |
skip_entities=skip_entities, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
114 |
skip_relations=skip_relations).replace(';', ';;')) |
0 | 115 |
if dbhelper.users_support and user: |
116 |
w('') |
|
117 |
w(sqlgrants(schema, driver, user, text_index, set_owner, |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
118 |
skip_relations, skip_entities).replace(';', ';;')) |
0 | 119 |
return '\n'.join(output) |
120 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
121 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
122 |
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
|
123 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 124 |
"""return the sql to drop the schema, according to the given parameters""" |
125 |
from yams.schema2sql import dropschema2sql |
|
126 |
from cubicweb.server.sources import native |
|
127 |
output = [] |
|
128 |
w = output.append |
|
129 |
if text_index: |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
130 |
dbhelper = db.get_db_helper(driver) |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
131 |
w(dbhelper.sql_drop_fti()) |
0 | 132 |
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
|
133 |
w(dropschema2sql(schema, prefix=SQL_PREFIX, |
1954 | 134 |
skip_entities=skip_entities, |
135 |
skip_relations=skip_relations)) |
|
5214
3285b6e3b930
fix cwctl db-init -d on SQL Server
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
136 |
w('') |
3285b6e3b930
fix cwctl db-init -d on SQL Server
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
137 |
w(native.sql_drop_schema(driver)) |
0 | 138 |
return '\n'.join(output) |
139 |
||
140 |
||
141 |
class SQLAdapterMixIn(object): |
|
142 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
143 |
dictionary and handling connection locking |
|
144 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
145 |
|
0 | 146 |
def __init__(self, source_config): |
147 |
try: |
|
148 |
self.dbdriver = source_config['db-driver'].lower() |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
149 |
dbname = source_config['db-name'] |
0 | 150 |
except KeyError: |
151 |
raise ConfigurationError('missing some expected entries in sources file') |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
152 |
dbhost = source_config.get('db-host') |
0 | 153 |
port = source_config.get('db-port') |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
154 |
dbport = port and int(port) or None |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
155 |
dbuser = source_config.get('db-user') |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
156 |
dbpassword = source_config.get('db-password') |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
157 |
dbencoding = source_config.get('db-encoding', 'UTF-8') |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
158 |
dbextraargs = source_config.get('db-extra-arguments') |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
159 |
self.dbhelper = db.get_db_helper(self.dbdriver) |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
160 |
self.dbhelper.record_connection_info(dbname, dbhost, dbport, dbuser, |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
161 |
dbpassword, dbextraargs, |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
162 |
dbencoding) |
0 | 163 |
self.sqlgen = SQLGenerator() |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
164 |
# copy back some commonly accessed attributes |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
165 |
dbapi_module = self.dbhelper.dbapi_module |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
166 |
self.OperationalError = dbapi_module.OperationalError |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
167 |
self.InterfaceError = dbapi_module.InterfaceError |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
168 |
self._binary = dbapi_module.Binary |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
169 |
self._process_value = dbapi_module.process_value |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
170 |
self._dbencoding = dbencoding |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
171 |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
172 |
def get_connection(self): |
0 | 173 |
"""open and return a connection to the database""" |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
174 |
return self.dbhelper.get_connection() |
0 | 175 |
|
4893
15ae9a33a7f2
[db backup] fix name error in backup_to_file: we've to pass .confirm all along the chain as for restore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4854
diff
changeset
|
176 |
def backup_to_file(self, backupfile, confirm): |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
177 |
for cmd in self.dbhelper.backup_commands(backupfile, |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
183 |
def restore_from_file(self, backupfile, confirm, drop=True): |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
184 |
for cmd in self.dbhelper.restore_commands(backupfile, |
4854
b06d2a3b27d9
logilab.db compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
185 |
keepownership=False, |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
186 |
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
|
187 |
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
|
188 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
189 |
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
|
190 |
|
0 | 191 |
def merge_args(self, args, query_args): |
192 |
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
|
193 |
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
|
194 |
for key, val in args.iteritems(): |
0 | 195 |
# convert cubicweb binary into db binary |
196 |
if isinstance(val, Binary): |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
197 |
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
|
198 |
newargs[key] = val |
0 | 199 |
# 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
|
200 |
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
|
201 |
return newargs |
0 | 202 |
return query_args |
203 |
||
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
204 |
def process_result(self, cursor, column_callbacks=None): |
0 | 205 |
"""return a list of CubicWeb compliant values from data in the given cursor |
206 |
""" |
|
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
207 |
# use two different implementations to avoid paying the price of |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
208 |
# callback lookup for each *cell* in results when there is nothing to |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
209 |
# lookup |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
210 |
if not column_callbacks: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
211 |
return self._process_result(cursor) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
212 |
return self._cb_process_result(cursor, column_callbacks) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
213 |
|
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
214 |
def _process_result(self, cursor, column_callbacks=None): |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
215 |
# begin bind to locals for optimization |
0 | 216 |
descr = cursor.description |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
217 |
encoding = self._dbencoding |
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
218 |
process_value = self._process_value |
0 | 219 |
binary = Binary |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
220 |
# /end |
0 | 221 |
results = cursor.fetchall() |
222 |
for i, line in enumerate(results): |
|
223 |
result = [] |
|
224 |
for col, value in enumerate(line): |
|
225 |
if value is None: |
|
226 |
result.append(value) |
|
227 |
continue |
|
228 |
result.append(process_value(value, descr[col], encoding, binary)) |
|
229 |
results[i] = result |
|
230 |
return results |
|
231 |
||
5013
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
232 |
def _cb_process_result(self, cursor, column_callbacks): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
233 |
# begin bind to locals for optimization |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
234 |
descr = cursor.description |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
235 |
encoding = self._dbencoding |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
236 |
process_value = self._process_value |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
237 |
binary = Binary |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
238 |
# /end |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
239 |
results = cursor.fetchall() |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
240 |
for i, line in enumerate(results): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
241 |
result = [] |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
242 |
for col, value in enumerate(line): |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
243 |
if value is None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
244 |
result.append(value) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
245 |
continue |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
246 |
cbstack = column_callbacks.get(col, None) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
247 |
if cbstack is None: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
248 |
value = process_value(value, descr[col], encoding, binary) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
249 |
else: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
250 |
for cb in cbstack: |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
251 |
value = cb(self, value) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
252 |
result.append(value) |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
253 |
results[i] = result |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
254 |
return results |
ad91f93bbb93
[source storage] refactor source sql generation and results handling to allow repository side callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4965
diff
changeset
|
255 |
|
0 | 256 |
def preprocess_entity(self, entity): |
257 |
"""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
|
258 |
to insert/update an entity into a SQL database |
0 | 259 |
""" |
260 |
attrs = {} |
|
261 |
eschema = entity.e_schema |
|
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
262 |
for attr in entity.edited_attributes: |
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
263 |
value = entity[attr] |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3623
diff
changeset
|
264 |
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
|
265 |
if rschema.final: |
0 | 266 |
atype = str(entity.e_schema.destination(attr)) |
267 |
if atype == 'Boolean': |
|
268 |
value = self.dbhelper.boolean_value(value) |
|
269 |
elif atype == 'Password': |
|
270 |
# if value is a Binary instance, this mean we got it |
|
271 |
# from a query result and so it is already encrypted |
|
272 |
if isinstance(value, Binary): |
|
273 |
value = value.getvalue() |
|
274 |
else: |
|
275 |
value = crypt_password(value) |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
276 |
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
|
277 |
# 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
|
278 |
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
|
279 |
value = todatetime(value) |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
280 |
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
|
281 |
value = todate(value) |
0 | 282 |
elif isinstance(value, Binary): |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
283 |
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
|
284 |
attrs[SQL_PREFIX+str(attr)] = value |
4965
04543ed0bbdc
[source] only consider edited_attributes in source.preprocess_entity()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4913
diff
changeset
|
285 |
attrs[SQL_PREFIX+'eid'] = entity.eid |
0 | 286 |
return attrs |
287 |
||
288 |
||
289 |
from logging import getLogger |
|
290 |
from cubicweb import set_log_methods |
|
291 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
292 |
||
293 |
def init_sqlite_connexion(cnx): |
|
294 |
||
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
295 |
class group_concat(object): |
0 | 296 |
def __init__(self): |
297 |
self.values = [] |
|
298 |
def step(self, value): |
|
299 |
if value is not None: |
|
300 |
self.values.append(value) |
|
301 |
def finalize(self): |
|
302 |
return ', '.join(self.values) |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
303 |
cnx.create_aggregate("GROUP_CONCAT", 1, group_concat) |
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) |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
321 |
|
0 | 322 |
import yams.constraints |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
323 |
yams.constraints.patch_sqlite_decimal() |
0 | 324 |
|
325 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
326 |
sqlite_hooks.append(init_sqlite_connexion) |