use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.
the class OracleJdbcQueryResultPager method updateResults.
/**
* Update the cached results for the current page.
*/
@Override
protected void updateResults() {
synchronized (this) {
final JdbcRecordStore recordStore = getRecordStore();
final Query query = getQuery();
setNumResults(recordStore.getRecordCount(query));
updateNumPages();
final ArrayList<Record> results = new ArrayList<>();
final int pageSize = getPageSize();
final int pageNumber = getPageNumber();
if (pageNumber != -1) {
String sql = getSql();
final int startRowNum = (pageNumber - 1) * pageSize + 1;
final int endRowNum = startRowNum + pageSize - 1;
sql = "SELECT * FROM ( SELECT T2.*, ROWNUM TROWNUM FROM ( " + sql + ") T2 ) WHERE TROWNUM BETWEEN " + startRowNum + " AND " + endRowNum;
try (final JdbcConnection connection = getRecordStore().getJdbcConnection()) {
final RecordFactory<Record> recordFactory = getRecordFactory();
final RecordDefinition recordDefinition = getRecordDefinition();
final List<FieldDefinition> attributes = new ArrayList<>();
final List<String> fieldNames = query.getFieldNames();
if (fieldNames.isEmpty()) {
attributes.addAll(recordDefinition.getFields());
} else {
for (final String fieldName : fieldNames) {
if (fieldName.equals("*")) {
attributes.addAll(recordDefinition.getFields());
} else {
final FieldDefinition attribute = recordDefinition.getField(fieldName);
if (attribute != null) {
attributes.add(attribute);
}
}
}
}
try (final PreparedStatement statement = connection.prepareStatement(sql);
final ResultSet resultSet = JdbcQueryIterator.getResultSet(statement, getQuery())) {
if (resultSet.next()) {
int i = 0;
do {
final Record record = JdbcQueryIterator.getNextRecord(recordStore, recordDefinition, attributes, recordFactory, resultSet, this.internStrings);
results.add(record);
i++;
} while (resultSet.next() && i < pageSize);
}
} catch (final SQLException e) {
throw connection.getException("updateResults", sql, e);
}
}
setResults(results);
}
}
}
use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.
the class OracleSdoGeometryFieldAdder method initialize.
@Override
public void initialize(final JdbcRecordStoreSchema schema) {
try (final JdbcConnection connection = this.recordStore.getJdbcConnection()) {
final String schemaName = schema.getDbName();
final String sridSql = "select M.TABLE_NAME, M.COLUMN_NAME, M.SRID, M.DIMINFO, C.GEOMETRY_TYPE " + "from ALL_SDO_GEOM_METADATA M " + "LEFT OUTER JOIN ALL_GEOMETRY_COLUMNS C ON (M.OWNER = C.F_TABLE_SCHEMA AND M.TABLE_NAME = C.F_TABLE_NAME AND M.COLUMN_NAME = C.F_GEOMETRY_COLUMN) " + "where OWNER = ?";
try (final PreparedStatement statement = connection.prepareStatement(sridSql)) {
statement.setString(1, schemaName);
try (final ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
final String tableName = resultSet.getString(1);
final String columnName = resultSet.getString(2);
final PathName typePath = schema.getPathName().newChild(tableName);
int srid = resultSet.getInt(3);
if (resultSet.wasNull() || srid < 0) {
srid = 0;
}
final Object[] dimInfo = (Object[]) resultSet.getArray("DIMINFO").getArray();
int axisCount = dimInfo.length;
setColumnProperty(schema, typePath, columnName, AXIS_COUNT, axisCount);
if (axisCount < 2) {
axisCount = 2;
} else if (axisCount > 4) {
axisCount = 4;
}
final double[] scales = new double[axisCount];
for (int i = 0; i < scales.length; i++) {
scales[i] = getScale(dimInfo, i);
}
final GeometryFactory geometryFactory = this.recordStore.getGeometryFactory(srid, axisCount, scales);
setColumnProperty(schema, typePath, columnName, GEOMETRY_FACTORY, geometryFactory);
setColumnProperty(schema, typePath, columnName, ORACLE_SRID, srid);
final int geometryType = resultSet.getInt(5);
DataType geometryDataType;
if (resultSet.wasNull()) {
geometryDataType = DataTypes.GEOMETRY;
} else {
geometryDataType = ID_TO_DATA_TYPE.get(geometryType);
if (geometryDataType == null) {
geometryDataType = DataTypes.GEOMETRY;
}
}
setColumnProperty(schema, typePath, columnName, GEOMETRY_TYPE, geometryDataType);
}
}
} catch (final SQLException e) {
Logs.error(this, "Unable to initialize", e);
}
}
}
use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.
the class PostgreSQLJdbcQueryResultPager method getList.
@Override
public List<Record> getList() {
synchronized (this) {
if (this.results == null) {
final ArrayList<Record> results = new ArrayList<>();
final int pageSize = getPageSize();
final int pageNumber = getPageNumber();
if (pageNumber != -1) {
String sql = getSql();
final int startRowNum = (pageNumber - 1) * pageSize;
sql = getSql() + " OFFSET " + startRowNum + " LIMIT " + pageSize;
final RecordDefinition recordDefinition = getRecordDefinition();
if (recordDefinition != null) {
final RecordFactory recordFactory = getRecordFactory();
final JdbcRecordStore recordStore = getRecordStore();
try (JdbcConnection connection = recordStore.getJdbcConnection()) {
final List<FieldDefinition> attributes = recordDefinition.getFields();
try (final PreparedStatement statement = connection.prepareStatement(sql);
final ResultSet resultSet = JdbcQueryIterator.getResultSet(statement, getQuery())) {
if (resultSet.next()) {
int i = 0;
do {
final Record object = JdbcQueryIterator.getNextRecord(recordStore, recordDefinition, attributes, recordFactory, resultSet, this.internStrings);
results.add(object);
i++;
} while (resultSet.next() && i < pageSize);
}
} catch (final SQLException e) {
throw connection.getException("updateResults", sql, e);
}
}
}
}
this.results = results;
}
return this.results;
}
}
use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method deleteRecords.
@Override
public int deleteRecords(final Query query) {
final String typeName = query.getTypeName();
RecordDefinition recordDefinition = query.getRecordDefinition();
if (recordDefinition == null) {
if (typeName != null) {
recordDefinition = getRecordDefinition(typeName);
query.setRecordDefinition(recordDefinition);
}
}
final String sql = JdbcUtils.getDeleteSql(query);
try (Transaction transaction = newTransaction(com.revolsys.transaction.Propagation.REQUIRED)) {
// rolled back.
try (JdbcConnection connection = getJdbcConnection(isAutoCommit());
final PreparedStatement statement = connection.prepareStatement(sql)) {
JdbcUtils.setPreparedStatementParameters(statement, query);
return statement.executeUpdate();
} catch (final SQLException e) {
transaction.setRollbackOnly();
throw new RuntimeException("Unable to delete : " + sql, e);
} catch (final RuntimeException e) {
transaction.setRollbackOnly();
throw e;
} catch (final Error e) {
transaction.setRollbackOnly();
throw e;
}
}
}
use of com.revolsys.jdbc.JdbcConnection in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method getRecordCount.
@Override
public int getRecordCount(Query query) {
if (query == null) {
return 0;
} else {
query = query.clone();
query.setSql(null);
query.setFieldNames("count(*)");
query.clearOrderBy();
final String sql = JdbcUtils.getSelectSql(query);
try (JdbcConnection connection = getJdbcConnection()) {
try (final PreparedStatement statement = connection.prepareStatement(sql)) {
JdbcUtils.setPreparedStatementParameters(statement, query);
try (final ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
final int rowCount = resultSet.getInt(1);
return rowCount;
} else {
return 0;
}
}
} catch (final SQLException e) {
throw connection.getException("getRecordCount", sql, e);
} catch (final IllegalArgumentException e) {
Logs.error(this, "Cannot get row count: " + query, e);
return 0;
}
}
}
}
Aggregations