author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 31 Jul 2009 23:33:05 +0200 | |
changeset 2598 | a66fe74bd9fc |
parent 2596 | d02eed70937f |
child 2616 | 4501ee760eec |
permissions | -rw-r--r-- |
0 | 1 |
"""SQL utilities functions and classes. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
10 |
import os |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
11 |
from os.path import exists |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
12 |
from warnings import warn |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
13 |
from datetime import datetime, date, timedelta |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
14 |
|
1783 | 15 |
import logilab.common as lgc |
0 | 16 |
from logilab.common.shellutils import ProgressBar |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
17 |
from logilab.common import db |
0 | 18 |
from logilab.common.adbh import get_adv_func_helper |
19 |
from logilab.common.sqlgen import SQLGenerator |
|
20 |
||
21 |
from indexer import get_indexer |
|
22 |
||
23 |
from cubicweb import Binary, ConfigurationError |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
24 |
from cubicweb.utils import todate, todatetime |
0 | 25 |
from cubicweb.common.uilib import remove_html_tags |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
26 |
from cubicweb.toolsutils import restrict_perms_to_user |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2512
diff
changeset
|
27 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 28 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 29 |
from cubicweb.server.utils import crypt_password |
0 | 30 |
|
31 |
||
1783 | 32 |
lgc.USE_MX_DATETIME = False |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
33 |
SQL_PREFIX = 'cw_' |
0 | 34 |
|
35 |
||
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
36 |
def sqlexec(sqlstmts, cursor_or_execute, withpb=True, pbtitle='', delimiter=';'): |
0 | 37 |
"""execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
38 |
error. If a cnx is given, commit at each statement |
|
39 |
""" |
|
40 |
if hasattr(cursor_or_execute, 'execute'): |
|
41 |
execute = cursor_or_execute.execute |
|
42 |
else: |
|
43 |
execute = cursor_or_execute |
|
44 |
sqlstmts = sqlstmts.split(delimiter) |
|
45 |
if withpb: |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
46 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
0 | 47 |
for sql in sqlstmts: |
48 |
sql = sql.strip() |
|
49 |
if withpb: |
|
50 |
pb.update() |
|
51 |
if not sql: |
|
52 |
continue |
|
53 |
# some dbapi modules doesn't accept unicode for sql string |
|
54 |
execute(str(sql)) |
|
55 |
if withpb: |
|
56 |
print |
|
57 |
||
58 |
||
59 |
def sqlgrants(schema, driver, user, |
|
60 |
text_index=True, set_owner=True, |
|
61 |
skip_relations=(), skip_entities=()): |
|
62 |
"""return sql to give all access privileges to the given user on the system |
|
63 |
schema |
|
64 |
""" |
|
65 |
from yams.schema2sql import grant_schema |
|
66 |
from cubicweb.server.sources import native |
|
67 |
output = [] |
|
68 |
w = output.append |
|
69 |
w(native.grant_schema(user, set_owner)) |
|
70 |
w('') |
|
71 |
if text_index: |
|
72 |
indexer = get_indexer(driver) |
|
73 |
w(indexer.sql_grant_user(user)) |
|
74 |
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
|
75 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 76 |
return '\n'.join(output) |
77 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
78 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
79 |
def sqlschema(schema, driver, text_index=True, |
0 | 80 |
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
|
81 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 82 |
"""return the system sql schema, according to the given parameters""" |
83 |
from yams.schema2sql import schema2sql |
|
84 |
from cubicweb.server.sources import native |
|
85 |
if set_owner: |
|
86 |
assert user, 'user is argument required when set_owner is true' |
|
87 |
output = [] |
|
88 |
w = output.append |
|
89 |
w(native.sql_schema(driver)) |
|
90 |
w('') |
|
91 |
if text_index: |
|
92 |
indexer = get_indexer(driver) |
|
93 |
w(indexer.sql_init_fti()) |
|
94 |
w('') |
|
95 |
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
|
96 |
w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
0 | 97 |
skip_entities=skip_entities, skip_relations=skip_relations)) |
98 |
if dbhelper.users_support and user: |
|
99 |
w('') |
|
100 |
w(sqlgrants(schema, driver, user, text_index, set_owner, |
|
101 |
skip_relations, skip_entities)) |
|
102 |
return '\n'.join(output) |
|
103 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
104 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
105 |
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
|
106 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 107 |
"""return the sql to drop the schema, according to the given parameters""" |
108 |
from yams.schema2sql import dropschema2sql |
|
109 |
from cubicweb.server.sources import native |
|
110 |
output = [] |
|
111 |
w = output.append |
|
112 |
w(native.sql_drop_schema(driver)) |
|
113 |
w('') |
|
114 |
if text_index: |
|
115 |
indexer = get_indexer(driver) |
|
116 |
w(indexer.sql_drop_fti()) |
|
117 |
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
|
118 |
w(dropschema2sql(schema, prefix=SQL_PREFIX, |
1954 | 119 |
skip_entities=skip_entities, |
120 |
skip_relations=skip_relations)) |
|
0 | 121 |
return '\n'.join(output) |
122 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
123 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
124 |
def sql_source_backup(source, sqladapter, confirm, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
125 |
askconfirm=False): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
126 |
if exists(backupfile): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
127 |
if not confirm('Backup file %s exists, overwrite it?' % backupfile): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
128 |
return |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
129 |
elif askconfirm and not confirm('Backup %s database?' |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
130 |
% source.repo.config.appid): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
131 |
print '-> no backup done.' |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
132 |
return |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
133 |
# should close opened connection before backuping |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
134 |
source.close_pool_connections() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
135 |
try: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
136 |
sqladapter.backup_to_file(backupfile, confirm) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
137 |
finally: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
138 |
source.open_pool_connections() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
139 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
140 |
def sql_source_restore(source, sqladapter, confirm, backupfile, drop=True, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
141 |
askconfirm=False): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
142 |
if not exists(backupfile): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
143 |
raise Exception("backup file %s doesn't exist" % backupfile) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
144 |
app = source.repo.config.appid |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
145 |
if askconfirm and not confirm('Restore %s %s database from %s ?' |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
146 |
% (app, source.uri, backupfile)): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
147 |
return |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
148 |
# should close opened connection before restoring |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
149 |
source.close_pool_connections() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
150 |
try: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
151 |
sqladapter.restore_from_file(backupfile, confirm, drop=drop) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
152 |
finally: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
153 |
source.open_pool_connections() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
154 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
155 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
156 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
157 |
from mx.DateTime import DateTimeType, DateTimeDeltaType |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
158 |
except ImportError: |
1953 | 159 |
DateTimeType = DateTimeDeltaType = None |
0 | 160 |
|
161 |
class SQLAdapterMixIn(object): |
|
162 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
163 |
dictionary and handling connection locking |
|
164 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
165 |
|
0 | 166 |
def __init__(self, source_config): |
167 |
try: |
|
168 |
self.dbdriver = source_config['db-driver'].lower() |
|
169 |
self.dbname = source_config['db-name'] |
|
170 |
except KeyError: |
|
171 |
raise ConfigurationError('missing some expected entries in sources file') |
|
172 |
self.dbhost = source_config.get('db-host') |
|
173 |
port = source_config.get('db-port') |
|
174 |
self.dbport = port and int(port) or None |
|
175 |
self.dbuser = source_config.get('db-user') |
|
176 |
self.dbpasswd = source_config.get('db-password') |
|
177 |
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
|
178 |
self.dbapi_module = db.get_dbapi_compliant_module(self.dbdriver) |
0 | 179 |
self.binary = self.dbapi_module.Binary |
180 |
self.dbhelper = self.dbapi_module.adv_func_helper |
|
181 |
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
|
182 |
|
0 | 183 |
def get_connection(self, user=None, password=None): |
184 |
"""open and return a connection to the database""" |
|
185 |
if user or self.dbuser: |
|
186 |
self.info('connecting to %s@%s for user %s', self.dbname, |
|
187 |
self.dbhost or 'localhost', user or self.dbuser) |
|
188 |
else: |
|
189 |
self.info('connecting to %s@%s', self.dbname, |
|
190 |
self.dbhost or 'localhost') |
|
191 |
cnx = self.dbapi_module.connect(self.dbhost, self.dbname, |
|
192 |
user or self.dbuser, |
|
193 |
password or self.dbpasswd, |
|
194 |
port=self.dbport) |
|
195 |
init_cnx(self.dbdriver, cnx) |
|
196 |
#self.dbapi_module.type_code_test(cnx.cursor()) |
|
197 |
return cnx |
|
198 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
199 |
def backup_to_file(self, backupfile, confirm): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
200 |
cmd = self.dbhelper.backup_command(self.dbname, self.dbhost, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
201 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
202 |
keepownership=False) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
203 |
while True: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
204 |
print cmd |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
205 |
if os.system(cmd): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
206 |
print '-> error while backuping the base' |
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
207 |
answer = confirm('Continue anyway?', |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
208 |
shell=False, abort=False, retry=True) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
209 |
if not answer: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
210 |
raise SystemExit(1) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
211 |
if answer == 1: # 1: continue, 2: retry |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
212 |
break |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
213 |
else: |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2510
diff
changeset
|
214 |
print '-> backup file', backupfile |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
215 |
restrict_perms_to_user(backupfile, self.info) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
216 |
break |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
217 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
218 |
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
|
219 |
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
|
220 |
self.dbuser, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
221 |
self.encoding, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
222 |
keepownership=False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
223 |
drop=drop): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
224 |
while True: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
225 |
print cmd |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
226 |
if os.system(cmd): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
227 |
print 'error while restoring the base' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
228 |
print 'OOOOOPS', confirm |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
229 |
answer = confirm('continue anyway?', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
230 |
shell=False, abort=False, retry=True) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
231 |
if not answer: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
232 |
raise SystemExit(1) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
233 |
if answer == 1: # 1: continue, 2: retry |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
234 |
break |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
235 |
else: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
236 |
break |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
237 |
print 'database restored' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
238 |
|
0 | 239 |
def merge_args(self, args, query_args): |
240 |
if args is not None: |
|
241 |
args = dict(args) |
|
242 |
for key, val in args.items(): |
|
243 |
# convert cubicweb binary into db binary |
|
244 |
if isinstance(val, Binary): |
|
245 |
val = self.binary(val.getvalue()) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
246 |
# XXX <3.2 bw compat |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
247 |
elif type(val) is DateTimeType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
248 |
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
|
249 |
DeprecationWarning) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
250 |
val = datetime(val.year, val.month, val.day, |
1026 | 251 |
val.hour, val.minute, int(val.second)) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
252 |
elif type(val) is DateTimeDeltaType: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
253 |
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
|
254 |
DeprecationWarning) |
1026 | 255 |
val = timedelta(0, int(val.seconds), 0) |
0 | 256 |
args[key] = val |
257 |
# should not collide |
|
258 |
args.update(query_args) |
|
259 |
return args |
|
260 |
return query_args |
|
261 |
||
262 |
def process_result(self, cursor): |
|
263 |
"""return a list of CubicWeb compliant values from data in the given cursor |
|
264 |
""" |
|
265 |
descr = cursor.description |
|
266 |
encoding = self.encoding |
|
267 |
process_value = self.dbapi_module.process_value |
|
268 |
binary = Binary |
|
269 |
results = cursor.fetchall() |
|
270 |
for i, line in enumerate(results): |
|
271 |
result = [] |
|
272 |
for col, value in enumerate(line): |
|
273 |
if value is None: |
|
274 |
result.append(value) |
|
275 |
continue |
|
276 |
result.append(process_value(value, descr[col], encoding, binary)) |
|
277 |
results[i] = result |
|
278 |
return results |
|
279 |
||
280 |
||
281 |
def preprocess_entity(self, entity): |
|
282 |
"""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
|
283 |
to insert/update an entity into a SQL database |
0 | 284 |
""" |
285 |
attrs = {} |
|
286 |
eschema = entity.e_schema |
|
287 |
for attr, value in entity.items(): |
|
288 |
rschema = eschema.subject_relation(attr) |
|
289 |
if rschema.is_final(): |
|
290 |
atype = str(entity.e_schema.destination(attr)) |
|
291 |
if atype == 'Boolean': |
|
292 |
value = self.dbhelper.boolean_value(value) |
|
293 |
elif atype == 'Password': |
|
294 |
# if value is a Binary instance, this mean we got it |
|
295 |
# from a query result and so it is already encrypted |
|
296 |
if isinstance(value, Binary): |
|
297 |
value = value.getvalue() |
|
298 |
else: |
|
299 |
value = crypt_password(value) |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
300 |
# 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
|
301 |
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
|
302 |
value = todatetime(value) |
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
303 |
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
|
304 |
value = todate(value) |
0 | 305 |
elif isinstance(value, Binary): |
306 |
value = self.binary(value.getvalue()) |
|
1408
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
307 |
# XXX <3.2 bw compat |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
308 |
elif type(value) is DateTimeType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
309 |
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
|
310 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
311 |
value = datetime(value.year, value.month, value.day, |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
312 |
value.hour, value.minute, int(value.second)) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
313 |
elif type(value) is DateTimeDeltaType: |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
314 |
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
|
315 |
DeprecationWarning) |
6bf19f175ea5
mx.DateTime fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1263
diff
changeset
|
316 |
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
|
317 |
attrs[SQL_PREFIX+str(attr)] = value |
0 | 318 |
return attrs |
319 |
||
320 |
||
321 |
from logging import getLogger |
|
322 |
from cubicweb import set_log_methods |
|
323 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
324 |
||
325 |
def init_sqlite_connexion(cnx): |
|
326 |
# XXX should not be publicly exposed |
|
327 |
#def comma_join(strings): |
|
328 |
# return ', '.join(strings) |
|
329 |
#cnx.create_function("COMMA_JOIN", 1, comma_join) |
|
330 |
||
331 |
class concat_strings(object): |
|
332 |
def __init__(self): |
|
333 |
self.values = [] |
|
334 |
def step(self, value): |
|
335 |
if value is not None: |
|
336 |
self.values.append(value) |
|
337 |
def finalize(self): |
|
338 |
return ', '.join(self.values) |
|
339 |
# renamed to GROUP_CONCAT in cubicweb 2.45, keep old name for bw compat for |
|
340 |
# some time |
|
341 |
cnx.create_aggregate("CONCAT_STRINGS", 1, concat_strings) |
|
342 |
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
|
343 |
|
0 | 344 |
def _limit_size(text, maxsize, format='text/plain'): |
345 |
if len(text) < maxsize: |
|
346 |
return text |
|
347 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
348 |
text = remove_html_tags(text) |
|
349 |
if len(text) > maxsize: |
|
350 |
text = text[:maxsize] + '...' |
|
351 |
return text |
|
352 |
||
353 |
def limit_size3(text, format, maxsize): |
|
354 |
return _limit_size(text, maxsize, format) |
|
355 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
356 |
||
357 |
def limit_size2(text, maxsize): |
|
358 |
return _limit_size(text, maxsize) |
|
359 |
cnx.create_function("TEXT_LIMIT_SIZE", 2, limit_size2) |
|
360 |
import yams.constraints |
|
361 |
if hasattr(yams.constraints, 'patch_sqlite_decimal'): |
|
362 |
yams.constraints.patch_sqlite_decimal() |
|
363 |
||
364 |
||
365 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
366 |
sqlite_hooks.append(init_sqlite_connexion) |
|
367 |
||
368 |
def init_cnx(driver, cnx): |
|
369 |
for hook in SQL_CONNECT_HOOKS.get(driver, ()): |
|
370 |
hook(cnx) |