我正在尝试从这里实现 sqlparser 并使用 gsqlparser 。jar 的来源是 Java,但我在 Scala 中实现了相同的。
下面是我的查询,其中包含一个连接条件。
SELECT e.last_name AS name, e.commission_pct comm, e.salary * 12 "Annual Salary" FROM scott.employees AS e right join scott.companies as c on c.orgid = e.orgid and c.orgname = e.orgn WHERE e.salary > 1000 ORDER BY e.first_name, e.last_name
我能够解析查询以读取列的名称和别名、条件、表名(直接在查询中检查表名),如下所示。
val sqlParser = new TGSqlParser(EDbVendor.dbvsnowflake)
sqlParser.sqltext = "SELECT e.last_name AS name, e.commission_pct comm, e.salary * 12 \"Annual Salary\" FROM scott.employees AS e right join scott.companies as c on c.orgid = e.orgid and c.orgname = e.orgn WHERE e.salary > 1000 ORDER BY e.first_name, e.last_name"
val selectStmnt = sqlParser.sqltext
println("Columns List:")
for(i <- 0 until selectStmnt.getResultColumnList.size()) {
val resCol = selectStmnt.getResultColumnList.getResultColumn(i)
println("Column: " + resCol.getExpr.toString + " Alias: " + resCol
.getAliasClause().toString)
}
输出:
Columns List:
Column: e.last_name Alias: name
Column: e.commission_pct Alias: comm
Column: e.salary * 12 Alias: "Annual Salary"
我正在尝试解析连接条件并获取其中的详细信息
for(j <- 0 until selectStmnt.getJoins.size()) {
println(selectStmnt.getJoins.getJoin(j).getTable)
}
这里的问题是查询中只有一个连接条件,因此返回的大小为 1。因此输出为scott.employees
. 如果我这样做有点不同,如下所示getJoinItems
println("Parsing Join items")
for(j <- 0 until selectStmnt.getJoins.size()) {
println(selectStmnt.getJoins.getJoin(j).getJoinItems)
}
我通过从连接条件中截断第一个表来获得输出,如下所示:
scott.companies as c on c.orgid = e.orgid and c.orgname = e.orgn
方法:getJoinItems() 返回一个列表:TJoinItemList
我想遍历它。但即使它的大小也是 1。
println(selectStmnt.getJoins.getJoin(j).getJoinItems.size()) -> 1
我现在没有主意了。谁能让我知道如何解析查询的联接条件并获取联接内的表名?