Skip to content

Commit

Permalink
doc: explain the TablesNamesFinder
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Aug 20, 2023
1 parent 3cae390 commit 5c2648c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/site/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ How to use it

4) Oracle Alternative Quoting is partially supported for common brackets such as ``q'{...}'``, ``q'[...]'``, ``q'(...)'`` and ``q''...''``.

5) Supported Statement Separators are Semicolon ``\;``, ``GO``, Slash ``\/`` or 2 empty lines.
5) Supported Statement Separators are Semicolon ``;``, ``GO``, Slash ``/`` or two empty lines ``\n\n\n``.


Compile from Source Code
Expand Down Expand Up @@ -186,6 +186,23 @@ Traverse the Java Object Tree using the Visitor Patterns:
// Invoke the Statement Visitor
stmt.accept(statementVisitor);
Find Table Names
==============================

The class ``net.sf.jsqlparser.util.TablesNamesFinder`` can be used to return all Table Names from a Query or an Expression.

.. code-block:: java
// find in Statements
String sqlStr = "select * from A left join B on A.id=B.id and A.age = (select age from C)";
Set<String> tableNames = TablesNamesFinder.findTables(sqlStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
// find in Expressions
String exprStr = "A.id=B.id and A.age = (select age from C)";
tableNames = TablesNamesFinder.findTables(sqlStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
Build a SQL Statement
==============================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ public void testConnectedByRootOperator() throws JSQLParserException {
@Test
void testJoinSubSelect() throws JSQLParserException {
String sqlStr = "select * from A left join B on A.id=B.id and A.age = (select age from C)";
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("A", "B", "C");
Set<String> tableNames = TablesNamesFinder.findTables(sqlStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");

String exprStr = "A.id=B.id and A.age = (select age from C)";
tableNames = TablesNamesFinder.findTables(sqlStr);
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
}
}

0 comments on commit 5c2648c

Please sign in to comment.