├── README
├── tests.html
└── sqlite.js
/README:
--------------------------------------------------------------------------------
1 | A very simple Javascript layer for the web based SQLite database.
2 |
3 | example usage:
4 |
5 | var db = SQLite({ shortName: 'mydb' });
6 |
7 | db.createTable('people', 'name TEXT, age INTEGER');
8 |
9 | db.insert('people', { name: 'Jeremy', age: 29 });
10 |
11 | db.update('people', { age: 30 }, { name: 'Jeremy' });
12 |
13 | db.select('people', '*', { age: 30 }, function (results) { var x; for(x=0; x
2 |
3 |
4 |
5 | Test Suite for sqlite.js
6 |
7 |
8 |
9 |
10 |
37 |
38 |
--------------------------------------------------------------------------------
/sqlite.js:
--------------------------------------------------------------------------------
1 | function SQLite(cfg) {
2 | if (typeof window.openDatabase === 'undefined') {
3 | return;
4 | }
5 |
6 | function log(str) {
7 | if (typeof console !== 'undefined') {
8 | console.log(str);
9 | }
10 | }
11 |
12 | function isNumber(val) {
13 | switch (typeof val) {
14 | case 'number':
15 | return true;
16 | case 'string':
17 | return (/^\d+$/).test(val);
18 | case 'object':
19 | return false;
20 | }
21 | }
22 |
23 | // Default Handlers
24 | function nullDataHandler(results) { }
25 |
26 | function errorHandler(error) {
27 | log('Oops. ' + error.message + ' (Code ' + error.code + ')');
28 | }
29 |
30 | var config = cfg || {}, db;
31 |
32 | config.shortName = config.shortName || 'mydatabase';
33 | config.version = config.version || '1.0';
34 | config.displayName = config.displayName || 'My SQLite Database';
35 | config.maxSize = 65536;
36 | config.defaultErrorHandler = config.defaultErrorHandler || errorHandler;
37 | config.defaultDataHandler = config.defaultDataHandler || nullDataHandler;
38 |
39 | try {
40 | db = openDatabase(config.shortName, config.version, config.displayName, config.maxSize);
41 | } catch (e) {
42 | if (e === 2) {
43 | log("Invalid database version.");
44 | } else {
45 | log("Unknown error " + e + ".");
46 | }
47 |
48 | return;
49 | }
50 |
51 | function execute(query, v, d, e) {
52 | var values = v || [],
53 | dH = d || config.defaultDataHandler,
54 | eH = e || config.defaultErrorHandler;
55 |
56 | if (!query || query === '') {
57 | return;
58 | }
59 |
60 | function err(t, error) {
61 | eH(error, query);
62 | }
63 |
64 | function data(t, result) {
65 | dH(result, query);
66 | }
67 |
68 | db.transaction(
69 | function (transaction) {
70 | transaction.executeSql(query, values, data, err);
71 | }
72 | );
73 | }
74 |
75 | function buildConditions(conditions) {
76 | var results = [], values = [], x;
77 |
78 | if (typeof conditions === 'string') {
79 | results.push(conditions);
80 | } else if (typeof conditions === 'number') {
81 | results.push("id=?");
82 | values.push(conditions);
83 | } else if (typeof conditions === 'object') {
84 | for (x in conditions) {
85 | if (conditions.hasOwnProperty(x)) {
86 | if (isNumber(x)) {
87 | results.push(conditions[x]);
88 | } else {
89 | results.push(x + '=?');
90 | values.push(conditions[x]);
91 | }
92 | }
93 | }
94 | }
95 |
96 | if (results.length > 0) {
97 | results = " WHERE " + results.join(' AND ');
98 | } else {
99 | results = '';
100 | }
101 |
102 | return [results, values];
103 | }
104 |
105 | function createTableSQL(name, cols) {
106 | var query = "CREATE TABLE " + name + "(" + cols + ");";
107 |
108 | return [query, []];
109 | }
110 |
111 | function dropTableSQL(name) {
112 | var query = "DROP TABLE " + name + ";";
113 |
114 | return [query, []];
115 | }
116 |
117 | function insertSQL(table, map) {
118 | var query = "INSERT INTO " + table + " (#k#) VALUES(#v#);", keys = [], holders = [], values = [], x;
119 |
120 | for (x in map) {
121 | if (map.hasOwnProperty(x)) {
122 | keys.push(x);
123 | holders.push('?');
124 | values.push(map[x]);
125 | }
126 | }
127 |
128 | query = query.replace("#k#", keys.join(','));
129 | query = query.replace("#v#", holders.join(','));
130 |
131 | return [query, values];
132 | }
133 |
134 | function updateSQL(table, map, conditions) {
135 | var query = "UPDATE " + table + " SET #k##m#", keys = [], values = [], x;
136 |
137 | for (x in map) {
138 | if (map.hasOwnProperty(x)) {
139 | keys.push(x + '=?');
140 | values.push(map[x]);
141 | }
142 | }
143 |
144 | conditions = buildConditions(conditions);
145 |
146 | values = values.concat(conditions[1]);
147 |
148 | query = query.replace("#k#", keys.join(','));
149 | query = query.replace("#m#", conditions[0]);
150 |
151 | return [query, values];
152 | }
153 |
154 | function selectSQL(table, columns, conditions, options) {
155 | var query = 'SELECT #col# FROM ' + table + '#cond#', values = [];
156 |
157 | if (typeof columns === 'undefined') {
158 | columns = '*';
159 | } else if (typeof columns === 'object') {
160 | columns.join(',');
161 | }
162 |
163 | conditions = buildConditions(conditions);
164 |
165 | values = values.concat(conditions[1]);
166 |
167 | query = query.replace("#col#", columns);
168 | query = query.replace('#cond#', conditions[0]);
169 |
170 | if (options) {
171 | if (options.limit) {
172 | query = query + ' LIMIT ?';
173 | values.push(options.limit);
174 | }
175 | if (options.order) {
176 | query = query + ' ORDER BY ?';
177 | values.push(options.order);
178 | }
179 | if (options.offset) {
180 | query = query + ' OFFSET ?';
181 | values.push(options.offset);
182 | }
183 | }
184 |
185 | query = query + ';';
186 |
187 | return [query, values];
188 | }
189 |
190 | function destroySQL(table, conditions) {
191 | var query = 'DELETE FROM ' + table + '#c#;';
192 |
193 | conditions = buildConditions(conditions);
194 |
195 | query = query.replace('#c#', conditions[0]);
196 |
197 | return [query, conditions[1]];
198 | }
199 |
200 | return {
201 | database: db,
202 | createTable: function (name, cols, data, error) {
203 | var sql = createTableSQL(name, cols);
204 | execute(sql[0], sql[1], data, error);
205 | },
206 | dropTable: function (name, data, error) {
207 | var sql = dropTableSQL(name);
208 | execute(sql[0], sql[1], data, error);
209 | },
210 | insert: function (table, map, data, error) {
211 | var sql = insertSQL(table, map);
212 | execute(sql[0], sql[1], data, error);
213 | },
214 | update: function (table, map, conditions, data, error) {
215 | var sql = updateSQL(table, map, conditions);
216 | execute(sql[0], sql[1], data, error);
217 | },
218 | select: function (table, columns, conditions, options, data, error) {
219 | var sql = selectSQL(table, columns, conditions, options);
220 | execute(sql[0], sql[1], data, error);
221 | },
222 | destroy: function (table, conditions, data, error) {
223 | var sql = destroySQL(table, conditions);
224 | execute(sql[0], sql[1], data, error);
225 | }
226 | };
227 | }
--------------------------------------------------------------------------------