use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class HiveSchemaStatVisitor method visit.
@Override
public boolean visit(HiveInsertStatement x) {
if (repository != null && x.getParent() == null) {
repository.resolve(x);
}
SQLWithSubqueryClause with = x.getWith();
if (with != null) {
with.accept(this);
}
setMode(x, TableStat.Mode.Insert);
SQLExprTableSource tableSource = x.getTableSource();
SQLExpr tableName = tableSource.getExpr();
if (tableName instanceof SQLName) {
TableStat stat = getTableStat((SQLName) tableName);
stat.incrementInsertCount();
List<SQLExpr> columns = x.getColumns();
for (SQLExpr column : columns) {
if (column instanceof SQLIdentifierExpr) {
addColumn((SQLName) tableName, ((SQLIdentifierExpr) column).normalizedName());
}
}
}
for (SQLAssignItem partition : x.getPartitions()) {
partition.accept(this);
}
accept(x.getQuery());
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class SQLSelectBuilderImpl method from.
@Override
public SQLSelectBuilderImpl from(String table, String alias) {
SQLSelectQueryBlock queryBlock = getQueryBlock();
SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(table), alias);
queryBlock.setFrom(from);
return this;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class PGSelectParser method query.
@Override
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
if (lexer.token() == Token.VALUES) {
return valuesQuery(acceptUnion);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
if (select instanceof SQLSelectQueryBlock) {
((SQLSelectQueryBlock) select).setParenthesized(true);
}
accept(Token.RPAREN);
return queryRest(select, acceptUnion);
}
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
if (lexer.token() == Token.ON) {
lexer.nextToken();
for (; ; ) {
SQLExpr expr = this.createExprParser().expr();
queryBlock.getDistinctOn().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
} else if (lexer.token() == Token.ALL) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseSelectList(queryBlock);
if (lexer.token() == Token.INTO) {
lexer.nextToken();
if (lexer.token() == Token.TEMPORARY) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.TEMPORARY);
} else if (lexer.token() == Token.TEMP) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.TEMP);
} else if (lexer.token() == Token.UNLOGGED) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.UNLOGGED);
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
}
SQLExpr name = this.createExprParser().name();
queryBlock.setInto(new SQLExprTableSource(name));
}
}
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
if (lexer.token() == Token.WINDOW) {
this.parseWindow(queryBlock);
}
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
for (; ; ) {
if (lexer.token() == Token.LIMIT) {
SQLLimit limit = new SQLLimit();
lexer.nextToken();
if (lexer.token() == Token.ALL) {
limit.setRowCount(new SQLIdentifierExpr("ALL"));
lexer.nextToken();
} else {
limit.setRowCount(expr());
}
queryBlock.setLimit(limit);
} else if (lexer.token() == Token.OFFSET) {
SQLLimit limit = queryBlock.getLimit();
if (limit == null) {
limit = new SQLLimit();
queryBlock.setLimit(limit);
}
lexer.nextToken();
SQLExpr offset = expr();
limit.setOffset(offset);
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
}
} else {
break;
}
}
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
if (lexer.token() == Token.FIRST) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
} else if (lexer.token() == Token.NEXT) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'. " + lexer.info());
}
SQLExpr count = expr();
fetch.setCount(count);
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'. " + lexer.info());
}
if (lexer.token() == Token.ONLY) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ONLY'. " + lexer.info());
}
queryBlock.setFetch(fetch);
}
if (lexer.token() == Token.FOR) {
lexer.nextToken();
PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
if (lexer.token() == Token.UPDATE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
lexer.nextToken();
} else if (lexer.token() == Token.SHARE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
lexer.nextToken();
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'. " + lexer.info());
}
if (lexer.token() == Token.OF) {
for (; ; ) {
SQLExpr expr = this.createExprParser().expr();
forClause.getOf().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forClause.setNoWait(true);
} else if (lexer.identifierEquals(FnvHash.Constants.SKIP)) {
lexer.nextToken();
acceptIdentifier("LOCKED");
forClause.setSkipLocked(true);
}
queryBlock.setForClause(forClause);
}
return queryRest(queryBlock, acceptUnion);
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class SQLServerStatementParser method parseSet.
public SQLStatement parseSet() {
accept(Token.SET);
if (lexer.identifierEquals(FnvHash.Constants.TRANSACTION)) {
lexer.nextToken();
acceptIdentifier("ISOLATION");
acceptIdentifier("LEVEL");
SQLServerSetTransactionIsolationLevelStatement stmt = new SQLServerSetTransactionIsolationLevelStatement();
if (lexer.identifierEquals("READ")) {
lexer.nextToken();
if (lexer.identifierEquals("UNCOMMITTED")) {
stmt.setLevel("READ UNCOMMITTED");
lexer.nextToken();
} else if (lexer.identifierEquals("COMMITTED")) {
stmt.setLevel("READ COMMITTED");
lexer.nextToken();
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
}
} else if (lexer.identifierEquals("SERIALIZABLE")) {
stmt.setLevel("SERIALIZABLE");
lexer.nextToken();
} else if (lexer.identifierEquals("SNAPSHOT")) {
stmt.setLevel("SNAPSHOT");
lexer.nextToken();
} else if (lexer.identifierEquals("REPEATABLE")) {
lexer.nextToken();
if (lexer.identifierEquals("READ")) {
stmt.setLevel("REPEATABLE READ");
lexer.nextToken();
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
}
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal() + ", " + lexer.info());
}
return stmt;
}
if (lexer.identifierEquals(FnvHash.Constants.STATISTICS)) {
lexer.nextToken();
SQLSetStatement stmt = new SQLSetStatement();
if (lexer.identifierEquals("IO") || lexer.identifierEquals("XML") || lexer.identifierEquals("PROFILE") || lexer.identifierEquals("TIME")) {
SQLExpr target = new SQLIdentifierExpr("STATISTICS " + lexer.stringVal().toUpperCase());
lexer.nextToken();
if (lexer.token() == Token.ON) {
stmt.set(target, new SQLIdentifierExpr("ON"));
lexer.nextToken();
} else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
stmt.set(target, new SQLIdentifierExpr("OFF"));
lexer.nextToken();
}
}
return stmt;
}
if (lexer.identifierEquals(FnvHash.Constants.IDENTITY_INSERT)) {
SQLSetStatement stmt = new SQLSetStatement();
stmt.setOption(SQLSetStatement.Option.IDENTITY_INSERT);
lexer.nextToken();
SQLName table = this.exprParser.name();
if (lexer.token() == Token.ON) {
SQLExpr value = new SQLIdentifierExpr("ON");
stmt.set(table, value);
lexer.nextToken();
} else if (lexer.identifierEquals(FnvHash.Constants.OFF)) {
SQLExpr value = new SQLIdentifierExpr("OFF");
stmt.set(table, value);
lexer.nextToken();
}
return stmt;
}
if (lexer.token() == Token.VARIANT) {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
parseAssignItems(stmt.getItems(), stmt);
return stmt;
} else {
SQLSetStatement stmt = new SQLSetStatement();
SQLExpr target = this.exprParser.expr();
if (lexer.token() == Token.ON) {
stmt.set(target, new SQLIdentifierExpr("ON"));
lexer.nextToken();
} else if (lexer.identifierEquals("OFF")) {
stmt.set(target, new SQLIdentifierExpr("OFF"));
lexer.nextToken();
} else {
stmt.set(target, this.exprParser.expr());
}
return stmt;
}
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class SchemaRepository method findTable.
public SchemaObject findTable(String tableName) {
if (tableName.indexOf('.') != -1) {
SQLExpr expr = SQLUtils.toSQLExpr(tableName, dbType);
if (!(expr instanceof SQLIdentifierExpr)) {
return findTable((SQLName) expr);
}
}
SchemaObject object = getDefaultSchema().findTable(tableName);
if (object != null) {
return object;
}
String ddl = loadDDL(tableName);
if (ddl == null) {
return null;
}
DbType schemaDbType = this.schemaDbType;
if (schemaDbType == null) {
schemaDbType = dbType;
}
SchemaObject schemaObject = acceptDDL(ddl, schemaDbType);
if (schemaObject != null) {
return schemaObject;
}
return getDefaultSchema().findTable(tableName);
}
Aggregations