mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
custom SQL condition #369
This commit is contained in:
parent
a89c6810aa
commit
973f64f4d7
@ -190,6 +190,16 @@ const unaryCondition = conditionType => () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sqlTemplate = templateSql => {
|
||||||
|
return {
|
||||||
|
conditionType: 'rawTemplate',
|
||||||
|
templateSql,
|
||||||
|
expr: {
|
||||||
|
exprType: 'placeholder',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const createParser = () => {
|
const createParser = () => {
|
||||||
const langDef = {
|
const langDef = {
|
||||||
comma: () => word(','),
|
comma: () => word(','),
|
||||||
@ -198,6 +208,11 @@ const createParser = () => {
|
|||||||
notNull: r => r.not.then(r.null).map(unaryCondition('isNotNull')),
|
notNull: r => r.not.then(r.null).map(unaryCondition('isNotNull')),
|
||||||
null: () => word('NULL').map(unaryCondition('isNull')),
|
null: () => word('NULL').map(unaryCondition('isNull')),
|
||||||
|
|
||||||
|
sql: () =>
|
||||||
|
token(P.regexp(/\{(.*?)\}/, 1))
|
||||||
|
.map(sqlTemplate)
|
||||||
|
.desc('sql literal'),
|
||||||
|
|
||||||
yearNum: () => P.regexp(/\d\d\d\d/).map(yearCondition()),
|
yearNum: () => P.regexp(/\d\d\d\d/).map(yearCondition()),
|
||||||
yearMonthNum: () => P.regexp(/\d\d\d\d-\d\d?/).map(yearMonthCondition()),
|
yearMonthNum: () => P.regexp(/\d\d\d\d-\d\d?/).map(yearMonthCondition()),
|
||||||
yearMonthDayNum: () => P.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayCondition()),
|
yearMonthDayNum: () => P.regexp(/\d\d\d\d-\d\d?-\d\d?/).map(yearMonthDayCondition()),
|
||||||
@ -282,7 +297,8 @@ const createParser = () => {
|
|||||||
r.le,
|
r.le,
|
||||||
r.lt,
|
r.lt,
|
||||||
r.ge,
|
r.ge,
|
||||||
r.gt
|
r.gt,
|
||||||
|
r.sql
|
||||||
).trim(whitespace),
|
).trim(whitespace),
|
||||||
factor: r => r.element.sepBy(whitespace).map(compoudCondition('$and')),
|
factor: r => r.element.sepBy(whitespace).map(compoudCondition('$and')),
|
||||||
list: r => r.factor.sepBy(r.comma).map(compoudCondition('$or')),
|
list: r => r.factor.sepBy(r.comma).map(compoudCondition('$or')),
|
||||||
|
@ -68,6 +68,16 @@ const negateCondition = condition => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sqlTemplate = templateSql => {
|
||||||
|
return {
|
||||||
|
conditionType: 'rawTemplate',
|
||||||
|
templateSql,
|
||||||
|
expr: {
|
||||||
|
exprType: 'placeholder',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const createParser = (filterType: FilterType) => {
|
const createParser = (filterType: FilterType) => {
|
||||||
const langDef = {
|
const langDef = {
|
||||||
string1: () =>
|
string1: () =>
|
||||||
@ -97,6 +107,11 @@ const createParser = (filterType: FilterType) => {
|
|||||||
|
|
||||||
noQuotedString: () => P.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
|
noQuotedString: () => P.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
|
||||||
|
|
||||||
|
sql: () =>
|
||||||
|
token(P.regexp(/\{(.*?)\}/, 1))
|
||||||
|
.map(sqlTemplate)
|
||||||
|
.desc('sql literal'),
|
||||||
|
|
||||||
value: r => P.alt(...allowedValues.map(x => r[x])),
|
value: r => P.alt(...allowedValues.map(x => r[x])),
|
||||||
valueTestEq: r => r.value.map(binaryCondition('=')),
|
valueTestEq: r => r.value.map(binaryCondition('=')),
|
||||||
valueTestStr: r => r.value.map(likeCondition('like', '%#VALUE#%')),
|
valueTestStr: r => r.value.map(likeCondition('like', '%#VALUE#%')),
|
||||||
@ -139,7 +154,7 @@ const createParser = (filterType: FilterType) => {
|
|||||||
allowedValues.push('string1Num', 'string2Num', 'number');
|
allowedValues.push('string1Num', 'string2Num', 'number');
|
||||||
}
|
}
|
||||||
|
|
||||||
const allowedElements = ['null', 'notNull', 'eq', 'ne', 'ne2'];
|
const allowedElements = ['null', 'notNull', 'eq', 'ne', 'ne2', 'sql'];
|
||||||
if (filterType == 'number' || filterType == 'datetime' || filterType == 'eval') {
|
if (filterType == 'number' || filterType == 'datetime' || filterType == 'eval') {
|
||||||
allowedElements.push('le', 'ge', 'lt', 'gt');
|
allowedElements.push('le', 'ge', 'lt', 'gt');
|
||||||
}
|
}
|
||||||
|
@ -72,5 +72,15 @@ export function dumpSqlCondition(dmp: SqlDumper, condition: Condition) {
|
|||||||
dumpSqlExpression(dmp, condition.expr);
|
dumpSqlExpression(dmp, condition.expr);
|
||||||
dmp.put(' ^in (%,v)', condition.values);
|
dmp.put(' ^in (%,v)', condition.values);
|
||||||
break;
|
break;
|
||||||
|
case 'rawTemplate':
|
||||||
|
let was = false;
|
||||||
|
for (const item of condition.templateSql.split('$$')) {
|
||||||
|
if (was) {
|
||||||
|
dumpSqlExpression(dmp, condition.expr);
|
||||||
|
}
|
||||||
|
dmp.putRaw(item);
|
||||||
|
was = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,12 @@ export interface InCondition {
|
|||||||
values: any[];
|
values: any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RawTemplateCondition {
|
||||||
|
conditionType: 'rawTemplate';
|
||||||
|
templateSql: string;
|
||||||
|
expr: Expression;
|
||||||
|
}
|
||||||
|
|
||||||
export type Condition =
|
export type Condition =
|
||||||
| BinaryCondition
|
| BinaryCondition
|
||||||
| NotCondition
|
| NotCondition
|
||||||
@ -114,7 +120,8 @@ export type Condition =
|
|||||||
| ExistsCondition
|
| ExistsCondition
|
||||||
| NotExistsCondition
|
| NotExistsCondition
|
||||||
| BetweenCondition
|
| BetweenCondition
|
||||||
| InCondition;
|
| InCondition
|
||||||
|
| RawTemplateCondition;
|
||||||
|
|
||||||
export interface Source {
|
export interface Source {
|
||||||
name?: NamedObjectInfo;
|
name?: NamedObjectInfo;
|
||||||
|
Loading…
Reference in New Issue
Block a user