Skip to content

Commit

Permalink
Adds DELETE, REPLACE, UPSERT
Browse files Browse the repository at this point in the history
Updates UPDATE
  • Loading branch information
johnedquinn committed Nov 27, 2024
1 parent 0eb786a commit c055f0a
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 28 deletions.
28 changes: 28 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/AstVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,38 @@ public R visitInsert(Insert node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpsert(Upsert node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitReplace(Replace node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdate(Update node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTarget(UpdateTarget node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTargetStep(UpdateTarget node, C ctx) {
return node.accept(this, ctx);
}

public R visitUpdateTargetStepElement(UpdateTargetStep.Element node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitUpdateTargetStepField(UpdateTargetStep.Field node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitDelete(Delete node, C ctx) {
return defaultVisit(node, ctx);
}

public R visitSetClause(SetClause node, C ctx) {
return defaultVisit(node, ctx);
}
Expand Down
57 changes: 57 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.partiql.ast;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.partiql.ast.expr.Expr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This is the update searched statement.
* @see InsertColumnList
* @see InsertSource
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public final class Delete extends Statement {
// TODO: Equals and hashcode

/**
* TODO
*/
@NotNull
public final IdentifierChain tableName;

/**
* TODO
*/
@Nullable
public final Expr condition;

/**
* TODO
* @param tableName TODO
* @param condition TODO
*/
public Delete(@NotNull IdentifierChain tableName, @Nullable Expr condition) {
this.tableName = tableName;
this.condition = condition;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(tableName);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitDelete(this, ctx);
}
}
63 changes: 63 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/Replace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.partiql.ast;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This is the REPLACE INTO statement.
* @see InsertSource
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public final class Replace extends Statement {
// TODO: Equals and hashcode

/**
* TODO
*/
@NotNull
public final IdentifierChain tableName;

/**
* TODO
*/
@Nullable
public final Identifier asAlias;

/**
* TODO
*/
@NotNull
public final InsertSource source;

/**
* TODO
* @param tableName TODO
* @param asAlias TODO
* @param source TODO
*/
public Replace(@NotNull IdentifierChain tableName, @Nullable Identifier asAlias, @NotNull InsertSource source) {
this.tableName = tableName;
this.asAlias = asAlias;
this.source = source;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(tableName);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitReplace(this, ctx);
}
}
58 changes: 58 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/UpdateTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.partiql.ast;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.partiql.ast.expr.Expr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This references a column or column's nested properties. In SQL:1999, the EBNF rule is &lt;update target&gt;.
* This implementation differs from SQL by allowing for references to deeply nested data of varying types.
* @see SetClause
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public final class UpdateTarget extends AstNode {
// TODO: Equals and hashcode

/**
* TODO
*/
@NotNull
public final Identifier root;

/**
* TODO
*/
@NotNull
public final List<UpdateTargetStep> steps;

/**
* TODO
* @param root TODO
* @param steps TODO
*/
public UpdateTarget(@NotNull Identifier root, @NotNull List<UpdateTargetStep> steps) {
this.root = root;
this.steps = steps;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(root);
kids.addAll(steps);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitUpdateTarget(this, ctx);
}
}
115 changes: 115 additions & 0 deletions partiql-ast/src/main/java/org/partiql/ast/UpdateTargetStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.partiql.ast;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.partiql.ast.expr.ExprLit;
import org.partiql.value.PartiQL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* This references a column or column's nested properties. In SQL:1999, the EBNF rule is &lt;update target&gt;.
* This implementation differs from SQL by allowing for references to deeply nested data of varying types.
*
* @see SetClause
*/
public abstract class UpdateTargetStep extends AstNode {

/**
* This is a reference to a field of an array/struct using the bracket notation.
* @see UpdateTarget
* @see UpdateTargetStep
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class Element extends UpdateTargetStep {
// TODO: Equals and hashcode
// TODO: Should we explicitly have an ElementInt and ElementString? Especially once PartiQLValue is removed.

/**
* TODO
*/
// TODO: Change this to a literal, not an ExprLit
@NotNull
public final ExprLit key;

/**
* TODO
* @param key TODO
*/
public Element(@NotNull ExprLit key) {
this.key = key;
}

/**
* TODO
* @param key TODO
*/
public Element(int key) {
this.key = new ExprLit(PartiQL.int32Value(key));
}

/**
* TODO
* @param key TODO
*/
public Element(@NotNull String key) {
this.key = new ExprLit(PartiQL.stringValue(key));
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(key);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitUpdateTargetStepElement(this, ctx);
}
}

/**
* This is a reference to a field of a struct using the dot notation.
* @see UpdateTarget
* @see UpdateTargetStep
*/
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(callSuper = false)
public static final class Field extends UpdateTargetStep {
// TODO: Equals and hashcode

/**
* TODO
*/
// TODO: Change this to a literal, not an ExprLit
@NotNull
public final Identifier key;

/**
* TODO
* @param key TODO
*/
public Field(@NotNull Identifier key) {
this.key = key;
}

@NotNull
@Override
public Collection<AstNode> children() {
List<AstNode> kids = new ArrayList<>();
kids.add(key);
return kids;
}

@Override
public <R, C> R accept(@NotNull AstVisitor<R, C> visitor, C ctx) {
return visitor.visitUpdateTargetStepField(this, ctx);
}
}
}
Loading

0 comments on commit c055f0a

Please sign in to comment.