Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use modern variable declarations, replace loops with functional methods #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 50 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,99 @@
var sqltoes = function (input) {
var select = input.select
var where = input.where
var groupBy = input.groupBy

const sqltoes = (input) => {
const { select, where, groupBy } = input
// anything wrapped in the select statement becomes the end filter: sum, avg, min, max to start
var lastFilter = []
for (var i = 0; i < select.length; i++) {
var m = select[i].match(/sum\(|avg\(|min\(|max\(/g)
if (m) {
lastFilter.push(m[0].replace(/\(/, ''))
lastFilter.push(select[i].replace(m[0], '').replace(')', ''))
const lastFilter = []
select.forEach((s) => {
const match = s.match(/sum\(|avg\(|min\(|max\(/g)
if (match) {
lastFilter.push(match[0].replace(/\(/, ''))
lastFilter.push(s.replace(match[0], '').replace(')', ''))
}
}
})

// anything in where becomes the filter (in order), currently only supports =, should add 'in' soon
var filters = []
for (i = 0; i < where.length; i++) {
filters.push(where[i].replace(/\(|\)/g, '').split(/= | in |,/))
}
const filters = where.map((filter) => filter.replace(/\(|\)/g, '').split(/= | in |,/))

// make the object (tiny hack for non grouped objects)
let aggregate
if (groupBy.length) {
var a = aggWrap(whereObj(filters, groupByObj(groupBy, lastFilterObj(lastFilter))))
aggregate = aggWrap(whereObj(filters, groupByObj(groupBy, lastFilterObj(lastFilter))))
} else {
var temp = whereObj(filters, {})
var topKey = Object.keys(temp)[0]
a = temp[topKey]
a['size'] = 30
a.filter.terms ? delete a.filter.size : 0
const temp = whereObj(filters, {})
const topKey = Object.keys(temp)[0]
aggregate = temp[topKey]
aggregate.size = 30
aggregate.filter.terms ? delete aggregate.filter.size : 0
}
return a
return aggregate
}

var lastFilterObj = function (lf) {
var lastFilterObject = {}
var wrapper = {}
const lastFilterObj = (lf) => {
const lastFilterObject = {}
const wrapper = {}
lastFilterObject[lf[0]] = { 'field': lf[1] }
wrapper[lf[0] + '_' + lf[1]] = lastFilterObject
return wrapper
}

var groupByObj = function (array, object) {
var currentArray = array.slice(0)
if (currentArray.length === 0) {
const groupByObj = (array, object) => {
const currentArray = array.slice(0)
if (!currentArray.length) {
return object
} else if (currentArray.length === 1) {
var obj = {}
var groupByName = 'group_by_' + currentArray[0]
const obj = {}
const groupByName = 'group_by_' + currentArray[0]
obj[groupByName] = { 'terms': { 'field': currentArray[0], 'size': 1000 }, 'aggs': object }
return obj
} else {
var aggs = {}
var gb = currentArray.shift()
groupByName = 'group_by_' + gb
const aggs = {}
const gb = currentArray.shift()
const groupByName = 'group_by_' + gb
aggs[groupByName] = { 'terms': { 'field': gb, 'size': 1000 }, 'aggs': groupByObj(currentArray, object) }
return aggs
}
}

var whereObj = function (array, object) {
var currentArray = array.slice(0)
if (currentArray.length === 0) {
const whereObj = (array, object) => {
const currentArray = array.slice(0)
if (!currentArray.length) {
return object
} else if (currentArray.length === 1) {
var obj = {}
var term = {}
var terms = {}
var w = currentArray[0]
var tw = w[0].replace(/ /g, '')
const obj = {}
const term = {}
const terms = {}
const w = currentArray[0]
const tw = w[0].replace(/ /g, '')
term[tw] = w[1].replace(/\\|'| /g, '')
if (w.length === 2) {
var wName = 'where_' + tw + '_' + term[tw]
const wName = 'where_' + tw + '_' + term[tw]
obj[wName] = { 'filter': { 'term': term }, 'aggs': object }
} else {
var termsArray = []
for (var t = 0; t < w.length - 1; t++) {
termsArray.push(w[t + 1].replace(/ /g, '')) // purposefully not removing quotes here
}
const termsArray = w.shift().map((w) => w.replace(/ /g, '')) // purposefully not removing quotes here)
terms[tw] = termsArray
wName = 'where_' + tw + '_multiple'
const wName = 'where_' + tw + '_multiple'
obj[wName] = { 'filter': { 'terms': terms }, 'aggs': object }
}
return obj
} else {
var aggs = {}
term = {}
terms = {}
w = currentArray.shift()
tw = w[0].replace(/ /g, '')
const aggs = {}
const term = {}
const terms = {}
const w = currentArray.shift()
const tw = w[0].replace(/ /g, '')
term[tw] = w[1].replace(/\\|'| /g, '')
if (w.length === 2) {
wName = 'where_' + tw + '_' + term[tw]
const wName = 'where_' + tw + '_' + term[tw]
aggs[wName] = { 'filter': { 'term': term }, 'aggs': whereObj(currentArray, object) }
} else {
termsArray = []
for (t = 0; t < w.length - 1; t++) {
termsArray.push(w[t + 1].replace(/ /g, '')) // purposefully not removing quotes here
};
const termsArray = w.shift().map((w) => w.replace(/ /g, '')) // purposefully not removing quotes here)
terms[tw] = termsArray
wName = 'where_' + tw + '_multiple'
const wName = 'where_' + tw + '_multiple'
aggs[wName] = { 'filter': { 'terms': terms }, 'agg': whereObj(currentArray, object) }
}
return aggs
}
}

var aggWrap = function (object) {
return {
'aggs': object
}
const aggWrap = (object) => {
return {'aggs': object}
}

module.exports = sqltoes