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

--no commit message

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