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 26WebInspector.DatabaseObject = function(id, host, name, version) 27{ 28 this._id = id; 29 this._host = host ? host : WebInspector.UIString("Local File"); 30 this._name = name; 31 this._version = version; 32}; 33 34WebInspector.DatabaseObject.TypeIdentifier = "database"; 35WebInspector.DatabaseObject.HostCookieKey = "database-object-host"; 36WebInspector.DatabaseObject.NameCookieKey = "database-object-name"; 37 38WebInspector.DatabaseObject.prototype = { 39 constructor: WebInspector.DatabaseObject, 40 41 // Public 42 43 get id() 44 { 45 return this._id; 46 }, 47 48 get host() 49 { 50 return this._host; 51 }, 52 53 get name() 54 { 55 return this._name; 56 }, 57 58 get version() 59 { 60 return this._version; 61 }, 62 63 saveIdentityToCookie: function(cookie) 64 { 65 cookie[WebInspector.DatabaseObject.HostCookieKey] = this.host; 66 cookie[WebInspector.DatabaseObject.NameCookieKey] = this.name; 67 }, 68 69 getTableNames: function(callback) 70 { 71 function sortingCallback(error, names) 72 { 73 if (!error) 74 callback(names.sort()); 75 } 76 77 DatabaseAgent.getDatabaseTableNames(this._id, sortingCallback); 78 }, 79 80 executeSQL: function(query, successCallback, errorCallback) 81 { 82 function queryCallback(columnNames, values, sqlError) 83 { 84 if (sqlError) { 85 var message; 86 87 switch (sqlError.code) { 88 case SQLException.VERSION_ERR: 89 message = WebInspector.UIString("Database no longer has expected version."); 90 break; 91 case SQLException.TOO_LARGE_ERR: 92 message = WebInspector.UIString("Data returned from the database is too large."); 93 break; 94 default: 95 message = WebInspector.UIString("An unexpected error occurred."); 96 break; 97 } 98 99 errorCallback(message); 100 return; 101 } 102 103 successCallback(columnNames, values); 104 } 105 106 function callback(error, result) 107 { 108 if (error) { 109 errorCallback(WebInspector.UIString("An unexpected error occurred.")); 110 return; 111 } 112 113 // COMPATIBILITY (iOS 6): Newer versions of DatabaseAgent.executeSQL can delay before 114 // sending the results. The version on iOS 6 instead returned a transactionId that 115 // would be used later in the sqlTransactionSucceeded or sqlTransactionFailed events. 116 if ("transactionId" in result) { 117 if (!result.success) { 118 errorCallback(WebInspector.UIString("An unexpected error occurred.")); 119 return; 120 } 121 122 WebInspector.DatabaseObserver._callbacks[result.transactionId] = queryCallback; 123 return; 124 } 125 126 queryCallback(result.columnNames, result.values, result.sqlError); 127 } 128 129 // COMPATIBILITY (iOS 6): Since the parameters of the DatabaseAgent.executeSQL callback differ 130 // we need the result object to lookup parameters by name. 131 callback.expectsResultObject = true; 132 133 DatabaseAgent.executeSQL(this._id, query, callback); 134 } 135}; 136