-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: INSERT, UPDATE, DELETE, MERGE transpilers
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
- Loading branch information
1 parent
e74bd0d
commit e53e9a0
Showing
6 changed files
with
243 additions
and
144 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/ai/starlake/transpiler/JSQLDeleteTranspiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ai.starlake.transpiler; | ||
|
||
import net.sf.jsqlparser.expression.ExpressionVisitor; | ||
import net.sf.jsqlparser.util.deparser.DeleteDeParser; | ||
|
||
public class JSQLDeleteTranspiler extends DeleteDeParser { | ||
JSQLDeleteTranspiler(ExpressionVisitor expressionVisitor, StringBuilder buffer) { | ||
this.buffer = buffer; | ||
this.setExpressionVisitor(expressionVisitor); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/ai/starlake/transpiler/JSQLInsertTranspiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ai.starlake.transpiler; | ||
|
||
import net.sf.jsqlparser.expression.ExpressionVisitor; | ||
import net.sf.jsqlparser.statement.select.SelectVisitor; | ||
import net.sf.jsqlparser.util.deparser.InsertDeParser; | ||
|
||
public class JSQLInsertTranspiler extends InsertDeParser { | ||
|
||
JSQLInsertTranspiler(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuilder buffer) { | ||
this.buffer = buffer; | ||
this.setExpressionVisitor(expressionVisitor); | ||
this.setSelectVisitor(selectVisitor); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/ai/starlake/transpiler/JSQLMergeTranspiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ai.starlake.transpiler; | ||
|
||
import net.sf.jsqlparser.util.deparser.ExpressionDeParser; | ||
import net.sf.jsqlparser.util.deparser.MergeDeParser; | ||
import net.sf.jsqlparser.util.deparser.SelectDeParser; | ||
|
||
public class JSQLMergeTranspiler extends MergeDeParser { | ||
public JSQLMergeTranspiler(ExpressionDeParser expressionDeParser, | ||
SelectDeParser selectDeParser, | ||
StringBuilder buffer) { | ||
super(expressionDeParser, selectDeParser, buffer); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/ai/starlake/transpiler/JSQLSelectTranspiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package ai.starlake.transpiler; | ||
|
||
import net.sf.jsqlparser.expression.ExpressionVisitor; | ||
import net.sf.jsqlparser.parser.SimpleNode; | ||
import net.sf.jsqlparser.schema.Table; | ||
import net.sf.jsqlparser.statement.select.FromItem; | ||
import net.sf.jsqlparser.statement.select.Limit; | ||
import net.sf.jsqlparser.statement.select.ParenthesedSelect; | ||
import net.sf.jsqlparser.statement.select.PlainSelect; | ||
import net.sf.jsqlparser.statement.select.SelectItem; | ||
import net.sf.jsqlparser.statement.select.TableFunction; | ||
import net.sf.jsqlparser.statement.select.Top; | ||
import net.sf.jsqlparser.util.deparser.SelectDeParser; | ||
|
||
|
||
public class JSQLSelectTranspiler extends SelectDeParser { | ||
/** | ||
* The Expression transpiler. | ||
*/ | ||
protected JSQLExpressionTranspiler expressionTranspiler; | ||
|
||
|
||
/** | ||
* Instantiates a new transpiler. | ||
*/ | ||
protected JSQLSelectTranspiler(JSQLExpressionTranspiler expressionTranspiler, StringBuilder resultBuilder) { | ||
super(expressionTranspiler, resultBuilder); | ||
} | ||
|
||
|
||
/** | ||
* Gets result builder. | ||
* | ||
* @return the result builder | ||
*/ | ||
public StringBuilder getResultBuilder() { | ||
return getBuffer(); | ||
} | ||
|
||
public void visit(Top top) { | ||
// get the parent SELECT | ||
SimpleNode node = (SimpleNode) top.getASTNode().jjtGetParent(); | ||
while (node.jjtGetValue() == null) { | ||
node = (SimpleNode) node.jjtGetParent(); | ||
} | ||
PlainSelect select = (PlainSelect) node.jjtGetValue(); | ||
|
||
// rewrite the TOP into a LIMIT | ||
select.setTop(null); | ||
select.setLimit(new Limit().withRowCount(top.getExpression())); | ||
} | ||
|
||
public void visit(TableFunction tableFunction) { | ||
String name = tableFunction.getFunction().getName(); | ||
if (name.equalsIgnoreCase("unnest")) { | ||
PlainSelect select = new PlainSelect() | ||
.withSelectItems(new SelectItem<>(tableFunction.getFunction(), tableFunction.getAlias())); | ||
|
||
ParenthesedSelect parenthesedSelect = | ||
new ParenthesedSelect().withSelect(select).withAlias(tableFunction.getAlias()); | ||
|
||
visit(parenthesedSelect); | ||
} else { | ||
super.visit(tableFunction); | ||
} | ||
} | ||
|
||
public void visit(PlainSelect plainSelect) { | ||
// remove any DUAL pseudo tables | ||
FromItem fromItem = plainSelect.getFromItem(); | ||
if (fromItem instanceof Table) { | ||
Table table = (Table) fromItem; | ||
if (table.getName().equalsIgnoreCase("dual")) { | ||
plainSelect.setFromItem(null); | ||
} | ||
} | ||
super.visit(plainSelect); | ||
} | ||
|
||
public void visit(Table table) { | ||
String name = table.getName().toLowerCase(); | ||
String aliasName = table.getAlias() != null ? table.getAlias().getName() : null; | ||
|
||
for (String[] keyword : JSQLExpressionTranspiler.KEYWORDS) { | ||
if (keyword[0].equals(name)) { | ||
table.setName("\"" + table.getName() + "\""); | ||
name = null; | ||
if (aliasName == null) { | ||
break; | ||
} | ||
} | ||
|
||
if (keyword[0].equals(aliasName)) { | ||
table.getAlias().setName("\"" + table.getAlias().getName() + "\""); | ||
aliasName = null; | ||
if (name == null) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
super.visit(table); | ||
} | ||
|
||
public void visit(SelectItem selectItem) { | ||
if (selectItem.getAlias() != null) { | ||
String aliasName = selectItem.getAlias().getName().toLowerCase(); | ||
for (String[] keyword : JSQLExpressionTranspiler.KEYWORDS) { | ||
if (keyword[0].equals(aliasName)) { | ||
selectItem.getAlias().setName("\"" + selectItem.getAlias().getName() + "\""); | ||
break; | ||
} | ||
} | ||
} | ||
super.visit(selectItem); | ||
} | ||
} |
Oops, something went wrong.