/
moosql.js
executable file
·135 lines (130 loc) · 4.46 KB
/
moosql.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
---
description: MooSQL class, a wrapper for the HTML5 SQL storage.
license: MIT-style
authors:
- Szikszai Gusztáv
requires:
- Class
- Class.Options
- Class.Events
provides: [MooSQL]
...
*/
var MooSQL;
MooSQL = new Class({
Implements: [Options, Events],
options: {
dbName: '',
dbVersion: '',
dbDesc: '',
dbSize: 20 * 100
},
initialize: function(options) {
this.setOptions(options);
if (window.openDatabase !== null) {
this.db = window.openDatabase(this.options.dbName, this.options.dbVersion, this.options.dbDesc, this.options.dbSize, (function(db) {
this.db = db;
return this.fireEvent('databaseCreated');
}).bind(this));
}
if (this.db !== null) {
this.fireEvent('databaseReady');
} else {
this.fireEvent('notSupported');
}
return this;
},
exec: function(statement, callback, args) {
return this.db.transaction((function(tr) {
return tr.executeSql(statement, args, callback || function() {}, (function(tr, err) {
callback.run([tr, err]);
this.fireEvent('statementError', err);
return false;
}).bind(this));
}).bind(this), (function(tr, err) {
callback.run(tr, err);
this.fireEvent('transactionError', err);
return false;
}).bind(this));
},
like: function(table, what, Where, callback) {
var wheremap;
if (Where === null) {
wheremap = '';
} else {
wheremap = this.likeMap(Where);
}
return this.exec("SELECT " + $splat(what).join(',') + (" FROM '" + (table) + "' ") + (Where === (typeof null !== "undefined" && null !== null) ? ';' : ("WHERE " + (wheremap) + ";")), callback);
},
findAll: function(tables, callback) {
var query;
query = '';
$splat(tables).each(function(item, i) {
return i === 0 ? query += ("SELECT *, '" + (item) + "' AS type FROM '" + (item) + "' ") : query += ("union SELECT *, '" + (item) + "' AS type FROM '" + (item) + "' ");
});
return this.exec("" + (query) + ";", callback);
},
findA: function(table, callback) {
return this.exec("SELECT * FROM '" + table + "';", callback);
},
find: function(table, what, Where, callback) {
var wheremap;
if (Where === null) {
wheremap = '';
} else {
wheremap = this.whereMap(Where);
}
return this.exec("SELECT " + $splat(what).join(',') + (" FROM '" + (table) + "' ") + (Where === (typeof null !== "undefined" && null !== null) ? ';' : ("WHERE " + (wheremap) + ";")), callback);
},
tableExists: function(name, callback) {
return this.exec("select * from " + (name), callback);
},
create: function(table, values, callback) {
var valuesmap;
valuesmap = new Hash(values).map(function(value, key) {
return '"' + key + '" ' + $splat(value).join(" ").toUpperCase();
});
values = $splat(valuesmap.getValues()).join(', ');
return this.exec("CREATE TABLE '" + (table) + "' ( " + (values) + " )", callback);
},
save: function(table, set, Where, callback) {
return this.find(table, '*', Where, (function(tr, result) {
return result.rows.length > 0 ? this.update(table, set, Where, callback) : this.insert(table, $extend(set, Where), callback);
}).bindWithEvent(this));
},
insert: function(table, values, callback) {
var keys, vals, valuesa;
vals = new Hash(values);
valuesa = vals.getValues().map(function(item) {
return "'" + (item) + "'";
});
keys = vals.getKeys().map(function(item) {
return "'" + (item) + "'";
});
return this.exec("INSERT INTO '" + (table) + "' ( " + (keys) + " ) VALUES ( " + (valuesa) + " );", callback);
},
remove: function(table, Where, callback) {
var wheremap;
wheremap = this.whereMap(Where);
return this.exec("DELETE FROM '" + (table) + "' WHERE " + (wheremap) + ";", callback);
},
update: function(table, set, Where, callback) {
var setmap, wheremap;
wheremap = this.whereMap(Where);
setmap = $splat(new Hash(set).map(function(value, key) {
return "" + (key) + "='" + (value) + "'";
}).getValues()).join(', ');
return this.exec("UPDATE '" + (table) + "' SET " + (setmap) + " WHERE " + (wheremap) + ";", callback);
},
whereMap: function(wher) {
return $splat(new Hash(wher).map(function(value, key) {
return key + "='" + value + "'";
}).getValues()).join(" AND ");
},
likeMap: function(wher) {
return $splat(new Hash(wher).map(function(value, key) {
return key + " LIKE '" + value + "'";
}).getValues()).join(" AND ");
}
});