autocomplete.utils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function hac_jsoncalls(options) {
  17. if (typeof HIVE_AUTOCOMPLETE_BASE_URL != "undefined") {
  18. if (options.database == null) {
  19. $.getJSON(HIVE_AUTOCOMPLETE_BASE_URL, options.onDataReceived);
  20. }
  21. if (options.database != null) {
  22. if (options.table != null) {
  23. $.getJSON(HIVE_AUTOCOMPLETE_BASE_URL + options.database + "/" + options.table, options.onDataReceived);
  24. }
  25. else {
  26. $.getJSON(HIVE_AUTOCOMPLETE_BASE_URL + options.database + "/", options.onDataReceived);
  27. }
  28. }
  29. }
  30. else {
  31. try {
  32. console.error("You need to specify a HIVE_AUTOCOMPLETE_BASE_URL to use the autocomplete")
  33. }
  34. catch (e) {
  35. }
  36. }
  37. }
  38. function hac_getTableAliases(textScanned) {
  39. var _aliases = {};
  40. var _val = textScanned; //codeMirror.getValue();
  41. var _from = _val.toUpperCase().indexOf("FROM");
  42. if (_from > -1) {
  43. var _match = _val.toUpperCase().substring(_from).match(/ON|WHERE|GROUP|SORT/);
  44. var _to = _val.length;
  45. if (_match) {
  46. _to = _match.index;
  47. }
  48. var _found = _val.substr(_from, _to).replace(/(\r\n|\n|\r)/gm, "").replace(/from/gi, "").replace(/join/gi, ",").split(",");
  49. for (var i = 0; i < _found.length; i++) {
  50. var _tablealias = $.trim(_found[i]).split(" ");
  51. if (_tablealias.length > 1) {
  52. _aliases[_tablealias[1]] = _tablealias[0];
  53. }
  54. }
  55. }
  56. return _aliases;
  57. }
  58. function hac_getTableColumns(databaseName, tableName, textScanned, callback) {
  59. if (tableName.indexOf("(") > -1) {
  60. tableName = tableName.substr(tableName.indexOf("(") + 1);
  61. }
  62. var _aliases = hac_getTableAliases(textScanned);
  63. if (_aliases[tableName]) {
  64. tableName = _aliases[tableName];
  65. }
  66. if ($.totalStorage('columns_' + databaseName + '_' + tableName) != null) {
  67. callback($.totalStorage('columns_' + databaseName + '_' + tableName));
  68. hac_jsoncalls({
  69. database: databaseName,
  70. table: tableName,
  71. onDataReceived: function (data) {
  72. if (data.error) {
  73. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == undefined || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  74. $(document).trigger('error', data.error);
  75. }
  76. }
  77. else {
  78. $.totalStorage('columns_' + databaseName + '_' + tableName, (data.columns ? data.columns.join(" ") : ""));
  79. }
  80. }
  81. });
  82. }
  83. else {
  84. hac_jsoncalls({
  85. database: databaseName,
  86. table: tableName,
  87. onDataReceived: function (data) {
  88. if (data.error) {
  89. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == undefined || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  90. $(document).trigger('error', data.error);
  91. }
  92. }
  93. else {
  94. $.totalStorage('columns_' + databaseName + '_' + tableName, (data.columns ? data.columns.join(" ") : ""));
  95. callback($.totalStorage('columns_' + databaseName + '_' + tableName));
  96. }
  97. }
  98. });
  99. }
  100. }
  101. function hac_tableHasAlias(tableName, textScanned) {
  102. var _aliases = hac_getTableAliases(textScanned);
  103. for (var alias in _aliases) {
  104. if (_aliases[alias] == tableName) {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. function hac_getTables(databaseName, callback) {
  111. if ($.totalStorage('tables_' + databaseName) != null) {
  112. callback($.totalStorage('tables_' + databaseName));
  113. hac_jsoncalls({
  114. database: databaseName,
  115. onDataReceived: function (data) {
  116. if (data.error) {
  117. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == undefined || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  118. $(document).trigger('error', data.error);
  119. }
  120. }
  121. else {
  122. $.totalStorage('tables_' + databaseName, data.tables.join(" "));
  123. }
  124. }
  125. });
  126. }
  127. else {
  128. hac_jsoncalls({
  129. database: databaseName,
  130. onDataReceived: function (data) {
  131. if (data.error) {
  132. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == undefined || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  133. $(document).trigger('error', data.error);
  134. }
  135. }
  136. else {
  137. $.totalStorage('tables_' + databaseName, data.tables.join(" "));
  138. callback($.totalStorage('tables_' + databaseName));
  139. }
  140. }
  141. });
  142. }
  143. }