Skip to content

Commit

Permalink
Merge pull request #93 from synatic/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
filepounder authored Jan 30, 2023
2 parents 3359854 + 38b7754 commit dda42a9
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 18 deletions.
17 changes: 16 additions & 1 deletion lib/MongoFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,22 @@ class AllowableFunctions {
}

return {
$indexOfCP: parameters,
$add: [{$indexOfCP: parameters}, 1],
};
},
},
{
name: 'locate',
allowQuery: true,
parse: (parameters) => {
if (!$check.array(parameters))
throw new Error('Invalid parameters for substring');
if (parameters.length !== 2) {
throw new Error('Invalid parameters starts_with');
}

return {
$add: [{$indexOfCP: [parameters[1], parameters[0]]}, 1],
};
},
},
Expand Down
32 changes: 32 additions & 0 deletions lib/make/makeQueryPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,25 @@ function makeQueryPart(
throw new Error(`Unsupported operator:${queryPart.operator}`);
}

if (queryPart.type === 'function' && queryPart.name === 'NOT') {
return {
$nor: makeQueryPart(
queryPart.args,
ignorePrefix,
allowedTypes,
includeThis,
tableAlias
),
};
}

if (queryPart.type === 'function' || queryPart.type === 'select')
return makeProjectionExpressionPartModule.makeProjectionExpressionPart(
queryPart,
0,
true
);

if (queryPart.type === 'expr_list') {
return queryPart.value.map((v) => makeQueryPart(v));
}
Expand All @@ -189,6 +202,25 @@ function makeQueryPart(
};
}

// NOT Expression
if (
queryPart.type === 'unary_expr' &&
queryPart.operator === 'NOT' &&
queryPart.expr
) {
const exprQuery = makeQueryPart(
queryPart.expr,
ignorePrefix,
allowedTypes,
includeThis,
tableAlias
);

return {
$nor: $check.array(exprQuery) ? exprQuery : [exprQuery],
};
}

// todo add not

if (queryPart.type === 'aggr_func') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@synatic/sql-to-mongo",
"version": "1.1.7",
"version": "1.1.8",
"description": "Convert SQL to mongo queries or aggregates",
"main": "index.js",
"files": [
Expand Down
30 changes: 20 additions & 10 deletions test/aggregateTests/aggregateTests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@
},
{
"name": "Aggregate:strpos",
"query": " select strpos(Title,'B') as x,* from films where strpos(Title,'B') > 0",
"query": " select strpos(Title,'B') as x,* from films where strpos(Title,'B') > 1",
"type": "aggregate",
"output": {
"pipeline": [
Expand All @@ -1739,14 +1739,19 @@
"$expr": {
"$gt": [
{
"$indexOfCP": [
"$Title",
"$add": [
{
"$literal": "B"
}
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
},
0
1
]
}
}
Expand All @@ -1758,11 +1763,16 @@
"$$ROOT",
{
"x": {
"$indexOfCP": [
"$Title",
"$add": [
{
"$literal": "B"
}
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/queryTests/comparisonOperators.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,34 @@
}
}

}
},
{
"name": "Comparison:NOT OR",
"query": "select * from films where NOT (Title = 'Test' or Title = 'test2')",
"output": {
"collection": "films",
"limit": 100,
"query": {
"$nor": [
{
"$or": [
{
"Title": {
"$eq": "Test"
}
},
{
"Title": {
"$eq": "test2"
}
}
]
}
]

}

}
}
]
99 changes: 93 additions & 6 deletions test/queryTests/stringOperators.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,57 @@
},
{
"name": "String:STRPOS",
"query": "select strpos(Title,'B') as x,filmId from films where strpos(Title,'B') > -1",
"query": "select strpos(Title,'B') as x,filmId from films where strpos(Title,'B') > 0",
"output": {
"collection": "films",
"limit": 100,
"projection": {
"x": {
"$indexOfCP": [
"$Title",
"$add": [
{
"$literal": "B"
}
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
},
"filmId": "$filmId"
},
"query": {
"$expr": {
"$gt": [
{
"$add": [
{
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
},
0
]
}
}
}
},
{
"name": "String:LOCATE",
"query": "select locate('B',Title) as x,filmId from films where locate('B',Title) > 0",
"output": {
"collection": "films",
"limit": 100,
"projection": {
"x": {
"$add": [
{
"$indexOfCP": [
"$Title",
Expand All @@ -225,12 +258,66 @@
}
]
},
-1
1
]
},
"filmId": "$filmId"
},
"query": {
"$expr": {
"$gt": [
{
"$add": [
{
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
},
0
]
}
}
}
},
{
"name": "String:NOT STRPOS",
"query": "select * from films where NOT strpos(Title,'B')>0",
"output": {
"collection": "films",
"limit": 100,
"query": {
"$nor": [
{
"$expr": {
"$gt": [
{
"$add": [
{
"$indexOfCP": [
"$Title",
{
"$literal": "B"
}
]
},
1
]
},
0
]
}
}
]
}
}
},

{
"name": "String:STARTS_WITH",
"query": "select starts_with(Title,'B') as x,filmId from films where starts_with(Title,'B') is true",
Expand Down

0 comments on commit dda42a9

Please sign in to comment.