DatabaseContentView.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.DatabaseContentView = function(representedObject)
  26. {
  27. WebInspector.ContentView.call(this, representedObject);
  28. this.database = representedObject;
  29. this.element.classList.add("storage-view");
  30. this.element.classList.add("query");
  31. this.element.classList.add("monospace");
  32. this._promptElement = document.createElement("div");
  33. this._promptElement.className = "database-query-prompt";
  34. this.element.appendChild(this._promptElement);
  35. this.prompt = new WebInspector.ConsolePrompt(this, "text/x-sql");
  36. this._promptElement.appendChild(this.prompt.element);
  37. this.element.addEventListener("click", this._messagesClicked.bind(this), true);
  38. }
  39. WebInspector.DatabaseContentView.Event = {
  40. SchemaUpdated: "SchemaUpdated"
  41. }
  42. WebInspector.DatabaseContentView.prototype = {
  43. constructor: WebInspector.DatabaseContentView,
  44. shown: function()
  45. {
  46. this.prompt.shown();
  47. },
  48. updateLayout: function()
  49. {
  50. this.prompt.updateLayout();
  51. var results = this.element.querySelectorAll(".database-query-result");
  52. for (var i = 0; i < results.length; ++i) {
  53. var resultElement = results[i];
  54. if (resultElement.dataGrid)
  55. resultElement.dataGrid.updateLayout();
  56. }
  57. },
  58. _messagesClicked: function()
  59. {
  60. this.prompt.focus();
  61. },
  62. consolePromptCompletionsNeeded: function(prompt, defaultCompletions, base, prefix, suffix)
  63. {
  64. var results = [];
  65. prefix = prefix.toLowerCase();
  66. function accumulateMatches(textArray)
  67. {
  68. for (var i = 0; i < textArray.length; ++i) {
  69. var lowerCaseText = textArray[i].toLowerCase();
  70. if (lowerCaseText.startsWith(prefix))
  71. results.push(textArray[i]);
  72. }
  73. }
  74. function tableNamesCallback(tableNames)
  75. {
  76. accumulateMatches(tableNames);
  77. accumulateMatches(["SELECT", "FROM", "WHERE", "LIMIT", "DELETE FROM", "CREATE", "DROP", "TABLE", "INDEX", "UPDATE", "INSERT INTO", "VALUES"]);
  78. this.prompt.updateCompletions(results, " ");
  79. }
  80. this.database.getTableNames(tableNamesCallback.bind(this));
  81. },
  82. consolePromptTextCommitted: function(prompt, query)
  83. {
  84. this.database.executeSQL(query, this._queryFinished.bind(this, query), this._queryError.bind(this, query));
  85. },
  86. _queryFinished: function(query, columnNames, values)
  87. {
  88. var dataGrid = WebInspector.DataGrid.createSortableDataGrid(columnNames, values);
  89. var trimmedQuery = query.trim();
  90. if (dataGrid) {
  91. dataGrid.element.classList.add("inline");
  92. this._appendViewQueryResult(trimmedQuery, dataGrid);
  93. dataGrid.autoSizeColumns(5);
  94. }
  95. if (trimmedQuery.match(/^create /i) || trimmedQuery.match(/^drop table /i))
  96. this.dispatchEventToListeners(WebInspector.DatabaseContentView.Event.SchemaUpdated, this.database);
  97. },
  98. _queryError: function(query, error)
  99. {
  100. if (error.message)
  101. var message = error.message;
  102. else if (error.code == 2)
  103. var message = WebInspector.UIString("Database no longer has expected version.");
  104. else
  105. var message = WebInspector.UIString("An unexpected error %s occurred.").format(error.code);
  106. this._appendErrorQueryResult(query, message);
  107. },
  108. /**
  109. * @param {string} query
  110. * @param {WebInspector.View} view
  111. */
  112. _appendViewQueryResult: function(query, view)
  113. {
  114. var resultElement = this._appendQueryResult(query);
  115. // Add our DataGrid with the results to the database query result div.
  116. resultElement.dataGrid = view;
  117. resultElement.appendChild(view.element);
  118. this._promptElement.scrollIntoView(false);
  119. },
  120. /**
  121. * @param {string} query
  122. * @param {string} errorText
  123. */
  124. _appendErrorQueryResult: function(query, errorText)
  125. {
  126. var resultElement = this._appendQueryResult(query);
  127. resultElement.classList.add("error")
  128. resultElement.textContent = errorText;
  129. this._promptElement.scrollIntoView(false);
  130. },
  131. _appendQueryResult: function(query)
  132. {
  133. var element = document.createElement("div");
  134. element.className = "database-user-query";
  135. this.element.insertBefore(element, this._promptElement);
  136. var commandTextElement = document.createElement("span");
  137. commandTextElement.className = "database-query-text";
  138. commandTextElement.textContent = query;
  139. element.appendChild(commandTextElement);
  140. var resultElement = document.createElement("div");
  141. resultElement.className = "database-query-result";
  142. element.appendChild(resultElement);
  143. return resultElement;
  144. }
  145. }
  146. WebInspector.DatabaseContentView.prototype.__proto__ = WebInspector.ContentView.prototype;