use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.
the class JdbcDdlWriter method writeCreateTable.
public void writeCreateTable(final RecordDefinition recordDefinition) {
final String typePath = recordDefinition.getPath();
this.out.println();
this.out.print("CREATE TABLE ");
writeTableName(typePath);
this.out.println(" (");
for (int i = 0; i < recordDefinition.getFieldCount(); i++) {
final FieldDefinition attribute = recordDefinition.getField(i);
if (i > 0) {
this.out.println(",");
}
final String name = attribute.getName();
this.out.print(" ");
this.out.print(name);
for (int j = name.length(); j < 32; j++) {
this.out.print(' ');
}
writeColumnDataType(attribute);
if (attribute.isRequired()) {
this.out.print(" NOT NULL");
}
}
this.out.println();
this.out.println(");");
writeAddPrimaryKeyConstraint(recordDefinition);
writeGeometryRecordDefinition(recordDefinition);
final FieldDefinition idField = recordDefinition.getIdField();
if (idField != null) {
if (Number.class.isAssignableFrom(idField.getDataType().getJavaClass())) {
writeCreateSequence(recordDefinition);
}
}
}
use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.
the class JdbcQueryIterator method getResultSet.
protected ResultSet getResultSet() {
final String tableName = this.query.getTypeName();
final RecordDefinition queryRecordDefinition = this.query.getRecordDefinition();
if (queryRecordDefinition != null) {
this.recordDefinition = this.recordStore.getRecordDefinition(queryRecordDefinition);
if (this.recordDefinition != null) {
this.query.setRecordDefinition(this.recordDefinition);
}
}
if (this.recordDefinition == null) {
if (tableName != null) {
this.recordDefinition = this.recordStore.getRecordDefinition(tableName);
this.query.setRecordDefinition(this.recordDefinition);
}
}
String dbTableName;
if (this.recordDefinition == null) {
final PathName pathName = PathName.newPathName(tableName);
if (pathName == null) {
dbTableName = null;
} else {
dbTableName = pathName.getName();
}
} else {
dbTableName = this.recordDefinition.getDbTableName();
}
final String sql = getSql(this.query);
try {
this.statement = this.connection.prepareStatement(sql);
this.statement.setFetchSize(this.fetchSize);
this.resultSet = getResultSet(this.statement, this.query);
final ResultSetMetaData resultSetMetaData = this.resultSet.getMetaData();
if (this.recordDefinition == null) {
this.recordDefinition = this.recordStore.getRecordDefinition(tableName, resultSetMetaData, dbTableName);
}
final List<String> fieldNames = new ArrayList<>(this.query.getFieldNames());
if (fieldNames.isEmpty()) {
this.fields.addAll(this.recordDefinition.getFields());
} else {
for (String fieldName : fieldNames) {
if (fieldName.equals("*")) {
this.fields.addAll(this.recordDefinition.getFields());
} else {
if (fieldName.endsWith("\"")) {
final int index = fieldName.indexOf('"');
if (index > 0 && fieldName.charAt(index - 1) == ' ') {
fieldName = fieldName.substring(index + 1, fieldName.length() - 1);
}
}
final FieldDefinition field = this.recordDefinition.getField(fieldName);
if (field != null) {
this.fields.add(field);
}
}
}
}
final String typePath = this.query.getTypeNameAlias();
if (typePath != null) {
final JdbcRecordDefinition newRecordDefinition = this.recordDefinition.rename(typePath);
this.recordDefinition = newRecordDefinition;
}
} catch (final SQLException e) {
JdbcUtils.close(this.statement, this.resultSet);
throw this.connection.getException("Execute Query", sql, e);
}
return this.resultSet;
}
use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.
the class JmxService method getAttributes.
/**
* Get the values for all attributes for an object on a server.
*
* @param serverId The name of the server.
* @param objectId The name of the object.
* @return The attribute values.
*/
public List<FieldDefinition> getAttributes(final String serverId, final String objectId) {
final MBeanServerConnection connection = getConnection(serverId);
try {
final String[] attributeNames = getAttributeNames(serverId, objectId).toArray(new String[0]);
final ObjectName objectName = getObjectName(serverId, objectId);
final List<FieldDefinition> attributeValues = new ArrayList<>();
for (final Object attribute : connection.getAttributes(objectName, attributeNames)) {
attributeValues.add((FieldDefinition) attribute);
}
return attributeValues;
} catch (final InstanceNotFoundException e) {
throw new IllegalArgumentException("MBean not found " + serverId + " " + objectId);
} catch (final MalformedObjectNameException e) {
throw new IllegalArgumentException("MBean name not valid " + serverId + " " + objectId);
} catch (final Throwable e) {
return Exceptions.throwUncheckedException(e);
}
}
use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.
the class JdbcRecordWriter method getUpdateSql.
private String getUpdateSql(final JdbcRecordDefinition recordDefinition) {
final List<FieldDefinition> idFields = recordDefinition.getIdFields();
if (idFields.isEmpty()) {
throw new RuntimeException("No primary key found for: " + recordDefinition);
} else {
final String tableName = recordDefinition.getDbTableQualifiedName();
String sql = this.typeUpdateSqlMap.get(recordDefinition);
if (sql == null) {
final StringBuilder sqlBuffer = new StringBuilder();
if (this.sqlPrefix != null) {
sqlBuffer.append(this.sqlPrefix);
}
sqlBuffer.append("update ");
sqlBuffer.append(tableName);
sqlBuffer.append(" set ");
boolean first = true;
for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
if (!idFields.contains(fieldDefinition)) {
final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
if (first) {
first = false;
} else {
sqlBuffer.append(", ");
}
jdbcFieldDefinition.appendColumnName(sqlBuffer, this.quoteColumnNames);
sqlBuffer.append(" = ");
jdbcFieldDefinition.addInsertStatementPlaceHolder(sqlBuffer, false);
}
}
sqlBuffer.append(" where ");
appendIdEquals(sqlBuffer, idFields);
sqlBuffer.append(" ");
if (this.sqlSuffix != null) {
sqlBuffer.append(this.sqlSuffix);
}
sql = sqlBuffer.toString();
this.typeUpdateSqlMap.put(recordDefinition, sql);
}
return sql;
}
}
use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.
the class JdbcRecordWriter method setIdEqualsValues.
private int setIdEqualsValues(final PreparedStatement statement, int parameterIndex, final JdbcRecordDefinition recordDefinition, final Record record) throws SQLException {
for (final FieldDefinition idField : recordDefinition.getIdFields()) {
final Object value = record.getValue(idField);
parameterIndex = ((JdbcFieldDefinition) idField).setPreparedStatementValue(statement, parameterIndex++, value);
}
return parameterIndex;
}
Aggregations