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

Avoid StatementBuilder where resetCount() is required

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