Unverified 提交 12ec6d9d authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1419 from katzyn/misc

Assorted minor changes
...@@ -1397,18 +1397,17 @@ public class Parser { ...@@ -1397,18 +1397,17 @@ public class Parser {
private Prepared parseMerge() { private Prepared parseMerge() {
Merge command = new Merge(session);
currentPrepared = command;
int start = lastParseIndex; int start = lastParseIndex;
read("INTO"); read("INTO");
List<String> excludeIdentifiers = Arrays.asList("USING", "KEY", "VALUES"); List<String> excludeIdentifiers = Arrays.asList("USING", "KEY", "VALUES");
TableFilter targetTableFilter = readSimpleTableFilter(0, excludeIdentifiers); TableFilter targetTableFilter = readSimpleTableFilter(0, excludeIdentifiers);
command.setTargetTableFilter(targetTableFilter);
Table table = command.getTargetTable();
if (readIf("USING")) { if (readIf("USING")) {
return parseMergeUsing(command, start); return parseMergeUsing(targetTableFilter, start);
} }
Merge command = new Merge(session);
currentPrepared = command;
command.setTargetTableFilter(targetTableFilter);
Table table = command.getTargetTable();
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
if (isSelect()) { if (isSelect()) {
command.setQuery(parseSelect()); command.setQuery(parseSelect());
...@@ -1434,8 +1433,8 @@ public class Parser { ...@@ -1434,8 +1433,8 @@ public class Parser {
return command; return command;
} }
private MergeUsing parseMergeUsing(Merge oldCommand, int start) { private MergeUsing parseMergeUsing(TableFilter targetTableFilter, int start) {
MergeUsing command = new MergeUsing(oldCommand); MergeUsing command = new MergeUsing(session, targetTableFilter);
currentPrepared = command; currentPrepared = command;
if (readIf(OPEN_PAREN)) { if (readIf(OPEN_PAREN)) {
......
...@@ -15,6 +15,7 @@ import org.h2.api.Trigger; ...@@ -15,6 +15,7 @@ import org.h2.api.Trigger;
import org.h2.command.CommandInterface; import org.h2.command.CommandInterface;
import org.h2.command.Prepared; import org.h2.command.Prepared;
import org.h2.engine.Right; import org.h2.engine.Right;
import org.h2.engine.Session;
import org.h2.expression.ConditionAndOr; import org.h2.expression.ConditionAndOr;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn; import org.h2.expression.ExpressionColumn;
...@@ -32,7 +33,7 @@ import org.h2.value.Value; ...@@ -32,7 +33,7 @@ import org.h2.value.Value;
/** /**
* This class represents the statement syntax * This class represents the statement syntax
* MERGE table alias USING... * MERGE INTO table alias USING...
* *
* It does not replace the existing MERGE INTO... KEYS... form. * It does not replace the existing MERGE INTO... KEYS... form.
* *
...@@ -119,12 +120,10 @@ public class MergeUsing extends Prepared { ...@@ -119,12 +120,10 @@ public class MergeUsing extends Prepared {
private int sourceQueryRowNumber; private int sourceQueryRowNumber;
public MergeUsing(Merge merge) { public MergeUsing(Session session, TableFilter targetTableFilter) {
super(merge.getSession()); super(session);
this.targetTable = targetTableFilter.getTable();
// bring across only the already parsed data from Merge... this.targetTableFilter = targetTableFilter;
this.targetTable = merge.getTargetTable();
this.targetTableFilter = merge.getTargetTableFilter();
} }
@Override @Override
......
...@@ -577,14 +577,13 @@ public abstract class Query extends Prepared { ...@@ -577,14 +577,13 @@ public abstract class Query extends Prepared {
/** /**
* Create a {@link SortOrder} object given the list of {@link SelectOrderBy} * Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
* objects. The expression list is extended if necessary. * objects.
* *
* @param orderList a list of {@link SelectOrderBy} elements * @param orderList a list of {@link SelectOrderBy} elements
* @param expressionCount the number of columns in the query * @param expressionCount the number of columns in the query
* @return the {@link SortOrder} object * @return the {@link SortOrder} object
*/ */
public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, int expressionCount) {
int expressionCount) {
int size = orderList.size(); int size = orderList.size();
int[] index = new int[size]; int[] index = new int[size];
int[] sortType = new int[size]; int[] sortType = new int[size];
...@@ -592,8 +591,7 @@ public abstract class Query extends Prepared { ...@@ -592,8 +591,7 @@ public abstract class Query extends Prepared {
SelectOrderBy o = orderList.get(i); SelectOrderBy o = orderList.get(i);
int idx; int idx;
boolean reverse = false; boolean reverse = false;
Expression expr = o.columnIndexExpr; Value v = o.columnIndexExpr.getValue(null);
Value v = expr.getValue(null);
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
// parameter not yet set - order by first column // parameter not yet set - order by first column
idx = 0; idx = 0;
...@@ -612,11 +610,7 @@ public abstract class Query extends Prepared { ...@@ -612,11 +610,7 @@ public abstract class Query extends Prepared {
int type = o.sortType; int type = o.sortType;
if (reverse) { if (reverse) {
// TODO NULLS FIRST / LAST should be inverted too? // TODO NULLS FIRST / LAST should be inverted too?
if ((type & SortOrder.DESCENDING) != 0) { type ^= SortOrder.DESCENDING;
type &= ~SortOrder.DESCENDING;
} else {
type |= SortOrder.DESCENDING;
}
} }
sortType[i] = type; sortType[i] = type;
} }
......
...@@ -116,7 +116,7 @@ public class ConditionInParameter extends Condition { ...@@ -116,7 +116,7 @@ public class ConditionInParameter extends Condition {
@Override @Override
public Expression optimize(Session session) { public Expression optimize(Session session) {
left = left.optimize(session); left = left.optimize(session);
if (left.isConstant() && left == ValueExpression.getNull()) { if (left == ValueExpression.getNull()) {
return left; return left;
} }
return this; return this;
......
...@@ -104,7 +104,7 @@ public class Transfer { ...@@ -104,7 +104,7 @@ public class Transfer {
* @return the value * @return the value
*/ */
public boolean readBoolean() throws IOException { public boolean readBoolean() throws IOException {
return in.readByte() == 1; return in.readByte() != 0;
} }
/** /**
...@@ -217,11 +217,8 @@ public class Transfer { ...@@ -217,11 +217,8 @@ public class Transfer {
if (s == null) { if (s == null) {
out.writeInt(-1); out.writeInt(-1);
} else { } else {
int len = s.length(); out.writeInt(s.length());
out.writeInt(len); out.writeChars(s);
for (int i = 0; i < len; i++) {
out.writeChar(s.charAt(i));
}
} }
return this; return this;
} }
......
...@@ -153,8 +153,7 @@ public class ValueBytes extends Value { ...@@ -153,8 +153,7 @@ public class ValueBytes extends Value {
return this; return this;
} }
int len = MathUtils.convertLongToInt(precision); int len = MathUtils.convertLongToInt(precision);
byte[] buff = Arrays.copyOf(value, len); return getNoCopy(Arrays.copyOf(value, len));
return get(buff);
} }
} }
...@@ -289,3 +289,43 @@ SELECT ID FROM TEST FETCH NEXT ROW ONLY LIMIT 1; ...@@ -289,3 +289,43 @@ SELECT ID FROM TEST FETCH NEXT ROW ONLY LIMIT 1;
DROP TABLE TEST; DROP TABLE TEST;
> ok > ok
-- ORDER BY with parameter
CREATE TABLE TEST(A INT, B INT);
> ok
INSERT INTO TEST VALUES (1, 1), (1, 2), (2, 1), (2, 2);
> update count: 4
SELECT * FROM TEST ORDER BY ?, ? FETCH FIRST ROW ONLY;
{
1, 2
> A B
> - -
> 1 1
> rows (ordered): 1
-1, 2
> A B
> - -
> 2 1
> rows (ordered): 1
1, -2
> A B
> - -
> 1 2
> rows (ordered): 1
-1, -2
> A B
> - -
> 2 2
> rows (ordered): 1
2, -1
> A B
> - -
> 2 1
> rows (ordered): 1
}
> update count: 0
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论