def get_layers(cursor) -> [LayerEntry]: """ Returns a list of LayerEntry's built from the contents of the geopackage. :param cursor: the cursor to the GeoPackage database's connection """ Table.validate_table(cursor, LayerEntry.NAME, LayerEntry.COLUMNS) # select all the rows Table(cursor, table_name=LayerEntry.NAME).select().execute() rows = cursor.fetchall() # get the results return [LayerEntry.from_row(_row) for _row in rows]
def test_get_table_exists(self): cursor_mock = Mock() def fill_cursor(*args): if args[1][0] in ["gpkg_contents", "other_table"]: cursor_mock.fetchone.return_value = ("gpkg_contents",) else: cursor_mock.fetchone.return_value = tuple() cursor_mock.execute.side_effect = fill_cursor self.assertTrue(Table.exists(cursor_mock, "gpkg_contents")) self.assertTrue(cursor_mock.execute.called_once) self.assertTrue(not Table.exists(cursor_mock, "gpkg_metadata")) self.assertTrue(Table.exists(cursor_mock, "other_table"))
def get_all_metadata(cursor): """ Returns all the rows in the gpkg_metadata table :param cursor: the cursor to the GeoPackage database's connection :return all the rows in the gpkg_metadata table """ Table.validate_table(cursor, MetadataEntry.NAME, MetadataEntry.COLUMNS) # select all the rows Table(cursor, table_name=TableNames.GPKG_METADATA).select().execute() rows = cursor.fetchall() # get the results return [MetadataEntry.from_row(_row) for _row in rows]
def add_extension(cursor, extension): """ Adds an extension to the db if not already present. :param cursor: active cursor used to connect affect the db. :param extension: ExtensionEntry definition """ if not Table.exists(cursor, TableNames.GPKG_EXTENSIONS): Extension.create_extensions_table(cursor) Table.validate_table(cursor, ExtensionEntry.NAME, ExtensionEntry.COLUMNS) if extension["table_name"] is not None and not Table.exists( cursor=cursor, table_name=extension["table_name"]): raise ValueError( "Extension's table_name is not None and it does not exist in the GeoPackage. The " "table_name must exist before adding it to the extensions table" ) if extension["column_name"] is not None and not Table.columns_exists( cursor=cursor, table_name=extension["table_name"], column_names=[extension["column_name"]]): raise ValueError( f"Extension's column_name is not None and table {extension['table_name']} " f"does not have a column named {extension['column_name']}.") # sqlite does not check uniqueness if any values are null, therefore we have to check ourselves # to avoid duplicates set_columns = extension.to_dict() where_columns = dict(set_columns, definition=extension["definition"], scope=extension["scope"]) Table(cursor, TableNames.GPKG_EXTENSIONS).insert_or_update_row( set_columns, where_columns)
def test_get_table_query_validate(self): cursor_mock = Mock() def fill_cursor(*args): if args[1][0] in ["gpkg_contents"]: cursor_mock.fetchone.return_value = ("gpkg_contents",) else: cursor_mock.fetchone.return_value = tuple() cursor_mock.execute.side_effect = fill_cursor passed = True try: Table(cursor_mock, "gpkg_contents").validate() except ValueError: passed = False self.assertTrue(passed) self.assertRaises(ValueError, Table(cursor_mock, "gpkg_metadata").validate) try: Table(cursor_mock, "sqlite_master").validate() except ValueError: passed = False self.assertTrue(passed)
def has_extension(cursor, extension): """ Returns True if the GeoPackage has the extension, False otherwise :param cursor: the cursor to the GeoPackage database's connection :param extension: an Extension entry in the gpkg_extensions table to check if it exists or not :return Returns True if the GeoPackage has the extension, False otherwise """ if not Table.exists(cursor=cursor, table_name=TableNames.GPKG_EXTENSIONS): return False # select all the rows Table(cursor=cursor, table_name=TableNames.GPKG_EXTENSIONS).select( "table_name", "column_name", "extension_name", "definition", "scope").where( table_name=extension["table_name"], column_name=extension["column_name"], extension_name=extension["extension_name"], ).execute() rows = cursor.fetchall() return rows is not None and len(rows) > 0
def insert_or_update_metadata_row(cursor, metadata): """ Inserts or updates the metadata entry into gpkg_metadata table. :param cursor: the cursor to the GeoPackage database's connection :param metadata: The Metadata entry to insert into the gpkg_metadata table """ if not Table.exists(cursor=cursor, table_name=TableNames.GPKG_METADATA): Metadata.create_metadata_table(cursor=cursor) columns = metadata.to_dict() Table(cursor=cursor, table_name=TableNames.GPKG_METADATA).insert_or_update_row( set_columns=columns, where_columns=dict(id=metadata["id"])) set_columns = Metadata.reference_template().to_dict() where_columns = Metadata.reference_template().to_dict() del where_columns["timestamp"] Table(cursor=cursor, table_name=TableNames.GPKG_METADATA_REFERENCE ).insert_or_update_row(set_columns=set_columns, where_columns=where_columns)