use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method getRecordDefinition.
@Override
public JdbcRecordDefinition getRecordDefinition(String typePath, final ResultSetMetaData resultSetMetaData, final String dbTableName) {
if (Property.isEmpty(typePath)) {
typePath = "Record";
}
try {
final PathName pathName = PathName.newPathName(typePath);
final PathName schemaName = pathName.getParent();
final JdbcRecordStoreSchema schema = getSchema(schemaName);
final JdbcRecordDefinition resultRecordDefinition = newRecordDefinition(schema, pathName, dbTableName);
final RecordDefinition recordDefinition = getRecordDefinition(typePath);
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
final String fieldName = resultSetMetaData.getColumnName(i).toUpperCase();
if (recordDefinition != null && recordDefinition.isIdField(fieldName)) {
resultRecordDefinition.setIdFieldIndex(i - 1);
}
addField(resultSetMetaData, resultRecordDefinition, fieldName, i, null);
}
addRecordDefinitionProperties(resultRecordDefinition);
return resultRecordDefinition;
} catch (final SQLException e) {
throw new IllegalArgumentException("Unable to load metadata for " + typePath);
}
}
use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method loadRecordDefinitionsPermissions.
protected Map<PathName, JdbcRecordDefinition> loadRecordDefinitionsPermissions(final JdbcRecordStoreSchema schema) {
final PathName schemaPath = schema.getPathName();
final String dbSchemaName = schema.getDbName();
try (final Connection connection = getJdbcConnection();
final PreparedStatement statement = connection.prepareStatement(this.schemaTablePermissionsSql)) {
if (this.schemaTablePermissionsSql.indexOf('?') != -1) {
statement.setString(1, dbSchemaName);
}
try (final ResultSet resultSet = statement.executeQuery()) {
final Map<PathName, JdbcRecordDefinition> recordDefinitionMap = new TreeMap<>();
while (resultSet.next()) {
final String dbTableName = resultSet.getString("TABLE_NAME");
if (!isExcluded(dbSchemaName, dbTableName)) {
final String tableName = dbTableName.toUpperCase();
final PathName pathName = schemaPath.newChild(tableName);
JdbcRecordDefinition recordDefinition = recordDefinitionMap.get(pathName);
Set<String> tablePermissions;
if (recordDefinition == null) {
recordDefinition = newRecordDefinition(schema, pathName, dbTableName);
recordDefinitionMap.put(pathName, recordDefinition);
tablePermissions = new LinkedHashSet<>();
recordDefinition.setProperty("permissions", tablePermissions);
final String description = resultSet.getString("REMARKS");
recordDefinition.setDescription(description);
final String tableType = resultSet.getString("TABLE_TYPE");
recordDefinition.setProperty("tableType", tableType);
} else {
tablePermissions = recordDefinition.getProperty("permissions");
}
final String privilege = resultSet.getString("PRIVILEGE");
if ("ALL".equals(privilege)) {
tablePermissions.add("SELECT");
tablePermissions.add("INSERT");
tablePermissions.add("UPDATE");
tablePermissions.add("DELETE");
} else {
tablePermissions.add(privilege);
}
}
}
return recordDefinitionMap;
}
} catch (final Throwable e) {
throw Exceptions.wrap("Unable to get schema and table permissions: " + dbSchemaName, e);
}
}
use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.
the class AbstractJdbcRecordStore method refreshSchemaElementsDo.
protected Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElementsDo(final JdbcRecordStoreSchema schema, final PathName schemaPath) {
final String dbSchemaName = schema.getDbName();
final Map<PathName, JdbcRecordDefinition> recordDefinitionMap = loadRecordDefinitionsPermissions(schema);
final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
try {
try (final Connection connection = getJdbcConnection()) {
final DatabaseMetaData databaseMetaData = connection.getMetaData();
final Map<String, List<String>> idFieldNameMap = loadIdFieldNames(dbSchemaName);
for (final JdbcRecordDefinition recordDefinition : recordDefinitionMap.values()) {
final PathName typePath = recordDefinition.getPathName();
final List<String> idFieldNames = idFieldNameMap.get(typePath);
if (Property.isEmpty(idFieldNames)) {
addRowIdFieldDefinition(recordDefinition);
}
elementsByPath.put(typePath, recordDefinition);
}
try (final ResultSet columnsRs = databaseMetaData.getColumns(null, dbSchemaName, "%", "%")) {
while (columnsRs.next()) {
final String tableName = columnsRs.getString("TABLE_NAME").toUpperCase();
final PathName typePath = schemaPath.newChild(tableName);
final JdbcRecordDefinition recordDefinition = recordDefinitionMap.get(typePath);
if (recordDefinition != null) {
final String dbColumnName = columnsRs.getString("COLUMN_NAME");
final String name = dbColumnName.toUpperCase();
final int sqlType = columnsRs.getInt("DATA_TYPE");
final String dataType = columnsRs.getString("TYPE_NAME");
final int length = columnsRs.getInt("COLUMN_SIZE");
int scale = columnsRs.getInt("DECIMAL_DIGITS");
if (columnsRs.wasNull()) {
scale = -1;
}
final boolean required = !columnsRs.getString("IS_NULLABLE").equals("YES");
final String description = columnsRs.getString("REMARKS");
addField(recordDefinition, dbColumnName, name, dataType, sqlType, length, scale, required, description);
}
}
for (final RecordDefinitionImpl recordDefinition : recordDefinitionMap.values()) {
final String typePath = recordDefinition.getPath();
final List<String> idFieldNames = idFieldNameMap.get(typePath);
if (!Property.isEmpty(idFieldNames)) {
recordDefinition.setIdFieldNames(idFieldNames);
}
}
}
}
} catch (final Throwable e) {
throw new IllegalArgumentException("Unable to load metadata for schema " + dbSchemaName, e);
}
return elementsByPath;
}
use of com.revolsys.io.PathName 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.io.PathName in project com.revolsys.open by revolsys.
the class JdbcRecordDefinition method rename.
@Override
public JdbcRecordDefinition rename(final String path) {
final JdbcRecordStoreSchema schema = getSchema();
final PathName pathName = PathName.newPathName(path);
return new JdbcRecordDefinition(schema, pathName, this.dbTableName);
}
Aggregations