use of org.apache.drill.exec.store.jdbc.utils.CreateTableStmtBuilder in project drill by apache.
the class JdbcRecordWriter method updateSchema.
@Override
public void updateSchema(VectorAccessible batch) {
BatchSchema schema = batch.getSchema();
String columnName;
MinorType type;
String sql;
boolean nullable = false;
CreateTableStmtBuilder queryBuilder = new CreateTableStmtBuilder(tableName, dialect);
for (MaterializedField field : schema) {
columnName = JdbcDDLQueryUtils.addBackTicksToField(field.getName());
type = field.getType().getMinorType();
logger.debug("Adding column {} of type {}.", columnName, type);
if (field.getType().getMode() == DataMode.REPEATED) {
throw UserException.dataWriteError().message("Drill does not yet support writing arrays to JDBC. " + columnName + " is an array.").build(logger);
}
if (field.getType().getMode() == DataMode.OPTIONAL) {
nullable = true;
}
int precision = field.getPrecision();
int scale = field.getScale();
queryBuilder.addColumn(columnName, field.getType().getMinorType(), nullable, precision, scale);
}
sql = queryBuilder.build().getCreateTableQuery();
sql = JdbcDDLQueryUtils.cleanDDLQuery(sql, dialect);
logger.debug("Final query: {}", sql);
// Execute the query to build the schema
try (Statement statement = connection.createStatement()) {
logger.debug("Executing CREATE query: {}", sql);
statement.execute(sql);
} catch (SQLException e) {
throw UserException.dataReadError(e).message("The JDBC storage plugin failed while trying to create the schema. ").addContext("Sql", sql).build(logger);
}
}
Aggregations