Ich habe einen funktionierenden Prototyp, der einfache SELECT-, UPDATE- und DELETE-Anweisungen verarbeitet. Mein aktueller Ansatz besteht jedoch darin, bestimmte Ausdruckstypen wie InExpression, ExistsExpression, SubSelect und andere manuell zu überprüfen.
Aktueller Ansatz (manuell)
Ich entpacke derzeit Ausdrücke manuell. Dies funktioniert für Abfragen der obersten Ebene, wird aber schnell chaotisch:
Code: Select all
public void modifyPlainSelect(PlainSelect plainSelect, String simId) {
// 1. Add to main table
if (plainSelect.getFromItem() instanceof Table) {
addWhereCondition(plainSelect, simId);
}
// 2. Manually check WHERE clause for subqueries
Expression where = plainSelect.getWhere();
if (where instanceof InExpression inExpr) {
// Manually handle the right side
if (inExpr.getRightItemsList() instanceof SubSelect) {
modifySelect(((SubSelect) inExpr.getRightItemsList()).getSelectBody());
}
}
// I have to add manual checks for Exists, Between, BinaryExpression, etc...
}
Komplexe Szenarien können dadurch nicht automatisch abgedeckt werden. Zum Beispiel:
Eingabe:
Code: Select all
SELECT * FROM orders o
WHERE o.customer_id IN (
SELECT c.id FROM customers c
WHERE c.status = (SELECT s.code FROM status s WHERE s.active = 1)
)
Code: Select all
SELECT * FROM orders o
WHERE o.simulation_id = '123' -- Injected
AND o.customer_id IN (
SELECT c.id FROM customers c
WHERE c.simulation_id = '123' -- Injected (Recursion Level 1)
AND c.status = (
SELECT s.code FROM status s
WHERE s.simulation_id = '123' -- Injected (Recursion Level 2)
AND s.active = 1
)
)
Gibt es in JSqlParser ein Standardmuster wie StatementVisitor, TablesNamesFinder oder ExpressionDeParser, mit dem ich Folgendes tun kann:
- Besuchen Sie jeden Knoten im AST automatisch, wobei die Rekursion von verwaltet wird die Bibliothek.
- Identifizieren Sie, wann immer auf eine Tabelle zugegriffen wird, sei es in FROM, JOIN, UPDATE oder DELETE.
- Fügen Sie die WHERE-Klausel in das übergeordnete SELECT- oder UPDATE-Objekt ein.
Mobile version