forked from facebookarchive/IndexedDB-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebSql.js
110 lines (91 loc) · 3.5 KB
/
webSql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright 2012 Facebook Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
if (window.indexedDB.polyfill)
(function (window, util, undefined) {
var DEFAULT_DB_SIZE = 5 * 1024 * 1024;
util.openDatabase = function (name) {
return new Database(window.openDatabase(indexedDB.DB_PREFIX + name, "", "IndexedDB " + name, DEFAULT_DB_SIZE));
};
var Database_prototype = function (db) {
var db = db;
this.version = db && db.version;
this.transaction = function (callback, errorCallback, successCallback) {
db.transaction(function (tx) {
if (callback) callback(new Transaction(tx));
},
function (error) {
if (errorCallback) errorCallback(wrapSqlError(error));
},
function () {
if (successCallback) successCallback();
});
};
this.readTransaction = function (callback, errorCallback, successCallback) {
db.readTransaction(function (tx) {
if (callback) callback(new Transaction(tx));
},
function (error) {
if (errorCallback) errorCallback(wrapSqlError(error));
},
function () {
if (successCallback) successCallback();
});
};
this.changeVersion = function (oldVersion, newVersion, callback, errorCallback, successCallback) {
db.changeVersion(oldVersion, newVersion,
function (tx) {
if (callback) callback(new Transaction(tx));
},
function (error) {
if (errorCallback) errorCallback(wrapSqlError(error));
},
function () {
if (successCallback) successCallback();
});
};
};
var Database = function (db) {
Database_prototype.call(this, db);
};
Database.prototype = new Database_prototype();
Database.prototype.constructor = Database;
var Transaction_prototype = function (tx) {
var tx = tx;
this.executeSql = function (sql, args, callback, errorCallback) {
//console.log("[SQL]: %s; args: %o", sql, args);
tx.executeSql(sql, args,
function (tx, resultSet) {
if (callback) callback(new Transaction(tx), resultSet);
},
function (tx, error) {
console.error("[SQL Error]: ", error);
if (errorCallback) errorCallback(new Transaction(tx), wrapSqlError(error));
});
}
};
var Transaction = function (tx) {
Transaction_prototype.call(this, tx);
};
Transaction.prototype = new Transaction_prototype();
Transaction.prototype.constructor = Transaction;
// Utils
function wrapSqlError(error) {
// UnknownError
if (error == null || error.message == null) return util.error("UnknownError", undefined, error);
var msg = error.message.toLowerCase();
// ConstraintError
if (msg.indexOf("constraint failed") >= 0 || msg.indexOf("is not unique")) {
return util.error("ConstraintError");
}
}
}(window, window.indexedDB.util));