use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.
the class TableView method createTempView.
/**
* Create a temporary view out of the given query.
*
* @param session the session
* @param owner the owner of the query
* @param name the view name
* @param query the query
* @param topQuery the top level query
* @return the view table
*/
public static TableView createTempView(Session session, User owner, String name, Query query, Query topQuery) {
Schema mainSchema = session.getDatabase().getSchema(Constants.SCHEMA_MAIN);
String querySQL = query.getPlanSQL();
TableView v = new TableView(mainSchema, 0, name, querySQL, query.getParameters(), null, session, false);
if (v.createException != null) {
throw v.createException;
}
v.setTopQuery(topQuery);
v.setOwner(owner);
v.setTemporary(true);
return v;
}
use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.
the class ExpressionColumn method optimize.
@Override
public Expression optimize(Session session) {
if (columnResolver == null) {
Schema schema = session.getDatabase().findSchema(tableAlias == null ? session.getCurrentSchemaName() : tableAlias);
if (schema != null) {
Constant constant = schema.findConstant(columnName);
if (constant != null) {
return constant.getValue();
}
}
String name = columnName;
if (tableAlias != null) {
name = tableAlias + "." + name;
if (schemaName != null) {
name = schemaName + "." + name;
}
}
throw DbException.get(ErrorCode.COLUMN_NOT_FOUND_1, name);
}
return columnResolver.optimize(this, column);
}
use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.
the class Parser method getDualTable.
private Table getDualTable(boolean noColumns) {
Schema main = database.findSchema(Constants.SCHEMA_MAIN);
Expression one = ValueExpression.get(ValueLong.get(1));
return new RangeTable(main, one, one, noColumns);
}
use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.
the class Parser method parseWith.
private Query parseWith() {
readIf("RECURSIVE");
String tempViewName = readIdentifierWithSchema();
Schema schema = getSchema();
Table recursiveTable;
read("(");
ArrayList<Column> columns = New.arrayList();
String[] cols = parseColumnList();
for (String c : cols) {
columns.add(new Column(c, Value.STRING));
}
Table old = session.findLocalTempTable(tempViewName);
if (old != null) {
if (!(old instanceof TableView)) {
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, tempViewName);
}
TableView tv = (TableView) old;
if (!tv.isTableExpression()) {
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, tempViewName);
}
session.removeLocalTempTable(old);
}
CreateTableData data = new CreateTableData();
data.id = database.allocateObjectId();
data.columns = columns;
data.tableName = tempViewName;
data.temporary = true;
data.create = true;
data.session = session;
recursiveTable = schema.createTable(data);
session.addLocalTempTable(recursiveTable);
String querySQL;
try {
read("AS");
read("(");
Query withQuery = parseSelect();
read(")");
withQuery.prepare();
querySQL = StringUtils.fromCacheOrNew(withQuery.getPlanSQL());
} finally {
session.removeLocalTempTable(recursiveTable);
}
int id = database.allocateObjectId();
TableView view = new TableView(schema, id, tempViewName, querySQL, null, cols, session, true);
view.setTableExpression(true);
view.setTemporary(true);
session.addLocalTempTable(view);
Query q = parseSelect();
q.setPrepareAlways(true);
return q;
}
use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.
the class Parser method parseCreateTable.
private CreateTable parseCreateTable(boolean temp, boolean globalTemp) {
boolean ifNotExists = readIfNoExists();
String tableName = readIdentifierWithSchema();
if (temp && globalTemp && equalsToken("SESSION", schemaName)) {
// support weird syntax: declare global temporary table session.xy
// (...) not logged
schemaName = session.getCurrentSchemaName();
globalTemp = false;
}
Schema schema = getSchema();
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(")")) {
do {
DefineCommand c = parseAlterTableAddConstraintIf(tableName, schema);
if (c != null) {
command.addConstraintCommand(c);
} else {
String columnName = readColumnIdentifier();
Column column = parseColumnForTable(columnName, true);
if (column.isAutoIncrement() && column.isPrimaryKey()) {
column.setPrimaryKey(false);
IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = column.getName();
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
pk.setTableName(tableName);
pk.setIndexColumns(cols);
command.addConstraintCommand(pk);
}
command.addColumn(column);
String constraintName = null;
if (readIf("CONSTRAINT")) {
constraintName = readColumnIdentifier();
}
if (readIf("PRIMARY")) {
read("KEY");
boolean hash = readIf("HASH");
IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = column.getName();
AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
pk.setPrimaryKeyHash(hash);
pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
pk.setTableName(tableName);
pk.setIndexColumns(cols);
command.addConstraintCommand(pk);
if (readIf("AUTO_INCREMENT")) {
parseAutoIncrement(column);
}
} else if (readIf("UNIQUE")) {
AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema, false);
unique.setConstraintName(constraintName);
unique.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE);
IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = columnName;
unique.setIndexColumns(cols);
unique.setTableName(tableName);
command.addConstraintCommand(unique);
}
if (readIf("NOT")) {
read("NULL");
column.setNullable(false);
} else {
readIf("NULL");
}
if (readIf("CHECK")) {
Expression expr = readExpression();
column.addCheckConstraint(session, expr);
}
if (readIf("REFERENCES")) {
AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema, false);
ref.setConstraintName(constraintName);
ref.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL);
IndexColumn[] cols = { new IndexColumn() };
cols[0].columnName = columnName;
ref.setIndexColumns(cols);
ref.setTableName(tableName);
parseReferences(ref, schema, tableName);
command.addConstraintCommand(ref);
}
}
} while (readIfMore());
}
}
// Allows "COMMENT='comment'" in DDL statements (MySQL syntax)
if (readIf("COMMENT")) {
if (readIf("=")) {
// read the complete string comment, but nothing with it for now
readString();
}
}
if (readIf("ENGINE")) {
if (readIf("=")) {
// map MySQL engine types onto H2 behavior
String tableEngine = readUniqueIdentifier();
if ("InnoDb".equalsIgnoreCase(tableEngine)) {
command.setTableEngine("InnoDb");
} else if ("MyISAM".equalsIgnoreCase(tableEngine)) {
command.setTableEngine("MyISAM");
} else {
throw DbException.getUnsupportedException(tableEngine);
}
} else {
command.setTableEngine(readUniqueIdentifier());
if (readIf("WITH")) {
ArrayList<String> tableEngineParams = New.arrayList();
do {
tableEngineParams.add(readUniqueIdentifier());
} while (readIf(","));
command.setTableEngineParams(tableEngineParams);
}
}
} else if (database.getSettings().defaultTableEngine != null) {
command.setTableEngine(database.getSettings().defaultTableEngine);
}
// MySQL compatibility
if (readIf("AUTO_INCREMENT")) {
read("=");
if (currentTokenType != VALUE || currentValue.getType() != Value.INT) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "integer");
}
read();
}
readIf("DEFAULT");
if (readIf("CHARSET")) {
read("=");
// read("UTF8");
String charset = StringUtils.toLowerEnglish(readUniqueIdentifier());
command.setCharset(charset);
}
if (temp) {
if (readIf("ON")) {
read("COMMIT");
if (readIf("DROP")) {
command.setOnCommitDrop();
} else if (readIf("DELETE")) {
read("ROWS");
command.setOnCommitTruncate();
}
} else if (readIf("NOT")) {
if (readIf("PERSISTENT")) {
throw getSyntaxError();
} else {
read("LOGGED");
}
}
if (readIf("TRANSACTIONAL")) {
command.setTransactional(true);
}
} else if (readIf("NOT")) {
read("PERSISTENT");
throw getSyntaxError();
}
if (readIf("HIDDEN")) {
command.setHidden(true);
}
if (readIf("AS")) {
if (readIf("SORTED")) {
command.setSortedInsertMode(true);
}
command.setQuery(parseSelect());
}
// for MySQL compatibility
if (readIf("ROW_FORMAT")) {
if (readIf("=")) {
readColumnIdentifier();
}
}
return command;
}
Aggregations