private void generateColumnsFromQuery() { int columnCount = prepared.getQuery().getColumnCount(); ArrayList<Expression> expressions = prepared.getQuery().getExpressions(); for (int i = 0; i < columnCount; i++) { Expression expr = expressions.get(i); int type = expr.getType(); String name = expr.getAlias(); long precision = expr.getPrecision(); int displaySize = expr.getDisplaySize(); DataType dt = DataType.getDataType(type); if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) { // dont' set precision to MAX_VALUE if this is the default precision = dt.defaultPrecision; } int scale = expr.getScale(); if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) { scale = dt.defaultScale; } if (scale > precision) { precision = scale; } Column col = new Column(name, type, precision, scale, displaySize); prepared.addColumn(col); } }
@Override public UpdateWorker createUpdateWorker(CreateTable createTable, ObjectNode node, ObjectNode refNode) { SQLTranslated translated = repo.getSQLTranslator().translate(createTable, node, refNode); JdbcUpdateWorker handler = new JdbcUpdateWorker(createTable.getSession(), node.getShardName(), translated.sql, translated.params); return handler; }
CreateTable command = new CreateTable(session, schema); command.setTemporary(temp); command.setGlobalTemporary(globalTemp); command.setIfNotExists(ifNotExists); command.setTableName(tableName); command.setComment(readCommentIf()); if (readIf("(")) { if (!readIf(")")) { schema); if (c != null) { command.addConstraintCommand(c); } else { String columnName = readColumnIdentifier(); pk.setTableName(tableName); pk.setIndexColumns(cols); command.addConstraintCommand(pk); command.addColumn(column); String constraintName = null; if (readIf("CONSTRAINT")) { pk.setTableName(tableName); pk.setIndexColumns(cols); command.addConstraintCommand(pk); if (readIf("AUTO_INCREMENT")) { parseAutoIncrement(column);
public SQLTranslated translate(CreateTable prepared, ObjectNode node, ObjectNode refNode) { StatementBuilder buff = new StatementBuilder("CREATE "); if (prepared.isTemporary()) { buff.append("TEMPORARY "); if (prepared.isIfNotExists()) { buff.append("IF NOT EXISTS "); if (prepared.getComment() != null) { for (Column column : prepared.getColumns()) { buff.appendExceptFirst(", "); buff.append(column.getCreateSQL()); for (DefineCommand command : prepared.getConstraintCommands()) { buff.appendExceptFirst(", "); int type = command.getType(); if (prepared.getTableEngine() != null) { buff.append(" ENGINE = "); buff.append(prepared.getTableEngine()); ArrayList<String> tableEngineParams = prepared.getTableEngineParams(); if (tableEngineParams != null && tableEngineParams.isEmpty()) { buff.append("WITH "); if (prepared.getCharset() != null) { buff.append(" DEFAULT CHARACTER SET = "); buff.append(prepared.getCharset());
@Override public void doPrepare() { String tableName = prepared.getTableName(); TableMate tableMate = getTableMate(tableName); TableMate refTable = null; for (DefineCommand command : prepared.getConstraintCommands()) { if (command.getType() == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL) { AlterTableAddConstraint stmt = (AlterTableAddConstraint) command; Query query = prepared.getQuery(); if (query != null) { query.prepare(); if (prepared.getColumnCount() == 0) { generateColumnsFromQuery(); } else if (prepared.getColumnCount() != query.getColumnCount()) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); asQueryInsert.setSortedInsertMode(prepared.isSortedInsertMode()); asQueryInsert.setQuery(query); asQueryInsert.setTable(tableMate);