-
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.
Merge pull request #105 from datafuselabs/fix/impl-preparestmt-method
feat: impl prepareStatement method
- Loading branch information
Showing
20 changed files
with
1,030 additions
and
16 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
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
74 changes: 74 additions & 0 deletions
74
databend-jdbc/src/main/java/com/databend/jdbc/LoggerUtil.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,74 @@ | ||
package com.databend.jdbc; | ||
|
||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.stream.Collectors; | ||
|
||
import com.databend.jdbc.log.DatabendLogger; | ||
import com.databend.jdbc.log.JDKLogger; | ||
import com.databend.jdbc.log.SLF4JLogger; | ||
|
||
import lombok.CustomLog; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
@CustomLog | ||
public class LoggerUtil { | ||
|
||
private static Boolean slf4jAvailable; | ||
|
||
/** | ||
* Provides a {@link DatabendLogger} based on whether SLF4J is available or not. | ||
* | ||
* @param name logger name | ||
* @return a {@link DatabendLogger} | ||
*/ | ||
public static DatabendLogger getLogger(String name) { | ||
if (slf4jAvailable == null) { | ||
slf4jAvailable = isSlf4jJAvailable(); | ||
} | ||
|
||
if (slf4jAvailable) { | ||
return new SLF4JLogger(name); | ||
} else { | ||
return new JDKLogger(name); | ||
} | ||
} | ||
|
||
/** | ||
* Logs the {@link InputStream} | ||
* | ||
* @param is the {@link InputStream} | ||
* @return a copy of the {@link InputStream} provided | ||
*/ | ||
public InputStream logInputStream(InputStream is) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
try { | ||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = is.read(buffer)) > -1) { | ||
baos.write(buffer, 0, len); | ||
} | ||
baos.flush(); | ||
InputStream streamToLog = new ByteArrayInputStream(baos.toByteArray()); | ||
String text = new BufferedReader(new InputStreamReader(streamToLog, StandardCharsets.UTF_8)).lines() | ||
.collect(Collectors.joining("\n")); | ||
log.info("======================================"); | ||
log.info(text); | ||
log.info("======================================"); | ||
return new ByteArrayInputStream(baos.toByteArray()); | ||
} catch (Exception ex) { | ||
log.warn("Could not log the stream", ex); | ||
} | ||
return new ByteArrayInputStream(baos.toByteArray()); | ||
} | ||
|
||
private static boolean isSlf4jJAvailable() { | ||
try { | ||
Class.forName("org.slf4j.Logger"); | ||
return true; | ||
} catch (ClassNotFoundException ex) { | ||
return false; | ||
} | ||
} | ||
} |
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 + '}'; | ||
} | ||
|
||
} |
Oops, something went wrong.