db_query_generator.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview function used in QBE for DB
  4. * @name Database Operations
  5. *
  6. * @requires jQuery
  7. * @requires jQueryUI
  8. * @requires js/functions.js
  9. *
  10. */
  11. function getFormatsText () {
  12. return {
  13. '=': ' = \'%s\'',
  14. '>': ' > \'%s\'',
  15. '>=': ' >= \'%s\'',
  16. '<': ' < \'%s\'',
  17. '<=': ' <= \'%s\'',
  18. '!=': ' != \'%s\'',
  19. 'LIKE': ' LIKE \'%s\'',
  20. 'LIKE \%...\%': ' LIKE \'%%%s%%\'',
  21. 'NOT LIKE': ' NOT LIKE \'%s\'',
  22. 'BETWEEN': ' BETWEEN \'%s\'',
  23. 'NOT BETWEEN': ' NOT BETWEEN \'%s\'',
  24. 'IS NULL': ' \'%s\' IS NULL',
  25. 'IS NOT NULL': ' \'%s\' IS NOT NULL',
  26. 'REGEXP': ' REGEXP \'%s\'',
  27. 'REGEXP ^...$': ' REGEXP \'^%s$\'',
  28. 'NOT REGEXP': ' NOT REGEXP \'%s\''
  29. };
  30. }
  31. function generateCondition (criteriaDiv, table) {
  32. query = '`' + escapeBacktick(table.val()) + '`.';
  33. query += '`' + escapeBacktick(table.siblings('.columnNameSelect').first().val()) + '`';
  34. if (criteriaDiv.find('.criteria_rhs').first().val() === 'text') {
  35. formatsText = getFormatsText();
  36. query += sprintf(formatsText[criteriaDiv.find('.criteria_op').first().val()], escapeSingleQuote(criteriaDiv.find('.rhs_text_val').first().val()));
  37. } else {
  38. query += ' ' + criteriaDiv.find('.criteria_op').first().val();
  39. query += ' `' + escapeBacktick(criteriaDiv.find('.tableNameSelect').first().val()) + '`.';
  40. query += '`' + escapeBacktick(criteriaDiv.find('.columnNameSelect').first().val()) + '`';
  41. }
  42. return query;
  43. }
  44. function generateWhereBlock () {
  45. var count = 0;
  46. var query = '';
  47. $('.tableNameSelect').each(function () {
  48. var criteriaDiv = $(this).siblings('.slide-wrapper').first();
  49. var useCriteria = $(this).siblings('.criteria_col').first();
  50. if ($(this).val() !== '' && useCriteria.prop('checked')) {
  51. if (count > 0) {
  52. criteriaDiv.find('input.logical_op').each(function () {
  53. if ($(this).prop('checked')) {
  54. query += ' ' + $(this).val() + ' ';
  55. }
  56. });
  57. }
  58. query += generateCondition(criteriaDiv, $(this));
  59. count++;
  60. }
  61. });
  62. return query;
  63. }
  64. function generateJoin (newTable, tableAliases, fk) {
  65. query = '';
  66. query += ' \n\tLEFT JOIN ' + '`' + escapeBacktick(newTable) + '`';
  67. if (tableAliases[fk.TABLE_NAME][0] !== '') {
  68. query += ' AS `' + escapeBacktick(tableAliases[newTable][0]) + '`';
  69. query += ' ON `' + escapeBacktick(tableAliases[fk.TABLE_NAME][0]) + '`';
  70. } else {
  71. query += ' ON `' + escapeBacktick(fk.TABLE_NAME) + '`';
  72. }
  73. query += '.`' + fk.COLUMN_NAME + '`';
  74. if (tableAliases[fk.REFERENCED_TABLE_NAME][0] !== '') {
  75. query += ' = `' + escapeBacktick(tableAliases[fk.REFERENCED_TABLE_NAME][0]) + '`';
  76. } else {
  77. query += ' = `' + escapeBacktick(fk.REFERENCED_TABLE_NAME) + '`';
  78. }
  79. query += '.`' + fk.REFERENCED_COLUMN_NAME + '`';
  80. return query;
  81. }
  82. function existReference (table, fk, usedTables) {
  83. var isReferredBy = fk.TABLE_NAME === table && usedTables.includes(fk.REFERENCED_TABLE_NAME);
  84. var isReferencedBy = fk.REFERENCED_TABLE_NAME === table && usedTables.includes(fk.TABLE_NAME);
  85. return isReferredBy || isReferencedBy;
  86. }
  87. function tryJoinTable (table, tableAliases, usedTables, foreignKeys) {
  88. for (var i = 0; i < foreignKeys.length; i++) {
  89. var fk = foreignKeys[i];
  90. if (existReference(table, fk, usedTables)) {
  91. return generateJoin(table, tableAliases, fk);
  92. }
  93. }
  94. return '';
  95. }
  96. function appendTable (table, tableAliases, usedTables, foreignKeys) {
  97. var query = tryJoinTable (table, tableAliases, usedTables, foreignKeys);
  98. if (query === '') {
  99. if (usedTables.length > 0) {
  100. query += '\n\t, ';
  101. }
  102. query += '`' + escapeBacktick(table) + '`';
  103. if (tableAliases[table][0] !== '') {
  104. query += ' AS `' + escapeBacktick(tableAliases[table][0]) + '`';
  105. }
  106. }
  107. usedTables.push(table);
  108. return query;
  109. }
  110. function generateFromBlock (tableAliases, foreignKeys) {
  111. var usedTables = [];
  112. query = '';
  113. for (var table in tableAliases) {
  114. if (tableAliases.hasOwnProperty(table)) {
  115. query += appendTable(table, tableAliases, usedTables, foreignKeys);
  116. }
  117. }
  118. return query;
  119. }