public PrimaryKeyColumn getColumn(String name) { for (PrimaryKeyColumn col : columns) { if (name.equals((col.getName()))) { return col; } } return null; } }
@Override public String getPkColumnValue(ResultSet rs, PrimaryKeyColumn pkey, int columnIdx) throws SQLException { // by spec primary key of a GeoPackage column is integer, but maybe other tables do not // follow this rule. Calling rs.getInt avoids an inefficient block of code in sqlite driver if (Integer.class.equals(pkey.getType())) { // primary keys cannot contain null values, so no need to check rs.wasNull() and // thus saving an expensive native call return String.valueOf(rs.getInt(columnIdx)); } else { return rs.getString(columnIdx); } }
public void testGetFeatureById() throws Exception { FeatureSource fsView = dataStore.getFeatureSource("riverReducedPk"); assertFalse(fsView instanceof FeatureStore); // the problem is actually in pk computation PrimaryKey pk = dataStore.getPrimaryKey((SimpleFeatureType) fsView.getSchema()); assertEquals("riverReducedPk", pk.getTableName()); assertEquals(1, pk.getColumns().size()); PrimaryKeyColumn col = pk.getColumns().get(0); assertEquals(aname("id"), col.getName()); assertTrue(Number.class.isAssignableFrom(col.getType())); FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); Id filter = ff.id(Collections.singleton(ff.featureId("riverReducedPk.0"))); assertEquals(1, fsView.getCount(new Query(null, filter))); }
@Override public Object getLastValue(Connection cx, Statement st) { throw new IllegalArgumentException("Column " + col.getName() + " is not generated."); }
private KeyFetcher createKeyFetcher( JDBCDataStore ds, Connection cx, PrimaryKey key, PrimaryKeyColumn col) throws SQLException, IOException { final Class t = col.getType(); if (col instanceof AutoGeneratedPrimaryKeyColumn) { return new AutoGenerated(ds, key, col); } else if (col instanceof SequencedPrimaryKeyColumn) { return new FromSequence(ds, col); } else { // try to calculate // is the column numeric? if (Number.class.isAssignableFrom(t)) { // is the column integral? if (t == Short.class || t == Integer.class || t == Long.class || BigInteger.class.isAssignableFrom(t) || BigDecimal.class.isAssignableFrom(t)) { return new FromPreviousIntegral(ds, cx, key, col); } } else if (CharSequence.class.isAssignableFrom(t)) { return new FromRandom(ds, col); } } throw new IOException("Cannot generate key value for column of type: " + t.getName()); }
@Override public Object getLastValue(Connection cx, Statement st) throws SQLException { throw new IllegalArgumentException("Column " + col.getName() + " is not generated."); }
Object value = values.get(i); if (value != null) { Class type = key.getColumns().get(i).getType(); Object converted = Converters.convert(value, type); if (converted != null) {
@Override public Object getLastValue(Connection cx, Statement st) { throw new IllegalArgumentException("Column " + col.getName() + " is not generated."); }
/** Set all the key values (the ones that are known before insert) for a prepared statement. */ public int setKeyValues( PreparedStatementSQLDialect dialect, PreparedStatement ps, Connection cx, SimpleFeatureType featureType, SimpleFeature feature, int curFieldPos) throws IOException, SQLException { final List<Object> keyValues = getNextValues(cx, feature); for (int i = 0; i < key.getColumns().size(); i++) { final PrimaryKeyColumn col = key.getColumns().get(i); final Object value = keyValues.get(i); if (value != NOT_SET_BEFORE_INSERT) { dialect.setValue(value, col.getType(), ps, curFieldPos++, cx); } } if (!isPostInsert()) { // report the feature id as user data since we cant set the fid. String fid = featureType.getTypeName() + "." + JDBCDataStore.encodeFID(keyValues); feature.getUserData().put("fid", fid); } return curFieldPos; }
/** * Returns the set of the primary key column names. The set is guaranteed to have the same * iteration order as the primary key. */ protected static LinkedHashSet<String> getColumnNames(PrimaryKey key) { LinkedHashSet<String> pkColumnNames = new LinkedHashSet<String>(); for (PrimaryKeyColumn pkcol : key.getColumns()) { pkColumnNames.add(pkcol.getName()); } return pkColumnNames; }
if (value != NOT_SET_BEFORE_INSERT) { try { dialect.encodeValue(value, col.getType(), sql); sql.append(","); } catch (Exception e) {
private void encodeWhereStatement(StringBuffer buf, List<PrimaryKeyColumn> ids, String table) { buf.append(" WHERE "); for (int i = 0; i < ids.size(); i++) { encodeColumnName(ids.get(i).getName(), buf); buf.append('='); buf.append(table).append('.'); encodeColumnName(ids.get(i).getName(), buf); if (i + 1 < ids.size()) { buf.append(" AND "); } } buf.append(" "); }
@Override public String getPkColumnValue(ResultSet rs, PrimaryKeyColumn pkey, int columnIdx) throws SQLException { // by spec primary key of a GeoPackage column is integer, but maybe other tables do not // follow this rule. Calling rs.getInt avoids an inefficient block of code in sqlite driver if (Integer.class.equals(pkey.getType())) { // primary keys cannot contain null values, so no need to check rs.wasNull() and // thus saving an expensive native call return String.valueOf(rs.getInt(columnIdx)); } else { return rs.getString(columnIdx); } }
private String getPrimaryKeyColumnsAsCommaSeparatedList(List<PrimaryKeyColumn> pkColumns) { StringBuffer sb = new StringBuffer(); boolean first = true; for (PrimaryKeyColumn c : pkColumns) { if (first) { first = false; } else { sb.append(","); } dialect.encodeColumnName(c.getName(), sb); } return sb.toString(); }