-
Notifications
You must be signed in to change notification settings - Fork 39
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
T360 sql editor bug #28
base: master
Are you sure you want to change the base?
Changes from 9 commits
1f41bfc
19a8047
b8284a7
cc1b67b
b1809c4
e800985
b36e064
11c9ea6
2c41af7
c42a3cc
72d518c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ export default { | |
}; | ||
objectSQL.groupBy = getGroupBy(query, objectSQL.select); | ||
objectSQL.orderBy = getOrderBy(query, objectSQL.select); | ||
|
||
return objectSQL; | ||
}, | ||
parseToQueryObject(rawQuery, fields) { | ||
|
@@ -42,7 +43,7 @@ export default { | |
}; | ||
|
||
function getSelect(query) { | ||
let select = query.match(/^\s*select\s+([\w([\])"'`.,/\s]+)from\s/i); | ||
let select = query.match(/^\s*select\s+([\w([\])"'`*:&;.,/\s]+)from\s/i); | ||
if (!select) return { error: 'Wrong syntax: "SELECT <fields> FROM <collections>" expected' }; | ||
select = select[1]; | ||
const asNames = select.match(/\s+as\s+\w+/ig); | ||
|
@@ -74,21 +75,25 @@ const parseQueryPartial = (partial, paramName) => | |
partial.slice(0, paramName.index).match(/(\w+)\/(\w+)/i); | ||
|
||
function getFrom(query) { | ||
const from = query.match(/\sfrom\s+([\w([\])"'`.,/=<>\s]+$)/i)[1].replace(cutOff, ''); | ||
const matchRes = query.match(/\sfrom\s+([\w([\])"'`.,/=<>\s]+$)/im); | ||
if (!matchRes) return null; | ||
const from = matchRes[1].replace(cutOff, ''); | ||
const name = getParamName(from); | ||
const path = parseQueryPartial(from, name); | ||
return { db: path[1], collection: path[2], name: name[1] }; | ||
} | ||
|
||
function getJoin(query) { | ||
const from = query.match(/\sjoin\s+([\w([\])"'`.,/=<>\s]+$)/i)[1].replace(cutOff, ''); | ||
const matchRes = query.match(/\sjoin\s+([\w([\])"'`.,/=<>\s]+$)/im); | ||
if (!matchRes) return null; | ||
const from = matchRes[1].replace(cutOff, ''); | ||
const name = getParamName(from); | ||
const path = parseQueryPartial(from, name); | ||
return path[2]; | ||
} | ||
|
||
function getGroupBy(query, select) { | ||
let groupBy = query.match(/\sgroup by\s+([\w([\])"'`.,/=<>\s]+$)/i); | ||
let groupBy = query.match(/^\s*group by\s+([\w([\])"'`*:&;.,/\s]+)/im); | ||
if (groupBy) { | ||
groupBy = groupBy[1].replace(cutOff, '') | ||
.replace(/\s/g, ''); | ||
|
@@ -119,19 +124,57 @@ function getOrderBy(query, select) { | |
} | ||
|
||
function getHaving(query) { | ||
let having = query.match(/\shaving\s+([\w([\])"'`.,/=<>\s]+$)/i); | ||
|
||
let having = query.match(/^\s*having\s+([\w([\])"'`*:&;.,-<>%@=\/\s]+)limit\s/im); | ||
if (having) { | ||
having = having[1].replace(cutOff, ''); | ||
return parseFiltering(having); | ||
having = removeBraces(having); | ||
return parseHavingConditions(having); | ||
} | ||
return null; | ||
|
||
|
||
function removeBraces(queryPart) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can also be moved outside from function. |
||
let result = ''; | ||
|
||
if (isCountExist(queryPart)) { | ||
const countWordINdex = getWordIndex(queryPart, 'count('); | ||
const afterCountIndex = getWordIndex(queryPart, ')', countWordINdex); | ||
result += removebracesWithRegex(queryPart.substr(0, countWordINdex)); | ||
result += queryPart.substring(countWordINdex, afterCountIndex + 1); | ||
result += removeBraces(queryPart.substring(afterCountIndex + 1)); | ||
} else { | ||
result += removebracesWithRegex(queryPart); | ||
} | ||
return result; | ||
} | ||
|
||
function isCountExist(queryToCheck) { | ||
return queryToCheck.match(/COUNT[(]/gi); | ||
} | ||
|
||
function removebracesWithRegex(line) { | ||
return line.replace(/[()]/g, ''); | ||
} | ||
|
||
function getWordIndex(line, word, fromPosition = 0) { | ||
let result = line.indexOf(word, fromPosition); | ||
if (result === -1) { | ||
result = line.indexOf(word.toUpperCase(), fromPosition); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
function getWhere(query) { | ||
let where = query.match(/\swhere\s+([\w([\])"'`.,/=<>\s]+$)/i); | ||
let where = query.match(/\swhere\s+([\w([\])"'`.,/=<>\s]+$)/im); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace |
||
if (where) { | ||
where = where[1].replace(cutOff, ''); | ||
return parseFiltering(where); | ||
const parsed = parseFiltering(where); | ||
parsed.forEach( (item) => { | ||
item.exp2 = item.exp2.replace(/^["]/i, '').replace(/["]$/i, ''); | ||
}); | ||
return parsed; | ||
} | ||
return null; | ||
} | ||
|
@@ -148,6 +191,33 @@ function getOffset(query) { | |
return null; | ||
} | ||
|
||
function parseHavingConditions(str) { | ||
const filters = []; | ||
const andOr = str.match(/\s+(and|or)\s+/gi); | ||
|
||
if (andOr) { | ||
let rest = str.slice(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
andOr.forEach(item => { | ||
const part = rest.slice(0, rest.indexOf(item)); | ||
filters.push(part); | ||
rest = rest.slice(rest.indexOf(item) + item.length); | ||
}); | ||
filters.push(rest); | ||
} else { | ||
filters.push(str); | ||
} | ||
return filters.map((filter, i) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split to smaller functions |
||
const obj = { operator: filter.match(/(=|<>|<|>|\slike\s)/i)[1] }; | ||
const regex = new RegExp(`${obj.operator}([\\w([\\])"'\`*:&;%@.,-/\\s]+)`, 'i'); | ||
obj.exp2 = filter.match(regex)[1]; | ||
obj.exp1 = filter.replace(obj.operator, '').replace(obj.exp2, '').replace(/\s/g, ''); | ||
obj.operator = obj.operator.replace(/\s/g, ''); | ||
obj.exp2 = obj.exp2.replace(/^\s+|\s+$/g, ''); | ||
if (andOr && i < andOr.length) obj.join = andOr[i].replace(/\s/g, ''); | ||
return obj; | ||
}); | ||
} | ||
|
||
function parseFiltering(str) { | ||
const filters = []; | ||
const andOr = str.match(/\s+(and|or)\s+/gi); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
determineFieldsBy...
functions could be defined outside of targetdetermineDefaultFields
, e.g. as plain file variables