forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLUnaryPrefixOperation.java
More file actions
80 lines (68 loc) · 2.51 KB
/
MySQLUnaryPrefixOperation.java
File metadata and controls
80 lines (68 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package sqlancer.mysql.ast;
import sqlancer.IgnoreMeException;
import sqlancer.Randomly;
import sqlancer.common.ast.BinaryOperatorNode.Operator;
import sqlancer.common.ast.UnaryOperatorNode;
import sqlancer.mysql.ast.MySQLUnaryPrefixOperation.MySQLUnaryPrefixOperator;
public class MySQLUnaryPrefixOperation extends UnaryOperatorNode<MySQLExpression, MySQLUnaryPrefixOperator>
implements MySQLExpression {
public enum MySQLUnaryPrefixOperator implements Operator {
NOT("!", "NOT") {
@Override
public MySQLConstant applyNotNull(MySQLConstant expr) {
return MySQLConstant.createIntConstant(expr.asBooleanNotNull() ? 0 : 1);
}
},
PLUS("+") {
@Override
public MySQLConstant applyNotNull(MySQLConstant expr) {
return expr;
}
},
MINUS("-") {
@Override
public MySQLConstant applyNotNull(MySQLConstant expr) {
if (expr.isString()) {
// TODO: implement floating points
throw new IgnoreMeException();
} else if (expr.isInt()) {
if (!expr.isSigned()) {
// TODO
throw new IgnoreMeException();
}
return MySQLConstant.createIntConstant(-expr.getInt());
} else {
throw new AssertionError(expr);
}
}
};
private String[] textRepresentations;
MySQLUnaryPrefixOperator(String... textRepresentations) {
this.textRepresentations = textRepresentations.clone();
}
public abstract MySQLConstant applyNotNull(MySQLConstant expr);
public static MySQLUnaryPrefixOperator getRandom() {
return Randomly.fromOptions(values());
}
@Override
public String getTextRepresentation() {
return Randomly.fromOptions(textRepresentations);
}
}
public MySQLUnaryPrefixOperation(MySQLExpression expr, MySQLUnaryPrefixOperator op) {
super(expr, op);
}
@Override
public MySQLConstant getExpectedValue() {
MySQLConstant subExprVal = expr.getExpectedValue();
if (subExprVal.isNull()) {
return MySQLConstant.createNullConstant();
} else {
return op.applyNotNull(subExprVal);
}
}
@Override
public OperatorKind getOperatorKind() {
return OperatorKind.PREFIX;
}
}