Skip to content

Commit

Permalink
feat: impl prepareStatement method
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Oct 19, 2023
1 parent 16374ff commit 7f65072
Show file tree
Hide file tree
Showing 15 changed files with 824 additions and 5 deletions.
16 changes: 16 additions & 0 deletions databend-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
<artifactId>okhttp</artifactId>
<version>${dep.okhttp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
Expand Down
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 databend-jdbc/src/main/java/com/databend/jdbc/ParamMarker.java
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
}
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 databend-jdbc/src/main/java/com/databend/jdbc/RawStatement.java
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();
}
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 + '}';
}

}
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;
}

}
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);
}
}
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
}
Loading

0 comments on commit 7f65072

Please sign in to comment.