autocomplete.utils.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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" || typeof options.autocompleteBaseURL != "undefined") {
  18. var _baseURL = typeof options.autocompleteBaseURL != "undefined" ? options.autocompleteBaseURL : HIVE_AUTOCOMPLETE_BASE_URL;
  19. if (options.database == null) {
  20. $.getJSON(_baseURL, options.onDataReceived);
  21. }
  22. if (options.database != null) {
  23. if (options.table != null) {
  24. $.getJSON(_baseURL + options.database + "/" + options.table, options.onDataReceived);
  25. }
  26. else {
  27. $.getJSON(_baseURL + options.database + "/", options.onDataReceived);
  28. }
  29. }
  30. }
  31. else {
  32. try {
  33. console.error("You need to specify a HIVE_AUTOCOMPLETE_BASE_URL to use the autocomplete")
  34. }
  35. catch (e) {
  36. }
  37. }
  38. }
  39. function hac_hasExpired(timestamp){
  40. var TIME_TO_LIVE_IN_MILLIS = 86400000; // 1 day
  41. return (new Date()).getTime() - timestamp > TIME_TO_LIVE_IN_MILLIS;
  42. }
  43. function hac_getTableAliases(textScanned) {
  44. var _aliases = {};
  45. var _val = textScanned.split("\n").join(" ");
  46. var _from = _val.toUpperCase().indexOf("FROM ");
  47. if (_from > -1) {
  48. var _match = _val.toUpperCase().substring(_from).match(/ ON| LIMIT| WHERE| GROUP| SORT| ORDER BY|;/);
  49. var _to = _val.length;
  50. if (_match) {
  51. _to = _match.index;
  52. }
  53. var _found = _val.substr(_from, _to).replace(/(\r\n|\n|\r)/gm, "").replace(/from/gi, "").replace(/join/gi, ",").split(",");
  54. for (var i = 0; i < _found.length; i++) {
  55. var _tablealias = $.trim(_found[i]).split(" ");
  56. if (_tablealias.length > 1) {
  57. _aliases[_tablealias[1]] = _tablealias[0];
  58. }
  59. }
  60. }
  61. return _aliases;
  62. }
  63. function hac_getTotalStorageUserPrefix(){
  64. if (typeof HIVE_AUTOCOMPLETE_USER != "undefined") {
  65. return HIVE_AUTOCOMPLETE_USER + "_";
  66. }
  67. return "";
  68. }
  69. function hac_getTableColumns(databaseName, tableName, textScanned, callback) {
  70. if (tableName.indexOf("(") > -1) {
  71. tableName = tableName.substr(tableName.indexOf("(") + 1);
  72. }
  73. var _aliases = hac_getTableAliases(textScanned);
  74. if (_aliases[tableName]) {
  75. tableName = _aliases[tableName];
  76. }
  77. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'columns_' + databaseName + '_' + tableName) != null && $.totalStorage(hac_getTotalStorageUserPrefix() + 'extended_columns_' + databaseName + '_' + tableName) != null) {
  78. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'columns_' + databaseName + '_' + tableName), $.totalStorage(hac_getTotalStorageUserPrefix() + 'extended_columns_' + databaseName + '_' + tableName));
  79. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_columns_' + databaseName + '_' + tableName) == null || hac_hasExpired($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_columns_' + databaseName + '_' + tableName))){
  80. hac_jsoncalls({
  81. database: databaseName,
  82. table: tableName,
  83. onDataReceived: function (data) {
  84. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  85. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  86. }
  87. if (data.error) {
  88. hac_errorHandler(data);
  89. }
  90. else {
  91. $.totalStorage(hac_getTotalStorageUserPrefix() + 'columns_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  92. $.totalStorage(hac_getTotalStorageUserPrefix() + 'extended_columns_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  93. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_columns_' + databaseName + '_' + tableName, (new Date()).getTime());
  94. }
  95. }
  96. });
  97. }
  98. }
  99. else {
  100. hac_jsoncalls({
  101. database: databaseName,
  102. table: tableName,
  103. onDataReceived: function (data) {
  104. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  105. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  106. }
  107. if (data.error) {
  108. hac_errorHandler(data);
  109. }
  110. else {
  111. $.totalStorage(hac_getTotalStorageUserPrefix() + 'columns_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  112. $.totalStorage(hac_getTotalStorageUserPrefix() + 'extended_columns_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  113. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_columns_' + databaseName + '_' + tableName, (new Date()).getTime());
  114. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'columns_' + databaseName + '_' + tableName), $.totalStorage(hac_getTotalStorageUserPrefix() + 'extended_columns_' + databaseName + '_' + tableName));
  115. }
  116. }
  117. });
  118. }
  119. }
  120. function hac_tableHasAlias(tableName, textScanned) {
  121. var _aliases = hac_getTableAliases(textScanned);
  122. for (var alias in _aliases) {
  123. if (_aliases[alias] == tableName) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. function hac_getTables(databaseName, callback) {
  130. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'tables_' + databaseName) != null) {
  131. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'tables_' + databaseName));
  132. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_tables_' + databaseName) == null || hac_hasExpired($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_tables_' + databaseName))){
  133. hac_jsoncalls({
  134. database: databaseName,
  135. onDataReceived: function (data) {
  136. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  137. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  138. }
  139. if (data.error) {
  140. hac_errorHandler(data);
  141. }
  142. else {
  143. $.totalStorage(hac_getTotalStorageUserPrefix() + 'tables_' + databaseName, data.tables.join(" "));
  144. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_tables_' + databaseName, (new Date()).getTime());
  145. }
  146. }
  147. });
  148. }
  149. }
  150. else {
  151. hac_jsoncalls({
  152. database: databaseName,
  153. onDataReceived: function (data) {
  154. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  155. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  156. }
  157. if (data.error) {
  158. hac_errorHandler(data);
  159. }
  160. else {
  161. if (data.tables) {
  162. $.totalStorage(hac_getTotalStorageUserPrefix() + 'tables_' + databaseName, data.tables.join(" "));
  163. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_tables_' + databaseName, (new Date()).getTime());
  164. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'tables_' + databaseName));
  165. }
  166. }
  167. }
  168. });
  169. }
  170. }
  171. function hac_getDatabases(callback) {
  172. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'databases') != null) {
  173. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'databases'));
  174. if ($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_databases') == null || hac_hasExpired($.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_databases'))){
  175. hac_jsoncalls({
  176. onDataReceived: function (data) {
  177. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  178. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  179. }
  180. if (data.error) {
  181. hac_errorHandler(data);
  182. }
  183. else {
  184. $.totalStorage(hac_getTotalStorageUserPrefix() + 'databases', data.databases);
  185. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_databases', (new Date()).getTime());
  186. }
  187. }
  188. });
  189. }
  190. }
  191. else {
  192. hac_jsoncalls({
  193. onDataReceived: function (data) {
  194. if (typeof HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  195. HIVE_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  196. }
  197. if (data.error) {
  198. hac_errorHandler(data);
  199. }
  200. else {
  201. if (data.databases) {
  202. $.totalStorage(hac_getTotalStorageUserPrefix() + 'databases', data.databases);
  203. $.totalStorage(hac_getTotalStorageUserPrefix() + 'timestamp_databases', (new Date()).getTime());
  204. callback($.totalStorage(hac_getTotalStorageUserPrefix() + 'databases'));
  205. }
  206. }
  207. }
  208. });
  209. }
  210. }
  211. function hac_errorHandler(data) {
  212. $(document).trigger('error.autocomplete');
  213. if (typeof HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code == null || HIVE_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  214. if (typeof HIVE_AUTOCOMPLETE_FAILS_QUIETLY_ON != "undefined" && HIVE_AUTOCOMPLETE_FAILS_QUIETLY_ON.indexOf(data.code) > -1){
  215. $(document).trigger('info', data.error);
  216. }
  217. else {
  218. $(document).trigger('error', data.error);
  219. }
  220. }
  221. }