use of org.gdal.ogr.Layer in project com.revolsys.open by revolsys.
the class OgrRecordStore method getRecordCount.
@Override
public int getRecordCount(final Query query) {
if (query == null) {
return 0;
} else {
String typePath = query.getTypeName();
RecordDefinition recordDefinition = query.getRecordDefinition();
if (recordDefinition == null) {
typePath = query.getTypeName();
recordDefinition = getRecordDefinition(typePath);
if (recordDefinition == null) {
return 0;
}
} else {
typePath = recordDefinition.getPath();
}
final StringBuilder whereClause = getWhereClause(query);
final StringBuilder sql = new StringBuilder();
sql.append("SELECT COUNT(*) FROM ");
final String layerName = getLayerName(typePath);
sql.append(layerName);
if (whereClause.length() > 0) {
sql.append(" WHERE ");
sql.append(whereClause);
}
final DataSource dataSource = getDataSource();
if (dataSource != null) {
final Layer result = dataSource.ExecuteSQL(sql.toString());
if (result != null) {
addLayerToClose(result);
try {
final Feature feature = result.GetNextFeature();
if (feature != null) {
try {
return feature.GetFieldAsInteger(0);
} finally {
feature.delete();
}
}
} finally {
releaseLayerToClose(result);
}
}
}
}
return 0;
}
use of org.gdal.ogr.Layer in project com.revolsys.open by revolsys.
the class OgrRecordWriter method insert.
private void insert(final Record record) {
final RecordDefinition sourceRecordDefinition = record.getRecordDefinition();
final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(sourceRecordDefinition);
final String typePath = sourceRecordDefinition.getPath();
final List<FieldDefinition> attributes = recordDefinition.getFields();
final List<String> idFieldNames = recordDefinition.getIdFieldNames();
for (final FieldDefinition attribute : attributes) {
final String name = attribute.getName();
if (!idFieldNames.contains(name)) {
if (attribute.isRequired()) {
final Object value = record.getValue(name);
if (value == null) {
throw new IllegalArgumentException("Atribute " + typePath + "." + name + " is required");
}
}
}
}
try {
final Layer layer = getLayer(sourceRecordDefinition);
final FeatureDefn featureDefinition = layer.GetLayerDefn();
final Feature feature = new Feature(featureDefinition);
try {
setFieldValues(featureDefinition, record, feature);
setGeometries(featureDefinition, record, feature);
layer.CreateFeature(feature);
final String driverName = this.recordStore.getDriverName();
if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
record.setValue(OgrRecordStore.ROWID, feature.GetFieldAsInteger(OgrRecordStore.ROWID));
}
record.setState(RecordState.PERSISTED);
} finally {
feature.delete();
this.recordStore.addStatistic("Insert", record);
}
} catch (final IllegalArgumentException e) {
throw new RuntimeException("Unable to insert row " + e.getMessage() + "\n" + record.toString(), e);
} catch (final RuntimeException e) {
Logs.debug(OgrRecordWriter.class, "Unable to insert row \n:" + record.toString());
throw new RuntimeException("Unable to insert row", e);
}
}
use of org.gdal.ogr.Layer in project com.revolsys.open by revolsys.
the class OgrRecordWriter method update.
private void update(final Record record) {
final RecordDefinition recordDefinition = record.getRecordDefinition();
final Layer layer = getLayer(recordDefinition);
final String driverName = this.recordStore.getDriverName();
if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
final Integer fid = record.getInteger(OgrRecordStore.ROWID);
if (fid != null) {
final Feature feature = layer.GetFeature(fid);
if (feature != null) {
final FeatureDefn featureDefinition = layer.GetLayerDefn();
setFieldValues(featureDefinition, record, feature);
layer.SetFeature(feature);
}
}
}
}
Aggregations