equal
deleted
inserted
replaced
1 """SQL utilities functions and classes. |
1 """SQL utilities functions and classes. |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
9 from logilab.common.shellutils import ProgressBar |
9 from logilab.common.shellutils import ProgressBar |
16 from cubicweb import Binary, ConfigurationError |
16 from cubicweb import Binary, ConfigurationError |
17 from cubicweb.common.uilib import remove_html_tags |
17 from cubicweb.common.uilib import remove_html_tags |
18 from cubicweb.server import SQL_CONNECT_HOOKS |
18 from cubicweb.server import SQL_CONNECT_HOOKS |
19 from cubicweb.server.utils import crypt_password, cartesian_product |
19 from cubicweb.server.utils import crypt_password, cartesian_product |
20 |
20 |
|
21 SQL_PREFIX = 'cw_' |
21 |
22 |
22 def sqlexec(sqlstmts, cursor_or_execute, withpb=True, delimiter=';'): |
23 def sqlexec(sqlstmts, cursor_or_execute, withpb=True, delimiter=';'): |
23 """execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
24 """execute sql statements ignoring DROP/ CREATE GROUP or USER statements |
24 error. If a cnx is given, commit at each statement |
25 error. If a cnx is given, commit at each statement |
25 """ |
26 """ |
56 w('') |
57 w('') |
57 if text_index: |
58 if text_index: |
58 indexer = get_indexer(driver) |
59 indexer = get_indexer(driver) |
59 w(indexer.sql_grant_user(user)) |
60 w(indexer.sql_grant_user(user)) |
60 w('') |
61 w('') |
61 w(grant_schema(schema, user, set_owner, skip_entities=skip_entities)) |
62 w(grant_schema(schema, user, set_owner, skip_entities=skip_entities, prefix=SQL_PREFIX)) |
62 return '\n'.join(output) |
63 return '\n'.join(output) |
63 |
64 |
64 |
65 |
65 def sqlschema(schema, driver, text_index=True, |
66 def sqlschema(schema, driver, text_index=True, |
66 user=None, set_owner=False, |
67 user=None, set_owner=False, |
77 if text_index: |
78 if text_index: |
78 indexer = get_indexer(driver) |
79 indexer = get_indexer(driver) |
79 w(indexer.sql_init_fti()) |
80 w(indexer.sql_init_fti()) |
80 w('') |
81 w('') |
81 dbhelper = get_adv_func_helper(driver) |
82 dbhelper = get_adv_func_helper(driver) |
82 w(schema2sql(dbhelper, schema, |
83 w(schema2sql(dbhelper, schema, prefix=SQL_PREFIX, |
83 skip_entities=skip_entities, skip_relations=skip_relations)) |
84 skip_entities=skip_entities, skip_relations=skip_relations)) |
84 if dbhelper.users_support and user: |
85 if dbhelper.users_support and user: |
85 w('') |
86 w('') |
86 w(sqlgrants(schema, driver, user, text_index, set_owner, |
87 w(sqlgrants(schema, driver, user, text_index, set_owner, |
87 skip_relations, skip_entities)) |
88 skip_relations, skip_entities)) |
99 w('') |
100 w('') |
100 if text_index: |
101 if text_index: |
101 indexer = get_indexer(driver) |
102 indexer = get_indexer(driver) |
102 w(indexer.sql_drop_fti()) |
103 w(indexer.sql_drop_fti()) |
103 w('') |
104 w('') |
104 w(dropschema2sql(schema, |
105 w(dropschema2sql(schema, prefix=SQL_PREFIX, |
105 skip_entities=skip_entities, skip_relations=skip_relations)) |
106 skip_entities=skip_entities, skip_relations=skip_relations)) |
106 return '\n'.join(output) |
107 return '\n'.join(output) |
107 |
108 |
108 |
109 |
109 |
110 |
177 return results |
178 return results |
178 |
179 |
179 |
180 |
180 def preprocess_entity(self, entity): |
181 def preprocess_entity(self, entity): |
181 """return a dictionary to use as extra argument to cursor.execute |
182 """return a dictionary to use as extra argument to cursor.execute |
182 to insert/update an entity |
183 to insert/update an entity into a SQL database |
183 """ |
184 """ |
184 attrs = {} |
185 attrs = {} |
185 eschema = entity.e_schema |
186 eschema = entity.e_schema |
186 for attr, value in entity.items(): |
187 for attr, value in entity.items(): |
187 rschema = eschema.subject_relation(attr) |
188 rschema = eschema.subject_relation(attr) |
196 value = value.getvalue() |
197 value = value.getvalue() |
197 else: |
198 else: |
198 value = crypt_password(value) |
199 value = crypt_password(value) |
199 elif isinstance(value, Binary): |
200 elif isinstance(value, Binary): |
200 value = self.binary(value.getvalue()) |
201 value = self.binary(value.getvalue()) |
201 attrs[str(attr)] = value |
202 attrs[SQL_PREFIX+str(attr)] = value |
202 return attrs |
203 return attrs |
203 |
204 |
204 |
205 |
205 from logging import getLogger |
206 from logging import getLogger |
206 from cubicweb import set_log_methods |
207 from cubicweb import set_log_methods |