26 from datetime import date, datetime, time |
26 from datetime import date, datetime, time |
27 from collections import defaultdict |
27 from collections import defaultdict |
28 |
28 |
29 from six import string_types, integer_types, text_type, add_metaclass |
29 from six import string_types, integer_types, text_type, add_metaclass |
30 from six.moves import cPickle as pickle, range |
30 from six.moves import cPickle as pickle, range |
31 |
|
32 from logilab.common.deprecation import class_deprecated |
|
33 |
31 |
34 from cubicweb.utils import make_uid |
32 from cubicweb.utils import make_uid |
35 from cubicweb.server.sqlutils import SQL_PREFIX |
33 from cubicweb.server.sqlutils import SQL_PREFIX |
36 from cubicweb.dataimport.stores import NoHookRQLObjectStore |
34 from cubicweb.dataimport.stores import NoHookRQLObjectStore |
37 |
35 |
194 # We push the value to the new formatted row |
192 # We push the value to the new formatted row |
195 # if the value is not None and could be converted to a string. |
193 # if the value is not None and could be converted to a string. |
196 formatted_row.append(value) |
194 formatted_row.append(value) |
197 rows.append('\t'.join(formatted_row)) |
195 rows.append('\t'.join(formatted_row)) |
198 return StringIO('\n'.join(rows)) |
196 return StringIO('\n'.join(rows)) |
199 |
|
200 |
|
201 @add_metaclass(class_deprecated) |
|
202 class SQLGenObjectStore(NoHookRQLObjectStore): |
|
203 """Controller of the data import process. This version is based |
|
204 on direct insertions throught SQL command (COPY FROM or execute many). |
|
205 |
|
206 >>> store = SQLGenObjectStore(cnx) |
|
207 >>> store.create_entity('Person', ...) |
|
208 >>> store.flush() |
|
209 """ |
|
210 __deprecation_warning__ = '[3.23] this class is deprecated, use MassiveObjectStore instead' |
|
211 |
|
212 def __init__(self, cnx, dump_output_dir=None, nb_threads_statement=1): |
|
213 """ |
|
214 Initialize a SQLGenObjectStore. |
|
215 |
|
216 Parameters: |
|
217 |
|
218 - cnx: connection on the cubicweb instance |
|
219 - dump_output_dir: a directory to dump failed statements |
|
220 for easier recovery. Default is None (no dump). |
|
221 """ |
|
222 super(SQLGenObjectStore, self).__init__(cnx) |
|
223 ### hijack default source |
|
224 self._system_source = SQLGenSourceWrapper( |
|
225 self._system_source, cnx.vreg.schema, |
|
226 dump_output_dir=dump_output_dir) |
|
227 ### XXX This is done in super().__init__(), but should be |
|
228 ### redone here to link to the correct source |
|
229 self._add_relation = self._system_source.add_relation |
|
230 self.indexes_etypes = {} |
|
231 if nb_threads_statement != 1: |
|
232 warnings.warn('[3.21] SQLGenObjectStore is no longer threaded', DeprecationWarning) |
|
233 |
|
234 def flush(self): |
|
235 """Flush data to the database""" |
|
236 self._system_source.flush() |
|
237 |
|
238 def relate(self, subj_eid, rtype, obj_eid, **kwargs): |
|
239 if subj_eid is None or obj_eid is None: |
|
240 return |
|
241 # XXX Could subjtype be inferred ? |
|
242 self._add_relation(self._cnx, subj_eid, rtype, obj_eid, |
|
243 self.rschema(rtype).inlined, **kwargs) |
|
244 if self.rschema(rtype).symmetric: |
|
245 self._add_relation(self._cnx, obj_eid, rtype, subj_eid, |
|
246 self.rschema(rtype).inlined, **kwargs) |
|
247 |
|
248 def drop_indexes(self, etype): |
|
249 """Drop indexes for a given entity type""" |
|
250 if etype not in self.indexes_etypes: |
|
251 cu = self._cnx.cnxset.cu |
|
252 def index_to_attr(index): |
|
253 """turn an index name to (database) attribute name""" |
|
254 return index.replace(etype.lower(), '').replace('idx', '').strip('_') |
|
255 indices = [(index, index_to_attr(index)) |
|
256 for index in self._system_source.dbhelper.list_indices(cu, etype) |
|
257 # Do not consider 'cw_etype_pkey' index |
|
258 if not index.endswith('key')] |
|
259 self.indexes_etypes[etype] = indices |
|
260 for index, attr in self.indexes_etypes[etype]: |
|
261 self._cnx.system_sql('DROP INDEX %s' % index) |
|
262 |
|
263 def create_indexes(self, etype): |
|
264 """Recreate indexes for a given entity type""" |
|
265 for index, attr in self.indexes_etypes.get(etype, []): |
|
266 sql = 'CREATE INDEX %s ON cw_%s(%s)' % (index, etype, attr) |
|
267 self._cnx.system_sql(sql) |
|
268 |
197 |
269 |
198 |
270 ########################################################################### |
199 ########################################################################### |
271 ## SQL Source ############################################################# |
200 ## SQL Source ############################################################# |
272 ########################################################################### |
201 ########################################################################### |