author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 30 Jun 2016 11:18:01 +0200 | |
changeset 11412 | ac166217bd8c |
parent 11389 | dcfc446d8eae |
child 11413 | c172fa18565e |
permissions | -rw-r--r-- |
11412
ac166217bd8c
[schema2sql] Drop most of the DB DROP related code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11389
diff
changeset
|
1 |
# copyright 2003-2016 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.""" |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
19 |
from __future__ import print_function |
0 | 20 |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
23 |
import sys |
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
|
24 |
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
|
25 |
import subprocess |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
26 |
from os.path import abspath |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
27 |
from logging import getLogger |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
28 |
from datetime import time, datetime, timedelta |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
29 |
|
10760
c34bbdb18745
[server] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents:
10662
diff
changeset
|
30 |
from six import string_types, text_type |
10607
6519ae8cca0c
[py3k] import filter using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
31 |
from six.moves import filter |
6519ae8cca0c
[py3k] import filter using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
32 |
|
11034
75d752e6daf7
[server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11032
diff
changeset
|
33 |
from pytz import utc |
75d752e6daf7
[server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11032
diff
changeset
|
34 |
|
4849
3827b9ee77ac
missing rename
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
35 |
from logilab import database as db, common as lgc |
10375
28ec01db78b3
[server] Do not use progress bar when stdout is not a TTY
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10125
diff
changeset
|
36 |
from logilab.common.shellutils import ProgressBar, DummyProgressBar |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
37 |
from logilab.common.deprecation import deprecated |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
38 |
from logilab.common.logging_ext import set_log_methods |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
39 |
from logilab.common.date import utctime, utcdatetime, strptime |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4831
diff
changeset
|
40 |
from logilab.database.sqlgen import SQLGenerator |
0 | 41 |
|
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
42 |
from cubicweb import Binary, ConfigurationError |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
43 |
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
|
44 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES |
0 | 45 |
from cubicweb.server import SQL_CONNECT_HOOKS |
1132 | 46 |
from cubicweb.server.utils import crypt_password |
0 | 47 |
|
1783 | 48 |
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
|
49 |
SQL_PREFIX = 'cw_' |
0 | 50 |
|
11035 | 51 |
|
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
|
52 |
def _run_command(cmd): |
10824
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
53 |
if isinstance(cmd, string_types): |
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
54 |
print(cmd) |
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
55 |
return subprocess.call(cmd, shell=True) |
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
56 |
else: |
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
57 |
print(' '.join(cmd)) |
fbefdaa56d2b
[server] fix db-dump with sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents:
10760
diff
changeset
|
58 |
return subprocess.call(cmd) |
4342
b4e186da08f2
handle lgc.adbh api changes within regards of backup/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4322
diff
changeset
|
59 |
|
0 | 60 |
|
9891
3386fd89c914
remove references to global environment variable APYCOT_ROOT
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9724
diff
changeset
|
61 |
def sqlexec(sqlstmts, cursor_or_execute, withpb=True, |
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
|
62 |
pbtitle='', delimiter=';', cnx=None): |
0 | 63 |
"""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
|
64 |
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
|
65 |
|
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 |
: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
|
67 |
: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
|
68 |
: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
|
69 |
|
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
|
70 |
: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
|
71 |
: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
|
72 |
|
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 |
: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
|
74 |
|
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 |
Return the failed statements (same type as sqlstmts) |
0 | 76 |
""" |
77 |
if hasattr(cursor_or_execute, 'execute'): |
|
78 |
execute = cursor_or_execute.execute |
|
79 |
else: |
|
80 |
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
|
81 |
sqlstmts_as_string = False |
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10607
diff
changeset
|
82 |
if isinstance(sqlstmts, string_types): |
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
|
83 |
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
|
84 |
sqlstmts = sqlstmts.split(delimiter) |
0 | 85 |
if withpb: |
10375
28ec01db78b3
[server] Do not use progress bar when stdout is not a TTY
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10125
diff
changeset
|
86 |
if sys.stdout.isatty(): |
28ec01db78b3
[server] Do not use progress bar when stdout is not a TTY
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10125
diff
changeset
|
87 |
pb = ProgressBar(len(sqlstmts), title=pbtitle) |
28ec01db78b3
[server] Do not use progress bar when stdout is not a TTY
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10125
diff
changeset
|
88 |
else: |
28ec01db78b3
[server] Do not use progress bar when stdout is not a TTY
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
10125
diff
changeset
|
89 |
pb = DummyProgressBar() |
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
|
90 |
failed = [] |
0 | 91 |
for sql in sqlstmts: |
92 |
sql = sql.strip() |
|
93 |
if withpb: |
|
94 |
pb.update() |
|
95 |
if not sql: |
|
96 |
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
|
97 |
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
|
98 |
# 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
|
99 |
execute(str(sql)) |
11192
ba952f509af6
[server] print backend exception if we fail to execute an sql script
Julien Cristau <julien.cristau@logilab.fr>
parents:
11057
diff
changeset
|
100 |
except Exception as ex: |
ba952f509af6
[server] print backend exception if we fail to execute an sql script
Julien Cristau <julien.cristau@logilab.fr>
parents:
11057
diff
changeset
|
101 |
print(ex, file=sys.stderr) |
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
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
cnx.commit() |
0 | 108 |
if withpb: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10588
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
return failed |
0 | 113 |
|
114 |
||
115 |
def sqlgrants(schema, driver, user, |
|
116 |
text_index=True, set_owner=True, |
|
117 |
skip_relations=(), skip_entities=()): |
|
118 |
"""return sql to give all access privileges to the given user on the system |
|
119 |
schema |
|
120 |
""" |
|
10200
cceb2c7c02f4
Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10125
diff
changeset
|
121 |
from cubicweb.server.schema2sql import grant_schema |
0 | 122 |
from cubicweb.server.sources import native |
123 |
output = [] |
|
124 |
w = output.append |
|
125 |
w(native.grant_schema(user, set_owner)) |
|
126 |
w('') |
|
127 |
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
|
128 |
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
|
129 |
w(dbhelper.sql_grant_user_on_fti(user)) |
0 | 130 |
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
|
131 |
w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
0 | 132 |
return '\n'.join(output) |
133 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
134 |
|
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
135 |
def sqlschema(schema, driver, text_index=True, |
0 | 136 |
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
|
137 |
skip_relations=PURE_VIRTUAL_RTYPES, skip_entities=()): |
0 | 138 |
"""return the system sql schema, according to the given parameters""" |
10200
cceb2c7c02f4
Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents:
10125
diff
changeset
|
139 |
from cubicweb.server.schema2sql import schema2sql |
0 | 140 |
from cubicweb.server.sources import native |
141 |
if set_owner: |
|
142 |
assert user, 'user is argument required when set_owner is true' |
|
143 |
output = [] |
|
144 |
w = output.append |
|
145 |
w(native.sql_schema(driver)) |
|
146 |
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
|
147 |
dbhelper = db.get_db_helper(driver) |
0 | 148 |
if text_index: |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
149 |
w(dbhelper.sql_init_fti().replace(';', ';;')) |
0 | 150 |
w('') |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
151 |
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
|
152 |
skip_entities=skip_entities, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
153 |
skip_relations=skip_relations).replace(';', ';;')) |
0 | 154 |
if dbhelper.users_support and user: |
155 |
w('') |
|
156 |
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
|
157 |
skip_relations, skip_entities).replace(';', ';;')) |
0 | 158 |
return '\n'.join(output) |
159 |
||
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
160 |
|
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
|
161 |
_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
|
162 |
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
|
163 |
"""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
|
164 |
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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
|
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 |
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
|
170 |
# 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
|
171 |
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
|
172 |
cmds += ['DROP VIEW %s;' % name |
10607
6519ae8cca0c
[py3k] import filter using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
173 |
for name in filter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_views(sqlcursor))] |
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
|
174 |
cmds += ['DROP TABLE %s;' % name |
10607
6519ae8cca0c
[py3k] import filter using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10589
diff
changeset
|
175 |
for name in filter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_tables(sqlcursor))] |
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
|
176 |
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
|
177 |
|
9447
0636c4960259
[multi-sources-removal] Drop cw.server.sources.extlite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9364
diff
changeset
|
178 |
|
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
179 |
class ConnectionWrapper(object): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
180 |
"""handle connection to the system source, at some point associated to a |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
181 |
:class:`Session` |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
182 |
""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
183 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
184 |
# since 3.19, we only have to manage the system source connection |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
185 |
def __init__(self, system_source): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
186 |
# dictionary of (source, connection), indexed by sources'uri |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
187 |
self._source = system_source |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
188 |
self.cnx = system_source.get_connection() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
189 |
self.cu = self.cnx.cursor() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
190 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
191 |
def commit(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
192 |
"""commit the current transaction for this user""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
193 |
# let exception propagates |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
194 |
self.cnx.commit() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
195 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
196 |
def rollback(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
197 |
"""rollback the current transaction for this user""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
198 |
# catch exceptions, rollback other sources anyway |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
199 |
try: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
200 |
self.cnx.rollback() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
201 |
except Exception: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
202 |
self._source.critical('rollback error', exc_info=sys.exc_info()) |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
203 |
# error on rollback, the connection is much probably in a really |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
204 |
# bad state. Replace it by a new one. |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
205 |
self.reconnect() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
206 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
207 |
def close(self, i_know_what_i_do=False): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
208 |
"""close all connections in the set""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
209 |
if i_know_what_i_do is not True: # unexpected closing safety belt |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
210 |
raise RuntimeError('connections set shouldn\'t be closed') |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
211 |
try: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
212 |
self.cu.close() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
213 |
self.cu = None |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
214 |
except Exception: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
215 |
pass |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
216 |
try: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
217 |
self.cnx.close() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
218 |
self.cnx = None |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
219 |
except Exception: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
220 |
pass |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
221 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
222 |
# internals ############################################################### |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
223 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
224 |
def cnxset_freed(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
225 |
"""connections set is being freed from a session""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
226 |
pass # no nothing by default |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
227 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
228 |
def reconnect(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
229 |
"""reopen a connection for this source or all sources if none specified |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
230 |
""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
231 |
try: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
232 |
# properly close existing connection if any |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
233 |
self.cnx.close() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
234 |
except Exception: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
235 |
pass |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
236 |
self._source.info('trying to reconnect') |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
237 |
self.cnx = self._source.get_connection() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
238 |
self.cu = self.cnx.cursor() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
239 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
240 |
@deprecated('[3.19] use .cu instead') |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
241 |
def __getitem__(self, uri): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
242 |
assert uri == 'system' |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
243 |
return self.cu |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
244 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
245 |
@deprecated('[3.19] use repo.system_source instead') |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
246 |
def source(self, uid): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
247 |
assert uid == 'system' |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
248 |
return self._source |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
249 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
250 |
@deprecated('[3.19] use .cnx instead') |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
251 |
def connection(self, uid): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
252 |
assert uid == 'system' |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
253 |
return self.cnx |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
254 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
255 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
256 |
class SqliteConnectionWrapper(ConnectionWrapper): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
257 |
"""Sqlite specific connection wrapper: close the connection each time it's |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
258 |
freed (and reopen it later when needed) |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
259 |
""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
260 |
def __init__(self, system_source): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
261 |
# don't call parent's __init__, we don't want to initiate the connection |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
262 |
self._source = system_source |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
263 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
264 |
_cnx = None |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
265 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
266 |
def cnxset_freed(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
267 |
self.cu.close() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
268 |
self.cnx.close() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
269 |
self.cnx = self.cu = None |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
270 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
271 |
@property |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
272 |
def cnx(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
273 |
if self._cnx is None: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
274 |
self._cnx = self._source.get_connection() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
275 |
self._cu = self._cnx.cursor() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
276 |
return self._cnx |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
277 |
@cnx.setter |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
278 |
def cnx(self, value): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
279 |
self._cnx = value |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
280 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
281 |
@property |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
282 |
def cu(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
283 |
if self._cnx is None: |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
284 |
self._cnx = self._source.get_connection() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
285 |
self._cu = self._cnx.cursor() |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
286 |
return self._cu |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
287 |
@cu.setter |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
288 |
def cu(self, value): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
289 |
self._cu = value |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
290 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
291 |
|
0 | 292 |
class SQLAdapterMixIn(object): |
293 |
"""Mixin for SQL data sources, getting a connection from a configuration |
|
294 |
dictionary and handling connection locking |
|
295 |
""" |
|
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
296 |
cnx_wrap = ConnectionWrapper |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
297 |
|
9724
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
298 |
def __init__(self, source_config, repairing=False): |
0 | 299 |
try: |
300 |
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
|
301 |
dbname = source_config['db-name'] |
0 | 302 |
except KeyError: |
303 |
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
|
304 |
dbhost = source_config.get('db-host') |
0 | 305 |
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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
dbextraargs = source_config.get('db-extra-arguments') |
10125
bc6461a7d2da
[server] add a db-namespace option in source definition (closes #1631339)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10086
diff
changeset
|
311 |
dbnamespace = source_config.get('db-namespace') |
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
|
312 |
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
|
313 |
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
|
314 |
dbpassword, dbextraargs, |
10125
bc6461a7d2da
[server] add a db-namespace option in source definition (closes #1631339)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
10086
diff
changeset
|
315 |
dbencoding, dbnamespace) |
0 | 316 |
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
|
317 |
# 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
|
318 |
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
|
319 |
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
|
320 |
self.InterfaceError = dbapi_module.InterfaceError |
5605
2604545d7dd9
[win32 SQLServer] connection lost detection
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
self._dbencoding = dbencoding |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
325 |
if self.dbdriver == 'sqlite': |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
326 |
self.cnx_wrap = SqliteConnectionWrapper |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
327 |
self.dbhelper.dbname = abspath(self.dbhelper.dbname) |
9724
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
328 |
if not repairing: |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
329 |
statement_timeout = int(source_config.get('db-statement-timeout', 0)) |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
330 |
if statement_timeout > 0: |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
331 |
def set_postgres_timeout(cnx): |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
332 |
cnx.cursor().execute('SET statement_timeout to %d' % statement_timeout) |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
333 |
cnx.commit() |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
334 |
postgres_hooks = SQL_CONNECT_HOOKS['postgres'] |
e45bf9baa7b7
Add a db-statement-timeout option for postgresql sources
Julien Cristau <julien.cristau@logilab.fr>
parents:
9466
diff
changeset
|
335 |
postgres_hooks.append(set_postgres_timeout) |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
336 |
|
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
337 |
def wrapped_connection(self): |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
338 |
"""open and return a connection to the database, wrapped into a class |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
339 |
handling reconnection and all |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
340 |
""" |
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
341 |
return self.cnx_wrap(self) |
1619
e4845b54a704
force proper date/datetime according to type (necessary for sqlite at least)
sylvain.thenault@logilab.fr
parents:
1408
diff
changeset
|
342 |
|
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
|
343 |
def get_connection(self): |
0 | 344 |
"""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
|
345 |
return self.dbhelper.get_connection() |
0 | 346 |
|
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
|
347 |
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
|
348 |
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
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
|
353 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
354 |
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
|
355 |
for cmd in self.dbhelper.restore_commands(backupfile, |
4854
b06d2a3b27d9
logilab.db compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
356 |
keepownership=False, |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
357 |
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
|
358 |
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
|
359 |
if not confirm(' [Failed] Continue anyway?', default='n'): |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4179
diff
changeset
|
360 |
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
|
361 |
|
0 | 362 |
def merge_args(self, args, query_args): |
363 |
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
|
364 |
newargs = {} |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10651
diff
changeset
|
365 |
for key, val in args.items(): |
0 | 366 |
# convert cubicweb binary into db binary |
367 |
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
|
368 |
val = self._binary(val.getvalue()) |
10643
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
369 |
# convert timestamp to utc. |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
370 |
# expect SET TiME ZONE to UTC at connection opening time. |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
371 |
# This shouldn't change anything for datetime without TZ. |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
372 |
elif isinstance(val, datetime) and val.tzinfo is not None: |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
373 |
val = utcdatetime(val) |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
374 |
elif isinstance(val, time) and val.tzinfo is not None: |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
375 |
val = utctime(val) |
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
|
376 |
newargs[key] = val |
0 | 377 |
# should not collide |
10643
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
378 |
assert not (frozenset(newargs) & frozenset(query_args)), \ |
cfded6d0da11
fix bad-caching of datetime with tz info at sql generation time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10411
diff
changeset
|
379 |
'unexpected collision: %s' % (frozenset(newargs) & frozenset(query_args)) |
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
|
380 |
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
|
381 |
return newargs |
0 | 382 |
return query_args |
383 |
||
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
384 |
def process_result(self, cursor, cnx=None, column_callbacks=None): |
0 | 385 |
"""return a list of CubicWeb compliant values from data in the given cursor |
386 |
""" |
|
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
387 |
return list(self.iter_process_result(cursor, cnx, column_callbacks)) |
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
|
388 |
|
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
389 |
def iter_process_result(self, cursor, cnx, column_callbacks=None): |
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
|
390 |
"""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
|
391 |
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
|
392 |
""" |
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
|
393 |
# 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
|
394 |
# 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
|
395 |
# 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
|
396 |
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
|
397 |
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
|
398 |
Binary) |
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
399 |
assert cnx |
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
400 |
return self._cb_process_result(cursor, column_callbacks, cnx) |
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
|
401 |
|
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
402 |
def _cb_process_result(self, cursor, column_callbacks, cnx): |
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
|
403 |
# 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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
# /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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
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
|
420 |
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
|
421 |
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
|
422 |
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
|
423 |
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
|
424 |
for cb in cbstack: |
10086
98bc2ca1a816
[source/native] session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9891
diff
changeset
|
425 |
value = cb(self, cnx, 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
|
426 |
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
|
427 |
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
|
428 |
|
0 | 429 |
def preprocess_entity(self, entity): |
430 |
"""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
|
431 |
to insert/update an entity into a SQL database |
0 | 432 |
""" |
433 |
attrs = {} |
|
434 |
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
|
435 |
converters = getattr(self.dbhelper, 'TYPE_CONVERTERS', {}) |
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10651
diff
changeset
|
436 |
for attr, value in entity.cw_edited.items(): |
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
|
437 |
if value is not None and eschema.subjrels[attr].final: |
0 | 438 |
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
|
439 |
if atype in converters: |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
440 |
# 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
|
441 |
# 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
|
442 |
# 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
|
443 |
value = converters[atype](value) |
b167f039b6cb
[sql] preprocess_entity uses lgdb helper's SQL converters.
Vincent Michel <vincent.michel@logilab.fr>
parents:
8755
diff
changeset
|
444 |
elif atype == 'Password': # XXX could be done using a TYPE_CONVERTERS callback |
0 | 445 |
# if value is a Binary instance, this mean we got it |
446 |
# from a query result and so it is already encrypted |
|
447 |
if isinstance(value, Binary): |
|
448 |
value = value.getvalue() |
|
449 |
else: |
|
450 |
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
|
451 |
value = self._binary(value) |
0 | 452 |
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
|
453 |
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
|
454 |
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
|
455 |
attrs[SQL_PREFIX+'eid'] = entity.eid |
0 | 456 |
return attrs |
457 |
||
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6379
diff
changeset
|
458 |
# 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
|
459 |
# 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
|
460 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 461 |
|
462 |
set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter')) |
|
463 |
||
9447
0636c4960259
[multi-sources-removal] Drop cw.server.sources.extlite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9364
diff
changeset
|
464 |
|
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9447
diff
changeset
|
465 |
# connection initialization functions ########################################## |
9447
0636c4960259
[multi-sources-removal] Drop cw.server.sources.extlite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9364
diff
changeset
|
466 |
|
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
467 |
def _install_sqlite_querier_patch(): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
468 |
"""This monkey-patch hotfixes a bug sqlite causing some dates to be returned as strings rather than |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
469 |
date objects (http://www.sqlite.org/cvstrac/tktview?tn=1327,33) |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
470 |
""" |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
471 |
from cubicweb.server.querier import QuerierHelper |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
472 |
|
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
473 |
if hasattr(QuerierHelper, '_sqlite_patched'): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
474 |
return # already monkey patched |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
475 |
|
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
476 |
def wrap_execute(base_execute): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
477 |
def new_execute(*args, **kwargs): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
478 |
rset = base_execute(*args, **kwargs) |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
479 |
if rset.description: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
480 |
found_date = False |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
481 |
for row, rowdesc in zip(rset, rset.description): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
482 |
for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)): |
11032
38afb7e23c6c
[server] fix SQLite patch to ensure TZDatetime are properly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11031
diff
changeset
|
483 |
if vtype in ('TZDatetime', 'Date', 'Datetime') \ |
38afb7e23c6c
[server] fix SQLite patch to ensure TZDatetime are properly typed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11031
diff
changeset
|
484 |
and isinstance(value, text_type): |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
485 |
found_date = True |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
486 |
value = value.rsplit('.', 1)[0] |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
487 |
try: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
488 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
489 |
except Exception: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
490 |
row[cellindex] = strptime(value, '%Y-%m-%d') |
11034
75d752e6daf7
[server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11032
diff
changeset
|
491 |
if vtype == 'TZDatetime': |
75d752e6daf7
[server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11032
diff
changeset
|
492 |
row[cellindex] = row[cellindex].replace(tzinfo=utc) |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
493 |
if vtype == 'Time' and isinstance(value, text_type): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
494 |
found_date = True |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
495 |
try: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
496 |
row[cellindex] = strptime(value, '%H:%M:%S') |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
497 |
except Exception: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
498 |
# DateTime used as Time? |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
499 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
500 |
if vtype == 'Interval' and isinstance(value, int): |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
501 |
found_date = True |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
502 |
# XXX value is in number of seconds? |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
503 |
row[cellindex] = timedelta(0, value, 0) |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
504 |
if not found_date: |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
505 |
break |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
506 |
return rset |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
507 |
return new_execute |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
508 |
|
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
509 |
QuerierHelper.execute = wrap_execute(QuerierHelper.execute) |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
510 |
QuerierHelper._sqlite_patched = True |
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
511 |
|
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
512 |
|
11031
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
513 |
def _init_sqlite_connection(cnx): |
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
514 |
"""Internal function that will be called to init a sqlite connection""" |
11030
c1fdd22232d1
[server] always monkeypatch QuerierHelper to handle dates and times on SQLite
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
10824
diff
changeset
|
515 |
_install_sqlite_querier_patch() |
0 | 516 |
|
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
|
517 |
class group_concat(object): |
0 | 518 |
def __init__(self): |
9364
73bd5012336f
Make the GROUP_CONCAT aggregate function not repeat values (closes #3223975)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9340
diff
changeset
|
519 |
self.values = set() |
0 | 520 |
def step(self, value): |
521 |
if value is not None: |
|
9364
73bd5012336f
Make the GROUP_CONCAT aggregate function not repeat values (closes #3223975)
Julien Cristau <julien.cristau@logilab.fr>
parents:
9340
diff
changeset
|
522 |
self.values.add(value) |
0 | 523 |
def finalize(self): |
10760
c34bbdb18745
[server] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents:
10662
diff
changeset
|
524 |
return ', '.join(text_type(v) for v in self.values) |
9334
ea12401c0a68
[sqlutils] avoid a crash with sqlite when using group_concat (closes #3331906)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9267
diff
changeset
|
525 |
|
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
|
526 |
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
|
527 |
|
0 | 528 |
def _limit_size(text, maxsize, format='text/plain'): |
529 |
if len(text) < maxsize: |
|
530 |
return text |
|
531 |
if format in ('text/html', 'text/xhtml', 'text/xml'): |
|
532 |
text = remove_html_tags(text) |
|
533 |
if len(text) > maxsize: |
|
534 |
text = text[:maxsize] + '...' |
|
535 |
return text |
|
536 |
||
537 |
def limit_size3(text, format, maxsize): |
|
538 |
return _limit_size(text, maxsize, format) |
|
539 |
cnx.create_function("LIMIT_SIZE", 3, limit_size3) |
|
540 |
||
541 |
def limit_size2(text, maxsize): |
|
542 |
return _limit_size(text, maxsize) |
|
543 |
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
|
544 |
|
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
|
545 |
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
|
546 |
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
|
547 |
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
|
548 |
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
|
549 |
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
|
550 |
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
|
551 |
# 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
|
552 |
# 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
|
553 |
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
|
554 |
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
|
555 |
|
10205
737271ffe407
Ask sqlite to check foreign keys
Julien Cristau <julien.cristau@logilab.fr>
parents:
10200
diff
changeset
|
556 |
cnx.cursor().execute("pragma foreign_keys = on") |
737271ffe407
Ask sqlite to check foreign keys
Julien Cristau <julien.cristau@logilab.fr>
parents:
10200
diff
changeset
|
557 |
|
0 | 558 |
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
|
559 |
yams.constraints.patch_sqlite_decimal() |
0 | 560 |
|
561 |
sqlite_hooks = SQL_CONNECT_HOOKS.setdefault('sqlite', []) |
|
11031
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
562 |
sqlite_hooks.append(_init_sqlite_connection) |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
563 |
|
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
564 |
|
11031
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
565 |
def _init_postgres_connection(cnx): |
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
566 |
"""Internal function that will be called to init a postgresql connection""" |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
567 |
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
|
568 |
# 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
|
569 |
# 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
|
570 |
cnx.commit() |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
571 |
|
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7083
diff
changeset
|
572 |
postgres_hooks = SQL_CONNECT_HOOKS.setdefault('postgres', []) |
11031
780939fc06da
[server] fix name and add docstring to connection initialization functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
11030
diff
changeset
|
573 |
postgres_hooks.append(_init_postgres_connection) |