author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 01 Aug 2013 13:58:48 +0200 | |
branch | stable |
changeset 9273 | f3795da61959 |
parent 9267 | 24d9b86dfa54 |
child 9321 | 212869484c65 |
child 9334 | ea12401c0a68 |
permissions | -rw-r--r-- |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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/>. |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5625
diff
changeset
|
18 |
"""SQL utilities functions and classes.""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
22 |
import os |
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
23 |
import re |
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 |
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
26 |
from itertools import ifilter |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
27 |
|
4849
3827b9ee77ac
missing rename
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
28 |
from logilab import database as db, common as lgc |
0 | 29 |
from logilab.common.shellutils import ProgressBar |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
30 |
from logilab.common.date import todate, todatetime, utcdatetime, utctime |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
31 |
from logilab.database.sqlgen import SQLGenerator |
0 | 32 |
|
33 |
from cubicweb import Binary, ConfigurationError |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
34 |
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
|
35 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 36 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 37 |
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
|
38 |
from rql.utils import RQL_FUNCTIONS_REGISTRY |
0 | 39 |
|
1783 | 40 |
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
|
41 |
SQL_PREFIX = 'cw_' |
0 | 42 |
|
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
|
43 |
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
|
44 |
"""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
|
45 |
""" |
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
|
0 | 52 |
|
3623
9b838e2d72bb
avoid progress bar when test launched from apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
53 |
def sqlexec(sqlstmts, cursor_or_execute, withpb=not os.environ.get('APYCOT_ROOT'), |
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
54 |
pbtitle='', delimiter=';', cnx=None): |
0 | 55 |
"""execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
56 |
error. |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
57 |
|
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
58 |
:sqlstmts_as_string: a string or a list of sql statements. |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
59 |
:cursor_or_execute: sql cursor or a callback used to execute statements |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
60 |
:cnx: if given, commit/rollback at each statement. |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
61 |
|
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
62 |
:withpb: if True, display a progresse bar |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
63 |
:pbtitle: a string displayed as the progress bar title (if `withpb=True`) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
64 |
|
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
65 |
:delimiter: a string used to split sqlstmts (if it is a string) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
66 |
|
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
67 |
Return the failed statements (same type as sqlstmts) |
0 | 68 |
""" |
69 |
if hasattr(cursor_or_execute, 'execute'): |
|
70 |
execute = cursor_or_execute.execute |
|
71 |
else: |
|
72 |
execute = cursor_or_execute |
|
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
73 |
sqlstmts_as_string = False |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
74 |
if isinstance(sqlstmts, basestring): |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
75 |
sqlstmts_as_string = True |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
76 |
sqlstmts = sqlstmts.split(delimiter) |
0 | 77 |
if withpb: |
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
78 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
79 |
failed = [] |
0 | 80 |
for sql in sqlstmts: |
81 |
sql = sql.strip() |
|
82 |
if withpb: |
|
83 |
pb.update() |
|
84 |
if not sql: |
|
85 |
continue |
|
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
86 |
try: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
87 |
# some dbapi modules doesn't accept unicode for sql string |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
88 |
execute(str(sql)) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
89 |
except Exception, err: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
90 |
if cnx: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
91 |
cnx.rollback() |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
92 |
failed.append(sql) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
93 |
else: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
94 |
if cnx: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
95 |
cnx.commit() |
0 | 96 |
if withpb: |
97 |
print |
|
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
98 |
if sqlstmts_as_string: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
99 |
failed = delimiter.join(failed) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
100 |
return failed |
0 | 101 |
|
102 |
||
103 |
def sqlgrants(schema, driver, user, |
|
104 |
text_index=True, set_owner=True, |
|
105 |
skip_relations=(), skip_entities=()): |
|
106 |
"""return sql to give all access privileges to the given user on the system |
|
107 |
schema |
|
108 |
""" |
|
109 |
from yams.schema2sql import grant_schema |
|
110 |
from cubicweb.server.sources import native |
|
111 |
output = [] |
|
112 |
w = output.append |
|
113 |
w(native.grant_schema(user, set_owner)) |
|
114 |
w('') |
|
115 |
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
|
116 |
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
|
117 |
w(dbhelper.sql_grant_user_on_fti(user)) |
0 | 118 |
w('') |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
119 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 120 |
return '\n'.join(output) |
121 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
122 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
123 |
def sqlschema(schema, driver, text_index=True, |
0 | 124 |
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
|
125 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 126 |
"""return the system sql schema, according to the given parameters""" |
127 |
from yams.schema2sql import schema2sql |
|
128 |
from cubicweb.server.sources import native |
|
129 |
if set_owner: |
|
130 |
assert user, 'user is argument required when set_owner is true' |
|
131 |
output = [] |
|
132 |
w = output.append |
|
133 |
w(native.sql_schema(driver)) |
|
134 |
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
|
135 |
dbhelper = db.get_db_helper(driver) |
0 | 136 |
if text_index: |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
137 |
w(dbhelper.sql_init_fti().replace(';', ';;')) |
0 | 138 |
w('') |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
139 |
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
|
140 |
skip_entities=skip_entities, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
141 |
skip_relations=skip_relations).replace(';', ';;')) |
0 | 142 |
if dbhelper.users_support and user: |
143 |
w('') |
|
144 |
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
|
145 |
skip_relations, skip_entities).replace(';', ';;')) |
0 | 146 |
return '\n'.join(output) |
147 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
148 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
149 |
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
|
150 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 151 |
"""return the sql to drop the schema, according to the given parameters""" |
152 |
from yams.schema2sql import dropschema2sql |
|
153 |
from cubicweb.server.sources import native |
|
154 |
output = [] |
|
155 |
w = output.append |
|
156 |
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
|
157 |
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
|
158 |
w(dbhelper.sql_drop_fti()) |
0 | 159 |
w('') |
7906
203d574c8a1d
repaire cctl db-init -d on sqlserver (closes #1979670)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7341
diff
changeset
|
160 |
w(dropschema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
1954 | 161 |
skip_entities=skip_entities, |
162 |
skip_relations=skip_relations)) |
|
5214
3285b6e3b930
fix cwctl db-init -d on SQL Server
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
163 |
w('') |
3285b6e3b930
fix cwctl db-init -d on SQL Server
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5013
diff
changeset
|
164 |
w(native.sql_drop_schema(driver)) |
0 | 165 |
return '\n'.join(output) |
166 |
||
167 |
||
8755
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
168 |
_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION = re.compile('^(?!(sql|pg)_)').match |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
169 |
def sql_drop_all_user_tables(driver_or_helper, sqlcursor): |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
170 |
"""Return ths sql to drop all tables found in the database system.""" |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
171 |
if not getattr(driver_or_helper, 'list_tables', None): |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
172 |
dbhelper = db.get_db_helper(driver_or_helper) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
173 |
else: |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
174 |
dbhelper = driver_or_helper |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
175 |
|
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
176 |
cmds = [dbhelper.sql_drop_sequence('entities_id_seq')] |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
177 |
# for mssql, we need to drop views before tables |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
178 |
if hasattr(dbhelper, 'list_views'): |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
179 |
cmds += ['DROP VIEW %s;' % name |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
180 |
for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_views(sqlcursor))] |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
181 |
cmds += ['DROP TABLE %s;' % name |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
182 |
for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_tables(sqlcursor))] |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
183 |
return '\n'.join(cmds) |
1f3757ef3762
[server] *init_repository* lookup the database instead of the schema to drop tables (closes #810743)
Alain Leufroy <alain.leufroy@logilab.fr>
parents:
8139
diff
changeset
|
184 |
|
0 | 185 |
class SQLAdapterMixIn(object): |
186 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
187 |
dictionary and handling connection locking |
|
188 |
""" |
|
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
189 |
|
0 | 190 |
def __init__(self, source_config): |
191 |
try: |
|
192 |
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
|
193 |
dbname = source_config['db-name'] |
0 | 194 |
except KeyError: |
195 |
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
|
196 |
dbhost = source_config.get('db-host') |
0 | 197 |
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
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
dbencoding) |
0 | 207 |
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
|
208 |
# 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
|
209 |
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
|
210 |
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
|
211 |
self.InterfaceError = dbapi_module.InterfaceError |
5605
2604545d7dd9
[win32 SQLServer] connection lost detection
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
212 |
self.DbapiError = dbapi_module.Error |
6379
3f67f7ea5632
[R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6333
diff
changeset
|
213 |
self._binary = self.dbhelper.binary_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
|
214 |
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
|
215 |
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
|
216 |
|
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 |
def get_connection(self): |
0 | 218 |
"""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
|
219 |
return self.dbhelper.get_connection() |
0 | 220 |
|
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
228 |
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
|
229 |
for cmd in self.dbhelper.restore_commands(backupfile, |
4854
b06d2a3b27d9
logilab.db compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
230 |
keepownership=False, |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
231 |
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
|
232 |
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
|
233 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
234 |
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
|
235 |
|
0 | 236 |
def merge_args(self, args, query_args): |
237 |
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
|
238 |
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
|
239 |
for key, val in args.iteritems(): |
0 | 240 |
# convert cubicweb binary into db binary |
241 |
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
|
242 |
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
|
243 |
newargs[key] = val |
0 | 244 |
# 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
|
245 |
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
|
246 |
return newargs |
0 | 247 |
return query_args |
248 |
||
5625
6ee2a7b6f194
[external storage] refactor to give session to storage's callback (needed by vcsfile storage)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5605
diff
changeset
|
249 |
def process_result(self, cursor, column_callbacks=None, session=None): |
0 | 250 |
"""return a list of CubicWeb compliant values from data in the given cursor |
251 |
""" |
|
7341
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
252 |
return list(self.iter_process_result(cursor, column_callbacks, session)) |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
253 |
|
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
254 |
def iter_process_result(self, cursor, column_callbacks=None, session=None): |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
255 |
"""return a iterator on tuples of CubicWeb compliant values from data |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
256 |
in the given cursor |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
257 |
""" |
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
|
258 |
# 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
|
259 |
# 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
|
260 |
# 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
|
261 |
if not column_callbacks: |
8139
f9ebb6d1abc3
[server] use lgd.process_cursor to optimize processing of large resultsets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7907
diff
changeset
|
262 |
return self.dbhelper.dbapi_module.process_cursor(cursor, self._dbencoding, |
f9ebb6d1abc3
[server] use lgd.process_cursor to optimize processing of large resultsets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7907
diff
changeset
|
263 |
Binary) |
5625
6ee2a7b6f194
[external storage] refactor to give session to storage's callback (needed by vcsfile storage)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5605
diff
changeset
|
264 |
assert session |
6ee2a7b6f194
[external storage] refactor to give session to storage's callback (needed by vcsfile storage)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5605
diff
changeset
|
265 |
return self._cb_process_result(cursor, column_callbacks, session) |
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
|
266 |
|
5625
6ee2a7b6f194
[external storage] refactor to give session to storage's callback (needed by vcsfile storage)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5605
diff
changeset
|
267 |
def _cb_process_result(self, cursor, column_callbacks, session): |
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
|
268 |
# 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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
# /end |
7341
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
274 |
cursor.arraysize = 100 |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
275 |
while True: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
276 |
results = cursor.fetchmany() |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
277 |
if not results: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
278 |
break |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
279 |
for line in results: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
280 |
result = [] |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
281 |
for col, value in enumerate(line): |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
282 |
if value is None: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
283 |
result.append(value) |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
284 |
continue |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
285 |
cbstack = column_callbacks.get(col, None) |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
286 |
if cbstack is None: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
287 |
value = process_value(value, descr[col], encoding, binary) |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
288 |
else: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
289 |
for cb in cbstack: |
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
290 |
value = cb(self, session, value) |
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
|
291 |
result.append(value) |
7341
c419c2d0d13e
add a new method iter_process_result which does the same as proces_result but is a generator (closes #1625374)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7229
diff
changeset
|
292 |
yield result |
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
|
293 |
|
0 | 294 |
def preprocess_entity(self, entity): |
295 |
"""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
|
296 |
to insert/update an entity into a SQL database |
0 | 297 |
""" |
298 |
attrs = {} |
|
299 |
eschema = entity.e_schema |
|
8944
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
300 |
converters = getattr(self.dbhelper, 'TYPE_CONVERTERS', {}) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
301 |
for attr, value in entity.cw_edited.iteritems(): |
6284
c35b2ebeb3c9
[repo/sql] don't do anything when value is None. This will avoid None to be turned into False in the case of a boolean for instance.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
302 |
if value is not None and eschema.subjrels[attr].final: |
0 | 303 |
atype = str(entity.e_schema.destination(attr)) |
8944
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
304 |
if atype in converters: |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
305 |
# It is easier to modify preprocess_entity rather |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
306 |
# than add_entity (native) as this behavior |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
307 |
# may also be used for update. |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
308 |
value = converters[atype](value) |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
309 |
elif atype == 'Password': # XXX could be done using a TYPE_CONVERTERS callback |
0 | 310 |
# if value is a Binary instance, this mean we got it |
311 |
# from a query result and so it is already encrypted |
|
312 |
if isinstance(value, Binary): |
|
313 |
value = value.getvalue() |
|
314 |
else: |
|
315 |
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
|
316 |
value = self._binary(value) |
0 | 317 |
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
|
318 |
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
|
319 |
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
|
320 |
attrs[SQL_PREFIX+'eid'] = entity.eid |
0 | 321 |
return attrs |
322 |
||
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6379
diff
changeset
|
323 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6379
diff
changeset
|
324 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6379
diff
changeset
|
325 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 326 |
|
327 |
from logging import getLogger |
|
328 |
from cubicweb import set_log_methods |
|
329 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
330 |
||
331 |
def init_sqlite_connexion(cnx): |
|
332 |
||
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
|
333 |
class group_concat(object): |
0 | 334 |
def __init__(self): |
335 |
self.values = [] |
|
336 |
def step(self, value): |
|
337 |
if value is not None: |
|
338 |
self.values.append(value) |
|
339 |
def finalize(self): |
|
340 |
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
|
341 |
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
|
342 |
|
0 | 343 |
def _limit_size(text, maxsize, format='text/plain'): |
344 |
if len(text) < maxsize: |
|
345 |
return text |
|
346 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
347 |
text = remove_html_tags(text) |
|
348 |
if len(text) > maxsize: |
|
349 |
text = text[:maxsize] + '...' |
|
350 |
return text |
|
351 |
||
352 |
def limit_size3(text, format, maxsize): |
|
353 |
return _limit_size(text, maxsize, format) |
|
354 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
355 |
||
356 |
def limit_size2(text, maxsize): |
|
357 |
return _limit_size(text, maxsize) |
|
358 |
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
|
359 |
|
7904
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
360 |
from logilab.common.date import strptime |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
361 |
def weekday(ustr): |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
362 |
try: |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
363 |
dt = strptime(ustr, '%Y-%m-%d %H:%M:%S') |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
364 |
except: |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
365 |
dt = strptime(ustr, '%Y-%m-%d') |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
366 |
# expect sunday to be 1, saturday 7 while weekday method return 0 for |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
367 |
# monday |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
368 |
return (dt.weekday() + 1) % 7 |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
369 |
cnx.create_function("WEEKDAY", 1, weekday) |
f41bb38dda7c
[rql, sql] support for weekday function introduced in lgdp 1.7 (closes #1979717)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7341
diff
changeset
|
370 |
|
0 | 371 |
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
|
372 |
yams.constraints.patch_sqlite_decimal() |
0 | 373 |
|
374 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
375 |
sqlite_hooks.append(init_sqlite_connexion) |
|
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
376 |
|
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
377 |
|
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
378 |
def init_postgres_connexion(cnx): |
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
379 |
cnx.cursor().execute('SET TIME ZONE UTC') |
7229
a60522259c2c
[tz postgres support] we've to commit the connection once time-zone is set, else we may loose the setting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7221
diff
changeset
|
380 |
# commit is needed, else setting are lost if the connection is first |
9267
24d9b86dfa54
spelling: rollbacked -> rolled back
Rémi Cardona <remi.cardona@logilab.fr>
parents:
8944
diff
changeset
|
381 |
# rolled back |
7229
a60522259c2c
[tz postgres support] we've to commit the connection once time-zone is set, else we may loose the setting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7221
diff
changeset
|
382 |
cnx.commit() |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
383 |
|
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
384 |
postgres_hooks = SQL_CONNECT_HOOKS.setdefault('postgres', []) |
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
385 |
postgres_hooks.append(init_postgres_connexion) |