mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-17 02:51:36 -05:00
Add >= and <= operators for context key expressions. (#2160)
This commit is contained in:
@@ -14,7 +14,11 @@ export enum ContextKeyExprType {
|
|||||||
Equals = 3,
|
Equals = 3,
|
||||||
NotEquals = 4,
|
NotEquals = 4,
|
||||||
And = 5,
|
And = 5,
|
||||||
Regex = 6
|
Regex = 6,
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
GreaterThanEquals = 7,
|
||||||
|
LessThanEquals = 8
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class ContextKeyExpr {
|
export abstract class ContextKeyExpr {
|
||||||
@@ -43,6 +47,15 @@ export abstract class ContextKeyExpr {
|
|||||||
return new ContextKeyAndExpr(expr);
|
return new ContextKeyAndExpr(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
public static greaterThanEquals(key: string, value: any): ContextKeyExpr {
|
||||||
|
return new ContextKeyGreaterThanEqualsExpr(key, value);
|
||||||
|
}
|
||||||
|
public static lessThanEquals(key: string, value: any): ContextKeyExpr {
|
||||||
|
return new ContextKeyLessThanEqualsExpr(key, value);
|
||||||
|
}
|
||||||
|
//
|
||||||
|
|
||||||
public static deserialize(serialized: string): ContextKeyExpr {
|
public static deserialize(serialized: string): ContextKeyExpr {
|
||||||
if (!serialized) {
|
if (!serialized) {
|
||||||
return null;
|
return null;
|
||||||
@@ -71,6 +84,17 @@ export abstract class ContextKeyExpr {
|
|||||||
return new ContextKeyRegexExpr(pieces[0].trim(), this._deserializeRegexValue(pieces[1]));
|
return new ContextKeyRegexExpr(pieces[0].trim(), this._deserializeRegexValue(pieces[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
if (serializedOne.indexOf('>=') >= 0) {
|
||||||
|
let pieces = serializedOne.split('>=');
|
||||||
|
return new ContextKeyGreaterThanEqualsExpr(pieces[0].trim(), this._deserializeValue(pieces[1]));
|
||||||
|
}
|
||||||
|
if (serializedOne.indexOf('<=') >= 0) {
|
||||||
|
let pieces = serializedOne.split('<=');
|
||||||
|
return new ContextKeyLessThanEqualsExpr(pieces[0].trim(), this._deserializeValue(pieces[1]));
|
||||||
|
}
|
||||||
|
//
|
||||||
|
|
||||||
if (/^\!\s*/.test(serializedOne)) {
|
if (/^\!\s*/.test(serializedOne)) {
|
||||||
return new ContextKeyNotExpr(serializedOne.substr(1).trim());
|
return new ContextKeyNotExpr(serializedOne.substr(1).trim());
|
||||||
}
|
}
|
||||||
@@ -146,6 +170,12 @@ function cmp(a: ContextKeyExpr, b: ContextKeyExpr): number {
|
|||||||
return (<ContextKeyNotEqualsExpr>a).cmp(<ContextKeyNotEqualsExpr>b);
|
return (<ContextKeyNotEqualsExpr>a).cmp(<ContextKeyNotEqualsExpr>b);
|
||||||
case ContextKeyExprType.Regex:
|
case ContextKeyExprType.Regex:
|
||||||
return (<ContextKeyRegexExpr>a).cmp(<ContextKeyRegexExpr>b);
|
return (<ContextKeyRegexExpr>a).cmp(<ContextKeyRegexExpr>b);
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
case ContextKeyExprType.GreaterThanEquals:
|
||||||
|
return (<ContextKeyGreaterThanEqualsExpr>a).cmp(<ContextKeyGreaterThanEqualsExpr>b);
|
||||||
|
case ContextKeyExprType.LessThanEquals:
|
||||||
|
return (<ContextKeyLessThanEqualsExpr>a).cmp(<ContextKeyLessThanEqualsExpr>b);
|
||||||
|
//
|
||||||
default:
|
default:
|
||||||
throw new Error('Unknown ContextKeyExpr!');
|
throw new Error('Unknown ContextKeyExpr!');
|
||||||
}
|
}
|
||||||
@@ -504,6 +534,108 @@ export class ContextKeyAndExpr implements ContextKeyExpr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
export class ContextKeyGreaterThanEqualsExpr implements ContextKeyExpr {
|
||||||
|
constructor(private key: string, private value: any) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): ContextKeyExprType {
|
||||||
|
return ContextKeyExprType.GreaterThanEquals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public cmp(other: ContextKeyGreaterThanEqualsExpr): number {
|
||||||
|
if (this.key < other.key) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (this.key > other.key) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (this.value < other.value) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (this.value > other.value) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public equals(other: ContextKeyExpr): boolean {
|
||||||
|
if (other instanceof ContextKeyGreaterThanEqualsExpr) {
|
||||||
|
return (this.key === other.key && this.value === other.value);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public evaluate(context: IContext): boolean {
|
||||||
|
let keyInt = parseFloat(context.getValue(this.key));
|
||||||
|
let valueInt = parseFloat(this.value);
|
||||||
|
return (keyInt >= valueInt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public normalize(): ContextKeyExpr {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public serialize(): string {
|
||||||
|
return this.key + ' >= \'' + this.value + '\'';
|
||||||
|
}
|
||||||
|
|
||||||
|
public keys(): string[] {
|
||||||
|
return [this.key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
export class ContextKeyLessThanEqualsExpr implements ContextKeyExpr {
|
||||||
|
constructor(private key: string, private value: any) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public getType(): ContextKeyExprType {
|
||||||
|
return ContextKeyExprType.LessThanEquals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public cmp(other: ContextKeyLessThanEqualsExpr): number {
|
||||||
|
if (this.key < other.key) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (this.key > other.key) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (this.value < other.value) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (this.value > other.value) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public equals(other: ContextKeyExpr): boolean {
|
||||||
|
if (other instanceof ContextKeyLessThanEqualsExpr) {
|
||||||
|
return (this.key === other.key && this.value === other.value);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public evaluate(context: IContext): boolean {
|
||||||
|
let keyInt = parseFloat(context.getValue(this.key));
|
||||||
|
let valueInt = parseFloat(this.value);
|
||||||
|
return (keyInt <= valueInt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public normalize(): ContextKeyExpr {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public serialize(): string {
|
||||||
|
return this.key + ' <= \'' + this.value + '\'';
|
||||||
|
}
|
||||||
|
|
||||||
|
public keys(): string[] {
|
||||||
|
return [this.key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class RawContextKey<T> extends ContextKeyDefinedExpr {
|
export class RawContextKey<T> extends ContextKeyDefinedExpr {
|
||||||
|
|
||||||
private _defaultValue: T;
|
private _defaultValue: T;
|
||||||
|
|||||||
@@ -28,18 +28,29 @@ suite('ContextKeyExpr', () => {
|
|||||||
ContextKeyExpr.notEquals('c1', 'cc1'),
|
ContextKeyExpr.notEquals('c1', 'cc1'),
|
||||||
ContextKeyExpr.notEquals('c2', 'cc2'),
|
ContextKeyExpr.notEquals('c2', 'cc2'),
|
||||||
ContextKeyExpr.not('d1'),
|
ContextKeyExpr.not('d1'),
|
||||||
ContextKeyExpr.not('d2')
|
ContextKeyExpr.not('d2'),
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
ContextKeyExpr.greaterThanEquals('e1', 'ee1'),
|
||||||
|
ContextKeyExpr.greaterThanEquals('e2', 'ee2'),
|
||||||
|
ContextKeyExpr.lessThanEquals('f1', 'ff1'),
|
||||||
|
ContextKeyExpr.lessThanEquals('f2', 'ff2')
|
||||||
|
//
|
||||||
);
|
);
|
||||||
let b = ContextKeyExpr.and(
|
let b = ContextKeyExpr.and(
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
ContextKeyExpr.greaterThanEquals('e2', 'ee2'),
|
||||||
ContextKeyExpr.equals('b2', 'bb2'),
|
ContextKeyExpr.equals('b2', 'bb2'),
|
||||||
ContextKeyExpr.notEquals('c1', 'cc1'),
|
ContextKeyExpr.notEquals('c1', 'cc1'),
|
||||||
ContextKeyExpr.not('d1'),
|
ContextKeyExpr.not('d1'),
|
||||||
|
ContextKeyExpr.lessThanEquals('f1', 'ff1'),
|
||||||
ContextKeyExpr.regex('d4', /\*\*3*/),
|
ContextKeyExpr.regex('d4', /\*\*3*/),
|
||||||
|
ContextKeyExpr.greaterThanEquals('e1', 'ee1'),
|
||||||
ContextKeyExpr.notEquals('c2', 'cc2'),
|
ContextKeyExpr.notEquals('c2', 'cc2'),
|
||||||
ContextKeyExpr.has('a2'),
|
ContextKeyExpr.has('a2'),
|
||||||
ContextKeyExpr.equals('b1', 'bb1'),
|
ContextKeyExpr.equals('b1', 'bb1'),
|
||||||
ContextKeyExpr.regex('d3', /d.*/),
|
ContextKeyExpr.regex('d3', /d.*/),
|
||||||
ContextKeyExpr.has('a1'),
|
ContextKeyExpr.has('a1'),
|
||||||
|
ContextKeyExpr.lessThanEquals('f2', 'ff2'),
|
||||||
ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)),
|
ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)),
|
||||||
ContextKeyExpr.not('d2')
|
ContextKeyExpr.not('d2')
|
||||||
);
|
);
|
||||||
@@ -82,6 +93,10 @@ suite('ContextKeyExpr', () => {
|
|||||||
testExpression('!' + expr, !value);
|
testExpression('!' + expr, !value);
|
||||||
testExpression(expr + ' =~ /d.*/', /d.*/.test(value));
|
testExpression(expr + ' =~ /d.*/', /d.*/.test(value));
|
||||||
testExpression(expr + ' =~ /D/i', /D/i.test(value));
|
testExpression(expr + ' =~ /D/i', /D/i.test(value));
|
||||||
|
// {{SQL CARBON EDIT}}
|
||||||
|
testExpression(expr + ' >= 10', parseFloat(value) >= 10);
|
||||||
|
testExpression(expr + ' <= 10', parseFloat(value) <= 10);
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
testBatch('a', true);
|
testBatch('a', true);
|
||||||
|
|||||||
Reference in New Issue
Block a user