예제 #1
0
    def checkout(self):
        """create working copy from versionned database layers"""
        # for each connection, we need the list of tables
        tables_for_conninfo = []
        uri = None
        conn_info = ''
        for layer_id in self.current_layers:
            layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
            uri = QgsDataSourceURI(layer.source())
            if not conn_info:
                conn_info = uri.connectionInfo()
            else:
                assert (conn_info == uri.connectionInfo())
            table = uri.schema() + "." + uri.table()
            tables_for_conninfo.append(table)

        filename = QFileDialog.getSaveFileName(self.iface.mainWindow(),
                                               'Save Versionned Layers As',
                                               '.', '*.sqlite')
        if not filename:
            print "aborted"
            return

        if os.path.isfile(filename):
            os.remove(filename)

        print "checkin out ", tables_for_conninfo, " from ", uri.connectionInfo(
        )
        versioning_base.checkout(self.pg_conn_info(), tables_for_conninfo,
                                 filename)

        # add layers from offline version
        grp_name = 'working copy'
        if grp_name in self.iface.legendInterface().groups():
            grp_name = filename
        grp_idx = self.iface.legendInterface().addGroup(grp_name)
        for layer_id in reversed(self.current_layers):
            layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
            uri = QgsDataSourceURI(layer.source())
            table = uri.table()
            display_name = layer.name()
            print "replacing ", display_name
            geom = '(GEOMETRY)' if uri.geometryColumn() else ''
            new_layer = self.iface.addVectorLayer(
                "dbname=" + filename + " key=\"OGC_FID\" table=\"" + table +
                "_view\" " + geom, display_name, 'spatialite')
            self.iface.legendInterface().moveLayer(new_layer, grp_idx)
        self.iface.legendInterface().setGroupExpanded(grp_idx, True)
예제 #2
0
    def checkout(self):
        """create working copy from versionned database layers"""
        # for each connection, we need the list of tables
        tables_for_conninfo = []
        uri = None
        conn_info = ''
        for layer_id in self.current_layers:
            layer = QgsMapLayerRegistry.instance().mapLayer( layer_id )
            uri = QgsDataSourceURI(layer.source())
            if not conn_info:
                conn_info = uri.connectionInfo()
            else:
                assert(conn_info == uri.connectionInfo())
            table =  uri.schema()+"."+uri.table()
            tables_for_conninfo.append(table)

        filename = QFileDialog.getSaveFileName(self.iface.mainWindow(),
                'Save Versionned Layers As', '.', '*.sqlite')
        if not filename:
            print "aborted"
            return

        if os.path.isfile(filename):
            os.remove(filename)

        print "checkin out ", tables_for_conninfo, " from ",uri.connectionInfo()
        versioning_base.checkout( self.pg_conn_info(),
                tables_for_conninfo, filename )

        # add layers from offline version
        grp_name = 'working copy'
        if grp_name in self.iface.legendInterface().groups():
            grp_name = filename
        grp_idx = self.iface.legendInterface().addGroup( grp_name )
        for layer_id in reversed(self.current_layers):
            layer = QgsMapLayerRegistry.instance().mapLayer( layer_id )
            uri = QgsDataSourceURI(layer.source())
            table = uri.table()
            display_name = layer.name()
            print "replacing ", display_name
            geom = '(GEOMETRY)' if uri.geometryColumn() else ''
            new_layer = self.iface.addVectorLayer("dbname="+filename+
                    " key=\"OGC_FID\" table=\""+table+"_view\" "
                    +geom,display_name, 'spatialite')
            self.iface.legendInterface().moveLayer( new_layer, grp_idx)
        self.iface.legendInterface().setGroupExpanded( grp_idx, True )
versioning_base.add_revision_view( pg_conn_info, 'epanet', 'mybranch', 2)
pcur.execute("SELECT * FROM epanet_mybranch_rev_2.junctions")
assert( len(pcur.fetchall()) == 2 )
pcur.execute("SELECT * FROM epanet_mybranch_rev_2.pipes")
assert( len(pcur.fetchall()) == 1 )

pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet_mybranch_rev_2.junctions")
res = pcur.fetchall()
assert( res[0][0] == 'POINT(0 0)' )
assert( res[1][1] == 'POLYGON((0 0,2 0,2 2,0 2,0 0))' )


wc = tmp_dir+'/wc_multiple_geometry_test.sqlite'
if os.path.isfile(wc): os.remove(wc) 
versioning_base.checkout( pg_conn_info, ['epanet_trunk_rev_head.pipes','epanet_trunk_rev_head.junctions'], wc )


scur = versioning_base.Db( dbapi2.connect(wc) )
scur.execute("UPDATE junctions_view SET GEOMETRY = GeometryFromText('POINT(3 3)',2154)")
scur.commit()
scur.close()
versioning_base.commit( wc, 'a commit msg', 'dbname=epanet_test_db' )

pcur.execute("SELECT ST_AsText(geometry), ST_AsText(geometry_schematic) FROM epanet_trunk_rev_head.junctions")
res = pcur.fetchall()
for r in res: print r
assert( res[0][0] == 'POINT(3 3)' )
assert( res[1][1] == 'POLYGON((0 0,2 0,2 2,0 2,0 0))' )
pcur.close()
예제 #4
0
import psycopg2
import os
import shutil

test_data_dir = os.path.dirname(os.path.realpath(__file__))
tmp_dir = "/tmp"

# create the test database

os.system("dropdb epanet_test_db")
os.system("createdb epanet_test_db")
os.system("psql epanet_test_db -c 'CREATE EXTENSION postgis'")
os.system("psql epanet_test_db -f "+test_data_dir+"/epanet_test_db_unversioned.sql")

versioning_base.historize("dbname=epanet_test_db","epanet")

# try the update
wc = tmp_dir+"/bug_in_branch_after_commit_wc.sqlite"
if os.path.isfile(wc): os.remove(wc) 
versioning_base.checkout("dbname=epanet_test_db", ['epanet_trunk_rev_head.junctions', 'epanet_trunk_rev_head.pipes'], wc)

scur = versioning_base.Db( dbapi2.connect( wc ) )

scur.execute("SELECT * FROM pipes")
scur.execute("UPDATE pipes_view SET length = 1 WHERE OGC_FID = 1")
scur.commit()

versioning_base.commit(wc,'test', "dbname=epanet_test_db" )

