Skip to content

Commit 47d70d3

Browse files
committed
first commit
0 parents  commit 47d70d3

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

README

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
db.insert('people', { name: 'Jeremy', age: 29 });
9+
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)); } });

sqlite.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
13+
// Default Handlers
14+
function nullDataHandler(transaction, results) { }
15+
16+
function errorHandler(transaction, error) {
17+
log('Oops. Error was ' + error.message + ' (Code ' + error.code + ')');
18+
}
19+
20+
var config = cfg || {}, db;
21+
22+
config.shortName = config.shortName || 'mydatabase';
23+
config.version = config.version || '1.0';
24+
config.displayName = config.displayName || 'My SQLite Database';
25+
config.maxSize = 65536;
26+
config.defaultErrorHandler = config.defaultErrorHandler || errorHandler;
27+
config.defaultDataHandler = config.defaultDataHandler || nullDataHandler;
28+
29+
try {
30+
db = openDatabase(config.shortName, config.version, config.displayName, config.maxSize);
31+
} catch (e) {
32+
if (e === 2) {
33+
log("Invalid database version.");
34+
} else {
35+
log("Unknown error " + e + ".");
36+
}
37+
38+
return;
39+
}
40+
41+
function execute(query, v, d, e) {
42+
var values = v || [],
43+
dH = d || config.defaultDataHandler,
44+
eH = e || config.defaultErrorHandler;
45+
46+
if (!query || query === '') {
47+
return;
48+
}
49+
50+
db.transaction(
51+
function (transaction) {
52+
transaction.executeSql(query, values, dH, eH);
53+
}
54+
);
55+
}
56+
57+
return {
58+
database: db,
59+
createTable: function (name, cols, data, error) {
60+
var query = "CREATE TABLE " + name + "(" + cols + ");";
61+
execute(query, null, data, error);
62+
},
63+
insert: function (table, map, data, error) {
64+
var query = "INSERT INTO " + table + " (#k#) VALUES(#v#);", keys = [], values = [], x;
65+
66+
for (x in map) {
67+
if (map.hasOwnProperty(x)) {
68+
keys.push(x);
69+
values.push('"' + map[x] + '"');
70+
}
71+
}
72+
73+
query = query.replace("#k#", keys.join(','));
74+
query = query.replace("#v#", values.join(','));
75+
76+
execute(query, null, data, error);
77+
},
78+
update: function (table, map, conditions, data, error) {
79+
var query = "UPDATE " + table + " SET #k##m#", keys = [], values = [], matches = [], x;
80+
81+
for (x in map) {
82+
if (map.hasOwnProperty(x)) {
83+
keys.push(x + '=?');
84+
values.push(map[x]);
85+
}
86+
}
87+
88+
if (typeof conditions === 'string') {
89+
matches.push(conditions);
90+
} else if (typeof conditions === 'number') {
91+
matches.push("id=?");
92+
values.push(conditions);
93+
} else if (typeof conditions === 'object') {
94+
for (x in conditions) {
95+
if (conditions.hasOwnProperty(x)) {
96+
if (x.match(/^\d+$/)) {
97+
matches.push(conditions[x]);
98+
} else {
99+
matches.push(x + '=?');
100+
values.push(conditions[x]);
101+
}
102+
}
103+
}
104+
}
105+
106+
if (matches.length > 0) {
107+
matches = " WHERE " + matches.join(' AND ');
108+
} else {
109+
matches = '';
110+
}
111+
112+
query = query.replace("#k#", keys.join(','));
113+
query = query.replace("#m#", matches);
114+
115+
execute(query, values, data, error);
116+
},
117+
select: function (table, columns, conditions, data, error) {
118+
var query = 'SELECT #col# FROM ' + table + '#cond#;', matches = [], x;
119+
120+
if (typeof columns === 'undefined') {
121+
columns = '*';
122+
} else if (typeof columns === 'object') {
123+
columns.join(',');
124+
}
125+
126+
if (typeof conditions === 'string') {
127+
matches.push(conditions);
128+
} else if (typeof conditions === 'number') {
129+
matches.push("id=" + conditions);
130+
} else if (typeof conditions === 'object') {
131+
for (x in conditions) {
132+
if (conditions.hasOwnProperty(x)) {
133+
if (x.match(/^\d+$/)) {
134+
matches.push(conditions[x]);
135+
} else {
136+
matches.push(x + '=' + conditions[x]);
137+
}
138+
}
139+
}
140+
}
141+
142+
if (matches.length > 0) {
143+
matches = " WHERE " + matches.join(' AND ');
144+
}
145+
146+
query = query.replace('#col#', columns);
147+
query = query.replace('#cond#', matches);
148+
149+
console.log(query);
150+
151+
execute(query, null, data, error);
152+
}
153+
};
154+
}

0 commit comments

Comments
 (0)