Skip to content

Commit 5c2648c

Browse files
doc: explain the TablesNamesFinder
Signed-off-by: Andreas Reichel <[email protected]>
1 parent 3cae390 commit 5c2648c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/site/sphinx/usage.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ How to use it
1212

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

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

1717

1818
Compile from Source Code
@@ -186,6 +186,23 @@ Traverse the Java Object Tree using the Visitor Patterns:
186186
// Invoke the Statement Visitor
187187
stmt.accept(statementVisitor);
188188
189+
Find Table Names
190+
==============================
191+
192+
The class ``net.sf.jsqlparser.util.TablesNamesFinder`` can be used to return all Table Names from a Query or an Expression.
193+
194+
.. code-block:: java
195+
196+
// find in Statements
197+
String sqlStr = "select * from A left join B on A.id=B.id and A.age = (select age from C)";
198+
Set<String> tableNames = TablesNamesFinder.findTables(sqlStr);
199+
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
200+
201+
// find in Expressions
202+
String exprStr = "A.id=B.id and A.age = (select age from C)";
203+
tableNames = TablesNamesFinder.findTables(sqlStr);
204+
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
205+
189206
190207
Build a SQL Statement
191208
==============================

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ public void testConnectedByRootOperator() throws JSQLParserException {
489489
@Test
490490
void testJoinSubSelect() throws JSQLParserException {
491491
String sqlStr = "select * from A left join B on A.id=B.id and A.age = (select age from C)";
492-
assertThat(TablesNamesFinder.findTables(sqlStr)).containsExactlyInAnyOrder("A", "B", "C");
492+
Set<String> tableNames = TablesNamesFinder.findTables(sqlStr);
493+
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
494+
495+
String exprStr = "A.id=B.id and A.age = (select age from C)";
496+
tableNames = TablesNamesFinder.findTables(sqlStr);
497+
assertThat( tableNames ).containsExactlyInAnyOrder("A", "B", "C");
493498
}
494499
}

0 commit comments

Comments
 (0)