13dae4282255243335f6391f061c20c1e7834dbd.svn-base 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. where = `where ${filterStr}`
  50. }
  51. return `SELECT ${fields.join()} FROM ${dataSrc} ${where || ''} ${groupBy || ''} ${orderBy || ''} LIMIT ${limit || 200}`
  52. }
  53. export function buildFilterSentence(filter) {
  54. let filterSentence
  55. let valueObj = filter.value
  56. if (trimColType(filter.colType).needQuotation) {
  57. valueObj = addQuotation(filter.value)
  58. }
  59. if (filter.operatorParamNum === 1) {
  60. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1}`
  61. } else if (filter.operatorParamNum === 2) {
  62. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1} and ${valueObj.value2}`
  63. } else {
  64. filterSentence = `${filter.filteCol} ${filter.filterOperator} ('${valueObj.arrValue.join(',')}')`
  65. }
  66. return filterSentence
  67. function addQuotation(valueObj) {
  68. return {
  69. value1: `'${valueObj.value1}'`,
  70. value2: `'${valueObj.value2}'`,
  71. arrValue: valueObj.arrValue.map(value => `'${value}'`)
  72. }
  73. }
  74. }