DatabaseObject.js 4.2 KB

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