autocomplete.utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 spark_jsoncalls(options) {
  17. if (typeof spark_AUTOCOMPLETE_BASE_URL != "undefined") {
  18. if (options.database === null) {
  19. $.getJSON(spark_AUTOCOMPLETE_BASE_URL, options.onDataReceived);
  20. }
  21. if (options.database) {
  22. if (options.table) {
  23. $.getJSON(spark_AUTOCOMPLETE_BASE_URL + "/servers/" + options.server + "/databases/" + options.database + "/tables/" + options.table + "/columns", options.onDataReceived);
  24. }
  25. else {
  26. $.getJSON(spark_AUTOCOMPLETE_BASE_URL + "/servers/" + options.server + "/databases/" + options.database + "/tables", options.onDataReceived);
  27. }
  28. }
  29. }
  30. else {
  31. try {
  32. console.error("You need to specify a spark_AUTOCOMPLETE_BASE_URL to use the autocomplete");
  33. }
  34. catch (e) {
  35. }
  36. }
  37. }
  38. function spark_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 spark_getTableAliases(textScanned) {
  43. var _aliases = {};
  44. var _val = textScanned; //codeMirror.getValue();
  45. var _from = _val.toUpperCase().indexOf("FROM ");
  46. if (_from > -1) {
  47. var _match = _val.toUpperCase().substring(_from).match(/ON|WHERE|GROUP|SORT|ORDER/);
  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 spark_tableHasAlias(tableName, textScanned) {
  63. var _aliases = spark_getTableAliases(textScanned);
  64. for (var alias in _aliases) {
  65. if (_aliases[alias] == tableName) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. function spark_getTableColumns(serverName, databaseName, tableName, textScanned, callback) {
  72. if (tableName.indexOf("(") > -1) {
  73. tableName = tableName.substr(tableName.indexOf("(") + 1);
  74. }
  75. var _aliases = spark_getTableAliases(textScanned);
  76. if (_aliases[tableName]) {
  77. tableName = _aliases[tableName];
  78. }
  79. if ($.totalStorage('spark_columns_' + serverName + '_' + databaseName + '_' + tableName) && $.totalStorage('spark_extended_columns_' + serverName + '_' + databaseName + '_' + tableName)) {
  80. callback($.totalStorage('spark_columns_' + serverName + '_' + databaseName + '_' + tableName), $.totalStorage('spark_extended_columns_' + serverName + '_' + databaseName + '_' + tableName));
  81. if ($.totalStorage('spark_timestamp_columns_' + serverName + '_' + databaseName + '_' + tableName) === null || spark_hasExpired($.totalStorage('spark_timestamp_columns_' + serverName + '_' + databaseName + '_' + tableName))){
  82. spark_jsoncalls({
  83. server: serverName,
  84. database: databaseName,
  85. table: tableName,
  86. onDataReceived: function (data) {
  87. if (typeof spark_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  88. spark_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  89. }
  90. if (data.error) {
  91. if (typeof spark_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code === null || spark_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  92. $(document).trigger('error', data.error);
  93. }
  94. }
  95. else {
  96. $.totalStorage('spark_columns_' + serverName + '_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  97. $.totalStorage('spark_extended_columns_' + serverName + '_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  98. $.totalStorage('spark_timestamp_columns_' + serverName + '_' + databaseName + '_' + tableName, (new Date()).getTime());
  99. }
  100. }
  101. });
  102. }
  103. }
  104. else {
  105. spark_jsoncalls({
  106. server: serverName,
  107. database: databaseName,
  108. table: tableName,
  109. onDataReceived: function (data) {
  110. if (typeof spark_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  111. spark_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  112. }
  113. if (data.error) {
  114. if (typeof spark_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code === null || spark_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  115. $(document).trigger('error', data.error);
  116. }
  117. }
  118. else {
  119. $.totalStorage('spark_columns_' + serverName + '_' + databaseName + '_' + tableName, (data.columns ? "* " + data.columns.join(" ") : "*"));
  120. $.totalStorage('spark_extended_columns_' + serverName + '_' + databaseName + '_' + tableName, (data.extended_columns ? data.extended_columns : []));
  121. $.totalStorage('spark_timestamp_columns_' + serverName + '_' + databaseName + '_' + tableName, (new Date()).getTime());
  122. callback($.totalStorage('spark_columns_' + serverName + '_' + databaseName + '_' + tableName), $.totalStorage('spark_extended_columns_' + serverName + '_' + databaseName + '_' + tableName));
  123. }
  124. }
  125. });
  126. }
  127. }
  128. function spark_getTables(serverName, databaseName, callback) {
  129. if ($.totalStorage('spark_tables_' + serverName + '_' + databaseName)) {
  130. callback($.totalStorage('spark_tables_' + serverName + '_' + databaseName));
  131. if ($.totalStorage('spark_timestamp_tables_' + serverName + '_' + databaseName) === null || spark_hasExpired($.totalStorage('spark_timestamp_tables_' + serverName + '_' + databaseName))){
  132. spark_jsoncalls({
  133. server: serverName,
  134. database: databaseName,
  135. onDataReceived: function (data) {
  136. if (typeof spark_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  137. spark_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  138. }
  139. if (data.error) {
  140. if (typeof spark_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code === null || spark_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  141. $(document).trigger('error', data.error);
  142. }
  143. }
  144. else {
  145. $.totalStorage('spark_tables_' + serverName + '_' + databaseName, data.tables.join(" "));
  146. $.totalStorage('spark_timestamp_tables_' + serverName + '_' + databaseName, (new Date()).getTime());
  147. }
  148. }
  149. });
  150. }
  151. }
  152. else {
  153. spark_jsoncalls({
  154. server: serverName,
  155. database: databaseName,
  156. onDataReceived: function (data) {
  157. if (typeof spark_AUTOCOMPLETE_GLOBAL_CALLBACK == "function") {
  158. spark_AUTOCOMPLETE_GLOBAL_CALLBACK(data);
  159. }
  160. if (data.error) {
  161. if (typeof spark_AUTOCOMPLETE_FAILS_SILENTLY_ON == "undefined" || data.code === null || spark_AUTOCOMPLETE_FAILS_SILENTLY_ON.indexOf(data.code) == -1){
  162. $(document).trigger('error', data.error);
  163. }
  164. }
  165. else {
  166. $.totalStorage('spark_tables_' + serverName + '_' + databaseName, data.tables.join(" "));
  167. $.totalStorage('spark_timestamp_tables_' + serverName + '_' + databaseName, (new Date()).getTime());
  168. callback($.totalStorage('spark_tables_' + serverName + '_' + databaseName));
  169. }
  170. }
  171. });
  172. }
  173. }