Skip to content

Commit a90055d

Browse files
committed
Add a destroy method and update the readme
1 parent 47d70d3 commit a90055d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

README

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ example usage:
55
var db = SQLite({ shortName: 'mydb' });
66

77
db.createTable('people', 'name TEXT, age INTEGER');
8+
89
db.insert('people', { name: 'Jeremy', age: 29 });
10+
911
db.update('people', { age: 30 }, { name: 'Jeremy' });
10-
db.select('people', '*', { age: 30 }, function (transaction, results) { var x; for(x=0; x<results.rows.length; x++) { console.log(results.rows.item(x)); } });
12+
13+
db.select('people', '*', { age: 30 }, function (transaction, results) { var x; for(x=0; x<results.rows.length; x++) { console.log(results.rows.item(x)); } });
14+
15+
db.destroy('people', { id: 1 });

sqlite.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,32 @@ function SQLite(cfg) {
146146
query = query.replace('#col#', columns);
147147
query = query.replace('#cond#', matches);
148148

149-
console.log(query);
149+
execute(query, null, data, error);
150+
},
151+
destroy: function (table, conditions, data, error) {
152+
var query = 'DELETE FROM ' + table + '#c#;', matches = [], x;
153+
154+
if (typeof conditions === 'string') {
155+
matches.push(conditions);
156+
} else if (typeof conditions === 'number') {
157+
matches.push("id=" + conditions);
158+
} else if (typeof conditions === 'object') {
159+
for (x in conditions) {
160+
if (conditions.hasOwnProperty(x)) {
161+
if (x.match(/^\d+$/)) {
162+
matches.push(conditions[x]);
163+
} else {
164+
matches.push(x + '=' + conditions[x]);
165+
}
166+
}
167+
}
168+
}
169+
170+
if (matches.length > 0) {
171+
matches = " WHERE " + matches.join(' AND ');
172+
}
173+
174+
query = query.replace('#c#', matches);
150175

151176
execute(query, null, data, error);
152177
}

0 commit comments

Comments
 (0)