private static boolean isLocalH2DataBase(DatabaseMetaData meta) throws SQLException { return JDBCUtilities.isH2DataBase(meta) && meta.getURL().startsWith("jdbc:h2:") && !meta.getURL().startsWith("jdbc:h2:tcp:/"); }
/** * Check if one table already exists * * @param connection * @param isH2 * @param requestedTable * @param osmTableName * @throws SQLException */ private void checkOSMTables(Connection connection, boolean isH2, TableLocation requestedTable, String osmTableName) throws SQLException { String[] omsTables = new String[]{TAG, NODE, NODE_TAG, WAY, WAY_NODE, WAY_TAG, RELATION, RELATION_TAG, NODE_MEMBER, WAY_MEMBER, RELATION_MEMBER}; for (String omsTableSuffix : omsTables) { String osmTable = caseIdentifier(requestedTable, osmTableName + omsTableSuffix, isH2); if (JDBCUtilities.tableExists(connection, osmTable)) { throw new SQLException("The table " + osmTable + " already exists."); } } }
private static boolean hasField(ResultSetMetaData resultSetMetaData, String fieldName) throws SQLException { return getFieldIndex(resultSetMetaData, fieldName) != -1; }
try (Statement st = connection.createStatement()) { DatabaseMetaData meta = connection.getMetaData(); int pkId = JDBCUtilities.getIntegerPrimaryKey(connection, tableLocation.toString()); if (pkId > 0) { return JDBCUtilities.getFieldName(connection.getMetaData(), tableLocation.toString(), pkId); boolean isH2 = JDBCUtilities.isH2DataBase(meta); String systemRowName = getSystemLongRowIdentifier(isH2);
/** * Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a temporary table. * @param connection Active connection not closed by this method * @param tableReference Table reference * @return True if the provided table is temporary. * @throws SQLException If the table does not exists. */ public static boolean isTemporaryTable(Connection connection, String tableReference) throws SQLException { TableLocation location = TableLocation.parse(tableReference); ResultSet rs = getTablesView(connection, location.getCatalog(), location.getSchema(), location.getTable()); boolean isTemporary = false; try { if(rs.next()) { String tableType; if(hasField(rs.getMetaData(), "STORAGE_TYPE")) { // H2 tableType = rs.getString("STORAGE_TYPE"); } else { // Standard SQL tableType = rs.getString("TABLE_TYPE"); } isTemporary = tableType.contains("TEMPORARY"); } else { throw new SQLException("The table "+location+" does not exists"); } } finally { rs.close(); } return isTemporary; }
List<String> spatialFieldNames = SFSUtilities.getGeometryFields(connection, TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection.getMetaData()))); if (spatialFieldNames.isEmpty()) { throw new SQLException(String.format("The table %s does not contain a geometry field", tableName)); ResultSet rs = st.executeQuery(String.format("select * from %s", tableName)); try { int recordCount = JDBCUtilities.getRowCount(connection, tableName); ProgressVisitor copyProgress = progress.subProcess(recordCount); ResultSetMetaData resultSetMetaData = rs.getMetaData(); int geoFieldIndex = JDBCUtilities.getFieldIndex(resultSetMetaData, spatialFieldNames.get(0));
if (JDBCUtilities.isH2DataBase(connection.getMetaData()) && !JDBCUtilities.tableExists(connection, "PUBLIC.GEOMETRY_COLUMNS")) { st.execute("CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR\n" + " \"org.h2gis.functions.factory.H2GISFunctions.load\";\n" +
final TableLocation nodesName = TableUtilities.suffixTableLocation(tableName, NODES_SUFFIX); final TableLocation edgesName = TableUtilities.suffixTableLocation(tableName, EDGES_SUFFIX); boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); if(deleteTables){ Statement stmt = connection.createStatement(); else if (JDBCUtilities.tableExists(connection, nodesName.getTable()) || JDBCUtilities.tableExists(connection, edgesName.getTable())) { throw new IllegalArgumentException(ALREADY_RUN_ERROR + tableName.getTable()); COORDS_TABLE = TableLocation.parse(System.currentTimeMillis()+"_COORDS", isH2).toString(); final int pkIndex = JDBCUtilities.getIntegerPrimaryKey(connection, tableName.getTable()); if (pkIndex == 0) { throw new IllegalStateException("Table " + tableName.getTable() final String pkColName = JDBCUtilities.getFieldName(md, tableName.getTable(), pkIndex); spatialFieldName = (String) spatialFieldIndexAndName[0]; checkGeometryType(connection, tableName, spatialFieldIndex); final String geomCol = JDBCUtilities.getFieldName(md, tableName.getTable(), spatialFieldIndex); final Statement st = connection.createStatement(); try {
final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); if (FileUtil.isExtensionWellFormated(fileName, "shp")) { TableLocation location = TableLocation.parse(tableReference, isH2); int recordCount = JDBCUtilities.getRowCount(connection, tableReference); ProgressVisitor copyProgress = progress.subProcess(recordCount);
fos = new FileOutputStream(fileName); final TableLocation parse = TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection.getMetaData())); List<String> spatialFieldNames = SFSUtilities.getGeometryFields(connection,parse); if (spatialFieldNames.isEmpty()) { int geoFieldIndex = JDBCUtilities.getFieldIndex(resultSetMetaData, spatialFieldNames.get(0));
listener); try { int pkIndex = JDBCUtilities.getIntegerPrimaryKey(connection, tableLocation.toString()); if (pkIndex > 0) { ProgressMonitor jobProgress = progressMonitor.startTask(2); String pkFieldName = TableLocation.quoteIdentifier(JDBCUtilities.getFieldName(meta, table, pkIndex)); String desc = ""; if(!ascending) {
@Override public void init(Connection conn, String schemaName, String triggerName, String tableName, boolean before, int type) throws SQLException { this.update = type == UPDATE; this.tableIdentifier = new TableLocation(schemaName, tableName).toString(true); if(!dataManager.hasTableEditListener(tableIdentifier)) { try(Statement st = conn.createStatement()) { st.execute("DROP TRIGGER IF EXISTS "+triggerName); } throw new SQLException("This trigger does not exists"); } else { // Fetch primary key column pkColumn = JDBCUtilities.getIntegerPrimaryKey(conn, tableIdentifier); } }
private static void checkGeometryType(Connection connection, TableLocation tableName, int spatialFieldIndex) throws SQLException { final String fieldName = JDBCUtilities.getFieldName(connection.getMetaData(), tableName.getTable(), spatialFieldIndex); int geomType = SFSUtilities.getGeometryType(connection, tableName, fieldName); if (geomType != GeometryTypeCodes.LINESTRING) { throw new IllegalArgumentException(TYPE_ERROR + SFSUtilities.getGeometryTypeNameFromCode(geomType)); } }
/** * Return true if table tableName contains field fieldName. * * @param connection Connection * @param tableName Table name * @param fieldName Field name * @return True if the table contains the field * @throws SQLException */ public static boolean hasField(Connection connection, String tableName, String fieldName) throws SQLException { final Statement statement = connection.createStatement(); try { final ResultSet resultSet = statement.executeQuery( "SELECT * FROM " + TableLocation.parse(tableName) + " LIMIT 0;"); try { return hasField(resultSet.getMetaData(), fieldName); } finally { resultSet.close(); } } catch (SQLException ex) { return false; } finally { statement.close(); } }
@Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { int recordCount = JDBCUtilities.getRowCount(connection, tableReference); ProgressVisitor copyProgress = progress.subProcess(recordCount); GeoJsonWriteDriver geoJsonDriver = new GeoJsonWriteDriver(connection, tableReference, fileName); geoJsonDriver.write(copyProgress); }
List<String> spatialFieldNames = SFSUtilities.getGeometryFields(connection, TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection.getMetaData()))); if (spatialFieldNames.isEmpty()) { throw new SQLException(String.format("The table %s does not contain a geometry field", tableName)); ResultSet rs = st.executeQuery(String.format("select * from %s", tableName)); try { int recordCount = JDBCUtilities.getRowCount(connection, tableName); ProgressVisitor copyProgress = progress.subProcess(recordCount); ResultSetMetaData resultSetMetaData = rs.getMetaData(); int geoFieldIndex = JDBCUtilities.getFieldIndex(resultSetMetaData, spatialFieldNames.get(0));
try(Connection connection = dataSource.getConnection()) { DatabaseMetaData meta = connection.getMetaData(); boolean isH2 = JDBCUtilities.isH2DataBase(meta); while (!inputAccepted) { newName = JOptionPane.showInputDialog( + "\n" + I18N.tr(newNameMessage)); else if (JDBCUtilities.tableExists(connection, newName)) { message.setText(I18N.tr("A datasource with that name already exists.") + "\n" + I18N.tr(newNameMessage));
final TableLocation nodesName = TableUtilities.suffixTableLocation(tableName, NODES_SUFFIX); final TableLocation edgesName = TableUtilities.suffixTableLocation(tableName, EDGES_SUFFIX); boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); if(deleteTables){ Statement stmt = connection.createStatement(); else if (JDBCUtilities.tableExists(connection, nodesName.getTable()) || JDBCUtilities.tableExists(connection, edgesName.getTable())) { throw new IllegalArgumentException(ALREADY_RUN_ERROR + tableName.getTable()); final int pkIndex = JDBCUtilities.getIntegerPrimaryKey(connection, tableName.getTable()); if (pkIndex == 0) { throw new IllegalStateException("Table " + tableName.getTable() final String pkColName = JDBCUtilities.getFieldName(md, tableName.getTable(), pkIndex); spatialFieldName = (String) spatialFieldIndexAndName[0]; checkGeometryType(connection, tableName, spatialFieldIndex); final String geomCol = JDBCUtilities.getFieldName(md, tableName.getTable(), spatialFieldIndex); final Statement st = connection.createStatement(); try {
@Override protected Object doInBackground() throws SQLException { try(Connection connection = dataSource.getConnection()) { boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); int columnId = JDBCUtilities.getIntegerPrimaryKey(connection, tableName); if(columnId > 0) { String pkColumnName = JDBCUtilities.getFieldName(connection.getMetaData(), tableName, columnId); // A deletion batch is an ordered set of pk values to remove (a complete serial of integer without holes) List<Long> deletionBatch = rowPkToDelete.getValueRanges(); ProgressMonitor pmBatch = getProgressMonitor().startTask(deletionBatch.size() / 2); try(PreparedStatement st = connection.prepareStatement( String.format("DELETE FROM %s WHERE %s >= ? AND %s <= ?", TableLocation.parse(tableName).toString(isH2),pkColumnName, pkColumnName))) { for (int idDelBatch = 0; idDelBatch < deletionBatch.size() - 1; idDelBatch += 2) { long startBatch = deletionBatch.get(idDelBatch); long endBatch = deletionBatch.get(idDelBatch + 1); st.setLong(1, startBatch); st.setLong(2, endBatch); st.execute(); pmBatch.endTask(); } } } } return null; }
public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress,String encoding) throws SQLException, IOException { if (FileUtil.isExtensionWellFormated(fileName, "dbf")) { int recordCount = JDBCUtilities.getRowCount(connection, tableReference); final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); String tableName = TableLocation.parse(tableReference, isH2).toString(isH2);