forked from sourcepole/pgversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgVersion.py
659 lines (520 loc) · 27.8 KB
/
pgVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Plugin for the Postgres Versioning System
-----------------------------------------------------------------------------------------------------------------
begin : 2010-07-31
copyright : (C) 2010 by Dr. Horst Duester
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import sys
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebView
from qgis.core import *
from qgis.gui import *
from dbtools.dbTools import *
from forms.Ui_CommitMessage import CommitMessageDialog
from forms.Ui_diff import DiffDlg
from pgVersionConflictWindow import ConflictWindow
# Initialize Qt resources from file resources.py
import resources_rc, traceback, string
# Import the code for the dialog
from forms.Ui_pgLoadVersion import PgVersionLoadDialog
from forms.Ui_help import HelpDialog
from forms.Ui_LogView import LogView
from pgVersionTools import PgVersionTools
from about.doAbout import DlgAbout
import apicompat, tempfile, os
class PgVersion(QObject):
def __init__(self, iface):
QObject.__init__(self)
# Save reference to the QGIS interface
self.iface = iface
self.w = None
self.vsCheck = None
self.layer_list = []
self.tools = PgVersionTools(self)
#Initialise thetranslation environment
self.plugin_path = QDir.cleanPath( os.path.abspath(os.path.dirname(__file__)) )
myLocaleName = QLocale.system().name()
myLocale = myLocaleName[0:2]
if QFileInfo(self.plugin_path).exists():
localePath = self.plugin_path+"/i18n/pgVersion_"+myLocale+".qm"
if QFileInfo(localePath).exists():
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# QgsMapLayerRegistry.instance().layerWasAdded.connect(self.add_layer)
# QgsMapLayerRegistry.instance().layerWillBeRemoved.connect(self.remove_layer)
def initGui(self):
self.helpDialog = HelpDialog()
self.LogViewDialog = LogView(self)
self.toolBar = self.iface.addToolBar("PG Version")
self.toolBar.setObjectName("PG Version")
self.actionInit = QAction(QIcon(":/plugins/pgversion/icons/pgversion-init.png"), self.tr("Prepare Layer for Versioning"), self.iface.mainWindow())
self.actionLoad = QAction(QIcon(":/plugins/pgversion/icons/pgversion-commit.png"), self.tr("Load Versioned Layer"), self.iface.mainWindow())
self.actionCommit = QAction(QIcon(":/plugins/pgversion/icons/pgversion-load.png"), self.tr("Commit Changes"), self.iface.mainWindow())
self.actionRevert = QAction(QIcon(":/plugins/pgversion/icons/pgversion-revert.png"), self.tr("Revert to HEAD Revision"), self.iface.mainWindow())
self.actionLogView = QAction(QIcon(":/plugins/pgversion/icons/pgversion-logview.png"), self.tr("Show Logs"), self.iface.mainWindow())
self.actionDiff = QAction(QIcon(":/plugins/pgversion/icons/pgversion-diff.png"), self.tr("Show Diffs"), self.iface.mainWindow())
# self.actionCommit.setEnabled(False)
self.actionDrop = QAction(QIcon(":/plugins/pgversion/icons/pgversion-drop.png"), self.tr("Drop Versioning from Layer"), self.iface.mainWindow())
self.actionHelp = QAction(QIcon(""), self.tr("Help"), self.iface.mainWindow())
self.actionAbout = QAction(QIcon(""), self.tr("About"), self.iface.mainWindow())
self.actionDelete = QAction(QIcon(":/plugins/pgversion/icons/pgversion-drop.png"), self.tr("Bulk delete directly in the database"), self.iface.mainWindow())
self.actionDelete.setEnabled(False)
self.actionList = [ self.actionInit,self.actionLoad, self.actionCommit, self.actionDiff, self.actionRevert, self.actionLogView, self.actionDrop, self.actionLogView, self.actionDelete, self.actionHelp, self.actionAbout]
# self.actionList = [ self.actionInit,self.actionLoad,self.actionCommit,self.actionRevert, self.actionLogView, self.actionDrop, self.actionLogView, self.actionHelp, self.actionAbout]
self.toolBar.addAction(self.actionInit)
self.toolBar.addAction(self.actionLoad)
self.toolBar.addAction(self.actionCommit)
self.toolBar.addAction(self.actionRevert)
self.toolBar.addAction(self.actionDiff)
self.toolBar.addAction(self.actionLogView)
self.toolBar.addAction(self.actionDelete)
# Add the Menubar into the new Database Main Menue starting with QGIS1.7
try:
for a in self.actionList:
self.iface.addPluginToDatabaseMenu("PG Version", a)
except AttributeError:
# For former QGIS Versions use the old Main Menue
self.menu = QMenu()
self.menu.setTitle( "PG Version" )
self.menu.addActions(self.actionList)
self.menuBar = self.iface.mainWindow().menuBar()
self.menuBar.addMenu(self.menu)
# connect the action to the run method
self.actionInit.triggered.connect(self.doInit)
self.actionLoad.triggered.connect(self.doLoad)
self.actionDiff.triggered.connect(self.doDiff)
self.actionCommit.triggered.connect(self.doCommit)
self.actionRevert.triggered.connect(self.doRevert)
self.actionLogView.triggered.connect(self.doLogView)
self.actionDrop.triggered.connect(self.doDrop)
self.actionHelp.triggered.connect(self.doHelp)
self.actionAbout.triggered.connect(self.doAbout)
self.actionDelete.triggered.connect(self.doDelete)
self.LogViewDialog.diffLayer.connect(self.doDiff)
self.LogViewDialog.rollbackLayer.connect(self.doRollback)
self.LogViewDialog.checkoutLayer.connect(self.doCheckout)
self.LogViewDialog.checkoutTag.connect(self.doCheckout)
for a in self.iface.digitizeToolBar().actions():
if a.objectName() == 'mActionToggleEditing':
a.triggered.connect(self.onSelectionChanged)
self.iface.mapCanvas().selectionChanged.connect(self.onSelectionChanged)
QgsMapLayerRegistry.instance().layerWasAdded.connect(self.add_layer)
QgsMapLayerRegistry.instance().layerWillBeRemoved.connect(self.remove_layer)
def onSelectionChanged(self):
current_layer = self.iface.mapCanvas().currentLayer()
if current_layer.selectedFeatureCount() == 0:
self.actionDelete.setEnabled(False)
else:
if self.tools.hasVersion(current_layer):
print current_layer.isEditable()
if current_layer.isEditable():
self.actionDelete.setEnabled(True) # true
else:
self.actionDelete.setEnabled(False)
else:
self.actionDelete.setEnabled(False) # true
def add_layer(self, l):
if self.tools.hasVersion(l):
if l.id not in self.layer_list:
l.editingStopped.connect(lambda my_list = self.layer_list: self.tools.setModified(my_list))
self.layer_list.append(l.id())
self.tools.setModified(self.layer_list)
def remove_layer(self, id):
self.layer_list = list(set(self.layer_list))
if id in set(self.layer_list):
self.layer_list.remove(id)
def unload(self):
# remove menubar
try:
for a in self.actionList:
self.iface.removePluginDatabaseMenu("PG Version", a)
except:
del self.menuBar
del self.toolBar
def doDelete(self):
res = QMessageBox.question(
None,
self.tr("Question"),
self.tr("Are you sure to delete all selected features. You cannot undo this action!"),
QMessageBox.StandardButtons(
QMessageBox.No |
QMessageBox.Yes))
if res == QMessageBox.Yes:
QApplication.restoreOverrideCursor()
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
myPkey = QgsDataSourceURI(currentLayer.dataProvider().dataSourceUri()).keyColumn()
myDb = self.tools.layerDB('doInit',currentLayer)
selectedFeatures = currentLayer.selectedFeatures()
delete_list = "("
if currentLayer.selectedFeatureCount() > 0:
for feature in selectedFeatures:
delete_list += str(feature.attributes()[0])+", "
delete_list = delete_list[:-2]
delete_list += ")"
sql = "delete from \"%s\".\"%s\" where \"%s\" in %s" % (mySchema, myTable, myPkey, delete_list)
QApplication.setOverrideCursor(Qt.WaitCursor)
myDb.run(sql)
QApplication.restoreOverrideCursor()
currentLayer.removeSelection()
currentLayer.triggerRepaint()
self.tools.setModified(self.layer_list)
# self.actionDelete.setEnabled(false)
def doInit(self):
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
if currentLayer == None:
QMessageBox.information(None, '', self.tr('Please select a layer for versioning'))
return
elif self.tools.hasVersion(currentLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr('The selected layer is already under versioning!'))
return
else:
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
myDb = self.tools.layerDB('doInit',currentLayer)
if not self.tools.check_PGVS_revision(myDb):
return
if not self.tools.versionExists(currentLayer):
answer = QMessageBox.question(None, '', self.tr('Do you want to create the version environment for the table {0}').format(mySchema+'.'+myTable), self.tr('Yes'), self.tr('No'))
QApplication.setOverrideCursor(Qt.WaitCursor)
sql = "select * from versions.pgvsinit('%s.%s')" % (mySchema, myTable)
result = myDb.runError(sql)
if result == ' ':
QMessageBox. information(None, 'Init', self.tr( 'Init was successful!\n\n\
Please set the user permissions for table {0} and reload it via Database -> PG Version!').format(myTable))
QgsMapLayerRegistry.instance().removeMapLayer(currentLayer.id())
QApplication.restoreOverrideCursor()
else:
self.iface.messageBar().pushMessage(self.tr('Init Error'), self.tr('Versioning envoronment for table {0} already exsists!').format(mySchema+"."+myTable), level=QgsMessageBar.CRITICAL, duration=3)
def doLoad(self):
self.dlg = PgVersionLoadDialog(self)
self.dlg.show()
def doRollback(self, item):
if item == None:
QMessageBox.information(None, self.tr('Error'), self.tr('Please select a valid revision'))
return
revision = item.text(0)
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
if currentLayer == None:
QMessageBox.information(None, '', self.tr('Please select a versioned layer'))
return
else:
answer = QMessageBox.question(None, '', self.tr('Are you sure to rollback to revision {0}?').format(revision), self.tr('Yes'), self.tr('No'))
if answer == 0:
if self.tools.isModified(currentLayer):
answer = QMessageBox.question(None, '', \
self.tr('Layer {0} has modifications which will be lost after rollback! \
If you want to keep this modifications please commit them. \
Are you sure to rollback to revision {1}?').format(currentLayer.name(), revision), self.tr('Yes'), self.tr('No'))
if answer == 1:
return
QApplication.setOverrideCursor(Qt.WaitCursor)
provider = currentLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('doRollback',currentLayer)
if not self.tools.check_PGVS_revision(myDb):
return
mySchema = QgsDataSourceURI(uri).schema()
if len(mySchema) == 0:
mySchema = 'public'
myTable = QgsDataSourceURI(uri).table()
sql = "select * from versions.pgvsrollback('"+mySchema+"."+myTable.replace('_version', '')+"',"+revision+")"
myDb.run(sql)
myDb.close()
self.LogViewDialog.close()
currentLayer.triggerRepaint()
QApplication.restoreOverrideCursor()
self.tools.setModified(self.layer_list)
self.iface.messageBar().pushMessage('INFO', self.tr('Rollback to revision {0} was successful!').format(revision), level=QgsMessageBar.INFO, duration=3)
return
def doCommit(self):
canvas = self.iface.mapCanvas()
if canvas.currentLayer() == None:
self.iface.messageBar().pushMessage('Error', self.tr('Please select a versioned layer for committing'), level=QgsMessageBar.CRITICAL, duration=3)
return
canvas = self.iface.mapCanvas()
theLayer = canvas.currentLayer()
mySchema = self.tools.layerSchema(theLayer)
myTable = self.tools.layerTable(theLayer).replace('_version', '')
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr('Please select a versioned layer!'))
else:
if self.tools.isModified(theLayer):
confRecords = self.tools.confRecords(theLayer)
if confRecords == None:
# show the dialog
self.dlgCommitMessage = CommitMessageDialog()
self.dlgCommitMessage.show()
result = self.dlgCommitMessage.exec_()
if result == QDialog.Accepted:
QApplication.setOverrideCursor(Qt.WaitCursor)
sql = "select * from versions.pgvscommit('%s.%s','%s')" % (mySchema, myTable, self.dlgCommitMessage.textEdit.toPlainText())
myDB = self.tools.layerDB('commit', theLayer)
myDB.run(sql)
myDB.close()
self.tools.layerRepaint()
self.iface.messageBar().pushMessage("Info", self.tr('Commit of your changes was successful'), level=QgsMessageBar.INFO, duration=3)
self.tools.setModified(self.layer_list)
QApplication.restoreOverrideCursor()
else:
if self.w != None:
self.w = None
self.w = ConflictWindow(theLayer, 'conflict', self)
self.w.mergeCompleted.connect(self.doCommit)
self.w.show()
self.tools.setModified(self.layer_list)
else:
self.iface.messageBar().pushMessage('INFO', self.tr('No layer changes for committing, everything is OK'), level=QgsMessageBar.INFO, duration=3)
self.tools.setModified(self.layer_list)
def doCheckout(self, revision, tag=None):
if revision == None:
QMessageBox.information(None, self.tr('Error'), self.tr('Please select a valid revision'))
return
# revision = item.text(0)
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
if currentLayer == None:
QMessageBox.information(None, '', self.tr('Please select a versioned layer'))
return
else:
answer = QMessageBox.question(None, '', self.tr('Are you sure to checkout the layer to revision {0}?').format(revision), self.tr('Yes'), self.tr('No'))
if answer == 0:
QApplication.setOverrideCursor(Qt.WaitCursor)
provider = currentLayer.dataProvider()
uri = provider.dataSourceUri()
uniqueCol = QgsDataSourceURI(uri).keyColumn()
geomCol = QgsDataSourceURI(uri).geometryColumn()
geometryType = currentLayer.geometryType()
mySchema = QgsDataSourceURI(uri).schema()
myTable = QgsDataSourceURI(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = "select * from versions.pgvscheckout(NULL::\""+mySchema+"\".\""+myTable.replace('_version', '')+"\", "+revision+")"
myUri = QgsDataSourceURI(uri)
myUri.setDataSource("", u"(%s\n)" % (sql), geomCol, "", uniqueCol)
layer = None
if tag:
table_ext = " (Tag: %s)" % tag
else:
table_ext = " (Revision: %s)" % revision
layer = QgsVectorLayer(myUri.uri(), myTable+table_ext, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(layer)
QApplication.restoreOverrideCursor()
if layer.isValid():
self.iface.messageBar().pushMessage('INFO', self.tr('Checkout to revision {0} was successful!').format(revision), level=QgsMessageBar.INFO, duration=3)
layer.triggerRepaint()
else:
self.iface.messageBar().pushMessage('INFO', self.tr('Something went wrong during checkout to revision {0}!').format(revision), level=QgsMessageBar.INFO, duration=3)
self.LogViewDialog.close()
return
def doRevert(self):
canvas = self.iface.mapCanvas()
theLayer = self.iface.activeLayer()
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('revert', theLayer)
mySchema = QgsDataSourceURI(uri).schema()
myTable = QgsDataSourceURI(uri).table()
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr('Please select a versioned layer!'))
else:
if len(mySchema) == 1:
mySchema = 'public'
answer = QMessageBox.question(None, '', self.tr('are you sure to revert to the HEAD revision?'), self.tr('Yes'), self.tr('No'))
if answer == 0:
sql = "select * from versions.pgvsrevert('"+mySchema+"."+myTable.replace('_version', '')+"')"
result = myDb.read(sql)
if len(result)>1:
QMessageBox.information(None, '',result)
else:
self.iface.messageBar().pushMessage("Info", self.tr('All changes are set back to the HEAD revision: {0}').format(str(result["PGVSREVERT"][0])), level=QgsMessageBar.INFO, duration=3)
self.tools.setModified(self.layer_list)
theLayer.triggerRepaint()
myDb.close()
pass
def doLogView(self):
canvas = self.iface.mapCanvas()
theLayer = self.iface.activeLayer()
if theLayer <> None:
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr('Please select a versioned layer!'))
else:
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('logview', theLayer)
mySchema = QgsDataSourceURI(uri).schema()
myTable = QgsDataSourceURI(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = "select * from versions.pgvslogview('"+mySchema+"."+myTable.replace('_version', '')+"') order by revision desc"
result = myDb.read(sql)
logHTML = "<html><head></head><body><Table>"
self.LogViewDialog.setLayer(theLayer)
self.LogViewDialog.createTagList()
self.LogViewDialog.treeWidget.clear()
itemList = []
for i in range(len(result["PROJECT"])):
myItem = QTreeWidgetItem()
myItem.setText(0, result["REVISION"][i])
myItem.setText(1, result["DATUM"][i])
myItem.setText(2, result["PROJECT"][i])
myItem.setText(3, result["LOGMSG"][i])
itemList.append(myItem)
self.LogViewDialog.treeWidget.addTopLevelItems(itemList)
self.LogViewDialog.show()
myDb.close()
canvas.refresh()
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr('Please select a versioned layer!'))
else:
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('logview', theLayer)
mySchema = QgsDataSourceURI(uri).schema()
myTable = QgsDataSourceURI(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = "select * from versions.pgvslogview('"+mySchema+"."+myTable.replace('_version', '')+"') order by revision desc"
result = myDb.read(sql)
logHTML = "<html><head></head><body><Table>"
self.LogViewDialog.setLayer(theLayer)
self.LogViewDialog.createTagList()
self.LogViewDialog.treeWidget.clear()
itemList = []
for i in range(len(result["PROJECT"])):
myItem = QTreeWidgetItem()
myItem.setText(0, result["REVISION"][i])
myItem.setText(1, result["DATUM"][i])
myItem.setText(2, result["PROJECT"][i])
myItem.setText(3, result["LOGMSG"][i])
itemList.append(myItem)
self.LogViewDialog.treeWidget.addTopLevelItems(itemList)
self.LogViewDialog.show()
myDb.close()
canvas.refresh()
pass
def doDiff(self):
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
myDb = self.tools.layerDB('logview', currentLayer)
if currentLayer == None:
QMessageBox.information(None, '', self.tr('Please select a versioned layer'))
return
else:
# answer = QMessageBox.question(None, '', self.tr('Are you sure to checkout diffs to HEAD revision?'), self.tr('Yes'), self.tr('No'))
answer = 0
if answer == 0:
QApplication.setOverrideCursor(Qt.WaitCursor)
uniqueCol = self.tools.layerKeyCol(currentLayer)
geomCol = self.tools.layerGeomCol(currentLayer)
geometryType = self.tools.layerGeometryType(currentLayer)
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
if len(mySchema) == 0:
mySchema = 'public'
sql = u"select * from \"%s\".\"%s\" limit 0" % (mySchema, myTable)
cols = myDb.cols(sql)
myCols = ', '.join(cols)+', st_asewkb("'+geomCol+'")'
extent = self.iface.mapCanvas().extent().toString().replace(':',',')
authority, crs = currentLayer.crs().authid().split(':')
geo_idx = '%s && ST_MakeEnvelope(%s,%s)' % (geomCol, extent, crs)
sql = ("with \
head as (select max(revision) as head from versions.\"{schema}_{origin}_version_log\"), \
delete as (\
select 'delete'::varchar as action, * \
from (\
select * from versions.pgvscheckout(NULL::\"{schema}\".\"{origin}\",(select * from head), \'{geo_idx}\') \
except \
select * from \"{schema}\".\"{origin}_version\" where {geo_idx}\
) as foo \
), \
insert as (\
select 'insert'::varchar as action, * \
from ( \
select * from \"{schema}\".\"{origin}_version\" where {geo_idx} \
except \
select * from versions.pgvscheckout(NULL::\"{schema}\".\"{origin}\",(select * from head), \'{geo_idx}\') \
) as foo) \
\
select row_number() OVER () AS rownum, * \
from \
(\
select * from delete \
union \
select * from insert) as foo").format(schema = mySchema, table=myTable, origin=myTable.replace('_version', ''), geo_idx = geo_idx)
myUri = QgsDataSourceURI(self.tools.layerUri(currentLayer))
myUri.setDataSource("", u"(%s\n)" % sql, geomCol, "", "rownum")
layer_name = myTable+" (Diff to HEAD Revision)"
layer = QgsVectorLayer(myUri.uri(), layer_name, "postgres")
mem_layer = self.tools.create_memory_layer(layer, layer_name )
if not mem_layer.isValid():
self.iface.messageBar().pushMessage('WARNING', self.tr('No diffs to HEAD detected! Layer could not be loaded.'), level=QgsMessageBar.INFO, duration=3)
else:
userPluginPath = QFileInfo(QgsApplication.qgisUserDbFilePath()).path()+"/python/plugins/pgversion"
mem_layer.setRendererV2(None)
if geometryType == 0:
mem_layer.loadNamedStyle(userPluginPath+"/legends/diff_point.qml")
elif geometryType == 1:
mem_layer.loadNamedStyle(userPluginPath+"/legends/diff_linestring.qml")
elif geometryType == 2:
mem_layer.loadNamedStyle(userPluginPath+"/legends/diff_polygon.qml")
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
self.iface.messageBar().pushMessage('INFO', self.tr('Diff to HEAD revision was successful!'), level=QgsMessageBar.INFO, duration=3)
QApplication.restoreOverrideCursor()
self.LogViewDialog.close()
return
def doDrop(self):
# try:
theLayer = self.iface.activeLayer()
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
mySchema = QgsDataSourceURI(uri).schema()
if len(mySchema) == 0:
mySchema = 'public'
myTable = QgsDataSourceURI(uri).table()
if theLayer == None:
QMessageBox.information(None, '', self.tr('Please select a layer for versioning'))
return
else:
answer = QMessageBox.question(None, '', self.tr('are you sure to to drop pgvs from the table {0}?').format(mySchema+"."+myTable.replace('_version', '')), self.tr('Yes'), self.tr('No'))
if answer == 0:
if self.tools.isModified(theLayer):
QMessageBox.warning(None, self.tr('Warning'), \
self.tr('Layer %s has uncommited changes, please commit them or revert to HEAD revision' % (theLayer.name())))
else:
myDb = self.tools.layerDB('doDrop',theLayer)
sql = "select versions.pgvsdrop('"+mySchema+"."+myTable.replace('_version', '')+"')"
result = myDb.read(sql)
myDb.close()
layer_name = theLayer.name()
QgsMapLayerRegistry.instance().removeMapLayer(theLayer.id())
self.iface.messageBar().pushMessage('INFO', self.tr('Versioning for layer {0} dropped!').format(layer_name), level=QgsMessageBar.INFO, duration=3)
def doHelp(self):
helpUrl = QUrl()
helpUrl.setUrl('file://'+self.plugin_path+'/docs/help.html')
self.helpDialog.webView.load(helpUrl)
self.helpDialog.show()
pass
def doAbout(self):
self.about = DlgAbout(self.plugin_path)
self.about.show()