Skip to content

Commit fd99b14

Browse files
committed
Abstract the SQL creation methods
1 parent 03cce14 commit fd99b14

File tree

1 file changed

+111
-98
lines changed

1 file changed

+111
-98
lines changed

sqlite.js

Lines changed: 111 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ function SQLite(cfg) {
99
}
1010
}
1111

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+
}
1222

1323
// Default Handlers
1424
function nullDataHandler(results) { }
@@ -62,126 +72,129 @@ function SQLite(cfg) {
6272
);
6373
}
6474

65-
return {
66-
database: db,
67-
createTable: function (name, cols, data, error) {
68-
var query = "CREATE TABLE " + name + "(" + cols + ");";
69-
execute(query, null, data, error);
70-
},
71-
insert: function (table, map, data, error) {
72-
var query = "INSERT INTO " + table + " (#k#) VALUES(#v#);", keys = [], values = [], x;
73-
74-
for (x in map) {
75-
if (map.hasOwnProperty(x)) {
76-
keys.push(x);
77-
values.push('"' + map[x] + '"');
75+
function buildConditions(conditions) {
76+
var results = [], x;
77+
78+
if (typeof conditions === 'string') {
79+
results.push(conditions);
80+
} else if (typeof conditions === 'number') {
81+
results.push("id=" + conditions);
82+
} else if (typeof conditions === 'object') {
83+
for (x in conditions) {
84+
if (conditions.hasOwnProperty(x)) {
85+
if (isNumber(x)) {
86+
results.push(conditions[x]);
87+
} else {
88+
if (isNumber(conditions[x])) {
89+
results.push(x + '=' + conditions[x]);
90+
} else {
91+
results.push(x + '="' + conditions[x] + '"');
92+
}
93+
}
7894
}
7995
}
96+
}
8097

81-
query = query.replace("#k#", keys.join(','));
82-
query = query.replace("#v#", values.join(','));
98+
if (results.length > 0) {
99+
results = " WHERE " + results.join(' AND ');
100+
} else {
101+
results = '';
102+
}
83103

84-
execute(query, null, data, error);
85-
},
86-
update: function (table, map, conditions, data, error) {
87-
var query = "UPDATE " + table + " SET #k##m#", keys = [], values = [], matches = [], x;
104+
return results;
105+
}
106+
107+
108+
function createTableSQL(name, cols) {
109+
var query = "CREATE TABLE " + name + "(" + cols + ");";
110+
111+
return query;
112+
}
113+
114+
function insertSQL(table, map) {
115+
var query = "INSERT INTO " + table + " (#k#) VALUES(#v#);", keys = [], values = [], x;
88116

89-
for (x in map) {
90-
if (map.hasOwnProperty(x)) {
91-
keys.push(x + '=?');
117+
for (x in map) {
118+
if (map.hasOwnProperty(x)) {
119+
keys.push(x);
120+
if (isNumber(map[x])) {
92121
values.push(map[x]);
122+
} else {
123+
values.push('"' + map[x] + '"');
93124
}
94125
}
126+
}
95127

96-
if (typeof conditions === 'string') {
97-
matches.push(conditions);
98-
} else if (typeof conditions === 'number') {
99-
matches.push("id=?");
100-
values.push(conditions);
101-
} else if (typeof conditions === 'object') {
102-
for (x in conditions) {
103-
if (conditions.hasOwnProperty(x)) {
104-
if (x.match(/^\d+$/)) {
105-
matches.push(conditions[x]);
106-
} else {
107-
matches.push(x + '=?');
108-
values.push(conditions[x]);
109-
}
110-
}
128+
query = query.replace("#k#", keys.join(','));
129+
query = query.replace("#v#", values.join(','));
130+
131+
return query;
132+
}
133+
134+
function updateSQL(table, map, conditions) {
135+
var query = "UPDATE " + table + " SET #k##m#", keys = [], matches = '', x;
136+
137+
for (x in map) {
138+
if (map.hasOwnProperty(x)) {
139+
if (isNumber(map[x])) {
140+
keys.push(x + '=' + map[x]);
141+
} else {
142+
keys.push(x + '="' + map[x] + '"');
111143
}
112144
}
145+
}
113146

114-
if (matches.length > 0) {
115-
matches = " WHERE " + matches.join(' AND ');
116-
} else {
117-
matches = '';
118-
}
147+
matches = buildConditions(conditions);
119148

120-
query = query.replace("#k#", keys.join(','));
121-
query = query.replace("#m#", matches);
149+
query = query.replace("#k#", keys.join(','));
150+
query = query.replace("#m#", matches);
122151

123-
execute(query, values, data, error);
124-
},
125-
select: function (table, columns, conditions, data, error) {
126-
var query = 'SELECT #col# FROM ' + table + '#cond#;', matches = [], x;
152+
return query;
153+
}
127154

128-
if (typeof columns === 'undefined') {
129-
columns = '*';
130-
} else if (typeof columns === 'object') {
131-
columns.join(',');
132-
}
155+
function selectSQL(table, columns, conditions, options) {
156+
var query = 'SELECT #col# FROM ' + table + '#cond#;', matches = '';
133157

134-
if (typeof conditions === 'string') {
135-
matches.push(conditions);
136-
} else if (typeof conditions === 'number') {
137-
matches.push("id=" + conditions);
138-
} else if (typeof conditions === 'object') {
139-
for (x in conditions) {
140-
if (conditions.hasOwnProperty(x)) {
141-
if (x.match(/^\d+$/)) {
142-
matches.push(conditions[x]);
143-
} else {
144-
matches.push(x + '=' + conditions[x]);
145-
}
146-
}
147-
}
148-
}
158+
if (typeof columns === 'undefined') {
159+
columns = '*';
160+
} else if (typeof columns === 'object') {
161+
columns.join(',');
162+
}
149163

150-
if (matches.length > 0) {
151-
matches = " WHERE " + matches.join(' AND ');
152-
}
164+
matches = buildConditions(conditions);
153165

154-
query = query.replace('#col#', columns);
155-
query = query.replace('#cond#', matches);
166+
query = query.replace('#col#', columns);
167+
query = query.replace('#cond#', matches);
156168

157-
execute(query, null, data, error);
158-
},
159-
destroy: function (table, conditions, data, error) {
160-
var query = 'DELETE FROM ' + table + '#c#;', matches = [], x;
161-
162-
if (typeof conditions === 'string') {
163-
matches.push(conditions);
164-
} else if (typeof conditions === 'number') {
165-
matches.push("id=" + conditions);
166-
} else if (typeof conditions === 'object') {
167-
for (x in conditions) {
168-
if (conditions.hasOwnProperty(x)) {
169-
if (x.match(/^\d+$/)) {
170-
matches.push(conditions[x]);
171-
} else {
172-
matches.push(x + '=' + conditions[x]);
173-
}
174-
}
175-
}
176-
}
169+
return query;
170+
}
177171

178-
if (matches.length > 0) {
179-
matches = " WHERE " + matches.join(' AND ');
180-
}
172+
function destroySQL(table, conditions) {
173+
var query = 'DELETE FROM ' + table + '#c#;', matches = '';
174+
175+
matches = buildConditions(conditions);
181176

182-
query = query.replace('#c#', matches);
177+
query = query.replace('#c#', matches);
183178

184-
execute(query, null, data, error);
179+
return query;
180+
}
181+
182+
return {
183+
database: db,
184+
createTable: function (name, cols, data, error) {
185+
execute(createTableSQL(name, cols), null, data, error);
186+
},
187+
insert: function (table, map, data, error) {
188+
execute(insertSQL(table, map), null, data, error);
189+
},
190+
update: function (table, map, conditions, data, error) {
191+
execute(updateSQL(table, map, conditions), null, data, error);
192+
},
193+
select: function (table, columns, conditions, options, data, error) {
194+
execute(selectSQL(table, columns, conditions, options), null, data, error);
195+
},
196+
destroy: function (table, conditions, data, error) {
197+
execute(destroySQL(table, conditions), null, data, error);
185198
}
186199
};
187200
}

0 commit comments

Comments
 (0)