Skip to content

Commit

Permalink
feat: INSERT, UPDATE, DELETE, MERGE transpilers
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 Jun 3, 2024
1 parent e74bd0d commit e53e9a0
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 144 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ai/starlake/transpiler/JSQLDeleteTranspiler.java
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 src/main/java/ai/starlake/transpiler/JSQLInsertTranspiler.java
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 src/main/java/ai/starlake/transpiler/JSQLMergeTranspiler.java
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 src/main/java/ai/starlake/transpiler/JSQLSelectTranspiler.java
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);
}
}
Loading

0 comments on commit e53e9a0

Please sign in to comment.