1 /* 2 * Copyright (C) 2007, 2008 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 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @constructor 31 * @param {!WebInspector.DatabaseModel} model 32 * @param {string} id 33 * @param {string} domain 34 * @param {string} name 35 * @param {string} version 36 */ 37 WebInspector.Database = function(model, id, domain, name, version) 38 { 39 this._model = model; 40 this._id = id; 41 this._domain = domain; 42 this._name = name; 43 this._version = version; 44 } 45 46 WebInspector.Database.prototype = { 47 /** @return {string} */ 48 get id() 49 { 50 return this._id; 51 }, 52 53 /** @return {string} */ 54 get name() 55 { 56 return this._name; 57 }, 58 59 set name(x) 60 { 61 this._name = x; 62 }, 63 64 /** @return {string} */ 65 get version() 66 { 67 return this._version; 68 }, 69 70 set version(x) 71 { 72 this._version = x; 73 }, 74 75 /** @return {string} */ 76 get domain() 77 { 78 return this._domain; 79 }, 80 81 set domain(x) 82 { 83 this._domain = x; 84 }, 85 86 /** 87 * @param {function(!Array.<string>)} callback 88 */ 89 getTableNames: function(callback) 90 { 91 function sortingCallback(error, names) 92 { 93 if (!error) 94 callback(names.sort()); 95 } 96 this._model._agent.getDatabaseTableNames(this._id, sortingCallback); 97 }, 98 99 /** 100 * @param {string} query 101 * @param {function(!Array.<string>=, !Array.<*>=)} onSuccess 102 * @param {function(string)} onError 103 */ 104 executeSql: function(query, onSuccess, onError) 105 { 106 /** 107 * @param {?Protocol.Error} error 108 * @param {!Array.<string>=} columnNames 109 * @param {!Array.<*>=} values 110 * @param {!DatabaseAgent.Error=} errorObj 111 */ 112 function callback(error, columnNames, values, errorObj) 113 { 114 if (error) { 115 onError(error); 116 return; 117 } 118 if (errorObj) { 119 var message; 120 if (errorObj.message) 121 message = errorObj.message; 122 else if (errorObj.code == 2) 123 message = WebInspector.UIString("Database no longer has expected version."); 124 else 125 message = WebInspector.UIString("An unexpected error %s occurred.", errorObj.code); 126 onError(message); 127 return; 128 } 129 onSuccess(columnNames, values); 130 } 131 this._model._agent.executeSQL(this._id, query, callback); 132 } 133 } 134 135 /** 136 * @constructor 137 * @extends {WebInspector.TargetAwareObject} 138 * @param {!WebInspector.Target} target 139 */ 140 WebInspector.DatabaseModel = function(target) 141 { 142 WebInspector.TargetAwareObject.call(this, target); 143 144 this._databases = []; 145 target.registerDatabaseDispatcher(new WebInspector.DatabaseDispatcher(this)); 146 this._agent = target.databaseAgent(); 147 this._agent.enable(); 148 } 149 150 WebInspector.DatabaseModel.Events = { 151 DatabaseAdded: "DatabaseAdded" 152 } 153 154 WebInspector.DatabaseModel.prototype = { 155 /** 156 * @return {!Array.<!WebInspector.Database>} 157 */ 158 databases: function() 159 { 160 var result = []; 161 for (var databaseId in this._databases) 162 result.push(this._databases[databaseId]); 163 return result; 164 }, 165 166 /** 167 * @param {!DatabaseAgent.DatabaseId} databaseId 168 * @return {!WebInspector.Database} 169 */ 170 databaseForId: function(databaseId) 171 { 172 return this._databases[databaseId]; 173 }, 174 175 /** 176 * @param {!WebInspector.Database} database 177 */ 178 _addDatabase: function(database) 179 { 180 this._databases.push(database); 181 this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.DatabaseAdded, database); 182 }, 183 184 __proto__: WebInspector.TargetAwareObject.prototype 185 } 186 187 /** 188 * @constructor 189 * @implements {DatabaseAgent.Dispatcher} 190 * @param {!WebInspector.DatabaseModel} model 191 */ 192 WebInspector.DatabaseDispatcher = function(model) 193 { 194 this._model = model; 195 } 196 197 WebInspector.DatabaseDispatcher.prototype = { 198 /** 199 * @param {!DatabaseAgent.Database} payload 200 */ 201 addDatabase: function(payload) 202 { 203 this._model._addDatabase(new WebInspector.Database( 204 this._model, 205 payload.id, 206 payload.domain, 207 payload.name, 208 payload.version)); 209 } 210 } 211 212 /** 213 * @type {!WebInspector.DatabaseModel} 214 */ 215 WebInspector.databaseModel; 216