提交 4f59a3c8 authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 013a9c2b
...@@ -1130,8 +1130,10 @@ public class Parser { ...@@ -1130,8 +1130,10 @@ public class Parser {
SelectOrderBy order = new SelectOrderBy(); SelectOrderBy order = new SelectOrderBy();
Expression expr = readExpression(); Expression expr = readExpression();
if(canBeNumber && expr instanceof ValueExpression && expr.getType() == Value.INT) { if(canBeNumber && expr instanceof ValueExpression && expr.getType() == Value.INT) {
int i = expr.getValue(null).getInt(); order.columnIndexExpr = expr;
order.column = i-1; } else if(expr instanceof Parameter) {
recompileAlways = true;
order.columnIndexExpr = expr;
} else { } else {
order.expression = expr; order.expression = expr;
} }
...@@ -2706,11 +2708,11 @@ public class Parser { ...@@ -2706,11 +2708,11 @@ public class Parser {
long start = 1, increment = 1; long start = 1, increment = 1;
if(readIf("(")) { if(readIf("(")) {
read("START"); read("START");
read("WITH"); readIf("WITH");
start = readLong(); start = readLong();
readIf(","); readIf(",");
if(readIf("INCREMENT")) { if(readIf("INCREMENT")) {
read("BY"); readIf("BY");
increment = readLong(); increment = readLong();
} }
read(")"); read(")");
...@@ -2772,6 +2774,10 @@ public class Parser { ...@@ -2772,6 +2774,10 @@ public class Parser {
if(readIf("PRECISION")) { if(readIf("PRECISION")) {
original += " PRECISION"; original += " PRECISION";
} }
} else if(readIf("CHARACTER")) {
if(readIf("VARYING")) {
original += " VARYING";
}
} else { } else {
regular = true; regular = true;
} }
...@@ -2874,7 +2880,7 @@ public class Parser { ...@@ -2874,7 +2880,7 @@ public class Parser {
read("TABLE"); read("TABLE");
return parseCreateTable(false, false, false); return parseCreateTable(false, false, false);
} else if(readIf("LINKED")) { } else if(readIf("LINKED")) {
return parseCreateLinkedTable(); return parseCreateLinkedTable(force);
} else if (readIf("CACHED")) { } else if (readIf("CACHED")) {
read("TABLE"); read("TABLE");
return parseCreateTable(false, false, true); return parseCreateTable(false, false, true);
...@@ -3038,12 +3044,12 @@ public class Parser { ...@@ -3038,12 +3044,12 @@ public class Parser {
command.setIfNotExists(ifNotExists); command.setIfNotExists(ifNotExists);
command.setSequenceName(sequenceName); command.setSequenceName(sequenceName);
if(readIf("START")) { if(readIf("START")) {
read("WITH"); readIf("WITH");
long start = readLong(); long start = readLong();
command.setStartWith(start); command.setStartWith(start);
} }
if(readIf("INCREMENT")) { if(readIf("INCREMENT")) {
read("BY"); readIf("BY");
long increment = readLong(); long increment = readLong();
command.setIncrement(increment); command.setIncrement(increment);
} }
...@@ -3437,6 +3443,12 @@ public class Parser { ...@@ -3437,6 +3443,12 @@ public class Parser {
} else if(readIf("DB_CLOSE_ON_EXIT")) { } else if(readIf("DB_CLOSE_ON_EXIT")) {
read(); read();
return new NoOperation(session); return new NoOperation(session);
} else if(readIf("WRITE_MODE_LOG")) {
read();
return new NoOperation(session);
} else if(readIf("WRITE_MODE_DATA")) {
read();
return new NoOperation(session);
} else if(readIf("RECOVER")) { } else if(readIf("RECOVER")) {
read(); read();
return new NoOperation(session); return new NoOperation(session);
...@@ -3809,11 +3821,12 @@ public class Parser { ...@@ -3809,11 +3821,12 @@ public class Parser {
} }
} }
private CreateLinkedTable parseCreateLinkedTable() throws SQLException { private CreateLinkedTable parseCreateLinkedTable(boolean force) throws SQLException {
read("TABLE"); read("TABLE");
boolean ifNotExists = readIfNoExists(); boolean ifNotExists = readIfNoExists();
String tableName = readIdentifierWithSchema(); String tableName = readIdentifierWithSchema();
CreateLinkedTable command = new CreateLinkedTable(session, getSchema()); CreateLinkedTable command = new CreateLinkedTable(session, getSchema());
command.setForce(force);
command.setIfNotExists(ifNotExists); command.setIfNotExists(ifNotExists);
command.setTableName(tableName); command.setTableName(tableName);
command.setComment(readCommentIf()); command.setComment(readCommentIf());
......
...@@ -108,7 +108,7 @@ public abstract class Prepared { ...@@ -108,7 +108,7 @@ public abstract class Prepared {
return id; return id;
} }
public String getPlan() { public String getPlanSQL() {
return null; return null;
} }
......
...@@ -24,6 +24,7 @@ public class CreateLinkedTable extends SchemaCommand { ...@@ -24,6 +24,7 @@ public class CreateLinkedTable extends SchemaCommand {
private boolean ifNotExists; private boolean ifNotExists;
private String comment; private String comment;
private boolean emitUpdates; private boolean emitUpdates;
private boolean force;
public CreateLinkedTable(Session session, Schema schema) { public CreateLinkedTable(Session session, Schema schema) {
super(session, schema); super(session, schema);
...@@ -69,7 +70,7 @@ public class CreateLinkedTable extends SchemaCommand { ...@@ -69,7 +70,7 @@ public class CreateLinkedTable extends SchemaCommand {
tableName); tableName);
} }
int id = getObjectId(false, true); int id = getObjectId(false, true);
TableLink table = new TableLink(getSchema(), id, tableName, driver, url, user, password, originalTable, emitUpdates); TableLink table = new TableLink(getSchema(), id, tableName, driver, url, user, password, originalTable, emitUpdates, force);
table.setComment(comment); table.setComment(comment);
db.addSchemaObject(session, table); db.addSchemaObject(session, table);
return 0; return 0;
...@@ -83,4 +84,8 @@ public class CreateLinkedTable extends SchemaCommand { ...@@ -83,4 +84,8 @@ public class CreateLinkedTable extends SchemaCommand {
this.comment = comment; this.comment = comment;
} }
public void setForce(boolean force) {
this.force = force;
}
} }
...@@ -79,7 +79,7 @@ public class Delete extends Prepared { ...@@ -79,7 +79,7 @@ public class Delete extends Prepared {
return rows.size(); return rows.size();
} }
public String getPlan() { public String getPlanSQL() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append("DELETE FROM "); buff.append("DELETE FROM ");
buff.append(tableFilter.getPlanSQL(false)); buff.append(tableFilter.getPlanSQL(false));
......
...@@ -39,7 +39,7 @@ public class ExplainPlan extends Prepared { ...@@ -39,7 +39,7 @@ public class ExplainPlan extends Prepared {
ExpressionColumn expr = new ExpressionColumn(session.getDatabase(), null, column); ExpressionColumn expr = new ExpressionColumn(session.getDatabase(), null, column);
expressions.add(expr); expressions.add(expr);
result = new LocalResult(session, expressions, 1); result = new LocalResult(session, expressions, 1);
String plan = command.getPlan(); String plan = command.getPlanSQL();
add(plan); add(plan);
result.done(); result.done();
return result; return result;
......
...@@ -120,7 +120,7 @@ public class Insert extends Prepared { ...@@ -120,7 +120,7 @@ public class Insert extends Prepared {
return count; return count;
} }
public String getPlan() { public String getPlanSQL() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append("INSERT INTO "); buff.append("INSERT INTO ");
buff.append(table.getSQL()); buff.append(table.getSQL());
...@@ -154,7 +154,7 @@ public class Insert extends Prepared { ...@@ -154,7 +154,7 @@ public class Insert extends Prepared {
buff.append(')'); buff.append(')');
} }
} else { } else {
buff.append(query.getPlan()); buff.append(query.getPlanSQL());
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -174,7 +174,7 @@ public class Merge extends Prepared { ...@@ -174,7 +174,7 @@ public class Merge extends Prepared {
} }
} }
public String getPlan() { public String getPlanSQL() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append("MERGE INTO "); buff.append("MERGE INTO ");
buff.append(table.getSQL()); buff.append(table.getSQL());
...@@ -219,7 +219,7 @@ public class Merge extends Prepared { ...@@ -219,7 +219,7 @@ public class Merge extends Prepared {
buff.append(')'); buff.append(')');
} }
} else { } else {
buff.append(query.getPlan()); buff.append(query.getPlanSQL());
} }
return buff.toString(); return buff.toString();
} }
......
...@@ -14,6 +14,7 @@ import org.h2.expression.Expression; ...@@ -14,6 +14,7 @@ import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn; import org.h2.expression.ExpressionColumn;
import org.h2.expression.ExpressionVisitor; import org.h2.expression.ExpressionVisitor;
import org.h2.expression.Parameter; import org.h2.expression.Parameter;
import org.h2.expression.ValueExpression;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.result.LocalResult; import org.h2.result.LocalResult;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
...@@ -21,6 +22,8 @@ import org.h2.table.ColumnResolver; ...@@ -21,6 +22,8 @@ import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.ObjectArray; import org.h2.util.ObjectArray;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueInt;
import org.h2.value.ValueNull;
public abstract class Query extends Prepared { public abstract class Query extends Prepared {
...@@ -96,20 +99,18 @@ public abstract class Query extends Prepared { ...@@ -96,20 +99,18 @@ public abstract class Query extends Prepared {
return lastResult; return lastResult;
} }
protected SortOrder initOrder(ObjectArray expressions, ObjectArray orderList, int visible, boolean mustBeInResult) throws SQLException { protected void initOrder(ObjectArray expressions, ObjectArray expressionSQL, ObjectArray orderList, int visible, boolean mustBeInResult) throws SQLException {
int[] index = new int[orderList.size()];
int[] sortType = new int[orderList.size()];
int originalLength = expressions.size();
for(int i=0; i<orderList.size(); i++) { for(int i=0; i<orderList.size(); i++) {
SelectOrderBy o = (SelectOrderBy) orderList.get(i); SelectOrderBy o = (SelectOrderBy) orderList.get(i);
int idx;
if(o.expression != null) {
Expression e = o.expression; Expression e = o.expression;
if(e == null) {
continue;
}
// special case: SELECT 1 AS A FROM DUAL ORDER BY A // special case: SELECT 1 AS A FROM DUAL ORDER BY A
// (oracle supports it, but only in order by, not in group by and not in having): // (oracle supports it, but only in order by, not in group by and not in having):
// SELECT 1 AS A FROM DUAL ORDER BY -A // SELECT 1 AS A FROM DUAL ORDER BY -A
boolean isAlias = false; boolean isAlias = false;
idx = expressions.size(); int idx = expressions.size();
if(e instanceof ExpressionColumn) { if(e instanceof ExpressionColumn) {
ExpressionColumn exprCol = (ExpressionColumn)e; ExpressionColumn exprCol = (ExpressionColumn)e;
String alias = exprCol.getOriginalAliasName(); String alias = exprCol.getOriginalAliasName();
...@@ -154,6 +155,16 @@ public abstract class Query extends Prepared { ...@@ -154,6 +155,16 @@ public abstract class Query extends Prepared {
break; break;
} }
} }
} else {
String s = e.getSQL();
for(int j=0; j<expressionSQL.size(); j++) {
String s2 = (String) expressionSQL.get(j);
if(s2.equals(s)) {
idx = j;
isAlias = true;
break;
}
}
} }
if(!isAlias) { if(!isAlias) {
if(mustBeInResult) { if(mustBeInResult) {
...@@ -161,14 +172,44 @@ public abstract class Query extends Prepared { ...@@ -161,14 +172,44 @@ public abstract class Query extends Prepared {
} }
expressions.add(e); expressions.add(e);
} }
o.expression = null;
o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
}
}
public SortOrder prepareOrder(ObjectArray expressions, ObjectArray orderList) throws SQLException {
int[] index = new int[orderList.size()];
int[] sortType = new int[orderList.size()];
int originalLength = expressions.size();
for(int i=0; i<orderList.size(); i++) {
SelectOrderBy o = (SelectOrderBy) orderList.get(i);
int idx;
boolean reverse = false;
if(o.expression != null) {
throw Message.getInternalError();
}
Expression expr = o.columnIndexExpr;
Value v = expr.getValue(null);
if(v == ValueNull.INSTANCE) {
// parameter not yet set - order by first column
idx = 0;
} else { } else {
idx = o.column; idx = v.getInt();
if(idx >= originalLength) { if(idx < 0) {
reverse = true;
idx = -idx;
}
idx -= 1;
if(idx < 0 || idx >= originalLength) {
throw Message.getSQLException(Message.ORDER_BY_NOT_IN_RESULT, "index " + idx); throw Message.getSQLException(Message.ORDER_BY_NOT_IN_RESULT, "index " + idx);
} }
} }
index[i] = idx; index[i] = idx;
int type = o.descending ? SortOrder.DESCENDING : SortOrder.ASCENDING; boolean desc = o.descending;
if(reverse) {
desc = !desc;
}
int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
if(o.nullsFirst) { if(o.nullsFirst) {
type += SortOrder.NULLS_FIRST; type += SortOrder.NULLS_FIRST;
} else if(o.nullsLast) { } else if(o.nullsLast) {
......
...@@ -79,7 +79,7 @@ public class ScriptBase extends Prepared implements DataHandler { ...@@ -79,7 +79,7 @@ public class ScriptBase extends Prepared implements DataHandler {
byte[] magic = Database.getMagic(true); byte[] magic = Database.getMagic(true);
Database db = session.getDatabase(); Database db = session.getDatabase();
// script files are always in text format // script files are always in text format
store = FileStore.open(db, fileName, magic, cipher, key); store = FileStore.open(db, fileName, "rw", magic, cipher, key);
store.setCheckedWriting(false); store.setCheckedWriting(false);
store.init(); store.init();
} }
...@@ -153,7 +153,7 @@ public class ScriptBase extends Prepared implements DataHandler { ...@@ -153,7 +153,7 @@ public class ScriptBase extends Prepared implements DataHandler {
return null; return null;
} }
public FileStore openFile(String name, boolean mustExist) throws SQLException { public FileStore openFile(String name, String mode, boolean mustExist) throws SQLException {
return null; return null;
} }
......
...@@ -207,7 +207,7 @@ public class Select extends Query { ...@@ -207,7 +207,7 @@ public class Select extends Query {
for(int i=0; i<indexes.length; i++) { for(int i=0; i<indexes.length; i++) {
int idx = indexes[i]; int idx = indexes[i];
if(idx < 0 || idx >= expressions.size()) { if(idx < 0 || idx >= expressions.size()) {
throw Message.getInvalidValueException("order by", ""+idx); throw Message.getInvalidValueException("" + (idx+1), "ORDER BY");
} }
Expression expr = (Expression) expressions.get(idx); Expression expr = (Expression) expressions.get(idx);
expr = expr.getNonAliasExpression(); expr = expr.getNonAliasExpression();
...@@ -384,12 +384,22 @@ public class Select extends Query { ...@@ -384,12 +384,22 @@ public class Select extends Query {
checkInit = true; checkInit = true;
expandColumnList(); expandColumnList();
visibleColumnCount = expressions.size(); visibleColumnCount = expressions.size();
ObjectArray expressionSQL;
if(orderList != null || group != null) {
expressionSQL = new ObjectArray();
for(int i=0; i<expressions.size(); i++) {
Expression expr = (Expression) expressions.get(i);
expr = expr.getNonAliasExpression();
String sql = expr.getSQL();
expressionSQL.add(sql);
}
} else {
expressionSQL = null;
}
if(orderList != null) { if(orderList != null) {
sort = initOrder(expressions, orderList, visibleColumnCount, distinct); initOrder(expressions, expressionSQL, orderList, visibleColumnCount, distinct);
orderList = null;
} }
distinctColumnCount = expressions.size(); distinctColumnCount = expressions.size();
if(having != null) { if(having != null) {
expressions.add(having); expressions.add(having);
havingIndex = expressions.size()-1; havingIndex = expressions.size()-1;
...@@ -401,13 +411,6 @@ public class Select extends Query { ...@@ -401,13 +411,6 @@ public class Select extends Query {
// first visible columns, then order by, then having, and then group by at the end // first visible columns, then order by, then having, and then group by at the end
if(group != null) { if(group != null) {
groupIndex = new int[group.size()]; groupIndex = new int[group.size()];
ObjectArray expressionSQL = new ObjectArray();
for(int i=0; i<expressions.size(); i++) {
Expression expr = (Expression) expressions.get(i);
expr = expr.getNonAliasExpression();
String sql = expr.getSQL();
expressionSQL.add(sql);
}
for(int i=0; i<group.size(); i++) { for(int i=0; i<group.size(); i++) {
Expression expr = (Expression) group.get(i); Expression expr = (Expression) group.get(i);
String sql = expr.getSQL(); String sql = expr.getSQL();
...@@ -455,6 +458,10 @@ public class Select extends Query { ...@@ -455,6 +458,10 @@ public class Select extends Query {
throw Message.getInternalError("already prepared"); throw Message.getInternalError("already prepared");
} }
isPrepared = true; isPrepared = true;
if(orderList != null) {
sort = prepareOrder(expressions, orderList);
orderList = null;
}
for(int i=0; i<expressions.size(); i++) { for(int i=0; i<expressions.size(); i++) {
Expression e = (Expression) expressions.get(i); Expression e = (Expression) expressions.get(i);
expressions.set(i, e.optimize(session)); expressions.set(i, e.optimize(session));
...@@ -535,7 +542,7 @@ public class Select extends Query { ...@@ -535,7 +542,7 @@ public class Select extends Query {
return cost; return cost;
} }
public String getPlan() { public String getPlanSQL() {
if(topTableFilter == null) { if(topTableFilter == null) {
return sql; return sql;
} }
...@@ -663,6 +670,7 @@ public class Select extends Query { ...@@ -663,6 +670,7 @@ public class Select extends Query {
public void addGlobalCondition(Expression expr, int columnId, int comparisonType) throws SQLException { public void addGlobalCondition(Expression expr, int columnId, int comparisonType) throws SQLException {
Expression col = (Expression)expressions.get(columnId); Expression col = (Expression)expressions.get(columnId);
col = col.getNonAliasExpression();
Expression comp = new Comparison(session, comparisonType, col, expr); Expression comp = new Comparison(session, comparisonType, col, expr);
comp = comp.optimize(session); comp = comp.optimize(session);
if(isGroupQuery) { if(isGroupQuery) {
......
...@@ -11,7 +11,7 @@ import org.h2.expression.Expression; ...@@ -11,7 +11,7 @@ import org.h2.expression.Expression;
*/ */
public class SelectOrderBy { public class SelectOrderBy {
public Expression expression; public Expression expression;
public int column; public Expression columnIndexExpr;
public boolean descending; public boolean descending;
public boolean nullsFirst; public boolean nullsFirst;
public boolean nullsLast; public boolean nullsLast;
......
...@@ -188,7 +188,8 @@ public class SelectUnion extends Query { ...@@ -188,7 +188,8 @@ public class SelectUnion extends Query {
expressions.add(e); expressions.add(e);
} }
if(orderList != null) { if(orderList != null) {
sort = initOrder(expressions, orderList, getColumnCount(), true); initOrder(expressions, null, orderList, getColumnCount(), true);
sort = prepareOrder(expressions, orderList);
orderList = null; orderList = null;
} }
} }
...@@ -249,10 +250,10 @@ public class SelectUnion extends Query { ...@@ -249,10 +250,10 @@ public class SelectUnion extends Query {
} }
} }
public String getPlan() { public String getPlanSQL() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append('('); buff.append('(');
buff.append(left.getPlan()); buff.append(left.getPlanSQL());
buff.append(") "); buff.append(") ");
switch(unionType) { switch(unionType) {
case UNION_ALL: case UNION_ALL:
...@@ -271,7 +272,7 @@ public class SelectUnion extends Query { ...@@ -271,7 +272,7 @@ public class SelectUnion extends Query {
throw Message.getInternalError("type="+unionType); throw Message.getInternalError("type="+unionType);
} }
buff.append('('); buff.append('(');
buff.append(right.getPlan()); buff.append(right.getPlanSQL());
buff.append(')'); buff.append(')');
Expression[] exprList = new Expression[expressions.size()]; Expression[] exprList = new Expression[expressions.size()];
expressions.toArray(exprList); expressions.toArray(exprList);
......
...@@ -113,7 +113,7 @@ public class Update extends Prepared { ...@@ -113,7 +113,7 @@ public class Update extends Prepared {
return newRows.size(); return newRows.size();
} }
public String getPlan() { public String getPlanSQL() {
StringBuffer buff = new StringBuffer(); StringBuffer buff = new StringBuffer();
buff.append("UPDATE "); buff.append("UPDATE ");
buff.append(tableFilter.getPlanSQL(false)); buff.append(tableFilter.getPlanSQL(false));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论