Database.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * @constructor
  30. * @param {WebInspector.DatabaseModel} model
  31. */
  32. WebInspector.Database = function(model, id, domain, name, version)
  33. {
  34. this._model = model;
  35. this._id = id;
  36. this._domain = domain;
  37. this._name = name;
  38. this._version = version;
  39. }
  40. WebInspector.Database.prototype = {
  41. /** @return {string} */
  42. get id()
  43. {
  44. return this._id;
  45. },
  46. /** @return {string} */
  47. get name()
  48. {
  49. return this._name;
  50. },
  51. set name(x)
  52. {
  53. this._name = x;
  54. },
  55. /** @return {string} */
  56. get version()
  57. {
  58. return this._version;
  59. },
  60. set version(x)
  61. {
  62. this._version = x;
  63. },
  64. /** @return {string} */
  65. get domain()
  66. {
  67. return this._domain;
  68. },
  69. set domain(x)
  70. {
  71. this._domain = x;
  72. },
  73. /**
  74. * @param {function(Array.<string>)} callback
  75. */
  76. getTableNames: function(callback)
  77. {
  78. function sortingCallback(error, names)
  79. {
  80. if (!error)
  81. callback(names.sort());
  82. }
  83. DatabaseAgent.getDatabaseTableNames(this._id, sortingCallback);
  84. },
  85. /**
  86. * @param {string} query
  87. * @param {function(Array.<string>=, Array.<*>=)} onSuccess
  88. * @param {function(string)} onError
  89. */
  90. executeSql: function(query, onSuccess, onError)
  91. {
  92. /**
  93. * @param {?Protocol.Error} error
  94. * @param {Array.<string>=} columnNames
  95. * @param {Array.<*>=} values
  96. * @param {DatabaseAgent.Error=} errorObj
  97. */
  98. function callback(error, columnNames, values, errorObj)
  99. {
  100. if (error) {
  101. onError(error);
  102. return;
  103. }
  104. if (errorObj) {
  105. var message;
  106. if (errorObj.message)
  107. message = errorObj.message;
  108. else if (errorObj.code == 2)
  109. message = WebInspector.UIString("Database no longer has expected version.");
  110. else
  111. message = WebInspector.UIString("An unexpected error %s occurred.", errorObj.code);
  112. onError(message);
  113. return;
  114. }
  115. onSuccess(columnNames, values);
  116. }
  117. DatabaseAgent.executeSQL(this._id, query, callback.bind(this));
  118. }
  119. }
  120. /**
  121. * @constructor
  122. * @extends {WebInspector.Object}
  123. */
  124. WebInspector.DatabaseModel = function()
  125. {
  126. this._databases = [];
  127. InspectorBackend.registerDatabaseDispatcher(new WebInspector.DatabaseDispatcher(this));
  128. DatabaseAgent.enable();
  129. }
  130. WebInspector.DatabaseModel.Events = {
  131. DatabaseAdded: "DatabaseAdded"
  132. }
  133. WebInspector.DatabaseModel.prototype = {
  134. /**
  135. * @return {Array.<WebInspector.Database>}
  136. */
  137. databases: function()
  138. {
  139. var result = [];
  140. for (var databaseId in this._databases)
  141. result.push(this._databases[databaseId]);
  142. return result;
  143. },
  144. /**
  145. * @param {DatabaseAgent.DatabaseId} databaseId
  146. * @return {WebInspector.Database}
  147. */
  148. databaseForId: function(databaseId)
  149. {
  150. return this._databases[databaseId];
  151. },
  152. /**
  153. * @param {WebInspector.Database} database
  154. */
  155. _addDatabase: function(database)
  156. {
  157. this._databases.push(database);
  158. this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.DatabaseAdded, database);
  159. },
  160. __proto__: WebInspector.Object.prototype
  161. }
  162. /**
  163. * @constructor
  164. * @implements {DatabaseAgent.Dispatcher}
  165. * @param {WebInspector.DatabaseModel} model
  166. */
  167. WebInspector.DatabaseDispatcher = function(model)
  168. {
  169. this._model = model;
  170. }
  171. WebInspector.DatabaseDispatcher.prototype = {
  172. /**
  173. * @param {DatabaseAgent.Database} payload
  174. */
  175. addDatabase: function(payload)
  176. {
  177. this._model._addDatabase(new WebInspector.Database(
  178. this._model,
  179. payload.id,
  180. payload.domain,
  181. payload.name,
  182. payload.version));
  183. }
  184. }
  185. /**
  186. * @type {WebInspector.DatabaseModel}
  187. */
  188. WebInspector.databaseModel = null;