Getting all column vs. table regardless of alias using jsqlparser? #2096
Replies: 4 comments
|
Greetings! Please have a look at https://manticore-projects.com/JSQLTranspiler/resolve.html for getting the correct column information and lineage. Its based on JSQLParser. |
|
Hi Thanks for replay. I want to resolve original table not alias for further processing it is possible with JSQLTranspiler? like for example in this query, SELECT e.name,
(SELECT COUNT(*) FROM projects AS p WHERE p.employee_id = e.employee_id) AS project_count,
(SELECT COUNT(*) FROM tasks AS p WHERE p.employee_id = e.employee_id) AS task_count
FROM employees AS e;I want to get projects.employee_id, tasks.employee_id, employees.employee_id, employees.name |
|
Yes, for your query, JSQLTranspiler will resolve the following information. Please note, that JSQLTranspiler uses the actual schema definition, either from a JDBC Connection or from a provided POJO. // provide minimum schema information,
// alternatively JSQLTranspiler can derive that from the Database itself when you provide a JDBC connection
String[][] schemaDefinition = {
{"projects", "employee_id"}
, {"tasks", "employee_id"}
, {"employees", "name"}
};
// the SQL Query to resolve
String sqlStr =
" SELECT e.name, \n" +
" (SELECT COUNT(*) FROM projects AS p WHERE p.employee_id = e.employee_id) AS project_count,\n" +
" (SELECT COUNT(*) FROM tasks AS p WHERE p.employee_id = e.employee_id) AS task_count\n" +
"FROM employees AS e"
;
// The expected output in ASCII (alternatively JSON and XML is available)
String expected =
"SELECT\n" +
" ├─employees.name : Other\n" +
" ├─project_count AS SELECT\n" +
" │ └─Function COUNT\n" +
" │ └─projects.employee_id : Other\n" +
" │ └─projects.employee_id : Other\n" +
" └─task_count AS SELECT\n" +
" └─Function COUNT\n" +
" └─tasks.employee_id : Other\n" +
" └─tasks.employee_id : Other\n"
;
assertLineage(schemaDefinition, sqlStr, expected); |
|
Hi I have tried the tool I was great but does it give the column used in all the SQL query not only from select for example, public class Main {
public static void main(String[] args) throws Exception {
String sql = "SELECT e.name, \n" + //
" (SELECT name FROM projects AS p WHERE p.employee_id = e.employee_id) AS project_count,\n" + //
" (SELECT name FROM tasks AS p WHERE p.employee_id = e.employee_id) AS task_count\n" + //
"FROM employees AS e;";
String[][] schemaDefinition = {
{ "projects", "employee_id", "name" }, { "tasks", "employee_id", "name" }, { "employees", "employee_id", "name" }
};
JSQLColumResolver resolver = new JSQLColumResolver(schemaDefinition);
System.out.println(resolver.getLineage(XmlTreeBuilder.class, sql));
}
}It gives this, <?xml version="1.0" encoding="UTF-8"?>
<ColumnSet>
<Column name='name' table='employees' scope='employees.name' dataType='java.sql.Types.OTHER' typeName='Other' columnSize='0' decimalDigits='0' nullable=''/>
<Column alias='project_count' name='name'>
<ColumnSet>
<Column name='name' table='projects' scope='projects.name' dataType='java.sql.Types.OTHER' typeName='Other' columnSize='0' decimalDigits='0' nullable=''/>
</ColumnSet>
</Column>
<Column alias='task_count' name='name'>
<ColumnSet>
<Column name='name' table='tasks' scope='tasks.name' dataType='java.sql.Types.OTHER' typeName='Other' columnSize='0' decimalDigits='0' nullable=''/>
</ColumnSet>
</Column>
</ColumnSet>because i want do some kind of column restriction using java so i can't allow user to use it anywhere even in |
Uh oh!
There was an error while loading. Please reload this page.
I have been trying to get all column vs. table regardless of alias used for example assume this query
Considering that alias name can be reused across different scopes I can't simply collect details like column, table and alias and do the work at last How I can do that, below is my try If you have any suggestion or any other algorithm please let me know thanks.
What I am currently doing is resolving tables for an given alias at end of each scope below is the code,
Output
You can Find original post on Code review
All reactions