@Override public String getSQL() { return "@" + Parser.quoteIdentifier(name); }
/** * Parse and prepare the given SQL statement. * * @param sql the SQL statement * @param rightsChecked true if the rights have already been checked * @return the prepared statement */ public Prepared prepare(String sql, boolean rightsChecked) { Parser parser = new Parser(this); parser.setRightsChecked(rightsChecked); return parser.prepare(sql); }
private void read(String expected) { if (currentTokenQuoted || !equalsToken(expected, currentToken)) { addExpected(expected); throw getSyntaxError(); } read(); }
private boolean isToken(String token) { boolean result = equalsToken(token, currentToken) && !currentTokenQuoted; if (result) { return true; } addExpected(token); return false; }
private Prepared parseCreate() { boolean orReplace = false; if (readIf("OR")) { read("REPLACE"); orReplace = true; boolean force = readIf("FORCE"); if (readIf("VIEW")) { return parseCreateView(force, orReplace); } else if (readIf("SEQUENCE")) { return parseCreateSequence(); } else if (readIf("USER")) { return parseCreateUser(); if (readIf("ROLE")) { return parseCreateRole(); } else if (readIf("SCHEMA")) { return parseCreateSchema(); } else if (readIf("CONSTANT")) { return parseCreateConstant(); } else if (readIf("DOMAIN")) { return parseCreateUserDataType(); } else if (readIf("TYPE")) { return parseCreateUserDataType(); } else if (readIf("DATATYPE")) { return parseCreateUserDataType(); } else if (readIf("AGGREGATE")) { return parseCreateAggregate(force); if (readIf("MEMORY")) {
private CreateTable parseCreateTable(boolean temp, boolean globalTemp) { boolean ifNotExists = readIfNotExists(); String tableName = readIdentifierWithSchema(); if (temp && globalTemp && equalsToken("SESSION", schemaName)) { Schema schema = getSchema(); CreateTable command = new CreateTable(session, schema); command.setTemporary(temp); 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); if (readIf("CONSTRAINT")) { constraintName = readColumnIdentifier(); if (readIf("PRIMARY")) { read("KEY");
private TableFilter readTableFilter(boolean fromOuter) { Table table; String alias = null; if (readIf("(")) { if (isSelect()) { Query query = parseSelectUnion(); read(")"); query.setParameterList(New.arrayList(parameters)); query.init(); TableFilter top; if (database.getSettings().nestedJoins) { top = readTableFilter(false); top = readJoin(top, currentSelect, false, false); top = getNested(top); } else { top = readTableFilter(fromOuter); top = readJoin(top, currentSelect, false, fromOuter); read(")"); alias = readFromAlias(null); if (alias != null) { top.setAlias(alias); } else if (readIf("VALUES")) { table = parseValuesTable().getTable(); } else { String tableName = readIdentifierWithSchema(null); Schema schema = getSchema(); boolean foundLeftBracket = readIf("(");
switch (currentTokenType) { case AT: read(); r = new Variable(session, readAliasIdentifier()); if (readIf(":=")) { Expression value = readExpression(); Function function = Function.getFunction(database, "SET"); function.setParameter(0, r); read(); Parameter p; if (indexed && currentTokenType == VALUE && throw getSyntaxError(); } else if (parameters.size() > 0) { throw DbException read(); } else { if (indexedParameterList != null) { break; case KEYWORD: if (isToken("SELECT") || isToken("FROM")) { Query query = parseSelect(); r = new Subquery(query); } else { throw getSyntaxError();
private Prepared parseAlterTable() { Table table = readTableOrView(); if (readIf("ADD")) { Prepared command = parseAlterTableAddConstraintIf(table.getName(), table.getSchema()); if (command != null) { return command; return parseAlterTableAddColumn(table); } else if (readIf("SET")) { read("REFERENTIAL_INTEGRITY"); int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY; boolean value = readBooleanSetting(); AlterTableSet command = new AlterTableSet(session, table.getSchema(), type, value); command.setTableName(table.getName()); if (readIf("CHECK")) { command.setCheckExisting(true); } else if (readIf("NOCHECK")) { command.setCheckExisting(false); } else if (readIf("RENAME")) { read("TO"); String newName = readIdentifierWithSchema(table.getSchema() .getName()); checkSchema(table.getSchema()); AlterTableRename command = new AlterTableRename(session, getSchema()); command.setOldTable(table); command.setNewTableName(newName);
boolean ifNotExists = false; boolean allowIndexDefinition = database.getMode().indexDefinitionInCreateTable; if (readIf("CONSTRAINT")) { ifNotExists = readIfNotExists(); constraintName = readIdentifierWithSchema(schema.getName()); checkSchema(schema); comment = readCommentIf(); allowIndexDefinition = true; if (readIf("PRIMARY")) { read("KEY"); AlterTableAddConstraint command = new AlterTableAddConstraint( session, schema, ifNotExists); command.setConstraintName(constraintName); command.setTableName(tableName); if (readIf("HASH")) { command.setPrimaryKeyHash(true); read("("); command.setIndexColumns(parseIndexColumnList()); if (readIf("INDEX")) { String indexName = readIdentifierWithSchema(); command.setIndex(getSchema().findIndex(session, indexName)); } else if (allowIndexDefinition && (isToken("INDEX") || isToken("KEY"))) { read(); if (DataType.getTypeByName(currentToken) != null) {
private Prepared parseSet() { if (readIf("@")) { Set command = new Set(session, SetTypes.VARIABLE); command.setString(readAliasIdentifier()); readIfEqualOrTo(); command.setExpression(readExpression()); return command; } else if (readIf("AUTOCOMMIT")) { readIfEqualOrTo(); boolean value = readBooleanSetting(); int setting = value ? CommandInterface.SET_AUTOCOMMIT_TRUE : CommandInterface.SET_AUTOCOMMIT_FALSE; return new TransactionCommand(session, setting); } else if (readIf("IGNORECASE")) { readIfEqualOrTo(); boolean value = readBooleanSetting(); Set command = new Set(session, SetTypes.IGNORECASE); command.setInt(value ? 1 : 0); return command; } else if (readIf("PASSWORD")) { readIfEqualOrTo(); AlterUser command = new AlterUser(session); command.setType(CommandInterface.ALTER_USER_SET_PASSWORD); command.setUser(session.getUser()); command.setPassword(readExpression()); return command; } else if (readIf("SALT")) { readIfEqualOrTo(); AlterUser command = new AlterUser(session); command.setType(CommandInterface.ALTER_USER_SET_PASSWORD);
private Expression readCondition() { if (readIf("NOT")) { return new ConditionNot(readCondition()); if (readIf("EXISTS")) { read("("); Query query = parseSelect(); read(")"); return new ConditionExists(query); Expression r = readConcat(); while (true) { if (readIf("NOT")) { not = true; if (isToken("NULL")) { if (readIf("LIKE")) { Expression b = readConcat(); Expression esc = null; if (readIf("ESCAPE")) { esc = readConcat(); } else if (readIf("REGEXP")) { Expression b = readConcat(); r = new CompareLike(database, r, b, null, true); } else if (readIf("IS")) { if (readIf("NOT")) { if (readIf("NULL")) {
private Expression readFunction(Schema schema, String name) { int agg = getAggregateType(name); if (agg >= 0) { return readAggregate(agg); function.setParameter(0, readExpression()); read("AS"); Column type = parseColumnWithType(null); function.setDataType(type); read(")"); break; Column type = parseColumnWithType(null); function.setDataType(type); read(","); function.setParameter(0, readExpression()); read(")"); } else { function.setParameter(0, readExpression()); read(","); Column type = parseColumnWithType(null); function.setDataType(type); read(")"); function.setParameter(0, ValueExpression.get(ValueString.get(currentToken))); read(); read("FROM"); function.setParameter(1, readExpression()); read(")");
boolean nestedJoins = database.getSettings().nestedJoins; while (true) { if (readIf("RIGHT")) { readIf("OUTER"); read("JOIN"); joined = true; TableFilter newTop = readTableFilter(fromOuter); newTop = readJoin(newTop, command, nested, true); Expression on = null; if (readIf("ON")) { on = readExpression(); top = getNested(top); newTop.addJoin(top, true, false, on); } else { } else if (readIf("LEFT")) { readIf("OUTER"); read("JOIN"); joined = true; TableFilter join = readTableFilter(true); if (nestedJoins) { join = readJoin(join, command, true, true); } else { top = readJoin(top, command, false, true); if (readIf("ON")) { on = readExpression();
private CreateAggregate parseCreateAggregate(boolean force) { boolean ifNotExists = readIfNotExists(); CreateAggregate command = new CreateAggregate(session); command.setForce(force); String name = readIdentifierWithSchema(); if (isKeyword(name) || Function.getFunction(database, name) != null || getAggregateType(name) >= 0) { throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, name); } command.setName(name); command.setSchema(getSchema()); command.setIfNotExists(ifNotExists); read("FOR"); command.setJavaClassMethod(readUniqueIdentifier()); return command; }
private Column parseColumnWithType(String columnName) { String original = currentToken; boolean regular = false; if (readIf("LONG")) { if (readIf("RAW")) { original += " RAW"; } else if (readIf("DOUBLE")) { if (readIf("PRECISION")) { original += " PRECISION"; } else if (readIf("CHARACTER")) { if (readIf("VARYING")) { original += " VARYING"; !equalsToken("VARCHAR_CASESENSITIVE", original)) { original = "VARCHAR_IGNORECASE"; dataType = DataType.getTypeByName(original); read(); scale = scale == -1 ? dataType.defaultScale : scale; if (dataType.supportsPrecision || dataType.supportsScale) { if (readIf("(")) { if (!readIf("MAX")) { long p = readLong(); if (readIf("K")) { p *= 1024; } else if (readIf("M")) { p *= 1024 * 1024;
currentTokenType = getTokenType(currentToken); parseIndex = i; return; currentTokenType = getSpecialType(currentToken); parseIndex = i; return; case CHAR_SPECIAL_1: currentToken = sqlCommand.substring(start, i); currentTokenType = getSpecialType(currentToken); parseIndex = i; return; c = chars[i]; if ((c < '0' || c > '9') && (c < 'A' || c > 'F')) { checkLiterals(false); currentValue = ValueInt.get((int) number); currentTokenType = VALUE; (c >= 'A' ? ('A' - 0xa) : ('0')); if (number > Integer.MAX_VALUE) { readHexDecimal(start, i); return; if (c < '0' || c > '9') { if (c == '.' || c == 'E' || c == 'L') { readDecimal(start, i); break;
private AlterSchemaRename parseAlterSchema() { String schemaName = readIdentifierWithSchema(); Schema old = getSchema(); AlterSchemaRename command = new AlterSchemaRename(session); command.setOldSchema(getSchema(schemaName)); read("RENAME"); read("TO"); String newName = readIdentifierWithSchema(old.getName()); checkSchema(old); command.setNewName(newName); return command; }
private TableFilter readSimpleTableFilter() { Table table = readTableOrView(); String alias = null; if (readIf("AS")) { alias = readAliasIdentifier(); } else if (currentTokenType == IDENTIFIER) { if (!equalsToken("SET", currentToken)) { // SET is not a keyword (PostgreSQL supports it as a table name) alias = readAliasIdentifier(); } } return new TableFilter(session, table, alias, rightsChecked, currentSelect); }
private boolean readIf(String token) { if (!currentTokenQuoted && equalsToken(token, currentToken)) { read(); return true; } addExpected(token); return false; }