提交 130a9f2d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Avoid StatementBuilder where resetCount() is required

上级 2c0a1fd5
...@@ -60,11 +60,13 @@ public class LinkedIndex extends BaseIndex { ...@@ -60,11 +60,13 @@ public class LinkedIndex extends BaseIndex {
@Override @Override
public void add(Session session, Row row) { public void add(Session session, Row row) {
ArrayList<Value> params = Utils.newSmallArrayList(); ArrayList<Value> params = Utils.newSmallArrayList();
StatementBuilder buff = new StatementBuilder("INSERT INTO "); StringBuilder buff = new StringBuilder("INSERT INTO ");
buff.append(targetTableName).append(" VALUES("); buff.append(targetTableName).append(" VALUES(");
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
buff.appendExceptFirst(", "); if (i > 0) {
buff.append(", ");
}
if (v == null) { if (v == null) {
buff.append("DEFAULT"); buff.append("DEFAULT");
} else if (isNull(v)) { } else if (isNull(v)) {
...@@ -100,7 +102,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -100,7 +102,7 @@ public class LinkedIndex extends BaseIndex {
buff.append(" IS NULL"); buff.append(" IS NULL");
} else { } else {
buff.append(">="); buff.append(">=");
addParameter(buff, col); addParameter(buff.builder(), col);
params.add(v); params.add(v);
} }
} }
...@@ -116,7 +118,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -116,7 +118,7 @@ public class LinkedIndex extends BaseIndex {
buff.append(" IS NULL"); buff.append(" IS NULL");
} else { } else {
buff.append("<="); buff.append("<=");
addParameter(buff, col); addParameter(buff.builder(), col);
params.add(v); params.add(v);
} }
} }
...@@ -131,16 +133,16 @@ public class LinkedIndex extends BaseIndex { ...@@ -131,16 +133,16 @@ public class LinkedIndex extends BaseIndex {
} }
} }
private void addParameter(StatementBuilder buff, Column col) { private void addParameter(StringBuilder builder, Column col) {
TypeInfo type = col.getType(); TypeInfo type = col.getType();
if (type.getValueType() == Value.STRING_FIXED && link.isOracle()) { if (type.getValueType() == Value.STRING_FIXED && link.isOracle()) {
// workaround for Oracle // workaround for Oracle
// create table test(id int primary key, name char(15)); // create table test(id int primary key, name char(15));
// insert into test values(1, 'Hello') // insert into test values(1, 'Hello')
// select * from test where name = ? -- where ? = "Hello" > no rows // select * from test where name = ? -- where ? = "Hello" > no rows
buff.append("CAST(? AS CHAR(").append(type.getPrecision()).append("))"); builder.append("CAST(? AS CHAR(").append(type.getPrecision()).append("))");
} else { } else {
buff.append('?'); builder.append('?');
} }
} }
...@@ -187,23 +189,24 @@ public class LinkedIndex extends BaseIndex { ...@@ -187,23 +189,24 @@ public class LinkedIndex extends BaseIndex {
@Override @Override
public void remove(Session session, Row row) { public void remove(Session session, Row row) {
ArrayList<Value> params = Utils.newSmallArrayList(); ArrayList<Value> params = Utils.newSmallArrayList();
StatementBuilder buff = new StatementBuilder("DELETE FROM "); StringBuilder builder = new StringBuilder("DELETE FROM ").append(targetTableName).append(" WHERE ");
buff.append(targetTableName).append(" WHERE ");
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
buff.appendExceptFirst("AND "); if (i > 0) {
builder.append("AND ");
}
Column col = table.getColumn(i); Column col = table.getColumn(i);
col.getSQL(buff.builder()); col.getSQL(builder);
Value v = row.getValue(i); Value v = row.getValue(i);
if (isNull(v)) { if (isNull(v)) {
buff.append(" IS NULL "); builder.append(" IS NULL ");
} else { } else {
buff.append('='); builder.append('=');
addParameter(buff, col); addParameter(builder, col);
params.add(v); params.add(v);
buff.append(' '); builder.append(' ');
} }
} }
String sql = buff.toString(); String sql = builder.toString();
try { try {
PreparedStatement prep = link.execute(sql, params, false); PreparedStatement prep = link.execute(sql, params, false);
int count = prep.executeUpdate(); int count = prep.executeUpdate();
...@@ -223,35 +226,37 @@ public class LinkedIndex extends BaseIndex { ...@@ -223,35 +226,37 @@ public class LinkedIndex extends BaseIndex {
*/ */
public void update(Row oldRow, Row newRow) { public void update(Row oldRow, Row newRow) {
ArrayList<Value> params = Utils.newSmallArrayList(); ArrayList<Value> params = Utils.newSmallArrayList();
StatementBuilder buff = new StatementBuilder("UPDATE "); StringBuilder builder = new StringBuilder("UPDATE ").append(targetTableName).append(" SET ");
buff.append(targetTableName).append(" SET ");
for (int i = 0; i < newRow.getColumnCount(); i++) { for (int i = 0; i < newRow.getColumnCount(); i++) {
buff.appendExceptFirst(", "); if (i > 0) {
table.getColumn(i).getSQL(buff.builder()).append('='); builder.append(", ");
}
table.getColumn(i).getSQL(builder).append('=');
Value v = newRow.getValue(i); Value v = newRow.getValue(i);
if (v == null) { if (v == null) {
buff.append("DEFAULT"); builder.append("DEFAULT");
} else { } else {
buff.append('?'); builder.append('?');
params.add(v); params.add(v);
} }
} }
buff.append(" WHERE "); builder.append(" WHERE ");
buff.resetCount();
for (int i = 0; i < oldRow.getColumnCount(); i++) { for (int i = 0; i < oldRow.getColumnCount(); i++) {
Column col = table.getColumn(i); Column col = table.getColumn(i);
buff.appendExceptFirst(" AND "); if (i > 0) {
col.getSQL(buff.builder()); builder.append(" AND ");
}
col.getSQL(builder);
Value v = oldRow.getValue(i); Value v = oldRow.getValue(i);
if (isNull(v)) { if (isNull(v)) {
buff.append(" IS NULL"); builder.append(" IS NULL");
} else { } else {
buff.append('='); builder.append('=');
params.add(v); params.add(v);
addParameter(buff, col); addParameter(builder, col);
} }
} }
String sql = buff.toString(); String sql = builder.toString();
try { try {
link.execute(sql, params, true); link.execute(sql, params, true);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -14,7 +14,6 @@ import org.h2.index.IndexType; ...@@ -14,7 +14,6 @@ import org.h2.index.IndexType;
import org.h2.mvstore.db.MVTableEngine; import org.h2.mvstore.db.MVTableEngine;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.Value; import org.h2.value.Value;
...@@ -91,7 +90,7 @@ public abstract class TableBase extends Table { ...@@ -91,7 +90,7 @@ public abstract class TableBase extends Table {
// closed // closed
return null; return null;
} }
StatementBuilder buff = new StatementBuilder("CREATE "); StringBuilder buff = new StringBuilder("CREATE ");
if (isTemporary()) { if (isTemporary()) {
if (isGlobalTemporary()) { if (isGlobalTemporary()) {
buff.append("GLOBAL "); buff.append("GLOBAL ");
...@@ -108,15 +107,17 @@ public abstract class TableBase extends Table { ...@@ -108,15 +107,17 @@ public abstract class TableBase extends Table {
if (isHidden) { if (isHidden) {
buff.append("IF NOT EXISTS "); buff.append("IF NOT EXISTS ");
} }
getSQL(buff.builder()); getSQL(buff);
if (comment != null) { if (comment != null) {
buff.append(" COMMENT "); buff.append(" COMMENT ");
StringUtils.quoteStringSQL(buff.builder(), comment); StringUtils.quoteStringSQL(buff, comment);
} }
buff.append("(\n "); buff.append("(\n ");
for (Column column : columns) { for (int i = 0, l = columns.length; i < l; i++) {
buff.appendExceptFirst(",\n "); if (i > 0) {
buff.append(column.getCreateSQL()); buff.append(",\n ");
}
buff.append(columns[i].getCreateSQL());
} }
buff.append("\n)"); buff.append("\n)");
if (tableEngine != null) { if (tableEngine != null) {
...@@ -127,15 +128,16 @@ public abstract class TableBase extends Table { ...@@ -127,15 +128,16 @@ public abstract class TableBase extends Table {
} }
if (d == null || !tableEngine.endsWith(d)) { if (d == null || !tableEngine.endsWith(d)) {
buff.append("\nENGINE "); buff.append("\nENGINE ");
StringUtils.quoteIdentifier(buff.builder(), tableEngine); StringUtils.quoteIdentifier(buff, tableEngine);
} }
} }
if (!tableEngineParams.isEmpty()) { if (!tableEngineParams.isEmpty()) {
buff.append("\nWITH "); buff.append("\nWITH ");
buff.resetCount(); for (int i = 0, l = tableEngineParams.size(); i < l; i++) {
for (String parameter : tableEngineParams) { if (i > 0) {
buff.appendExceptFirst(", "); buff.append(", ");
StringUtils.quoteIdentifier(buff.builder(), parameter); }
StringUtils.quoteIdentifier(buff, tableEngineParams.get(i));
} }
} }
if (!isPersistIndexes() && !isPersistData()) { if (!isPersistIndexes() && !isPersistData()) {
......
...@@ -14,7 +14,6 @@ import java.util.ArrayList; ...@@ -14,7 +14,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import com.sun.javadoc.ClassDoc; import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.ConstructorDoc; import com.sun.javadoc.ConstructorDoc;
...@@ -311,31 +310,34 @@ public class Doclet { ...@@ -311,31 +310,34 @@ public class Doclet {
return; return;
} }
Parameter[] params = method.parameters(); Parameter[] params = method.parameters();
StatementBuilder buff = new StatementBuilder(); StringBuilder builder = new StringBuilder();
buff.append('('); builder.append('(');
int i = 0; for (int i = 0, l = params.length; i < l; i++) {
for (Parameter p : params) { if (i > 0) {
boolean isVarArgs = method.isVarArgs() && i++ == params.length - 1; builder.append(", ");
buff.appendExceptFirst(", "); }
buff.append(getTypeName(false, isVarArgs, p.type())); boolean isVarArgs = method.isVarArgs() && i == params.length - 1;
buff.append(' '); Parameter p = params[i];
buff.append(p.name()); builder.append(getTypeName(false, isVarArgs, p.type()));
} builder.append(' ');
buff.append(')'); builder.append(p.name());
}
builder.append(')');
ClassDoc[] exceptions = method.thrownExceptions(); ClassDoc[] exceptions = method.thrownExceptions();
if (exceptions.length > 0) { if (exceptions.length > 0) {
buff.append(" throws "); builder.append(" throws ");
buff.resetCount(); for (int i = 0, l = exceptions.length; i < l; i++) {
for (ClassDoc ex : exceptions) { if (i > 0) {
buff.appendExceptFirst(", "); builder.append(", ");
buff.append(ex.typeName()); }
builder.append(exceptions[i].typeName());
} }
} }
if (isDeprecated(method)) { if (isDeprecated(method)) {
name = "<span class=\"deprecated\">" + name + "</span>"; name = "<span class=\"deprecated\">" + name + "</span>";
} }
writer.println("<a id=\"" + signature + "\" href=\"#" + signature + "\">" + writer.println("<a id=\"" + signature + "\" href=\"#" + signature + "\">" +
name + "</a>" + buff.toString()); name + "</a>" + builder.toString());
boolean hasComment = method.commentText() != null && boolean hasComment = method.commentText() != null &&
method.commentText().trim().length() != 0; method.commentText().trim().length() != 0;
writer.println("<div class=\"methodText\">" + writer.println("<div class=\"methodText\">" +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论