├── README
├── sqlite.js
└── tests.html
/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 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 | }
--------------------------------------------------------------------------------
/tests.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Test Suite for sqlite.js
6 |
7 |
8 |
9 |
10 |
37 |
38 |
--------------------------------------------------------------------------------