Skip to content

Commit

Permalink
enhancement: Abe type='data' - An array can be used in a IN and NOT I…
Browse files Browse the repository at this point in the history
…N request statement
  • Loading branch information
gregorybesson committed Dec 14, 2016
1 parent 7326eb0 commit 48936b1
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 5,131 deletions.
44 changes: 44 additions & 0 deletions docs/types/abe-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ select some_variable from / where `abe_meta.template` IN (`template_name_1`, `te
// ex with variables
select some_variable from / where `template` IN (`{{variable_array}}`)
select some_variable from / where `{{abe_meta.template}}` IN (`a title`)
// ex with an array
select some_variable from / where `array.attribute` IN (`{{variable_array}}`)
// the previous example is equivalent to this notation (notice the [] to indicate an array)
select some_variable from / where `array[].attribute` IN (`{{variable_array}}`)
```

variable_array can be something like this
Expand Down Expand Up @@ -203,6 +207,42 @@ select some_variable from / where `template` IN (`{{variable_array}}`)
select some_variable from / where `template` IN (`homepage`, `article`)
```

Use case :

If the template looks like this:

```json
{{abe type="data" key="products" source="select title,cover_300x200 from /products where `abe_meta.template`=`product`" desc="select products" display="{{title}}"}}

{{abe type="data" key="other_recipes" source="select title,cover_300x200 from /recipes where `products.title` IN (`{{products}}`) AND `abe_meta.link`!=`{{abe_meta.link}}`" editable="false"}}
```

> we could have written the other_recipes request this way (see the [] in products[].title):
```json
{{abe type="data" key="other_recipes" source="select title,cover_300x200 from /recipes where `products[].title` IN (`{{products}}`) AND `abe_meta.link`!=`{{abe_meta.link}}`" editable="false"}}
```

If the json post looks like this

```json
{
"name": "r2",
"recipe": "Recipe 2",
"products": [
{
"title": "p1",
"cover_300x200": "/image/turkey_300x200.jpg",
},
{
"title": "p3",
"cover_300x200": "/image/chicken_300x200.jpg",
}
]
}
```

the result of the request will return all the recipes containing one or more products included in the json post (containing p1 or p3 or both)

## NOT IN

Request:
Expand All @@ -211,6 +251,10 @@ select some_variable from / where `abe_meta.template` NOT IN (`template_name_1`,
// ex with variables
select some_variable from / where `template` NOT IN (`{{variable_array}}`)
select some_variable from / where `{{abe_meta.template}}` NOT IN (`a title`)
// ex with an array
select some_variable from / where `{{array.attribute}}` NOT IN (`{{variable_array}}`)
// the previous example is equivalent to this notation (notice the [] to indicate an array)
select some_variable from / where `{{array[].attribute}}` NOT IN (`{{variable_array}}`)
```

Same as `IN`
Expand Down
61 changes: 48 additions & 13 deletions src/cli/cms/data/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export function getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) {

try {
var variableLeft = where.left.column
var whereLeftColumn = where.left.column
var checkIfLeftIsAVariable = regexIsVariable.exec(variableLeft)
if(checkIfLeftIsAVariable != null && checkIfLeftIsAVariable.length > 0) {
variableLeft = checkIfLeftIsAVariable[1]
Expand All @@ -412,14 +413,37 @@ export function getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) {
}

if(where.operator === 'IN' || where.operator === 'NOT IN') {
if(value == null) {
// is the form of variableLeft something like elt.attribute or elt[].attribute?
// so maybe is elt an array ?
let arValues = variableLeft.split('.')
let key = arValues[0]
if(key.slice(-2) == '[]'){
key = key.slice(0, -2)
}
let records = eval('jsonDoc.' + key)
whereLeftColumn = arValues[1]
value = []
// if yes, value is then an array of values
if(records != null) {
Array.prototype.forEach.call(records, (record) => {
try {
let val = record[arValues[1]]
value.push(val)
} catch (e) {
console.log(e.stack)
}
})
}
}
compare = []
Array.prototype.forEach.call(where.right.value, (right) => {
var matchRightVariable = regexIsVariable.exec(right.column)
if(matchRightVariable != null && matchRightVariable.length > 0) {
try {
var jsonOriginalValues = eval('jsonOriginalDoc.' + matchRightVariable[1])
Array.prototype.forEach.call(jsonOriginalValues, (jsonOriginalValue) => {
compare.push(eval('jsonOriginalValue.' + where.left.column))
compare.push(eval('jsonOriginalValue.' + whereLeftColumn))
})
}catch(e) {}
}
Expand Down Expand Up @@ -456,6 +480,26 @@ export function getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) {
}
}

export function isInStatementCorrect(values, isCorrect){
if( Object.prototype.toString.call(values.left) === '[object Array]' ) {
Array.prototype.forEach.call(values.left, (left) => {
Array.prototype.forEach.call(values.right, (right) => {
if(left != null && left === right) {
isCorrect = !isCorrect
}
})
})
} else {
Array.prototype.forEach.call(values.right, (right) => {
if(values.left === right) {
isCorrect = !isCorrect
}
})
}

return isCorrect
}

/**
* Check where.left value that match where operator (=, !=, >, >=, <, <=, LIKE, NOT LIKE, AND, OR, IN, NOT IN)
* if operator AND or OR
Expand Down Expand Up @@ -518,22 +562,13 @@ export function recurseWhere(where, jsonDoc, jsonOriginalDoc) {
break
case 'IN':
values = getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc)
isNotCorrect = true
Array.prototype.forEach.call(values.right, (right) => {
if(values.left === right) {
isNotCorrect = false
}
})
isNotCorrect = isInStatementCorrect(values, true)
break
case 'NOT IN':
values = getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc)
isNotCorrect = false
Array.prototype.forEach.call(values.right, (right) => {
if(values.left === right) {
isNotCorrect = true
}
})
isNotCorrect = isInStatementCorrect(values, false)
break
}

return isNotCorrect
}
Loading

0 comments on commit 48936b1

Please sign in to comment.