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