versioning_base.add_branch("dbname=epanet_test_db","epanet","mybranch","add 'branch")
예제 #5
0
    def checkout(self):
        """create working copy from versioned database layers"""
        # for each connection, we need the list of tables
        tables_for_conninfo = []
        # for each layer, we need the list of user selected features to be
        # checked out; if a given layer has no user selected features, then all
        # features will be checked out
        user_selected_features = []
        uri = None
        conn_info = ''
        for layer_id in self.current_layers:
            layer = QgsMapLayerRegistry.instance().mapLayer( layer_id )
            uri = QgsDataSourceURI(layer.source())

            # Get actual PK fror corresponding table
            actual_table_pk = versioning_base.get_actual_pk( uri,self.pg_conn_info() )
            #print "Actual table pk = " + actual_table_pk

            layer_selected_features_ids = layer.selectedFeaturesIds()

            # Check if PK from view [uri.keyColumn()] matches actual PK. If not,
            # throw error.  We need the right PK from the view in order to use
            # the efficient selectedFeaturesIds().  selectedFeatures() or other
            # ways that lead to a list of QGSFeature objects do not scale well.
            if layer_selected_features_ids:
                if uri.keyColumn()!= actual_table_pk:
                    QMessageBox.warning(None,"Warning","Layer  \""+layer.name()+
                    " \" does not have the right primary key.\n\nCheckout will "
                    "proceed without the selected features subset.")
                    user_selected_features.append([])
                else:
                    QMessageBox.warning(None,"Warning","You will be checking out "
                    "the subset of "+str(len(layer_selected_features_ids))+" features "
                    "you selected in layer \""+layer.name()+"\".\n\nIf you want "
                    "the whole data set for that layer, abort checkout in the pop "
                    "up asking for a filename, unselect features and start over.")
                    user_selected_features.append(layer_selected_features_ids)
            else:
                user_selected_features.append([])
            if not conn_info:
                conn_info = uri.connectionInfo()
            else:
                assert(conn_info == uri.connectionInfo())
            table =  uri.schema()+"."+uri.table()
            tables_for_conninfo.append(table)

        filename = QFileDialog.getSaveFileName(self.iface.mainWindow(),
                'Save Versioned Layers As', '.', '*.sqlite')
        if not filename:
            print "aborted"
            return

        if os.path.isfile(filename):
            os.remove(filename)

        print "checking out ", tables_for_conninfo, " from ",uri.connectionInfo()
        versioning_base.checkout( self.pg_conn_info(),
                tables_for_conninfo, filename, user_selected_features )

        # add layers from offline version
        grp_name = 'working copy'
        if grp_name in self.iface.legendInterface().groups():
            grp_name = filename
        grp_idx = self.iface.legendInterface().addGroup( grp_name )
        for layer_id in reversed(self.current_layers):
            layer = QgsMapLayerRegistry.instance().mapLayer( layer_id )
            uri = QgsDataSourceURI(layer.source())
            table = uri.table()
            display_name = layer.name()
            print "replacing ", display_name
            geom = '(GEOMETRY)' if uri.geometryColumn() else ''
            new_layer = self.iface.addVectorLayer("dbname="+filename+
                    " key=\"OGC_FID\" table=\""+table+"_view\" "
                    +geom,display_name, 'spatialite')
            self.iface.legendInterface().moveLayer( new_layer, grp_idx)
        self.iface.legendInterface().setGroupExpanded( grp_idx, True )
예제 #6
0
    def checkout(self):
        """create working copy from versioned database layers"""
        # for each connection, we need the list of tables
        tables_for_conninfo = []
        # for each layer, we need the list of user selected features to be
        # checked out; if a given layer has no user selected features, then all
        # features will be checked out
        user_selected_features = []
        uri = None
        conn_info = ''
        for layer_id in self.current_layers:
            layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
            uri = QgsDataSourceURI(layer.source())

            # Get actual PK fror corresponding table
            actual_table_pk = versioning_base.get_actual_pk(
                uri, self.pg_conn_info())
            #print "Actual table pk = " + actual_table_pk

            layer_selected_features_ids = layer.selectedFeaturesIds()

            # Check if PK from view [uri.keyColumn()] matches actual PK. If not,
            # throw error.  We need the right PK from the view in order to use
            # the efficient selectedFeaturesIds().  selectedFeatures() or other
            # ways that lead to a list of QGSFeature objects do not scale well.
            if layer_selected_features_ids:
                if uri.keyColumn() != actual_table_pk:
                    QMessageBox.warning(
                        None, "Warning", "Layer  \"" + layer.name() +
                        " \" does not have the right primary key.\n\nCheckout will "
                        "proceed without the selected features subset.")
                    user_selected_features.append([])
                else:
                    QMessageBox.warning(
                        None, "Warning", "You will be checking out "
                        "the subset of " +
                        str(len(layer_selected_features_ids)) + " features "
                        "you selected in layer \"" + layer.name() +
                        "\".\n\nIf you want "
                        "the whole data set for that layer, abort checkout in the pop "
                        "up asking for a filename, unselect features and start over."
                    )
                    user_selected_features.append(layer_selected_features_ids)
            else:
                user_selected_features.append([])
            if not conn_info:
                conn_info = uri.connectionInfo()
            else:
                assert (conn_info == uri.connectionInfo())
            table = uri.schema() + "." + uri.table()
            tables_for_conninfo.append(table)

        filename = QFileDialog.getSaveFileName(self.iface.mainWindow(),
                                               'Save Versioned Layers As', '.',
                                               '*.sqlite')
        if not filename:
            print "aborted"
            return

        if os.path.isfile(filename):
            os.remove(filename)

        print "checking out ", tables_for_conninfo, " from ", uri.connectionInfo(
        )
        versioning_base.checkout(self.pg_conn_info(), tables_for_conninfo,
                                 filename, user_selected_features)

        # add layers from offline version
        grp_name = 'working copy'
        if grp_name in self.iface.legendInterface().groups():
            grp_name = filename
        grp_idx = self.iface.legendInterface().addGroup(grp_name)
        for layer_id in reversed(self.current_layers):
            layer = QgsMapLayerRegistry.instance().mapLayer(layer_id)
            uri = QgsDataSourceURI(layer.source())
            table = uri.table()
            display_name = layer.name()
            print "replacing ", display_name
            geom = '(GEOMETRY)' if uri.geometryColumn() else ''
            new_layer = self.iface.addVectorLayer(
                "dbname=" + filename + " key=\"OGC_FID\" table=\"" + table +
                "_view\" " + geom, display_name, 'spatialite')
            self.iface.legendInterface().moveLayer(new_layer, grp_idx)
        self.iface.legendInterface().setGroupExpanded(grp_idx, True)
