Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 442x 442x 442x 442x 442x 442x 34x 34x 442x 442x 1x 1x 1x 1x 52x 52x 3x 3x 52x 63x 11x 11x 63x 46x 46x 46x 46x 46x 18x 18x 63x 6x 6x 3x 3x 6x 63x 28x 28x | import * as Expressions from "../2_statements/expressions";
import {ExpressionNode, TokenNode} from "../nodes";
/** The number of NOT in front of a relational expression or a parenthesized condition, "NOT NOT a = 1" gives 2 */
export function countLeadingNot(node: ExpressionNode): number {
const children = node.getChildren();
let count = 0;
while (count < children.length
&& children[count] instanceof TokenNode
&& children[count].getFirstToken().getStr().toUpperCase() === "NOT") {
count++;
}
return count;
}
/** True if a conjunct of the WHERE condition reachable through AND only, ie. not under OR or an odd number of NOT,
* compares the key component with = or IS INITIAL. Without such a comparison of its first component,
* a sorted key cannot be used to optimize the WHERE condition */
export function hasKeyCompare(cond: ExpressionNode, component: string): boolean {
if (cond.findDirectTokenByText("OR") !== undefined) {
return false;
}
for (const child of cond.getChildren()) {
if (!(child instanceof ExpressionNode) || countLeadingNot(child) % 2 === 1) {
continue;
}
if (child.get() instanceof Expressions.ComponentCompare) {
const concat = child.getChildren().slice(countLeadingNot(child))
.map(c => c.concatTokens()).join(" ").toUpperCase().replace(/\s+/g, " ");
if (concat.startsWith(component + " = ")
|| concat.startsWith(component + " EQ ")
|| concat === component + " IS INITIAL") {
return true;
}
} else if (child.get() instanceof Expressions.ComponentCondSub) {
const sub = child.findDirectExpression(Expressions.ComponentCond);
if (sub !== undefined && hasKeyCompare(sub, component)) {
return true;
}
}
}
return false;
}
|