mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 16:13:58 +00:00
mongodb - filter by objectId imrpoved
This commit is contained in:
@@ -52,6 +52,18 @@ const binaryCondition =
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const simpleEqualCondition = () => value => ({
|
||||||
|
conditionType: 'binary',
|
||||||
|
operator: '=',
|
||||||
|
left: {
|
||||||
|
exprType: 'placeholder',
|
||||||
|
},
|
||||||
|
right: {
|
||||||
|
exprType: 'value',
|
||||||
|
value,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const likeCondition = (conditionType, likeString) => value => ({
|
const likeCondition = (conditionType, likeString) => value => ({
|
||||||
conditionType,
|
conditionType,
|
||||||
left: {
|
left: {
|
||||||
@@ -348,6 +360,8 @@ const createParser = (filterBehaviour: FilterBehaviour) => {
|
|||||||
|
|
||||||
objectid: () => token(P.regexp(/ObjectId\(['"]?[0-9a-f]{24}['"]?\)/)).desc('ObjectId'),
|
objectid: () => token(P.regexp(/ObjectId\(['"]?[0-9a-f]{24}['"]?\)/)).desc('ObjectId'),
|
||||||
|
|
||||||
|
objectidstr: () => token(P.regexp(/[0-9a-f]{24}/)).desc('ObjectId string'),
|
||||||
|
|
||||||
hexstring: () =>
|
hexstring: () =>
|
||||||
token(P.regexp(/0x(([0-9a-fA-F][0-9a-fA-F])+)/, 1))
|
token(P.regexp(/0x(([0-9a-fA-F][0-9a-fA-F])+)/, 1))
|
||||||
.map(x => ({
|
.map(x => ({
|
||||||
@@ -366,6 +380,7 @@ const createParser = (filterBehaviour: FilterBehaviour) => {
|
|||||||
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('=')),
|
||||||
hexTestEq: r => r.hexstring.map(binaryCondition('=')),
|
hexTestEq: r => r.hexstring.map(binaryCondition('=')),
|
||||||
|
valueTestObjectIdStr: r => r.objectidstr.map(simpleEqualCondition()),
|
||||||
valueTestStr: r => r.value.map(likeCondition('like', '%#VALUE#%')),
|
valueTestStr: r => r.value.map(likeCondition('like', '%#VALUE#%')),
|
||||||
valueTestNum: r => r.number.map(numberTestCondition()),
|
valueTestNum: r => r.number.map(numberTestCondition()),
|
||||||
valueTestObjectId: r => r.objectid.map(objectIdTestCondition()),
|
valueTestObjectId: r => r.objectid.map(objectIdTestCondition()),
|
||||||
@@ -546,12 +561,13 @@ const createParser = (filterBehaviour: FilterBehaviour) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filterBehaviour.allowNumberDualTesting) {
|
if (filterBehaviour.allowObjectIdTesting) {
|
||||||
allowedElements.push('valueTestNum');
|
allowedElements.push('valueTestObjectIdStr');
|
||||||
|
allowedElements.push('valueTestObjectId');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filterBehaviour.allowObjectIdTesting) {
|
if (filterBehaviour.allowNumberDualTesting) {
|
||||||
allowedElements.push('valueTestObjectId');
|
allowedElements.push('valueTestNum');
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be last
|
// must be last
|
||||||
|
|||||||
@@ -11,6 +11,21 @@ function convertRightOperandToMongoValue(right) {
|
|||||||
throw new Error(`Unknown right operand type ${right.exprType}`);
|
throw new Error(`Unknown right operand type ${right.exprType}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function convertRightEqualOperandToMongoCondition(right) {
|
||||||
|
if (right.exprType != 'value') {
|
||||||
|
throw new Error(`Unknown right operand type ${right.exprType}`);
|
||||||
|
}
|
||||||
|
const { value } = right;
|
||||||
|
if (/^[0-9a-fA-F]{24}$/.test(value)) {
|
||||||
|
return {
|
||||||
|
$in: [value, { $oid: value }],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
$eq: value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function convertToMongoCondition(filter) {
|
function convertToMongoCondition(filter) {
|
||||||
if (!filter) {
|
if (!filter) {
|
||||||
return null;
|
return null;
|
||||||
@@ -28,9 +43,7 @@ function convertToMongoCondition(filter) {
|
|||||||
switch (filter.operator) {
|
switch (filter.operator) {
|
||||||
case '=':
|
case '=':
|
||||||
return {
|
return {
|
||||||
[convertLeftOperandToMongoColumn(filter.left)]: {
|
[convertLeftOperandToMongoColumn(filter.left)]: convertRightEqualOperandToMongoCondition(filter.right),
|
||||||
$eq: convertRightOperandToMongoValue(filter.right),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
case '!=':
|
case '!=':
|
||||||
case '<>':
|
case '<>':
|
||||||
|
|||||||
Reference in New Issue
Block a user