提交 5288eafc authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not use StatementBuilder in JDBC Client classes

上级 4ef44393
......@@ -25,7 +25,6 @@ import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.message.TraceObject;
import org.h2.result.SimpleResult;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.value.ValueInt;
import org.h2.value.ValueString;
......@@ -1616,25 +1615,27 @@ public class JdbcDatabaseMetaData extends TraceObject implements
+ "FROM INFORMATION_SCHEMA.HELP WHERE SECTION = ?");
prep.setString(1, section);
ResultSet rs = prep.executeQuery();
StatementBuilder buff = new StatementBuilder();
StringBuilder builder = new StringBuilder();
while (rs.next()) {
String s = rs.getString(1).trim();
String[] array = StringUtils.arraySplit(s, ',', true);
for (String a : array) {
buff.appendExceptFirst(",");
if (builder.length() != 0) {
builder.append(',');
}
String f = a.trim();
int spaceIndex = f.indexOf(' ');
if (spaceIndex >= 0) {
// remove 'Function' from 'INSERT Function'
StringUtils.trimSubstring(buff.builder(), f, 0, spaceIndex);
StringUtils.trimSubstring(builder, f, 0, spaceIndex);
} else {
buff.append(f);
builder.append(f);
}
}
}
rs.close();
prep.close();
return buff.toString();
return builder.toString();
} catch (Exception e) {
throw logAndConvert(e);
}
......
......@@ -10,9 +10,7 @@ import java.util.ArrayList;
import org.h2.engine.SysProperties;
import org.h2.expression.ParameterInterface;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.value.Value;
/**
* This class represents a trace module.
......@@ -239,29 +237,23 @@ public class Trace {
* @param parameters the parameter list
* @return the formatted text
*/
public static String formatParams(
ArrayList<? extends ParameterInterface> parameters) {
public static String formatParams(ArrayList<? extends ParameterInterface> parameters) {
if (parameters.isEmpty()) {
return "";
}
StatementBuilder buff = new StatementBuilder();
StringBuilder builder = new StringBuilder();
int i = 0;
boolean params = false;
for (ParameterInterface p : parameters) {
if (p.isValueSet()) {
if (!params) {
buff.append(" {");
params = true;
}
buff.appendExceptFirst(", ");
Value v = p.getParamValue();
buff.append(++i).append(": ").append(v.getTraceSQL());
builder.append(i == 0 ? " {" : ", ") //
.append(++i).append(": ") //
.append(p.getParamValue().getTraceSQL());
}
}
if (params) {
buff.append('}');
if (i != 0) {
builder.append('}');
}
return buff.toString();
return builder.toString();
}
/**
......
......@@ -14,7 +14,6 @@ import java.util.ArrayList;
import org.h2.api.ErrorCode;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.DataType;
......@@ -156,24 +155,26 @@ public class UpdatableRow {
return index;
}
private void appendColumnList(StatementBuilder buff, boolean set) {
buff.resetCount();
private void appendColumnList(StringBuilder builder, boolean set) {
for (int i = 0; i < columnCount; i++) {
buff.appendExceptFirst(",");
if (i > 0) {
builder.append(',');
}
String col = result.getColumnName(i);
StringUtils.quoteIdentifier(buff.builder(), col);
StringUtils.quoteIdentifier(builder, col);
if (set) {
buff.append("=? ");
builder.append("=? ");
}
}
}
private void appendKeyCondition(StatementBuilder buff) {
buff.append(" WHERE ");
buff.resetCount();
for (String k : key) {
buff.appendExceptFirst(" AND ");
StringUtils.quoteIdentifier(buff.builder(), k).append("=?");
private void appendKeyCondition(StringBuilder builder) {
builder.append(" WHERE ");
for (int i = 0; i < key.size(); i++) {
if (i > 0) {
builder.append(" AND ");
}
StringUtils.quoteIdentifier(builder, key.get(i)).append("=?");
}
}
......@@ -218,12 +219,12 @@ public class UpdatableRow {
* @return the row
*/
public Value[] readRow(Value[] row) throws SQLException {
StatementBuilder buff = new StatementBuilder("SELECT ");
appendColumnList(buff, false);
buff.append(" FROM ");
appendTableName(buff.builder());
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
StringBuilder builder = new StringBuilder("SELECT ");
appendColumnList(builder, false);
builder.append(" FROM ");
appendTableName(builder);
appendKeyCondition(builder);
PreparedStatement prep = conn.prepareStatement(builder.toString());
setKey(prep, 1, row);
ResultSet rs = prep.executeQuery();
if (!rs.next()) {
......@@ -244,10 +245,10 @@ public class UpdatableRow {
* @throws SQLException if this row has already been deleted
*/
public void deleteRow(Value[] current) throws SQLException {
StatementBuilder buff = new StatementBuilder("DELETE FROM ");
appendTableName(buff.builder());
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
StringBuilder builder = new StringBuilder("DELETE FROM ");
appendTableName(builder);
appendKeyCondition(builder);
PreparedStatement prep = conn.prepareStatement(builder.toString());
setKey(prep, 1, current);
int count = prep.executeUpdate();
if (count != 1) {
......@@ -264,15 +265,15 @@ public class UpdatableRow {
* @throws SQLException if the row has been deleted
*/
public void updateRow(Value[] current, Value[] updateRow) throws SQLException {
StatementBuilder buff = new StatementBuilder("UPDATE ");
appendTableName(buff.builder());
buff.append(" SET ");
appendColumnList(buff, true);
StringBuilder builder = new StringBuilder("UPDATE ");
appendTableName(builder);
builder.append(" SET ");
appendColumnList(builder, true);
// TODO updatable result set: we could add all current values to the
// where clause
// - like this optimistic ('no') locking is possible
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
appendKeyCondition(builder);
PreparedStatement prep = conn.prepareStatement(builder.toString());
int j = 1;
for (int i = 0; i < columnCount; i++) {
Value v = updateRow[i];
......@@ -296,23 +297,24 @@ public class UpdatableRow {
* @throws SQLException if the row could not be inserted
*/
public void insertRow(Value[] row) throws SQLException {
StatementBuilder buff = new StatementBuilder("INSERT INTO ");
appendTableName(buff.builder());
buff.append('(');
appendColumnList(buff, false);
buff.append(")VALUES(");
buff.resetCount();
StringBuilder builder = new StringBuilder("INSERT INTO ");
appendTableName(builder);
builder.append('(');
appendColumnList(builder, false);
builder.append(")VALUES(");
for (int i = 0; i < columnCount; i++) {
buff.appendExceptFirst(",");
if (i > 0) {
builder.append(',');
}
Value v = row[i];
if (v == null) {
buff.append("DEFAULT");
builder.append("DEFAULT");
} else {
buff.append('?');
builder.append('?');
}
}
buff.append(')');
PreparedStatement prep = conn.prepareStatement(buff.toString());
builder.append(')');
PreparedStatement prep = conn.prepareStatement(builder.toString());
for (int i = 0, j = 0; i < columnCount; i++) {
Value v = row[i];
if (v != null) {
......
......@@ -370,10 +370,12 @@ public class StringUtils {
if (array == null) {
return "null";
}
StatementBuilder buff = new StatementBuilder("new String[]{");
for (String a : array) {
buff.appendExceptFirst(", ");
buff.append(quoteJavaString(a));
StringBuilder buff = new StringBuilder("new String[]{");
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(quoteJavaString(array[i]));
}
return buff.append('}').toString();
}
......@@ -389,12 +391,14 @@ public class StringUtils {
if (array == null) {
return "null";
}
StatementBuilder buff = new StatementBuilder("new int[]{");
for (int a : array) {
buff.appendExceptFirst(", ");
buff.append(a);
StringBuilder builder = new StringBuilder("new int[]{");
for (int i = 0; i < array.length; i++) {
if (i > 0) {
builder.append(", ");
}
builder.append(array[i]);
}
return buff.append('}').toString();
return builder.append('}').toString();
}
/**
......@@ -498,21 +502,24 @@ public class StringUtils {
* @return the combined string
*/
public static String arrayCombine(String[] list, char separatorChar) {
StatementBuilder buff = new StatementBuilder();
for (String s : list) {
buff.appendExceptFirst(String.valueOf(separatorChar));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.length; i++) {
if (i > 0) {
builder.append(separatorChar);
}
String s = list[i];
if (s == null) {
s = "";
continue;
}
for (int j = 0, length = s.length(); j < length; j++) {
char c = s.charAt(j);
if (c == '\\' || c == separatorChar) {
buff.append('\\');
builder.append('\\');
}
buff.append(c);
builder.append(c);
}
}
return buff.toString();
return builder.toString();
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论