-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
824 additions
and
5 deletions.
There are no files selected for viewing
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
25 changes: 25 additions & 0 deletions
25
databend-jdbc/src/main/java/com/databend/jdbc/NonQueryRawStatement.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,25 @@ | ||
package com.databend.jdbc; | ||
|
||
import static com.databend.jdbc.StatementType.NON_QUERY; | ||
|
||
import java.util.List; | ||
|
||
|
||
import lombok.EqualsAndHashCode; | ||
|
||
/** | ||
* A non query statement is a statement that does not return data (such as | ||
* INSERT) | ||
*/ | ||
@EqualsAndHashCode(callSuper = true) | ||
public class NonQueryRawStatement extends RawStatement { | ||
|
||
public NonQueryRawStatement(String sql, String cleanSql, List<ParamMarker> paramPositions) { | ||
super(sql, cleanSql, paramPositions); | ||
} | ||
|
||
@Override | ||
public StatementType getStatementType() { | ||
return NON_QUERY; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.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,10 @@ | ||
package com.databend.jdbc; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
|
||
@AllArgsConstructor | ||
@Value | ||
public class ParamMarker { | ||
int id; // Id / index of the param marker in the SQL statement | ||
int position; // Position in the SQL subStatement | ||
} |
39 changes: 39 additions & 0 deletions
39
databend-jdbc/src/main/java/com/databend/jdbc/QueryRawStatement.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,39 @@ | ||
package com.databend.jdbc; | ||
|
||
import static com.databend.jdbc.StatementType.QUERY; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
/** | ||
* A query statement is a statement that returns data (Typically starts with | ||
* SELECT, SHOW, etc) | ||
*/ | ||
@Getter | ||
@EqualsAndHashCode(callSuper = true) | ||
public class QueryRawStatement extends RawStatement { | ||
|
||
private final String database; | ||
|
||
private final String table; | ||
|
||
public QueryRawStatement(String sql, String cleanSql, List<ParamMarker> paramPositions) { | ||
super(sql, cleanSql, paramPositions); | ||
Pair<Optional<String>, Optional<String>> databaseAndTablePair = StatementUtil | ||
.extractDbNameAndTableNamePairFromCleanQuery(this.getCleanSql()); | ||
this.database = databaseAndTablePair.getLeft().orElse(null); | ||
this.table = databaseAndTablePair.getRight().orElse(null); | ||
} | ||
|
||
@Override | ||
public StatementType getStatementType() { | ||
return QUERY; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.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,57 @@ | ||
package com.databend.jdbc; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import com.databend.jdbc.ParamMarker; | ||
import com.databend.jdbc.StatementType; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public abstract class RawStatement { | ||
|
||
private final String sql; | ||
private final String cleanSql; | ||
private final List<ParamMarker> paramMarkers; | ||
|
||
protected RawStatement(String sql, String cleanSql, List<ParamMarker> paramPositions) { | ||
this.sql = sql; | ||
this.cleanSql = cleanSql; | ||
this.paramMarkers = paramPositions; | ||
} | ||
|
||
public static RawStatement of(String sql, List<ParamMarker> paramPositions, String cleanSql) { | ||
Optional<Pair<String, String>> additionalProperties = StatementUtil.extractParamFromSetStatement(cleanSql, sql); | ||
if (additionalProperties.isPresent()) { | ||
return new SetParamRawStatement(sql, cleanSql, paramPositions, additionalProperties.get()); | ||
} else if (StatementUtil.isQuery(cleanSql)) { | ||
return new QueryRawStatement(sql, cleanSql, paramPositions); | ||
} else { | ||
return new NonQueryRawStatement(sql, cleanSql, paramPositions); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RawSqlStatement{" + "sql='" + sql + '\'' + ", cleanSql='" + cleanSql + '\'' + ", paramMarkers=" | ||
+ StringUtils.join(paramMarkers, "|") + '}'; | ||
} | ||
|
||
public List<ParamMarker> getParamMarkers() { | ||
return paramMarkers; | ||
} | ||
|
||
public String getSql() { | ||
return sql; | ||
} | ||
|
||
public String getCleanSql() { | ||
return cleanSql; | ||
} | ||
|
||
public abstract StatementType getStatementType(); | ||
} |
29 changes: 29 additions & 0 deletions
29
databend-jdbc/src/main/java/com/databend/jdbc/RawStatementWrapper.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,29 @@ | ||
package com.databend.jdbc; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import lombok.CustomLog; | ||
import lombok.Value; | ||
|
||
@CustomLog | ||
@Value | ||
public class RawStatementWrapper { | ||
|
||
List<RawStatement> subStatements; | ||
|
||
long totalParams; | ||
|
||
public RawStatementWrapper(List<RawStatement> subStatements) { | ||
this.subStatements = subStatements; | ||
this.totalParams = subStatements.stream().map(RawStatement::getParamMarkers).mapToLong(Collection::size).sum(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SqlQueryWrapper{" + "subQueries=" + StringUtils.join(subStatements, "|") + ", totalParams=" | ||
+ totalParams + '}'; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
databend-jdbc/src/main/java/com/databend/jdbc/SetParamRawStatement.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,32 @@ | ||
package com.databend.jdbc; | ||
import static com.databend.jdbc.StatementType.PARAM_SETTING; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
/** | ||
* A Set param statement is a special statement that sets a parameter internally | ||
* (this type of statement starts with SET) | ||
*/ | ||
@Getter | ||
@EqualsAndHashCode(callSuper = true) | ||
public class SetParamRawStatement extends RawStatement { | ||
|
||
private final Pair<String, String> additionalProperty; | ||
|
||
public SetParamRawStatement(String sql, String cleanSql, List<ParamMarker> paramPositions, | ||
Pair<String, String> additionalProperty) { | ||
super(sql, cleanSql, paramPositions); | ||
this.additionalProperty = additionalProperty; | ||
} | ||
|
||
@Override | ||
public StatementType getStatementType() { | ||
return PARAM_SETTING; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
databend-jdbc/src/main/java/com/databend/jdbc/StatementInfoWrapper.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,52 @@ | ||
package com.databend.jdbc; | ||
|
||
|
||
import static com.databend.jdbc.StatementType.PARAM_SETTING; | ||
|
||
import java.util.UUID; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NonNull; | ||
|
||
/** | ||
* This represents a statement that is ready to be sent to Firebolt or executed | ||
* internally to set a param | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
public class StatementInfoWrapper { | ||
private String sql; | ||
private String id; | ||
private StatementType type; | ||
private Pair<String, String> param; | ||
private RawStatement initialStatement; | ||
|
||
/** | ||
* Creates a StatementInfoWrapper from the {@link RawStatement}. | ||
* | ||
* @param rawStatement the raw statement | ||
* @return the statement that will be sent to the server | ||
*/ | ||
public static StatementInfoWrapper of(@NonNull RawStatement rawStatement) { | ||
return of(rawStatement, UUID.randomUUID().toString()); | ||
} | ||
|
||
/** | ||
* Creates a StatementInfoWrapper from the {@link RawStatement}. | ||
* | ||
* @param rawStatement the raw statement | ||
* @param id the id of the statement to execute | ||
* @return the statement that will be sent to the server | ||
*/ | ||
public static StatementInfoWrapper of(@NonNull RawStatement rawStatement, String id) { | ||
Pair<String, String> additionalProperties = rawStatement.getStatementType() == PARAM_SETTING | ||
? ((SetParamRawStatement) rawStatement).getAdditionalProperty() | ||
: null; | ||
return new StatementInfoWrapper(rawStatement.getSql(), id, rawStatement.getStatementType(), | ||
additionalProperties, rawStatement); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
databend-jdbc/src/main/java/com/databend/jdbc/StatementType.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,6 @@ | ||
package com.databend.jdbc; | ||
public enum StatementType { | ||
PARAM_SETTING, // SET | ||
QUERY, // eg: SELECT, SHOW | ||
NON_QUERY // eg: INSERT | ||
} |
Oops, something went wrong.