@Test public void testSplitCatalogSchemaTableName() { check("mytable", null, "", "", "mytable", "\"mytable\"", "\"mytable\"", "mytable"); check( "myschema.mytable", null, "", "myschema", "mytable", "\"myschema\".\"mytable\"", "\"myschema\".\"mytable\"", "myschema.mytable"); check( "mydb.myschema.mytable", null, "mydb", "myschema", "mytable", "\"mydb\".\"myschema\".\"mytable\"", "\"mydb\".\"myschema\".\"mytable\"", "mydb.myschema.mytable"); check( TableLocation.parse("mydb.myschema.mytable").toString(), null, "mydb", "myschema", "mytable", "\"mydb\".\"myschema\".\"mytable\"", "\"mydb\".\"myschema\".\"mytable\"", "mydb.myschema.mytable"); }
@Test public void testSplitCatalogSchemaTableNameWithQuotes() { check("`mytable`", null, "", "", "mytable", "\"mytable\"", "\"mytable\"", "mytable"); check( "`myschema`.`mytable`", null, "", "myschema", "mytable", "\"myschema\".\"mytable\"", "\"myschema\".\"mytable\"", "myschema.mytable"); check( "`mydb`.`myschema`.`mytable`", null, "mydb", "myschema", "mytable", "\"mydb\".\"myschema\".\"mytable\"", "\"mydb\".\"myschema\".\"mytable\"", "mydb.myschema.mytable"); check( "`mydb`.`myschema`.`mytable.hello`", null, "mydb", "myschema", "mytable.hello", "\"mydb\".\"myschema\".\"mytable.hello\"", "\"mydb\".\"myschema\".\"mytable.hello\"", "mydb.myschema.\"mytable.hello\""); check( "`mydb`.`my schema`.`my table`", null, "mydb", "my schema", "my table", "\"mydb\".\"my schema\".\"my table\"", "\"mydb\".\"my schema\".\"my table\"", "mydb.\"my schema\".\"my table\""); check( TableLocation.parse("`mydb`.`my schema`.`my table`").toString(), null, "mydb", "my schema", "my table", "\"mydb\".\"my schema\".\"my table\"", "\"mydb\".\"my schema\".\"my table\"", "mydb.\"my schema\".\"my table\""); check( "public.MYTABLE", null, "", "public", "MYTABLE", "\"public\".\"MYTABLE\"", "\"public\".MYTABLE", "public.\"MYTABLE\""); }
private void check( String input, Boolean isH2, String catalog, String schema, String table, String toString, String toStringTrue, String toStringFalse) { TableLocation location = isH2 == null ? TableLocation.parse(input) : TableLocation.parse(input, isH2); assertEquals(catalog, location.getCatalog()); assertEquals(schema, location.getSchema()); assertEquals(table, location.getTable()); assertEquals(toString, location.toString()); assertEquals(toStringTrue, location.toString(true)); assertEquals(toStringFalse, location.toString(false)); }
/** * Return the first spatial geometry field name * * @param tableName * @param connection * @return the name of the first geometry column * @throws SQLException */ private static String getFirstGeometryField(String tableName, Connection connection) throws SQLException { // Find first geometry column List<String> geomFields = SFSUtilities.getGeometryFields( connection, TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection.getMetaData()))); if (!geomFields.isEmpty()) { return geomFields.get(0); } else { throw new SQLException("The table " + tableName + " does not contain a geometry field"); } }
/** * Copy fields from table into a {@link org.h2.tools.SimpleResultSet} * * @param connection Active connection * @param rs Result set that will receive columns * @param tableLocation Import columns from this table * @throws SQLException Error */ public static void copyFields( Connection connection, SimpleResultSet rs, TableLocation tableLocation) throws SQLException { DatabaseMetaData meta = connection.getMetaData(); ResultSet columnsRs = meta.getColumns( tableLocation.getCatalog(null), tableLocation.getSchema(null), tableLocation.getTable(), null); Map<Integer, Object[]> columns = new HashMap<Integer, Object[]>(); int COLUMN_NAME = 0, COLUMN_TYPE = 1, COLUMN_TYPENAME = 2, COLUMN_PRECISION = 3, COLUMN_SCALE = 4; try { while (columnsRs.next()) { Object[] columnInfoObjects = new Object[COLUMN_SCALE + 1]; columnInfoObjects[COLUMN_NAME] = columnsRs.getString("COLUMN_NAME"); columnInfoObjects[COLUMN_TYPE] = columnsRs.getInt("DATA_TYPE"); columnInfoObjects[COLUMN_TYPENAME] = columnsRs.getString("TYPE_NAME"); columnInfoObjects[COLUMN_PRECISION] = columnsRs.getInt("COLUMN_SIZE"); columnInfoObjects[COLUMN_SCALE] = columnsRs.getInt("DECIMAL_DIGITS"); columns.put(columnsRs.getInt("ORDINAL_POSITION"), columnInfoObjects); } } finally { columnsRs.close(); } for (int i = 1; i <= columns.size(); i++) { Object[] columnInfoObjects = columns.get(i); rs.addColumn( (String) columnInfoObjects[COLUMN_NAME], (Integer) columnInfoObjects[COLUMN_TYPE], (String) columnInfoObjects[COLUMN_TYPENAME], (Integer) columnInfoObjects[COLUMN_PRECISION], (Integer) columnInfoObjects[COLUMN_SCALE]); } }
/** * Populates the names array with the names of legends that can be applied to the given layer. * * @param layer Layer */ private void initNamesList(ILayer layer) { // Recover the geometry type of this ILayer. DataSource ds = layer.getDataManager().getDataSource(); int geometryColumnsCount = 0; int geomType = GeometryTypeCodes.GEOMETRY; try (Connection connection = ds.getConnection()) { TableLocation tableLocation = TableLocation.parse(layer.getTableReference()); geomType = SFSUtilities.getGeometryType(connection, tableLocation, ""); geometryColumnsCount = SFSUtilities.getGeometryFields(connection, tableLocation).size(); } catch (SQLException e) { LOGGER.warn("Could not determine the specific geometry type for " + "this layer."); } // Convert to a simple geometry. int simpleGeomType = SimpleGeometryType.getSimpleType(geomType); // Fill the names array. ArrayList<String> typeNames = new ArrayList<>(); final int lineOrPolygon = SimpleGeometryType.LINE | SimpleGeometryType.POLYGON; if ((simpleGeomType & SimpleGeometryType.ALL) != 0) { typeNames.add(UniqueSymbolPoint.NAME); if ((simpleGeomType & lineOrPolygon) != 0) { typeNames.add(UniqueSymbolLine.NAME); } if ((simpleGeomType & SimpleGeometryType.POLYGON) != 0) { typeNames.add(UniqueSymbolArea.NAME); } // Do not display the thematic maps that need an attribute if there is // only one geometry if (geometryColumnsCount > -1) { typeNames.add(RecodedPoint.NAME); if ((simpleGeomType & lineOrPolygon) != 0) { typeNames.add(RecodedLine.NAME); } if ((simpleGeomType & SimpleGeometryType.POLYGON) != 0) { typeNames.add(RecodedArea.NAME); } typeNames.add(ProportionalPoint.NAME); if ((simpleGeomType & lineOrPolygon) != 0) { typeNames.add(ProportionalLine.NAME); } typeNames.add(CategorizedPoint.NAME); if ((simpleGeomType & lineOrPolygon) != 0) { typeNames.add(CategorizedLine.NAME); } if ((simpleGeomType & SimpleGeometryType.POLYGON) != 0) { typeNames.add(CategorizedArea.NAME); } } } names = typeNames.toArray(new String[typeNames.size()]); }
/** * Copy data from Shape File into a new table in specified connection. * * @param connection Active connection * @param tableReference [[catalog.]schema.]table reference * @param fileName File path of the SHP file or URI * @param forceEncoding Use this encoding instead of DBF file header encoding property. */ public static void readShape( Connection connection, String fileName, String tableReference, String forceEncoding) throws IOException, SQLException { File file = URIUtility.fileFromString(fileName); if (!file.exists()) { throw new FileNotFoundException("The following file does not exists:\n" + fileName); } SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); shpDriverFunction.importFile( connection, TableLocation.parse(tableReference, true).toString(true), file, new EmptyProgressVisitor(), forceEncoding); }
@Override public ILayer createLayer(String layerName, String tableRef) throws LayerException { try { try (Connection connection = dataManager.getDataSource().getConnection()) { List<String> geoFields = SFSUtilities.getGeometryFields(connection, TableLocation.parse(tableRef)); if (!geoFields.isEmpty()) { return new Layer(layerName, tableRef, dataManager); } else { throw new LayerException(I18N.tr("The source contains no spatial info")); } } } catch (SQLException ex) { throw new LayerException("Cannot retrieve spatial metadata", ex); } }
@Override public ILayer createLayer(String tableRef) throws LayerException { return createLayer(TableLocation.parse(tableRef).getTable(), tableRef); }
/** * Suffix a TableLocation * * @param inputTable Input table * @param suffix Suffix * @return suffixed TableLocation */ public static TableLocation suffixTableLocation(TableLocation inputTable, String suffix) { return new TableLocation( inputTable.getCatalog(), inputTable.getSchema(), inputTable.getTable() + suffix); }
/** * Convert an input table String to a TableLocation * * @param connection Connection * @param inputTable Input table * @return corresponding TableLocation * @throws SQLException */ public static TableLocation parseInputTable(Connection connection, String inputTable) throws SQLException { return TableLocation.parse(inputTable, JDBCUtilities.isH2DataBase(connection.getMetaData())); }