/** * Return the table identifier in the best fit depending on database type * * @param requestedTable Catalog and schema used * @param tableName Table without quotes * @param isH2 True if H2, false if PostGRES * @return Find table identifier */ private static String caseIdentifier(TableLocation requestedTable, String tableName, boolean isH2) { return new TableLocation(requestedTable.getCatalog(), requestedTable.getSchema(), TableLocation.parse(tableName, isH2).getTable()).toString(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); if(!catalog.isEmpty()) { sb.append(quoteIdentifier(catalog)); sb.append("."); } if(!schema.isEmpty()) { sb.append(quoteIdentifier(schema)); sb.append("."); } sb.append(quoteIdentifier(table)); return sb.toString(); }
/** * Check if two table identifier are equals * @param tableIdentifier First table identifier * @param otherTableIdentifier Second table identifier * @return True if there are equal. */ public static boolean isTableIdentifierEquals(String tableIdentifier, String otherTableIdentifier) { TableLocation tableLocation = TableLocation.parse(tableIdentifier); TableLocation tableLocation1 = TableLocation.parse(otherTableIdentifier); return (tableLocation.getSchema().isEmpty() && tableLocation1.getSchema().equalsIgnoreCase("public") || tableLocation1.getSchema().isEmpty() && tableLocation.getSchema().equalsIgnoreCase("public") || tableLocation1.getSchema().equals(tableLocation.getSchema())) && tableLocation.getTable().equals(tableLocation1.getTable()); }
String[] sources = (String[]) t.getTransferData(TransferableSource.sourceFlavor); for(String source : sources) { TableLocation table = TableLocation.parse(source); try(ResultSet rs = metaData.getColumns(table.getCatalog(), table.getSchema(), table.getTable(), null)) { Map<Integer, String> columns = new HashMap<>(); while(rs.next()) { for(int colId : new TreeSet<>(columns.keySet())) { String fieldName = columns.remove(colId); fields.append(TableLocation.quoteIdentifier(fieldName, isH2)); if(!columns.isEmpty()) { fields.append(", "); textArea.append(String.format("SELECT %s FROM %s;\n",fields.toString(),table.toString(isH2)));
/** * Find geometry fields name of a table. * @param connection Active connection * @param location Table location * @return A list of Geometry fields name * @throws SQLException */ public static List<String> getGeometryFields(Connection connection,TableLocation location) throws SQLException { return getGeometryFields(connection, location.getCatalog(), location.getSchema(), location.getTable()); }
throw new SQLException("Specified source does not exists"); String tableName = findUniqueTableName(TableLocation.capsIdentifier(FileUtils.getNameFromURI(uri), isH2)); try (Connection connection = dataSource.getConnection()) { URI filePath = URI.create(remarks); if(filePath.equals(path.toURI())) { return new TableLocation(tablesRs.getString("TABLE_CAT"), tablesRs.getString("TABLE_SCHEM"), tablesRs.getString("TABLE_NAME")).toString(); st.setString(2, new TableLocation("","",tableName).toString(isH2)); st.execute(); return new TableLocation(query.get("catalog"),query.get("schema"),query.get("table")).toString();
/** * Returns the list of distinct values contained by a field from a table from the database * * @param connection Connection * @param tableName Name of the table containing the field. * @param fieldName Name of the field containing the values. * @return The list of distinct values of the field. */ public static List<String> getUniqueFieldValues(Connection connection, String tableName, String fieldName) throws SQLException { final Statement statement = connection.createStatement(); List<String> fieldValues = new ArrayList<String>(); try { ResultSet result = statement.executeQuery("SELECT DISTINCT "+TableLocation.quoteIdentifier(fieldName)+" FROM "+TableLocation.parse(tableName)); while(result.next()){ fieldValues.add(result.getString(1)); } } finally { statement.close(); } return fieldValues; }
@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"); } }
stringBuilder.append(", "); String fieldName = TableLocation.capsIdentifier(header.getFieldName(idColumn), isH2Database); stringBuilder.append(TableLocation.quoteIdentifier(fieldName,isH2Database)); stringBuilder.append(" "); switch (header.getFieldType(idColumn)) {
@Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, boolean deleteTables) throws SQLException, IOException { if(deleteTables) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); String table = requestedTable.getTable(); Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + table); stmt.close(); } importFile(connection, tableReference, fileName, progress); }
/** * Convert catalog.schema.table, schema.table or table into a TableLocation * instance. Non-specified schema or catalogs are converted to the empty * string. * * @param concatenatedTableLocation Table location [[Catalog.]Schema.]Table * @return Java beans for table location */ public static TableLocation parse(String concatenatedTableLocation) { return parse(concatenatedTableLocation, null); }
token = capsIdentifier(token, isH2Database); table = values[2].trim(); return new TableLocation(catalog,schema,table);
@Override public String getTable() { return location.toString(); }
private void doUpdate(Long pk, Object value, boolean callListeners) throws SQLException { try(Connection connection = dataManager.getDataSource().getConnection(); PreparedStatement st = connection.prepareStatement("UPDATE "+tableLocation+" SET "+TableLocation.quoteIdentifier(columnName, isH2)+" = ? WHERE "+pkName+" = ?")) { st.setObject(1, value); st.setLong(2, pk); st.execute(); if(callListeners) { try (Statement stat = connection.createStatement(); ResultSet rs = stat.executeQuery("SELECT * from " + tableLocation.toString(isH2) + " LIMIT 0")) { // Fire with the new PK Value Long pkToFire = pk; if( columnName.equals(pkName)) { pkToFire = Long.valueOf(value.toString()); } dataManager.fireTableEditHappened(new TableEditEvent(tableLocation.toString(isH2), JDBCUtilities.getFieldIndex(rs.getMetaData(), columnName), pkToFire, pkToFire, TableModelEvent.UPDATE)); } } } }
@Override public void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException { final TableLocation notificationTable = new TableLocation(TRIGGER_SCHEMA, NOTIFICATION_TABLE); try { PreparedStatement st = conn.prepareStatement("INSERT INTO "+notificationTable+"(idtrigger) VALUES(?)"); st.setInt(1, idTrigger); try { st.execute(); } finally { st.close(); } } catch (Exception ex) { // Ignore exception to not interfere with database } }
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)); } }
message.getText(), MetaData.getNewUniqueName(sourceTable, meta, TableLocation.capsIdentifier(I18N.tr("selection"), isH2)));
/** * Find geometry fields name of a table. * @param connection Active connection * @param location Table location * @return A list of Geometry fields name * @throws SQLException */ public static List<String> getGeometryFields(Connection connection,TableLocation location) throws SQLException { return getGeometryFields(connection, location.getCatalog(), location.getSchema(), location.getTable()); }