Skip to content

Commit b489c21

Browse files
committedMay 31, 2019
[dbmanager] add promary key to the view created by versioning plugin
(fix #25888)
1 parent 2cf1624 commit b489c21

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed
 

‎python/plugins/db_manager/db_plugins/postgis/plugins/versioning/dlg_versioning.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,21 @@ def sql_setPkey(self):
183183
self.schematable, self.origPkeyName, self.colPkey)
184184

185185
def sql_currentView(self):
186-
cols = ",".join(self.columns)
186+
cols = self.colPkey + "," + ",".join(self.columns)
187187

188188
return u"CREATE VIEW %(view)s AS SELECT %(cols)s FROM %(schematable)s WHERE %(end)s IS NULL;" % \
189189
{'view': self.view, 'cols': cols, 'schematable': self.schematable, 'end': self.colEnd}
190190

191191
def sql_functions(self):
192192
cols = ",".join(self.columns)
193+
all_cols = self.colPkey + "," + ",".join(self.columns)
193194
old_cols = ",".join([u"OLD." + x for x in self.columns])
194195

195196
sql = u"""
196197
CREATE OR REPLACE FUNCTION %(func_at_time)s(timestamp)
197198
RETURNS SETOF %(view)s AS
198199
$$
199-
SELECT %(cols)s FROM %(schematable)s WHERE
200+
SELECT %(all_cols)s FROM %(schematable)s WHERE
200201
( SELECT CASE WHEN %(end)s IS NULL THEN (%(start)s <= $1) ELSE (%(start)s <= $1 AND %(end)s > $1) END );
201202
$$
202203
LANGUAGE 'sql';
@@ -232,7 +233,7 @@ def sql_functions(self):
232233
$$
233234
LANGUAGE 'plpgsql';""" % {'view': self.view, 'schematable': self.schematable, 'cols': cols, 'oldcols': old_cols,
234235
'start': self.colStart, 'end': self.colEnd, 'user': self.colUser, 'func_at_time': self.func_at_time,
235-
'func_update': self.func_update, 'func_insert': self.func_insert}
236+
'all_cols': all_cols, 'func_update': self.func_update, 'func_insert': self.func_insert}
236237
return sql
237238

238239
def sql_triggers(self):
@@ -251,18 +252,20 @@ def sql_triggers(self):
251252

252253
def sql_updatesView(self):
253254
cols = ",".join(self.columns)
255+
return_cols = self.colPkey + "," + ",".join(self.columns)
254256
new_cols = ",".join([u"NEW." + x for x in self.columns])
255257
assign_cols = ",".join([u"%s = NEW.%s" % (x, x) for x in self.columns])
256258

257259
return u"""
258260
CREATE OR REPLACE RULE "_DELETE" AS ON DELETE TO %(view)s DO INSTEAD
259261
DELETE FROM %(schematable)s WHERE %(origpkey)s = old.%(origpkey)s;
260262
CREATE OR REPLACE RULE "_INSERT" AS ON INSERT TO %(view)s DO INSTEAD
261-
INSERT INTO %(schematable)s (%(cols)s) VALUES (%(newcols)s) RETURNING %(cols)s;
263+
INSERT INTO %(schematable)s (%(cols)s) VALUES (%(newcols)s) RETURNING %(return_cols)s;
262264
CREATE OR REPLACE RULE "_UPDATE" AS ON UPDATE TO %(view)s DO INSTEAD
263265
UPDATE %(schematable)s SET %(assign)s WHERE %(origpkey)s = NEW.%(origpkey)s;""" % {'view': self.view,
264266
'schematable': self.schematable,
265267
'cols': cols, 'newcols': new_cols,
268+
'return_cols': return_cols,
266269
'assign': assign_cols,
267270
'origpkey': self.colOrigPkey}
268271

0 commit comments

Comments
 (0)
Please sign in to comment.