use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class NameResolveVisitor method isAliasColumn.
/**
* 是否是 select item 字段的别名
*
* @param x x 是否是 select item 字段的别名
* @param source 从 source 数据中查找 and 判断
* @return true:是、false:不是
*/
public boolean isAliasColumn(SQLExpr x, SQLSelectQueryBlock source) {
if (x instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) x;
long nameHashCode64 = identifierExpr.nameHashCode64();
SQLSelectQueryBlock queryBlock = source;
SQLSelectItem selectItem = queryBlock.findSelectItem(nameHashCode64);
if (selectItem != null) {
return true;
}
if (queryBlock.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock subQueryBlock = ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQueryBlock();
if (isAliasColumn(x, subQueryBlock)) {
return true;
}
}
}
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class NameResolveVisitor method isRowNumColumn.
/**
* 是否是 rownum 或者 rownum 别名
*
* @param x x 是否是 rownum 或者 rownum 别名
* @param source 从 source 数据中查找 and 判断
* @return true:是、false:不是
*/
public boolean isRowNumColumn(SQLExpr x, SQLSelectQueryBlock source) {
if (x instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) x;
long nameHashCode64 = identifierExpr.nameHashCode64();
if (nameHashCode64 == FnvHash.Constants.ROWNUM) {
return true;
}
SQLSelectQueryBlock queryBlock = source;
if (queryBlock.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock subQueryBlock = ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQueryBlock();
SQLSelectItem selectItem = subQueryBlock.findSelectItem(nameHashCode64);
if (selectItem != null && isRowNumColumn(selectItem.getExpr(), subQueryBlock)) {
return true;
}
}
}
return false;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class HiveSelectTest_48 method test_select.
public void test_select() throws Exception {
String sql = "SELECT account_id FROM taobao_office.cloud_yunpan WHERE dt='2021-04-12' AND target='update'";
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.HIVE);
SQLStatement sqlStatement = stmtList.get(0);
SQLSelectStatement stmt = (SQLSelectStatement) sqlStatement;
List<SQLSelectItem> list = ((SQLSelectQueryBlock) stmt.getSelect().getQuery()).getSelectList();
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project canal by alibaba.
the class SqlParser method parse4SQLSelectItem.
public static String parse4SQLSelectItem(MySqlSelectQueryBlock sqlSelectQueryBlock) {
List<SQLSelectItem> selectItems = sqlSelectQueryBlock.getSelectList();
StringBuilder subSql = new StringBuilder();
int i = 0;
for (SQLSelectItem sqlSelectItem : selectItems) {
if (i != 0) {
subSql.append(",");
} else {
i++;
}
subSql.append(SQLUtils.toMySqlString(sqlSelectItem));
}
return subSql.toString();
}
use of com.alibaba.druid.sql.ast.statement.SQLSelectItem in project druid by alibaba.
the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.
public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
MockResultSetMetaData metaData = rs.getMockMetaData();
Object[] row = new Object[query.getSelectList().size()];
for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
ColumnMetaData column = new ColumnMetaData();
SQLSelectItem item = query.getSelectList().get(i);
SQLExpr expr = item.getExpr();
if (expr instanceof SQLIntegerExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.INTEGER);
} else if (expr instanceof SQLNumberExpr) {
row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
column.setColumnType(Types.DECIMAL);
} else if (expr instanceof SQLCharExpr) {
row[i] = ((SQLCharExpr) expr).getText();
column.setColumnType(Types.VARCHAR);
} else if (expr instanceof SQLNCharExpr) {
row[i] = ((SQLNCharExpr) expr).getText();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLBooleanExpr) {
row[i] = ((SQLBooleanExpr) expr).getBooleanValue();
column.setColumnType(Types.NVARCHAR);
} else if (expr instanceof SQLNullExpr) {
row[i] = null;
} else if (expr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
row[i] = new Timestamp(System.currentTimeMillis());
} else {
throw new SQLException("TODO");
}
} else if (expr instanceof SQLVariantRefExpr) {
SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
int varIndex = varExpr.getIndex();
if (statement instanceof MockPreparedStatement) {
MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
row[i] = mockPstmt.getParameters().get(varIndex);
} else {
row[i] = null;
}
} else {
row[i] = null;
}
metaData.getColumns().add(column);
}
rs.getRows().add(row);
return rs;
}
Aggregations