if os.path.isfile(sqlite_test_filename2): os.remove(sqlite_test_filename2)
if os.path.isfile(sqlite_test_filename3): os.remove(sqlite_test_filename3)
if os.path.isfile(sqlite_test_filename4): os.remove(sqlite_test_filename4)

# create the test database

os.system("dropdb epanet_test_db")
os.system("createdb epanet_test_db")
os.system("psql epanet_test_db -c 'CREATE EXTENSION postgis'")
os.system("psql epanet_test_db -f " + test_data_dir + "/epanet_test_db.sql")

# chechout two tables

try:
    versioning_base.checkout(
        "dbname=epanet_test_db",
        ["epanet_trunk_rev_head.junctions", "epanet.pipes"],
        sqlite_test_filename1)
    assert (
        False and
        "checkout from schema withouti suffix _branch_rev_head should not be successfull"
    )
except RuntimeError:
    pass

assert (not os.path.isfile(sqlite_test_filename1)
        and "sqlite file must not exist at this point")
versioning_base.checkout(
    "dbname=epanet_test_db",
    ["epanet_trunk_rev_head.junctions", "epanet_trunk_rev_head.pipes"],
    sqlite_test_filename1)
assert (os.path.isfile(sqlite_test_filename1)
if os.path.isfile(sqlite_test_filename1): os.remove(sqlite_test_filename1)
if os.path.isfile(sqlite_test_filename2): os.remove(sqlite_test_filename2)
if os.path.isfile(sqlite_test_filename3): os.remove(sqlite_test_filename3)
if os.path.isfile(sqlite_test_filename4): os.remove(sqlite_test_filename4)

# create the test database

os.system("dropdb epanet_test_db")
os.system("createdb epanet_test_db")
os.system("psql epanet_test_db -c 'CREATE EXTENSION postgis'")
os.system("psql epanet_test_db -f "+test_data_dir+"/epanet_test_db.sql")

# chechout two tables

try:
    versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet.pipes"], sqlite_test_filename1)
    assert(False and "checkout from schema withouti suffix _branch_rev_head should not be successfull")
except RuntimeError:
    pass

assert( not os.path.isfile(sqlite_test_filename1) and "sqlite file must not exist at this point" )
versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename1)
assert( os.path.isfile(sqlite_test_filename1) and "sqlite file must exist at this point" )

try:
    versioning_base.checkout("dbname=epanet_test_db",["epanet_trunk_rev_head.junctions","epanet_trunk_rev_head.pipes"], sqlite_test_filename1)
    assert(False and "trying to checkout on an existing file must fail")
except RuntimeError:
    pass

# edit one table and commit changes; rev = 2