sql_part.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { clone } from 'lodash';
  2. export class SqlPartDef {
  3. type: string;
  4. style: string;
  5. label: string;
  6. params: any[];
  7. defaultParams: any[];
  8. wrapOpen: string;
  9. wrapClose: string;
  10. separator: string;
  11. constructor(options: any) {
  12. this.type = options.type;
  13. if (options.label) {
  14. this.label = options.label;
  15. } else {
  16. this.label = this.type[0].toUpperCase() + this.type.substring(1) + ':';
  17. }
  18. this.style = options.style;
  19. if (this.style === 'function') {
  20. this.wrapOpen = '(';
  21. this.wrapClose = ')';
  22. this.separator = ', ';
  23. } else {
  24. this.wrapOpen = ' ';
  25. this.wrapClose = ' ';
  26. this.separator = ' ';
  27. }
  28. this.params = options.params;
  29. this.defaultParams = options.defaultParams;
  30. }
  31. }
  32. export class SqlPart {
  33. part: any;
  34. def: SqlPartDef;
  35. params: any[];
  36. label: string;
  37. name: string;
  38. datatype: string;
  39. constructor(part: any, def: any) {
  40. this.part = part;
  41. this.def = def;
  42. if (!this.def) {
  43. throw { message: 'Could not find sql part ' + part.type };
  44. }
  45. this.datatype = part.datatype;
  46. if (part.name) {
  47. this.name = part.name;
  48. this.label = def.label + ' ' + part.name;
  49. } else {
  50. this.name = '';
  51. this.label = def.label;
  52. }
  53. part.params = part.params || clone(this.def.defaultParams);
  54. this.params = part.params;
  55. }
  56. updateParam(strValue: string, index: number) {
  57. // handle optional parameters
  58. if (strValue === '' && this.def.params[index].optional) {
  59. this.params.splice(index, 1);
  60. } else {
  61. this.params[index] = strValue;
  62. }
  63. this.part.params = this.params;
  64. }
  65. }