persistence-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/lib/generators/persistence/install_generator.rb +22 -0
- data/lib/generators/persistence/templates/application.js +10 -0
- data/lib/persistence-rails.rb +1 -0
- data/lib/persistence/rails.rb +8 -0
- data/lib/persistence/rails/engine.rb +6 -0
- data/lib/persistence/rails/version.rb +5 -0
- data/persistence-rails.gemspec +17 -0
- data/vendor/assets/javascript/persistence.all.js +16 -0
- data/vendor/assets/javascript/persistence.core.js +2419 -0
- data/vendor/assets/javascript/persistence.jquery.js +103 -0
- data/vendor/assets/javascript/persistence.jquery.mobile.js +256 -0
- data/vendor/assets/javascript/persistence.js +5 -0
- data/vendor/assets/javascript/persistence.migrations.js +303 -0
- data/vendor/assets/javascript/persistence.pool.js +47 -0
- data/vendor/assets/javascript/persistence.search.js +293 -0
- data/vendor/assets/javascript/persistence.store.appengine.js +412 -0
- data/vendor/assets/javascript/persistence.store.config.js +29 -0
- data/vendor/assets/javascript/persistence.store.memory.js +239 -0
- data/vendor/assets/javascript/persistence.store.mysql.js +127 -0
- data/vendor/assets/javascript/persistence.store.sql.js +900 -0
- data/vendor/assets/javascript/persistence.store.sqlite.js +123 -0
- data/vendor/assets/javascript/persistence.store.sqlite3.js +124 -0
- data/vendor/assets/javascript/persistence.store.titanium.js +193 -0
- data/vendor/assets/javascript/persistence.store.websql.js +218 -0
- data/vendor/assets/javascript/persistence.sync.js +353 -0
- metadata +76 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
//= require persistence.core
|
2
|
+
|
3
|
+
/**
|
4
|
+
* This back-end depends on the node.js asynchronous SQLite driver as found on:
|
5
|
+
* https://github.com/orlandov/node-sqlite
|
6
|
+
* Easy install using npm:
|
7
|
+
* npm install sqlite
|
8
|
+
* @author Eugene Ware
|
9
|
+
*/
|
10
|
+
var sys = require('sys');
|
11
|
+
var sql = require('./persistence.store.sql');
|
12
|
+
var sqlite = require('sqlite');
|
13
|
+
|
14
|
+
var db, username, password;
|
15
|
+
|
16
|
+
function log(o) {
|
17
|
+
sys.print(sys.inspect(o) + "\n");
|
18
|
+
}
|
19
|
+
|
20
|
+
|
21
|
+
exports.config = function(persistence, dbPath) {
|
22
|
+
exports.getSession = function(cb) {
|
23
|
+
var that = {};
|
24
|
+
cb = cb || function() { };
|
25
|
+
var conn = new sqlite.Database();
|
26
|
+
conn.open(dbPath, cb);
|
27
|
+
|
28
|
+
var session = new persistence.Session(that);
|
29
|
+
session.transaction = function (explicitCommit, fn) {
|
30
|
+
if (typeof arguments[0] === "function") {
|
31
|
+
fn = arguments[0];
|
32
|
+
explicitCommit = false;
|
33
|
+
}
|
34
|
+
var tx = transaction(conn);
|
35
|
+
if (explicitCommit) {
|
36
|
+
tx.executeSql("START TRANSACTION", null, function(){
|
37
|
+
fn(tx)
|
38
|
+
});
|
39
|
+
}
|
40
|
+
else
|
41
|
+
fn(tx);
|
42
|
+
};
|
43
|
+
|
44
|
+
session.close = function(cb) {
|
45
|
+
cb = cb || function() {};
|
46
|
+
conn.close(cb);
|
47
|
+
};
|
48
|
+
return session;
|
49
|
+
};
|
50
|
+
|
51
|
+
function transaction(conn){
|
52
|
+
var that = {};
|
53
|
+
// TODO: add check for db opened or closed
|
54
|
+
that.executeSql = function(query, args, successFn, errorFn){
|
55
|
+
function cb(err, result){
|
56
|
+
if (err) {
|
57
|
+
log(err.message);
|
58
|
+
that.errorHandler && that.errorHandler(err);
|
59
|
+
errorFn && errorFn(null, err);
|
60
|
+
return;
|
61
|
+
}
|
62
|
+
if (successFn) {
|
63
|
+
successFn(result);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
if (persistence.debug) {
|
67
|
+
sys.print(query + "\n");
|
68
|
+
args && args.length > 0 && sys.print(args.join(",") + "\n")
|
69
|
+
}
|
70
|
+
if (!args) {
|
71
|
+
conn.execute(query, cb);
|
72
|
+
}
|
73
|
+
else {
|
74
|
+
conn.execute(query, args, cb);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
that.commit = function(session, callback){
|
79
|
+
session.flush(that, function(){
|
80
|
+
that.executeSql("COMMIT", null, callback);
|
81
|
+
})
|
82
|
+
}
|
83
|
+
|
84
|
+
that.rollback = function(session, callback){
|
85
|
+
that.executeSql("ROLLBACK", null, function() {
|
86
|
+
session.clean();
|
87
|
+
callback();
|
88
|
+
});
|
89
|
+
}
|
90
|
+
return that;
|
91
|
+
}
|
92
|
+
|
93
|
+
///////////////////////// SQLite dialect
|
94
|
+
|
95
|
+
persistence.sqliteDialect = {
|
96
|
+
// columns is an array of arrays, e.g.
|
97
|
+
// [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]]
|
98
|
+
createTable: function(tableName, columns) {
|
99
|
+
var tm = persistence.typeMapper;
|
100
|
+
var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (";
|
101
|
+
var defs = [];
|
102
|
+
for(var i = 0; i < columns.length; i++) {
|
103
|
+
var column = columns[i];
|
104
|
+
defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : ""));
|
105
|
+
}
|
106
|
+
sql += defs.join(", ");
|
107
|
+
sql += ')';
|
108
|
+
return sql;
|
109
|
+
},
|
110
|
+
|
111
|
+
// columns is array of column names, e.g.
|
112
|
+
// ["id"]
|
113
|
+
createIndex: function(tableName, columns, options) {
|
114
|
+
options = options || {};
|
115
|
+
return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") +
|
116
|
+
"` ON `" + tableName + "` (" +
|
117
|
+
columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
|
118
|
+
}
|
119
|
+
};
|
120
|
+
|
121
|
+
sql.config(persistence, persistence.sqliteDialect);
|
122
|
+
};
|
123
|
+
|
@@ -0,0 +1,124 @@
|
|
1
|
+
//= require persistence.core
|
2
|
+
|
3
|
+
/**
|
4
|
+
* This back-end depends on the node.js asynchronous SQLite3 driver as found on:
|
5
|
+
* https://github.com/developmentseed/node-sqlite3
|
6
|
+
* Easy install using npm:
|
7
|
+
* npm install sqlite3
|
8
|
+
* @author Eugene Ware
|
9
|
+
* @author Jeff Kunkle
|
10
|
+
* @author Joe Ferner
|
11
|
+
*/
|
12
|
+
var sys = require('sys');
|
13
|
+
var sql = require('./persistence.store.sql');
|
14
|
+
var sqlite = require('sqlite3');
|
15
|
+
|
16
|
+
var db, username, password;
|
17
|
+
|
18
|
+
function log(o) {
|
19
|
+
sys.print(sys.inspect(o) + "\n");
|
20
|
+
}
|
21
|
+
|
22
|
+
|
23
|
+
exports.config = function(persistence, dbPath) {
|
24
|
+
exports.getSession = function(cb) {
|
25
|
+
var that = {};
|
26
|
+
cb = cb || function() { };
|
27
|
+
var conn = new sqlite.Database(dbPath, cb);
|
28
|
+
|
29
|
+
var session = new persistence.Session(that);
|
30
|
+
session.transaction = function (explicitCommit, fn) {
|
31
|
+
if (typeof arguments[0] === "function") {
|
32
|
+
fn = arguments[0];
|
33
|
+
explicitCommit = false;
|
34
|
+
}
|
35
|
+
var tx = transaction(conn);
|
36
|
+
if (explicitCommit) {
|
37
|
+
tx.executeSql("START TRANSACTION", null, function(){
|
38
|
+
fn(tx)
|
39
|
+
});
|
40
|
+
}
|
41
|
+
else
|
42
|
+
fn(tx);
|
43
|
+
};
|
44
|
+
|
45
|
+
session.close = function(cb) {
|
46
|
+
cb = cb || function() {};
|
47
|
+
conn.close(cb);
|
48
|
+
};
|
49
|
+
return session;
|
50
|
+
};
|
51
|
+
|
52
|
+
function transaction(conn){
|
53
|
+
var that = {};
|
54
|
+
// TODO: add check for db opened or closed
|
55
|
+
that.executeSql = function(query, args, successFn, errorFn){
|
56
|
+
function cb(err, result){
|
57
|
+
if (err) {
|
58
|
+
log(err.message);
|
59
|
+
that.errorHandler && that.errorHandler(err);
|
60
|
+
errorFn && errorFn(null, err);
|
61
|
+
return;
|
62
|
+
}
|
63
|
+
if (successFn) {
|
64
|
+
successFn(result);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
if (persistence.debug) {
|
68
|
+
sys.print(query + "\n");
|
69
|
+
args && args.length > 0 && sys.print(args.join(",") + "\n")
|
70
|
+
}
|
71
|
+
if (!args) {
|
72
|
+
conn.all(query, cb);
|
73
|
+
}
|
74
|
+
else {
|
75
|
+
conn.all(query, args, cb);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
that.commit = function(session, callback){
|
80
|
+
session.flush(that, function(){
|
81
|
+
that.executeSql("COMMIT", null, callback);
|
82
|
+
})
|
83
|
+
}
|
84
|
+
|
85
|
+
that.rollback = function(session, callback){
|
86
|
+
that.executeSql("ROLLBACK", null, function() {
|
87
|
+
session.clean();
|
88
|
+
callback();
|
89
|
+
});
|
90
|
+
}
|
91
|
+
return that;
|
92
|
+
}
|
93
|
+
|
94
|
+
///////////////////////// SQLite dialect
|
95
|
+
|
96
|
+
persistence.sqliteDialect = {
|
97
|
+
// columns is an array of arrays, e.g.
|
98
|
+
// [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]]
|
99
|
+
createTable: function(tableName, columns) {
|
100
|
+
var tm = persistence.typeMapper;
|
101
|
+
var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (";
|
102
|
+
var defs = [];
|
103
|
+
for(var i = 0; i < columns.length; i++) {
|
104
|
+
var column = columns[i];
|
105
|
+
defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : ""));
|
106
|
+
}
|
107
|
+
sql += defs.join(", ");
|
108
|
+
sql += ')';
|
109
|
+
return sql;
|
110
|
+
},
|
111
|
+
|
112
|
+
// columns is array of column names, e.g.
|
113
|
+
// ["id"]
|
114
|
+
createIndex: function(tableName, columns, options) {
|
115
|
+
options = options || {};
|
116
|
+
return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") +
|
117
|
+
"` ON `" + tableName + "` (" +
|
118
|
+
columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
|
119
|
+
}
|
120
|
+
};
|
121
|
+
|
122
|
+
sql.config(persistence, persistence.sqliteDialect);
|
123
|
+
};
|
124
|
+
|
@@ -0,0 +1,193 @@
|
|
1
|
+
//= require persistence.core
|
2
|
+
|
3
|
+
try {
|
4
|
+
if(!window) {
|
5
|
+
window = {};
|
6
|
+
//exports.console = console;
|
7
|
+
}
|
8
|
+
} catch(e) {
|
9
|
+
window = {};
|
10
|
+
exports.console = console;
|
11
|
+
}
|
12
|
+
|
13
|
+
var persistence = (window && window.persistence) ? window.persistence : {};
|
14
|
+
|
15
|
+
if(!persistence.store) {
|
16
|
+
persistence.store = {};
|
17
|
+
}
|
18
|
+
|
19
|
+
persistence.store.titanium = {};
|
20
|
+
|
21
|
+
persistence.store.titanium.config = function(persistence, dbname) {
|
22
|
+
var conn = null;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Create a transaction
|
26
|
+
*
|
27
|
+
* @param callback,
|
28
|
+
* the callback function to be invoked when the transaction
|
29
|
+
* starts, taking the transaction object as argument
|
30
|
+
*/
|
31
|
+
persistence.transaction = function (callback) {
|
32
|
+
if(!conn) {
|
33
|
+
throw new Error("No ongoing database connection, please connect first.");
|
34
|
+
} else {
|
35
|
+
conn.transaction(callback);
|
36
|
+
}
|
37
|
+
};
|
38
|
+
|
39
|
+
////////// Low-level database interface, abstracting from HTML5 and Gears databases \\\\
|
40
|
+
persistence.db = persistence.db || {};
|
41
|
+
|
42
|
+
persistence.db.conn = null;
|
43
|
+
|
44
|
+
persistence.db.titanium = {};
|
45
|
+
|
46
|
+
persistence.db.titanium.connect = function (dbname) {
|
47
|
+
var that = {};
|
48
|
+
var conn = Titanium.Database.open(dbname);
|
49
|
+
|
50
|
+
that.transaction = function (fn) {
|
51
|
+
fn(persistence.db.titanium.transaction(conn));
|
52
|
+
};
|
53
|
+
return that;
|
54
|
+
};
|
55
|
+
|
56
|
+
persistence.db.titanium.transaction = function (conn) {
|
57
|
+
var that = {};
|
58
|
+
that.executeSql = function (query, args, successFn, errorFn) {
|
59
|
+
|
60
|
+
if(persistence.debug) {
|
61
|
+
console.log(query, args);
|
62
|
+
}
|
63
|
+
try {
|
64
|
+
var executeVarArgs = [query];
|
65
|
+
if (args) {
|
66
|
+
executeVarArgs = executeVarArgs.concat(args);
|
67
|
+
};
|
68
|
+
var rs = Function.apply.call(conn.execute, conn, executeVarArgs);
|
69
|
+
if (successFn) {
|
70
|
+
var results = [];
|
71
|
+
if (rs) {
|
72
|
+
while (rs.isValidRow()) {
|
73
|
+
var result = {};
|
74
|
+
for ( var i = 0; i < rs.fieldCount(); i++) {
|
75
|
+
result[rs.fieldName(i)] = rs.field(i);
|
76
|
+
}
|
77
|
+
results.push(result);
|
78
|
+
rs.next();
|
79
|
+
}
|
80
|
+
rs.close();
|
81
|
+
};
|
82
|
+
successFn(results);
|
83
|
+
}
|
84
|
+
} catch(e) {
|
85
|
+
if (errorFn) {
|
86
|
+
errorFn(null, e);
|
87
|
+
};
|
88
|
+
}
|
89
|
+
};
|
90
|
+
return that;
|
91
|
+
};
|
92
|
+
|
93
|
+
///////////////////////// SQLite dialect
|
94
|
+
|
95
|
+
persistence.store.titanium.sqliteDialect = {
|
96
|
+
// columns is an array of arrays, e.g.
|
97
|
+
// [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]]
|
98
|
+
createTable: function(tableName, columns) {
|
99
|
+
var tm = persistence.typeMapper;
|
100
|
+
var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (";
|
101
|
+
var defs = [];
|
102
|
+
for(var i = 0; i < columns.length; i++) {
|
103
|
+
var column = columns[i];
|
104
|
+
defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : ""));
|
105
|
+
}
|
106
|
+
sql += defs.join(", ");
|
107
|
+
sql += ')';
|
108
|
+
return sql;
|
109
|
+
},
|
110
|
+
|
111
|
+
// columns is array of column names, e.g.
|
112
|
+
// ["id"]
|
113
|
+
createIndex: function(tableName, columns, options) {
|
114
|
+
options = options || {};
|
115
|
+
return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") +
|
116
|
+
"` ON `" + tableName + "` (" +
|
117
|
+
columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
|
118
|
+
},
|
119
|
+
|
120
|
+
typeMapper: {
|
121
|
+
idType: persistence.store.sql.defaultTypeMapper.idType,
|
122
|
+
classNameType: persistence.store.sql.defaultTypeMapper.classNameType,
|
123
|
+
inVar: persistence.store.sql.defaultTypeMapper.inVar,
|
124
|
+
outVar: persistence.store.sql.defaultTypeMapper.outVar,
|
125
|
+
outId: persistence.store.sql.defaultTypeMapper.outId,
|
126
|
+
inIdVar: persistence.store.sql.defaultTypeMapper.inIdVar,
|
127
|
+
outIdVar: persistence.store.sql.defaultTypeMapper.outIdVar,
|
128
|
+
entityIdToDbId: persistence.store.sql.defaultTypeMapper.entityIdToDbId,
|
129
|
+
zeroPaddingMap: ['0000000000000000',
|
130
|
+
'000000000000000',
|
131
|
+
'00000000000000',
|
132
|
+
'0000000000000',
|
133
|
+
'000000000000',
|
134
|
+
'00000000000',
|
135
|
+
'0000000000',
|
136
|
+
'000000000',
|
137
|
+
'00000000',
|
138
|
+
'0000000',
|
139
|
+
'000000',
|
140
|
+
'00000',
|
141
|
+
'0000',
|
142
|
+
'000',
|
143
|
+
'00',
|
144
|
+
'0'],
|
145
|
+
zeroPadded: function(val) {
|
146
|
+
var result = val.toString();
|
147
|
+
if (result.length < 16) {
|
148
|
+
return persistence.store.titanium.sqliteDialect.typeMapper.zeroPaddingMap[result.length] + result;
|
149
|
+
} else {
|
150
|
+
return result;
|
151
|
+
};
|
152
|
+
},
|
153
|
+
columnType: function(type) {
|
154
|
+
if (type === 'BIGINT') {
|
155
|
+
return 'TEXT';
|
156
|
+
} else {
|
157
|
+
return persistence.store.sql.defaultTypeMapper.columnType(type);
|
158
|
+
};
|
159
|
+
},
|
160
|
+
dbValToEntityVal: function(val, type){
|
161
|
+
if (val === null || val === undefined) {
|
162
|
+
return val;
|
163
|
+
} else if (type === 'BIGIN') {
|
164
|
+
return parseInt(val);
|
165
|
+
} else {
|
166
|
+
return persistence.store.sql.defaultTypeMapper.dbValToEntityVal(val, type);
|
167
|
+
}
|
168
|
+
},
|
169
|
+
entityValToDbVal: function(val, type){
|
170
|
+
if (val === undefined || val === null) {
|
171
|
+
return null;
|
172
|
+
} else if (type === 'BIGINT') {
|
173
|
+
return persistence.store.titanium.sqliteDialect.typeMapper.zeroPadded(val);
|
174
|
+
} else {
|
175
|
+
return persistence.store.sql.defaultTypeMapper.entityValToDbVal(val, type);
|
176
|
+
};
|
177
|
+
}
|
178
|
+
}
|
179
|
+
};
|
180
|
+
|
181
|
+
// Configure persistence for generic sql persistence, using sqliteDialect
|
182
|
+
persistence.store.sql.config(persistence, persistence.store.titanium.sqliteDialect);
|
183
|
+
|
184
|
+
// Make the connection
|
185
|
+
conn = persistence.db.titanium.connect(dbname);
|
186
|
+
if(!conn) {
|
187
|
+
throw new Error("No supported database found");
|
188
|
+
}
|
189
|
+
};
|
190
|
+
|
191
|
+
try {
|
192
|
+
exports.persistence = persistence;
|
193
|
+
} catch(e) {}
|
@@ -0,0 +1,218 @@
|
|
1
|
+
//= require persistence.core
|
2
|
+
|
3
|
+
try {
|
4
|
+
if(!window) {
|
5
|
+
window = {};
|
6
|
+
//exports.console = console;
|
7
|
+
}
|
8
|
+
} catch(e) {
|
9
|
+
window = {};
|
10
|
+
exports.console = console;
|
11
|
+
}
|
12
|
+
|
13
|
+
var persistence = (window && window.persistence) ? window.persistence : {};
|
14
|
+
|
15
|
+
if(!persistence.store) {
|
16
|
+
persistence.store = {};
|
17
|
+
}
|
18
|
+
|
19
|
+
persistence.store.websql = {};
|
20
|
+
|
21
|
+
|
22
|
+
persistence.store.websql.config = function(persistence, dbname, description, size) {
|
23
|
+
var conn = null;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Create a transaction
|
27
|
+
*
|
28
|
+
* @param callback,
|
29
|
+
* the callback function to be invoked when the transaction
|
30
|
+
* starts, taking the transaction object as argument
|
31
|
+
*/
|
32
|
+
persistence.transaction = function (callback) {
|
33
|
+
if(!conn) {
|
34
|
+
throw new Error("No ongoing database connection, please connect first.");
|
35
|
+
} else {
|
36
|
+
conn.transaction(callback);
|
37
|
+
}
|
38
|
+
};
|
39
|
+
|
40
|
+
////////// Low-level database interface, abstracting from HTML5 and Gears databases \\\\
|
41
|
+
persistence.db = persistence.db || {};
|
42
|
+
|
43
|
+
persistence.db.implementation = "unsupported";
|
44
|
+
persistence.db.conn = null;
|
45
|
+
|
46
|
+
// window object does not exist on Qt Declarative UI (http://doc.trolltech.org/4.7-snapshot/declarativeui.html)
|
47
|
+
if (window && window.openDatabase) {
|
48
|
+
persistence.db.implementation = "html5";
|
49
|
+
} else if (window && window.google && google.gears) {
|
50
|
+
persistence.db.implementation = "gears";
|
51
|
+
} else {
|
52
|
+
try {
|
53
|
+
if (openDatabaseSync) {
|
54
|
+
// TODO: find a browser that implements openDatabaseSync and check out if
|
55
|
+
// it is attached to the window or some other object
|
56
|
+
persistence.db.implementation = "html5-sync";
|
57
|
+
}
|
58
|
+
} catch(e) {
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
persistence.db.html5 = {};
|
63
|
+
|
64
|
+
persistence.db.html5.connect = function (dbname, description, size) {
|
65
|
+
var that = {};
|
66
|
+
var conn = openDatabase(dbname, '1.0', description, size);
|
67
|
+
|
68
|
+
that.transaction = function (fn) {
|
69
|
+
return conn.transaction(function (sqlt) {
|
70
|
+
return fn(persistence.db.html5.transaction(sqlt));
|
71
|
+
});
|
72
|
+
};
|
73
|
+
return that;
|
74
|
+
};
|
75
|
+
|
76
|
+
persistence.db.html5.transaction = function (t) {
|
77
|
+
var that = {};
|
78
|
+
that.executeSql = function (query, args, successFn, errorFn) {
|
79
|
+
if(persistence.debug) {
|
80
|
+
console.log(query, args);
|
81
|
+
}
|
82
|
+
t.executeSql(query, args, function (_, result) {
|
83
|
+
if (successFn) {
|
84
|
+
var results = [];
|
85
|
+
for ( var i = 0; i < result.rows.length; i++) {
|
86
|
+
results.push(result.rows.item(i));
|
87
|
+
}
|
88
|
+
successFn(results);
|
89
|
+
}
|
90
|
+
}, errorFn);
|
91
|
+
};
|
92
|
+
return that;
|
93
|
+
};
|
94
|
+
|
95
|
+
persistence.db.html5Sync = {};
|
96
|
+
|
97
|
+
persistence.db.html5Sync.connect = function (dbname, description, size) {
|
98
|
+
var that = {};
|
99
|
+
var conn = openDatabaseSync(dbname, '1.0', description, size);
|
100
|
+
|
101
|
+
that.transaction = function (fn) {
|
102
|
+
return conn.transaction(function (sqlt) {
|
103
|
+
return fn(persistence.db.html5Sync.transaction(sqlt));
|
104
|
+
});
|
105
|
+
};
|
106
|
+
return that;
|
107
|
+
};
|
108
|
+
|
109
|
+
persistence.db.html5Sync.transaction = function (t) {
|
110
|
+
var that = {};
|
111
|
+
that.executeSql = function (query, args, successFn, errorFn) {
|
112
|
+
if (args == null) args = [];
|
113
|
+
|
114
|
+
if(persistence.debug) {
|
115
|
+
console.log(query, args);
|
116
|
+
}
|
117
|
+
|
118
|
+
var result = t.executeSql(query, args);
|
119
|
+
if (result) {
|
120
|
+
if (successFn) {
|
121
|
+
var results = [];
|
122
|
+
for ( var i = 0; i < result.rows.length; i++) {
|
123
|
+
results.push(result.rows.item(i));
|
124
|
+
}
|
125
|
+
successFn(results);
|
126
|
+
}
|
127
|
+
}
|
128
|
+
};
|
129
|
+
return that;
|
130
|
+
};
|
131
|
+
|
132
|
+
persistence.db.gears = {};
|
133
|
+
|
134
|
+
persistence.db.gears.connect = function (dbname) {
|
135
|
+
var that = {};
|
136
|
+
var conn = google.gears.factory.create('beta.database');
|
137
|
+
conn.open(dbname);
|
138
|
+
|
139
|
+
that.transaction = function (fn) {
|
140
|
+
fn(persistence.db.gears.transaction(conn));
|
141
|
+
};
|
142
|
+
return that;
|
143
|
+
};
|
144
|
+
|
145
|
+
persistence.db.gears.transaction = function (conn) {
|
146
|
+
var that = {};
|
147
|
+
that.executeSql = function (query, args, successFn, errorFn) {
|
148
|
+
if(persistence.debug) {
|
149
|
+
console.log(query, args);
|
150
|
+
}
|
151
|
+
var rs = conn.execute(query, args);
|
152
|
+
if (successFn) {
|
153
|
+
var results = [];
|
154
|
+
while (rs.isValidRow()) {
|
155
|
+
var result = {};
|
156
|
+
for ( var i = 0; i < rs.fieldCount(); i++) {
|
157
|
+
result[rs.fieldName(i)] = rs.field(i);
|
158
|
+
}
|
159
|
+
results.push(result);
|
160
|
+
rs.next();
|
161
|
+
}
|
162
|
+
successFn(results);
|
163
|
+
}
|
164
|
+
};
|
165
|
+
return that;
|
166
|
+
};
|
167
|
+
|
168
|
+
persistence.db.connect = function (dbname, description, size) {
|
169
|
+
if (persistence.db.implementation == "html5") {
|
170
|
+
return persistence.db.html5.connect(dbname, description, size);
|
171
|
+
} else if (persistence.db.implementation == "html5-sync") {
|
172
|
+
return persistence.db.html5Sync.connect(dbname, description, size);
|
173
|
+
} else if (persistence.db.implementation == "gears") {
|
174
|
+
return persistence.db.gears.connect(dbname);
|
175
|
+
}
|
176
|
+
};
|
177
|
+
|
178
|
+
///////////////////////// SQLite dialect
|
179
|
+
|
180
|
+
persistence.store.websql.sqliteDialect = {
|
181
|
+
// columns is an array of arrays, e.g.
|
182
|
+
// [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]]
|
183
|
+
createTable: function(tableName, columns) {
|
184
|
+
var tm = persistence.typeMapper;
|
185
|
+
var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (";
|
186
|
+
var defs = [];
|
187
|
+
for(var i = 0; i < columns.length; i++) {
|
188
|
+
var column = columns[i];
|
189
|
+
defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : ""));
|
190
|
+
}
|
191
|
+
sql += defs.join(", ");
|
192
|
+
sql += ')';
|
193
|
+
return sql;
|
194
|
+
},
|
195
|
+
|
196
|
+
// columns is array of column names, e.g.
|
197
|
+
// ["id"]
|
198
|
+
createIndex: function(tableName, columns, options) {
|
199
|
+
options = options || {};
|
200
|
+
return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") +
|
201
|
+
"` ON `" + tableName + "` (" +
|
202
|
+
columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
|
203
|
+
}
|
204
|
+
};
|
205
|
+
|
206
|
+
// Configure persistence for generic sql persistence, using sqliteDialect
|
207
|
+
persistence.store.sql.config(persistence, persistence.store.websql.sqliteDialect);
|
208
|
+
|
209
|
+
// Make the connection
|
210
|
+
conn = persistence.db.connect(dbname, description, size);
|
211
|
+
if(!conn) {
|
212
|
+
throw new Error("No supported database found in this browser.");
|
213
|
+
}
|
214
|
+
};
|
215
|
+
|
216
|
+
try {
|
217
|
+
exports.persistence = persistence;
|
218
|
+
} catch(e) {}
|