40 from logilab.common.decorators import cached, clear_cache |
40 from logilab.common.decorators import cached, clear_cache |
41 from logilab.common.configuration import Method |
41 from logilab.common.configuration import Method |
42 from logilab.common.shellutils import getlogin |
42 from logilab.common.shellutils import getlogin |
43 from logilab.database import get_db_helper |
43 from logilab.database import get_db_helper |
44 |
44 |
|
45 from yams import schema2sql as y2sql |
|
46 |
45 from cubicweb import UnknownEid, AuthenticationError, ValidationError, Binary |
47 from cubicweb import UnknownEid, AuthenticationError, ValidationError, Binary |
46 from cubicweb import transaction as tx, server, neg_role |
48 from cubicweb import transaction as tx, server, neg_role |
47 from cubicweb.schema import VIRTUAL_RTYPES |
49 from cubicweb.schema import VIRTUAL_RTYPES |
48 from cubicweb.cwconfig import CubicWebNoAppConfiguration |
50 from cubicweb.cwconfig import CubicWebNoAppConfiguration |
49 from cubicweb.server import hook |
51 from cubicweb.server import hook |
125 ' OR '.join(clauses)) |
127 ' OR '.join(clauses)) |
126 else: |
128 else: |
127 restr = '(%s)' % ' OR '.join(clauses) |
129 restr = '(%s)' % ' OR '.join(clauses) |
128 return '%s WHERE %s' % (select, restr) |
130 return '%s WHERE %s' % (select, restr) |
129 |
131 |
|
132 def rdef_table_column(rdef): |
|
133 """return table and column used to store the given relation definition in |
|
134 the database |
|
135 """ |
|
136 return (SQL_PREFIX + str(rdef.subject), |
|
137 SQL_PREFIX + str(rdef.rtype)) |
|
138 |
|
139 def rdef_physical_info(dbhelper, rdef): |
|
140 """return backend type and a boolean flag if NULL values should be allowed |
|
141 for a given relation definition |
|
142 """ |
|
143 coltype = y2sql.type_from_constraints(dbhelper, rdef.object, |
|
144 rdef.constraints, creating=False) |
|
145 allownull = rdef.cardinality[0] != '1' |
|
146 return coltype, allownull |
130 |
147 |
131 class UndoException(Exception): |
148 class UndoException(Exception): |
132 """something went wrong during undoing""" |
149 """something went wrong during undoing""" |
133 |
150 |
134 |
151 |
672 pass |
689 pass |
673 raise |
690 raise |
674 |
691 |
675 # short cut to method requiring advanced db helper usage ################## |
692 # short cut to method requiring advanced db helper usage ################## |
676 |
693 |
|
694 def update_rdef_column(self, session, rdef): |
|
695 """update physical column for a relation definition (final or inlined) |
|
696 """ |
|
697 table, column = rdef_table_column(rdef) |
|
698 coltype, allownull = rdef_physical_info(self.dbhelper, rdef) |
|
699 if not self.dbhelper.alter_column_support: |
|
700 self.error("backend can't alter %s.%s to %s%s", table, column, coltype, |
|
701 not allownull and 'NOT NULL' or '') |
|
702 return |
|
703 self.dbhelper.change_col_type(LogCursor(session.pool[self.uri]), |
|
704 table, column, coltype, allownull) |
|
705 self.info('altered %s.%s: now %s%s', table, column, coltype, |
|
706 not allownull and 'NOT NULL' or '') |
|
707 |
|
708 def update_rdef_null_allowed(self, session, rdef): |
|
709 """update NULL / NOT NULL of physical column for a relation definition |
|
710 (final or inlined) |
|
711 """ |
|
712 if not self.dbhelper.alter_column_support: |
|
713 # not supported (and NOT NULL not set by yams in that case, so no |
|
714 # worry) |
|
715 return |
|
716 table, column = rdef_table_column(rdef) |
|
717 coltype, allownull = rdef_physical_info(self.dbhelper, rdef) |
|
718 self.dbhelper.set_null_allowed(LogCursor(session.pool[self.uri]), |
|
719 table, column, coltype, allownull) |
|
720 |
|
721 def update_rdef_indexed(self, session, rdef): |
|
722 table, column = rdef_table_column(rdef) |
|
723 if rdef.indexed: |
|
724 self.create_index(session, table, column) |
|
725 else: |
|
726 self.drop_index(session, table, column) |
|
727 |
|
728 def update_rdef_unique(self, session, rdef): |
|
729 table, column = rdef_table_column(rdef) |
|
730 if rdef.constraint_by_type('UniqueConstraint'): |
|
731 self.create_index(session, table, column, unique=True) |
|
732 else: |
|
733 self.drop_index(session, table, column, unique=True) |
|
734 |
677 def create_index(self, session, table, column, unique=False): |
735 def create_index(self, session, table, column, unique=False): |
678 cursor = LogCursor(session.pool[self.uri]) |
736 cursor = LogCursor(session.pool[self.uri]) |
679 self.dbhelper.create_index(cursor, table, column, unique) |
737 self.dbhelper.create_index(cursor, table, column, unique) |
680 |
738 |
681 def drop_index(self, session, table, column, unique=False): |
739 def drop_index(self, session, table, column, unique=False): |
682 cursor = LogCursor(session.pool[self.uri]) |
740 cursor = LogCursor(session.pool[self.uri]) |
683 self.dbhelper.drop_index(cursor, table, column, unique) |
741 self.dbhelper.drop_index(cursor, table, column, unique) |
684 |
|
685 def change_col_type(self, session, table, column, coltype, null_allowed): |
|
686 cursor = LogCursor(session.pool[self.uri]) |
|
687 self.dbhelper.change_col_type(cursor, table, column, coltype, null_allowed) |
|
688 |
|
689 def set_null_allowed(self, session, table, column, coltype, null_allowed): |
|
690 cursor = LogCursor(session.pool[self.uri]) |
|
691 self.dbhelper.set_null_allowed(cursor, table, column, coltype, null_allowed) |
|
692 |
742 |
693 # system source interface ################################################# |
743 # system source interface ################################################# |
694 |
744 |
695 def eid_type_source(self, session, eid): |
745 def eid_type_source(self, session, eid): |
696 """return a tuple (type, source, extid) for the entity with id <eid>""" |
746 """return a tuple (type, source, extid) for the entity with id <eid>""" |