autocomplete.utils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_hasExpired(timestamp){
  39. var TIME_TO_LIVE_IN_MILLIS = 600000; // 10 minutes
  40. return (new Date()).getTime() - timestamp > TIME_TO_LIVE_IN_MILLIS;
  41. }
  42. function hac_getTableAliases(textScanned) {
  43. var _aliases = {};
  44. var _val = textScanned.split("\n").join(" ");
  45. var _from = _val.toUpperCase().indexOf("FROM ");
  46. if (_from > -1) {
  47. var _match = _val.toUpperCase().substring(_from).match(/ ON| LIMIT| WHERE| GROUP| SORT| ORDER BY|;/);
  48. var _to = _val.length;
  49. if (_match) {
  50. _to = _match.index;
  51. }
  52. var _found = _val.substr(_from, _to).replace(/(\r\n|\n|\r)/gm, "").replace(/from/gi, "").replace(/join/gi, ",").split(",");
  53. for (var i = 0; i < _found.length; i++) {
  54. var _tablealias = $.trim(_found[i]).split(" ");
  55. if (_tablealias.length > 1) {
  56. _aliases[_tablealias[1]] = _tablealias[0];
  57. }
  58. }
  59. }
  60. return _aliases;
  61. }
  62. function hac_getTableColumns(databaseName, tableName, textScanned, callback) {
  63. if (tableName.indexOf("(") > -1) {
  64. tableName = tableName.substr(tableName.indexOf("(") + 1);
  65. }
  66. var _aliases = hac_getTableAliases(textScanned);
  67. if (_aliases[tableName]) {
  68. tableName = _aliases[tableName];
  69. }
  70. if ($.totalStorage('columns_' + databaseName + '_' + tableName) != null && $.totalStorage('extended_columns_' + databaseName + '_' + tableName) != null) {
  71. callback($.totalStorage('columns_' + databaseName + '_' + tableName), $.totalStorage('extended_columns_' + databaseName + '_' + tableName));
  72. if ($.totalStorage('timestamp_columns_' + databaseName + '_' + tableName) == null || hac_hasExpired($.totalStorage('timestamp_columns_' + databaseName + '_' + tableName))){
  73. hac_jsoncalls({
  74. database: databaseName,
  75. table: tableName,
  76. onDataReceived: function (data) {
  77. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  78. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  79. }
  80. if (data.error) {
  81. hac_errorHandler(data);
  82. }
  83. else {
  84. $.totalStorage('columns_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  85. $.totalStorage('extended_columns_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  86. $.totalStorage('timestamp_columns_' + databaseName + '_' + tableName, (new Date()).getTime());
  87. }
  88. }
  89. });
  90. }
  91. }
  92. else {
  93. hac_jsoncalls({
  94. database: databaseName,
  95. table: tableName,
  96. onDataReceived: function (data) {
  97. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  98. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  99. }
  100. if (data.error) {
  101. hac_errorHandler(data);
  102. }
  103. else {
  104. $.totalStorage('columns_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  105. $.totalStorage('extended_columns_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  106. $.totalStorage('timestamp_columns_' + databaseName + '_' + tableName, (new Date()).getTime());
  107. callback($.totalStorage('columns_' + databaseName + '_' + tableName), $.totalStorage('extended_columns_' + databaseName + '_' + tableName));
  108. }
  109. }
  110. });
  111. }
  112. }
  113. function hac_tableHasAlias(tableName, textScanned) {
  114. var _aliases = hac_getTableAliases(textScanned);
  115. for (var alias in _aliases) {
  116. if (_aliases[alias] == tableName) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. function hac_getTables(databaseName, callback) {
  123. if ($.totalStorage('tables_' + databaseName) != null) {
  124. callback($.totalStorage('tables_' + databaseName));
  125. if ($.totalStorage('timestamp_tables_' + databaseName) == null || hac_hasExpired($.totalStorage('timestamp_tables_' + databaseName))){
  126. hac_jsoncalls({
  127. database: databaseName,
  128. onDataReceived: function (data) {
  129. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  130. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  131. }
  132. if (data.error) {
  133. hac_errorHandler(data);
  134. }
  135. else {
  136. $.totalStorage('tables_' + databaseName, data.tables.join(" "));
  137. $.totalStorage('timestamp_tables_' + databaseName, (new Date()).getTime());
  138. }
  139. }
  140. });
  141. }
  142. }
  143. else {
  144. hac_jsoncalls({
  145. database: databaseName,
  146. onDataReceived: function (data) {
  147. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  148. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  149. }
  150. if (data.error) {
  151. hac_errorHandler(data);
  152. }
  153. else {
  154. if (data.tables) {
  155. $.totalStorage('tables_' + databaseName, data.tables.join(" "));
  156. $.totalStorage('timestamp_tables_' + databaseName, (new Date()).getTime());
  157. callback($.totalStorage('tables_' + databaseName));
  158. }
  159. }
  160. }
  161. });
  162. }
  163. }
  164. function hac_errorHandler(data) {
  165. $(document).trigger('error.autocomplete');
  166. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  167. if (typeof HIVE_AUTOCOMPLETE_FAILS_QUIETLY_ON != "undefined" && HIVE_AUTOCOMPLETE_FAILS_QUIETLY_ON.indexOf(data.code) > -1){
  168. $(document).trigger('info', data.error);
  169. }
  170. else {
  171. $(document).trigger('error', data.error);
  172. }
  173. }
  174. }