use of com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo in project be5 by DevelopmentOnTheEdge.
the class H2SchemaReader method readColumns.
@Override
public Map<String, List<SqlColumnInfo>> readColumns(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
DbmsConnector connector = sql.getConnector();
Map<String, List<SqlColumnInfo>> result = new HashMap<>();
ResultSet rs = connector.executeQuery("SELECT " + "c.table_name, " + "c.column_name, " + "c.column_default, " + "c.TYPE_NAME, " + "c.character_maximum_length, " + "c.numeric_precision, " + "c.numeric_scale, " + "c.is_nullable, " + "c.CHECK_CONSTRAINT " + "FROM INFORMATION_SCHEMA.COLUMNS c " + "WHERE c.TABLE_SCHEMA <> 'INFORMATION_SCHEMA'");
try {
while (rs.next()) {
String tableName = rs.getString(1).toLowerCase();
if (!tableName.equals(tableName.toLowerCase()))
continue;
List<SqlColumnInfo> list = result.get(tableName);
if (list == null) {
list = new ArrayList<>();
result.put(tableName, list);
}
SqlColumnInfo info = new SqlColumnInfo();
list.add(info);
info.setName(rs.getString(2));
info.setType(rs.getString(4));
info.setCanBeNull("YES".equals(rs.getString(8)));
String defaultValue = rs.getString(3);
if (defaultValue != null) {
for (String suffix : SUFFICES) {
if (defaultValue.endsWith(suffix))
defaultValue = defaultValue.substring(0, defaultValue.length() - suffix.length());
}
defaultValue = DEFAULT_DATE_PATTERN.matcher(defaultValue).replaceFirst("'$1'");
}
info.setDefaultValue(defaultValue);
info.setSize(rs.getInt(5));
if (rs.wasNull()) {
info.setSize(rs.getInt(6));
}
info.setPrecision(rs.getInt(7));
info.setAutoIncrement(defaultValue != null && defaultValue.startsWith("nextval"));
String check = rs.getString(9);
if (check == null)
continue;
Matcher matcher = ENUM_VALUES_PATTERN.matcher(check);
List<String> vals = new ArrayList<>();
while (matcher.find()) {
vals.add(matcher.group(1));
}
info.setEnumValues(vals.toArray(new String[vals.size()]));
// Just to check for interrupts
controller.setProgress(0);
}
} finally {
connector.close(rs);
}
return result;
}
use of com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo in project be5 by DevelopmentOnTheEdge.
the class AppSync method createColumnType.
private static SqlColumnType createColumnType(final SqlColumnInfo info) {
SqlColumnType type = new SqlColumnType();
String[] enumValues = info.getEnumValues();
if (enumValues != null) {
if (isBool(enumValues)) {
type.setTypeName(SqlColumnType.TYPE_BOOL);
} else {
type.setTypeName(SqlColumnType.TYPE_ENUM);
Arrays.sort(enumValues);
type.setEnumValues(enumValues);
}
} else {
type.setTypeName(info.getType());
type.setSize(info.getSize());
type.setPrecision(info.getPrecision());
}
return type;
}
use of com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo in project be5 by DevelopmentOnTheEdge.
the class AppSync method createEntities.
private void createEntities() throws ExtendedSqlException, SQLException {
Rdbms databaseSystem = DatabaseUtils.getRdbms(connector);
DbmsTypeManager typeManager = databaseSystem == null ? new DefaultTypeManager() : databaseSystem.getTypeManager();
boolean casePreserved = typeManager.normalizeIdentifierCase("aA").equals("aA");
entities = new ArrayList<>();
Project project = new Project("internal-db");
project.setDatabaseSystem(be5Project.getDatabaseSystem());
Module module = new Module("temp", project);
for (String table : tableTypes.keySet()) {
if (!"TABLE".equals(tableTypes.get(table.toLowerCase())))
continue;
List<SqlColumnInfo> columnInfos = columns.get(table.toLowerCase());
if (columnInfos == null)
continue;
Entity entity = new Entity(table, module, EntityType.TABLE);
entities.add(entity);
TableDef tableDef = new TableDef(entity);
for (SqlColumnInfo info : columnInfos) {
ColumnDef column = new ColumnDef(info.getName(), tableDef.getColumns());
column.setType(createColumnType(info));
typeManager.correctType(column.getType());
// PENDING
column.setPrimaryKey(info.getName().equalsIgnoreCase(entity.getPrimaryKey()));
column.setCanBeNull(info.isCanBeNull());
String defaultValue = info.getDefaultValue();
column.setAutoIncrement(info.isAutoIncrement());
if (!info.isAutoIncrement()) {
column.setDefaultValue(defaultValue);
}
if (column.isPrimaryKey() && typeManager.getKeyType().equals(typeManager.getTypeClause(column.getType()))) {
column.getType().setTypeName(SqlColumnType.TYPE_KEY);
}
// column.setOriginModuleName( module.getName() );
DataElementUtils.saveQuiet(column);
}
List<IndexInfo> indexInfos = indices.get(table.toLowerCase(Locale.ENGLISH));
if (indexInfos != null) {
INDEX: for (IndexInfo info : indexInfos) {
if (!casePreserved)
info.setName(info.getName().toUpperCase(Locale.ENGLISH));
IndexDef index = new IndexDef(info.getName(), tableDef.getIndices());
index.setUnique(info.isUnique());
for (String indexCol : info.getColumns()) {
IndexColumnDef indexColumnDef = IndexColumnDef.createFromString(indexCol, index);
if (tableDef.getColumns().get(indexColumnDef.getName()) == null) {
if (debug) {
warnings.add("Unsupported functional index found: " + index.getName() + " (problem is here: " + indexCol + "); skipped");
}
continue INDEX;
}
DataElementUtils.saveQuiet(indexColumnDef);
}
if (index.isUnique() && index.getSize() == 1) {
IndexColumnDef indexColumnDef = index.iterator().next();
if (!indexColumnDef.isFunctional()) {
if (index.getName().equalsIgnoreCase(table + "_pkey")) {
entity.setPrimaryKey(indexColumnDef.getName());
continue;
}
}
}
DataElementUtils.saveQuiet(index);
}
}
DataElementUtils.saveQuiet(tableDef);
}
if (sqlExecutor.getConnector().getType() != DbmsType.MYSQL)
return;
// For MySQL only now
for (Entity entity : entities) {
final String table = entity.getName();
if (!"VIEW".equalsIgnoreCase(tableTypes.get(table)))
continue;
String createTable;
ResultSet rs = sqlExecutor.executeNamedQuery("sql.getTableDefinition", table);
try {
if (!rs.next())
continue;
createTable = rs.getString(2);
} finally {
sqlExecutor.getConnector().close(rs);
}
int as = createTable.indexOf(" AS ");
if (as < 0)
continue;
createTable = createTable.substring(as + " AS ".length());
ViewDef def = new ViewDef(entity);
def.setDefinition(createTable);
DataElementUtils.saveQuiet(def);
}
}
Aggregations