12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import { dataType } from './configs'
- export function trimColType(str) {
- let colType
- if (str.indexOf('(') >= 0) {
- colType = dataType.find(type => type.name === str.split('(')[0])
- } else {
- colType = dataType.find(type => type.name === str)
- }
- return colType
- }
- export function buildSqlSentence({ dataSrc, selectedCalcul, selectedCalcul2, selectedDimension, orderByStrs, filterStr, limit }) {
- let fields = []
- const groups = []
- let groupBy
- let orderBy
- let where
- let allSelected = []
- // allSelected = allSelected.concat(selectedCalcul).concat(selectedDimension)
- // fields = allSelected.map(field => {
- // const calculField = selectedCalcul.find(col => col.Column === field.Column)
- // if (calculField && calculField.calculFunc !== 'none') {
- // return `${calculField.calculFunc || trimColType(calculField.Type).availableFunc[0]}(${calculField.Column}) as ${calculField.Column}`
- // } else {
- // groups.push(field.Column)
- // return field.Column
- // }
- // })
- allSelected = allSelected.concat(selectedCalcul).concat(selectedCalcul2).concat(selectedDimension)
- fields = allSelected.map(field => {
- const calculField = selectedCalcul.find(col => col.Column === field.Column)
- const calculField2 = selectedCalcul2.find(col => col.Column === field.Column)
- if (calculField && calculField.calculFunc !== 'none') {
- return `${calculField.calculFunc || trimColType(calculField.Type).availableFunc[0]}(${calculField.Column}) as ${calculField.Column}`
- } else if (calculField2 && calculField2.calculFunc !== 'none') {
- return `${calculField2.calculFunc || trimColType(calculField2.Type).availableFunc[0]}(${calculField2.Column}) as ${calculField2.Column}`
- } else {
- groups.push(field.Column)
- return field.Column
- }
- })
- if (fields.length === 0 || !dataSrc) return
- if (groups.length > 0) {
- groupBy = `GROUP BY ${groups.join()}`
- }
- if (orderByStrs.length > 0) {
- orderBy = `ORDER BY ${orderByStrs.join()}`
- }
- if (filterStr) {
- console.log("buildSqlSentence filterStr!!!")
- where = `where ${filterStr}`
- }
- return `SELECT ${fields.join()} FROM ${dataSrc} ${where || ''} ${groupBy || ''} ${orderBy || ''} LIMIT ${limit || 200}`
- }
- export function buildFilterSentence(filter) {
- let filterSentence
- let valueObj = filter.value
- if (trimColType(filter.colType).needQuotation) {
- valueObj = addQuotation(filter.value)
- }
- if (filter.operatorParamNum === 1) {
- filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1}`
- } else if (filter.operatorParamNum === 2) {
- filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1} and ${valueObj.value2}`
- } else {
- filterSentence = `${filter.filteCol} ${filter.filterOperator} ('${valueObj.arrValue.join(',')}')`
- }
- return filterSentence
- function addQuotation(valueObj) {
- return {
- value1: `'${valueObj.value1}'`,
- value2: `'${valueObj.value2}'`,
- arrValue: valueObj.arrValue.map(value => `'${value}'`)
- }
- }
- }
|