2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * @extends {WebInspector.VBox}
30 WebInspector.DatabaseQueryView = function(database)
32 WebInspector.VBox.call(this);
34 this.database = database;
36 this.element.classList.add("storage-view");
37 this.element.classList.add("query");
38 this.element.classList.add("monospace");
39 this.element.addEventListener("selectstart", this._selectStart.bind(this), false);
41 this._promptElement = createElement("div");
42 this._promptElement.className = "database-query-prompt";
43 this._promptElement.appendChild(createElement("br"));
44 this._promptElement.addEventListener("keydown", this._promptKeyDown.bind(this), true);
45 this.element.appendChild(this._promptElement);
47 this._prompt = new WebInspector.TextPromptWithHistory(this.completions.bind(this), " ");
48 this._prompt.attach(this._promptElement);
50 this.element.addEventListener("click", this._messagesClicked.bind(this), true);
53 WebInspector.DatabaseQueryView.Events = {
54 SchemaUpdated: "SchemaUpdated"
57 WebInspector.DatabaseQueryView.prototype = {
58 _messagesClicked: function()
60 if (!this._prompt.isCaretInsidePrompt() && window.getSelection().isCollapsed)
61 this._prompt.moveCaretToEndOfPrompt();
65 * @param {!Element} proxyElement
66 * @param {!Range} wordRange
67 * @param {boolean} force
68 * @param {function(!Array.<string>, number=)} completionsReadyCallback
70 completions: function(proxyElement, wordRange, force, completionsReadyCallback)
72 var prefix = wordRange.toString().toLowerCase();
77 function accumulateMatches(textArray)
79 for (var i = 0; i < textArray.length; ++i) {
80 var text = textArray[i].toLowerCase();
81 if (text.length < prefix.length)
83 if (!text.startsWith(prefix))
85 results.push(textArray[i]);
89 function tableNamesCallback(tableNames)
91 accumulateMatches(tableNames.map(function(name) { return name + " " }));
92 accumulateMatches(["SELECT ", "FROM ", "WHERE ", "LIMIT ", "DELETE FROM ", "CREATE ", "DROP ", "TABLE ", "INDEX ", "UPDATE ", "INSERT INTO ", "VALUES ("]);
94 completionsReadyCallback(results);
96 this.database.getTableNames(tableNamesCallback);
99 _selectStart: function(event)
101 if (this._selectionTimeout)
102 clearTimeout(this._selectionTimeout);
104 this._prompt.clearAutoComplete();
107 * @this {WebInspector.DatabaseQueryView}
109 function moveBackIfOutside()
111 delete this._selectionTimeout;
112 if (!this._prompt.isCaretInsidePrompt() && window.getSelection().isCollapsed)
113 this._prompt.moveCaretToEndOfPrompt();
114 this._prompt.autoCompleteSoon();
117 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100);
120 _promptKeyDown: function(event)
122 if (isEnterKey(event)) {
123 this._enterKeyPressed(event);
128 _enterKeyPressed: function(event)
132 this._prompt.clearAutoComplete(true);
134 var query = this._prompt.text;
138 this._prompt.pushHistoryItem(query);
139 this._prompt.text = "";
141 this.database.executeSql(query, this._queryFinished.bind(this, query), this._queryError.bind(this, query));
144 _queryFinished: function(query, columnNames, values)
146 var dataGrid = WebInspector.SortableDataGrid.create(columnNames, values);
147 var trimmedQuery = query.trim();
150 dataGrid.renderInline();
151 this._appendViewQueryResult(trimmedQuery, dataGrid);
152 dataGrid.autoSizeColumns(5);
155 if (trimmedQuery.match(/^create /i) || trimmedQuery.match(/^drop table /i))
156 this.dispatchEventToListeners(WebInspector.DatabaseQueryView.Events.SchemaUpdated, this.database);
159 _queryError: function(query, errorMessage)
161 this._appendErrorQueryResult(query, errorMessage);
165 * @param {string} query
166 * @param {!WebInspector.View} view
168 _appendViewQueryResult: function(query, view)
170 var resultElement = this._appendQueryResult(query);
171 view.show(resultElement);
173 this._promptElement.scrollIntoView(false);
177 * @param {string} query
178 * @param {string} errorText
180 _appendErrorQueryResult: function(query, errorText)
182 var resultElement = this._appendQueryResult(query);
183 resultElement.classList.add("error")
184 resultElement.textContent = errorText;
186 this._promptElement.scrollIntoView(false);
189 _appendQueryResult: function(query)
191 var element = createElement("div");
192 element.className = "database-user-query";
193 this.element.insertBefore(element, this._prompt.proxyElement);
195 var commandTextElement = createElement("span");
196 commandTextElement.className = "database-query-text";
197 commandTextElement.textContent = query;
198 element.appendChild(commandTextElement);
200 var resultElement = createElement("div");
201 resultElement.className = "database-query-result";
202 element.appendChild(resultElement);
203 return resultElement;
206 __proto__: WebInspector.VBox.prototype