1 """Adapters for native cubicweb sources. |
1 """Adapters for native cubicweb sources. |
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 threading import Lock |
9 from threading import Lock |
16 |
16 |
17 from indexer import get_indexer |
17 from indexer import get_indexer |
18 |
18 |
19 from cubicweb import UnknownEid, AuthenticationError, Binary, server |
19 from cubicweb import UnknownEid, AuthenticationError, Binary, server |
20 from cubicweb.server.utils import crypt_password |
20 from cubicweb.server.utils import crypt_password |
21 from cubicweb.server.sqlutils import SQLAdapterMixIn |
21 from cubicweb.server.sqlutils import SQL_PREFIX, SQLAdapterMixIn |
22 from cubicweb.server.rqlannotation import set_qdata |
22 from cubicweb.server.rqlannotation import set_qdata |
23 from cubicweb.server.sources import AbstractSource |
23 from cubicweb.server.sources import AbstractSource |
24 from cubicweb.server.sources.rql2sql import SQLGenerator |
24 from cubicweb.server.sources.rql2sql import SQLGenerator |
25 |
25 |
26 |
26 |
365 continue |
365 continue |
366 |
366 |
367 def add_entity(self, session, entity): |
367 def add_entity(self, session, entity): |
368 """add a new entity to the source""" |
368 """add a new entity to the source""" |
369 attrs = self.preprocess_entity(entity) |
369 attrs = self.preprocess_entity(entity) |
370 sql = self.sqlgen.insert(str(entity.e_schema), attrs) |
370 sql = self.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs) |
371 self.doexec(session.pool[self.uri], sql, attrs) |
371 self.doexec(session.pool[self.uri], sql, attrs) |
372 |
372 |
373 def update_entity(self, session, entity): |
373 def update_entity(self, session, entity): |
374 """replace an entity in the source""" |
374 """replace an entity in the source""" |
375 attrs = self.preprocess_entity(entity) |
375 attrs = self.preprocess_entity(entity) |
376 sql = self.sqlgen.update(str(entity.e_schema), attrs, ['eid']) |
376 sql = self.sqlgen.update(SQL_PREFIX + str(entity.e_schema), attrs, [SQL_PREFIX + 'eid']) |
377 self.doexec(session.pool[self.uri], sql, attrs) |
377 self.doexec(session.pool[self.uri], sql, attrs) |
378 |
378 |
379 def delete_entity(self, session, etype, eid): |
379 def delete_entity(self, session, etype, eid): |
380 """delete an entity from the source""" |
380 """delete an entity from the source""" |
381 attrs = {'eid': eid} |
381 attrs = {SQL_PREFIX + 'eid': eid} |
382 sql = self.sqlgen.delete(etype, attrs) |
382 sql = self.sqlgen.delete(SQL_PREFIX + etype, attrs) |
383 self.doexec(session.pool[self.uri], sql, attrs) |
383 self.doexec(session.pool[self.uri], sql, attrs) |
384 |
384 |
385 def add_relation(self, session, subject, rtype, object): |
385 def add_relation(self, session, subject, rtype, object): |
386 """add a relation to the source""" |
386 """add a relation to the source""" |
387 attrs = {'eid_from': subject, 'eid_to': object} |
387 attrs = {'eid_from': subject, 'eid_to': object} |
390 |
390 |
391 def delete_relation(self, session, subject, rtype, object): |
391 def delete_relation(self, session, subject, rtype, object): |
392 """delete a relation from the source""" |
392 """delete a relation from the source""" |
393 rschema = self.schema.rschema(rtype) |
393 rschema = self.schema.rschema(rtype) |
394 if rschema.inlined: |
394 if rschema.inlined: |
395 etype = session.describe(subject)[0] |
395 table = SQL_PREFIX + session.describe(subject)[0] |
396 sql = 'UPDATE %s SET %s=NULL WHERE eid=%%(eid)s' % (etype, rtype) |
396 column = SQL_PREFIX + rtype |
|
397 sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, |
|
398 SQL_PREFIX) |
397 attrs = {'eid' : subject} |
399 attrs = {'eid' : subject} |
398 else: |
400 else: |
399 attrs = {'eid_from': subject, 'eid_to': object} |
401 attrs = {'eid_from': subject, 'eid_to': object} |
400 sql = self.sqlgen.delete('%s_relation' % rtype, attrs) |
402 sql = self.sqlgen.delete('%s_relation' % rtype, attrs) |
401 self.doexec(session.pool[self.uri], sql, attrs) |
403 self.doexec(session.pool[self.uri], sql, attrs) |