1319f8d4b1668140bf2d79099eeaa2f00e414f4c.svn-base 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, 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. if (fields.length === 0 || !dataSrc) return
  29. if (groups.length > 0) {
  30. groupBy = `GROUP BY ${groups.join()}`
  31. }
  32. if (orderByStrs.length > 0) {
  33. orderBy = `ORDER BY ${orderByStrs.join()}`
  34. }
  35. if (filterStr) {
  36. where = `where ${filterStr}`
  37. }
  38. return `SELECT ${fields.join()} FROM ${dataSrc} ${where || ''} ${groupBy || ''} ${orderBy || ''} LIMIT ${limit || 200}`
  39. }
  40. export function buildFilterSentence(filter) {
  41. let filterSentence
  42. let valueObj = filter.value
  43. if (trimColType(filter.colType).needQuotation) {
  44. valueObj = addQuotation(filter.value)
  45. }
  46. if (filter.operatorParamNum === 1) {
  47. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1}`
  48. } else if (filter.operatorParamNum === 2) {
  49. filterSentence = `${filter.filteCol} ${filter.filterOperator} ${valueObj.value1} and ${valueObj.value2}`
  50. } else {
  51. filterSentence = `${filter.filteCol} ${filter.filterOperator} ('${valueObj.arrValue.join(',')}')`
  52. }
  53. return filterSentence
  54. function addQuotation(valueObj) {
  55. return {
  56. value1: `'${valueObj.value1}'`,
  57. value2: `'${valueObj.value2}'`,
  58. arrValue: valueObj.arrValue.map(value => `'${value}'`)
  59. }
  60. }
  61. }