Search in sources :

Example 1 with FieldDefinition

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);
        }
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Example 2 with FieldDefinition

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) ArrayList(java.util.ArrayList) PathName(com.revolsys.io.PathName) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 3 with FieldDefinition

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);
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) InstanceNotFoundException(javax.management.InstanceNotFoundException) ArrayList(java.util.ArrayList) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Example 4 with FieldDefinition

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;
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Example 5 with FieldDefinition

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;
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Aggregations

FieldDefinition (com.revolsys.record.schema.FieldDefinition)133 RecordDefinition (com.revolsys.record.schema.RecordDefinition)38 DataType (com.revolsys.datatype.DataType)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)19 PathName (com.revolsys.io.PathName)15 Record (com.revolsys.record.Record)15 ArrayList (java.util.ArrayList)15 Geometry (com.revolsys.geometry.model.Geometry)13 CodeTable (com.revolsys.record.code.CodeTable)9 Query (com.revolsys.record.query.Query)8 LineString (com.revolsys.geometry.model.LineString)7 ArrayRecord (com.revolsys.record.ArrayRecord)7 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)7 SQLException (java.sql.SQLException)7 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 BadLocationException (javax.swing.text.BadLocationException)4