buildSentence.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { dataType } from './configs'
  2. export function trimColType(str) {
  3. let colType
  4. if (str.indexOf('(') >= 0) {
  5. colType = dataType.find(type => type.name === str.split('(')[0])
  6. } else {
  7. colType = dataType.find(type => type.name === str)
  8. }
  9. return colType
  10. }
  11. export function buildSqlSentence({ dataSrc, selectedCalcul, selectedCalcul2, selectedDimension, orderByStrs, filterStr, limit }) {
  12. let fields = []
  13. const groups = []
  14. let groupBy
  15. let orderBy
  16. let where
  17. let allSelected = []
  18. // allSelected = allSelected.concat(selectedCalcul).concat(selectedDimension)
  19. // fields = allSelected.map(field => {
  20. // const calculField = selectedCalcul.find(col => col.Column === field.Column)
  21. // if (calculField && calculField.calculFunc !== 'none') {
  22. // return `${calculField.calculFunc || trimColType(calculField.Type).availableFunc[0]}(${calculField.Column}) as ${calculField.Column}`
  23. // } else {
  24. // groups.push(field.Column)
  25. // return field.Column
  26. // }
  27. // })
  28. allSelected = allSelected.concat(selectedCalcul).concat(selectedCalcul2).concat(selectedDimension)
  29. fields = allSelected.map(field => {
  30. const calculField = selectedCalcul.find(col => col.Column === field.Column)
  31. const calculField2 = selectedCalcul2.find(col => col.Column === field.Column)
  32. if (calculField && calculField.calculFunc !== 'none') {
  33. return `${calculField.calculFunc || trimColType(calculField.Type).availableFunc[0]}(${calculField.Column}) as ${calculField.Column}`
  34. } else if (calculField2 && calculField2.calculFunc !== 'none') {
  35. return `${calculField2.calculFunc || trimColType(calculField2.Type).availableFunc[0]}(${calculField2.Column}) as ${calculField2.Column}`
  36. } else {
  37. groups.push(field.Column)
  38. return field.Column
  39. }
  40. })
  41. if (fields.length === 0 || !dataSrc) return
  42. if (groups.length > 0) {
  43. groupBy = `GROUP BY ${groups.join()}`
  44. }
  45. if (orderByStrs.length > 0) {
  46. orderBy = `ORDER BY ${orderByStrs.join()}`
  47. }
  48. if (filterStr) {
  49. console.log("buildSqlSentence filterStr!!!")
  50. where = `where ${filterStr}`
  51. }
  52. return `SELECT ${fields.join()} FROM ${dataSrc} ${where || ''} ${groupBy || ''} ${orderBy || ''} LIMIT ${limit || 200}`
  53. }
  54. export function buildFilterSentence(filter) {
  55. let filterSentence
  56. let valueObj = filter.value
  57. if (trimColType(filter.colType).needQuotation) {
  58. valueObj = addQuotation(filter.value)
  59. }
  60. if (filter.operatorParamNum === 1) {
  61. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1}`
  62. } else if (filter.operatorParamNum === 2) {
  63. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1} and ${valueObj.value2}`
  64. } else {
  65. filterSentence = `${filter.filteCol} ${filter.filterOperator} ('${valueObj.arrValue.join(',')}')`
  66. }
  67. return filterSentence
  68. function addQuotation(valueObj) {
  69. return {
  70. value1: `'${valueObj.value1}'`,
  71. value2: `'${valueObj.value2}'`,
  72. arrValue: valueObj.arrValue.map(value => `'${value}'`)
  73. }
  74. }
  75. }