Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor adjustments #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions lib/sqlite-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,42 @@ function Wrapper(db, debug) {
Wrapper.prototype.createTable = function(tblName, columns, cb) {
var query = 'CREATE TABLE IF NOT EXISTS ' + tblName + ' (';
var first = true;
var primaryKeys = [];
for (var key in columns) {
var deflt = columns[key]['default'],
ref = columns[key].ref;
var columnDec = (first ? (first = false, '') : ',') + key +
var columnDec = (first ? (first = false, '') : ', ') + key +
(columns[key].type ? ' ' + columns[key].type : '') +
(columns[key].primary ? ' PRIMARY KEY' : '') +
(columns[key].unique ? ' UNIQUE' : '') +
(columns[key].notnull ? ' NOT NULL' : '') +
(deflt ? (' DEFAULT ' + deflt) : '') +
(deflt ? (' DEFAULT ' + deflt) : '') +
(ref ? (' REFERENCES ' + ref) : '');
query += columnDec;
// Append key to primaryKeys if it is a primary key.
if (columns[key].primary) {
primaryKeys.push(key);
}
}
// Add primary keys to query.
if (primaryKeys.length > 0) {
first = true;
query += ', PRIMARY KEY(';
primaryKeys.forEach(function (primaryKey) {
if (first) {
query += primaryKey;
first = false;
} else {
query += ', ' + primaryKey;
}
});
query += ') ON CONFLICT REPLACE';
}
query += ');';

// Debug message.
if (this.__debug) {
console.log(query);
}
// Execute query.
this.__db.run(query, cb);
};

Expand All @@ -56,10 +78,13 @@ Wrapper.prototype.createTable = function(tblName, columns, cb) {
/**
* insert('Users', { username : 'foo', password : 'bar' }, function(err) { });
*/
Wrapper.prototype.insert = function(table, obj, cb) {
Wrapper.prototype.insert = function(table, obj, cb, orReplace) {
var dissected = utils.dissect(obj);
var query = 'INSERT INTO ' + table + ' (' + dissected.columns.join(',') + ')' +
' VALUES ' + dissected.valuesPlaceholder + ';';
var query = 'INSERT' +
(orReplace ? ' OR REPLACE' : '') +
' INTO ' + table +
' (' + dissected.columns.join(',') + ')' +
' VALUES ' + dissected.valuesPlaceholder + ';';
if (this.__debug) console.log(query, '\n', dissected.values);
this.__db.run(query, dissected.values, cb);
};
Expand Down Expand Up @@ -109,16 +134,28 @@ Wrapper.prototype.remove = function(table, whereClause, whereValues, cb) {
* select('Torrents', { 'Users' : 'Users.id=Torrents.UserId' }, null,
* 'Torrents.UserId=?', [1], function(err, rows) { ... });
*/
Wrapper.prototype.select = function(table, joins, columns, whereClause, whereValues, cb, order, limit, distinct) {
var query = 'SELECT ' + (distinct ? 'DISTINCT ' : '') +
(columns ? utils.cols(columns) : '*') + ' FROM ' + table;
Wrapper.prototype.select = function(table, joins, columns, whereClause, whereValues, cb, order, limit, distinct, group) {
var query = 'SELECT ' +
(distinct ? 'DISTINCT ' : '') +
(columns ? utils.cols(columns) : '*') +
' FROM ' + table;
if (joins) {
for (var tbl in joins) {
query += ', ' + tbl + ' ON ' + joins[tbl];
}
}
query += ' WHERE ' + whereClause + (order ? ' ORDER BY ' + order : '') +
(limit ? ' LIMIT ' + limit : '') + ';';
query += ' WHERE ' + whereClause +
(group ? ' GROUP BY ' + group : '') +
(order ? ' ORDER BY ' + order : '') +
(limit ? ' LIMIT ' + limit : '') + ';';
if (this.__debug) console.log(query, '\n', whereValues);
this.__db.all(query, whereValues || [], cb);
};

/**
* selectQuery('SELECT * FROM Torrents', null, function(err, rows) { ... });
*/
Wrapper.prototype.selectQuery = function(query, whereValues, cb) {
if (this.__debug) console.log(query, '\n', whereValues);
this.__db.all(query, whereValues || [], cb);
};
Expand Down Expand Up @@ -191,7 +228,7 @@ Wrapper.prototype.close = function(cb) {
};

/**
* Init with DB filename, or ':memory:' for in-memory DB, or no argument for
* Init with DB filename, or ':memory:' for in-memory DB, or no argument for
* anonymous DB.
*/
module.exports = function(dbFile, debug) {
Expand Down