From a853cd103f47c134e6084ecc1bbcb512da487a83 Mon Sep 17 00:00:00 2001 From: Tim Caswell Date: Wed, 16 Dec 2009 10:34:14 -0600 Subject: [PATCH] Refactor the sql common code so that postgres uses serial _id columns, but sqlite uses rowid as before. --- lib/persistence/common/sql.js | 55 ++++++++++++++++++++++------------- lib/persistence/postgres.js | 15 +++++++++- lib/persistence/sqlite.js | 15 +++++++++- 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/lib/persistence/common/sql.js b/lib/persistence/common/sql.js index 197f629..82d9d06 100644 --- a/lib/persistence/common/sql.js +++ b/lib/persistence/common/sql.js @@ -73,13 +73,23 @@ function condition_to_sql(condition, value) { throw "Invalid operator " + operator; } -function Store(conn, name, columns) { +// overrides needs to contain at least the following +// index_col: the name of the special index column rowid in sqlite and oid in postgres +// do_insert: function (promise, data, keys, values) +// do_update: function (promise, data, pairs) + +function Store(conn, name, columns, overrides) { var key, types = []; this.name = name; this.conn = conn; - + + if (overrides.types) { + types = overrides.types; + delete overrides.types; + } + if (columns) { for (key in columns) { if (columns.hasOwnProperty(key)) { @@ -89,13 +99,19 @@ function Store(conn, name, columns) { conn.execute("CREATE TABLE " + name + "(" + types.join(", ") +")").wait(); } + + if (overrides) { + process.mixin(this, overrides); + } + + } Store.prototype = { get: function (id) { var promise = new process.Promise(); this.conn.query( - "SELECT rowid AS _id, * FROM " + this.name + " WHERE rowid = ?", + "SELECT " + this.index_col + " AS _id, * FROM " + this.name + " WHERE " + this.index_col + " = ?", id ).addCallback(function (data) { promise.emitSuccess(data[0]); @@ -114,7 +130,7 @@ Store.prototype = { conditions = [conditions]; } - sql = "SELECT rowid AS _id, * FROM " + this.name + " WHERE " + + sql = "SELECT " + this.index_col + " AS _id, * FROM " + this.name + " WHERE " + conditions.map(function (group) { var ands = [], key; for (key in group) { @@ -132,13 +148,22 @@ Store.prototype = { }, each: function (row_callback) { - return this.conn.query("SELECT rowid AS _id, * FROM " + this.name, row_callback); + return this.conn.query("SELECT " + this.index_col + " AS _id, * FROM " + this.name, row_callback); }, all: function () { - return this.conn.query("SELECT rowid AS _id, * FROM " + this.name); + return this.conn.query("SELECT " + this.index_col + " AS _id, * FROM " + this.name); }, + do_update: function (promise, data, pairs) { + this.conn.execute("UPDATE " + this.name + + " SET " + pairs.join(", ") + + " WHERE " + this.index_col + " = " + sql_escape(data._id) + ).addCallback(function () { + promise.emitSuccess(); + }); + }, + // Save a data object to the database. If it already has an _id do an update. save: function (data) { var keys = [], @@ -153,12 +178,7 @@ Store.prototype = { pairs.push(key + " = " + sql_escape(data[key])); } } - this.conn.execute("UPDATE " + this.name + - " SET " + pairs.join(", ") + - " WHERE rowid = " + sql_escape(data._id) - ).addCallback(function () { - promise.emitSuccess(); - }); + this.do_update(promise, data, pairs); } else { for (key in data) { if (data.hasOwnProperty(key)) { @@ -166,14 +186,7 @@ Store.prototype = { values.push(sql_escape(data[key])); } } - this.conn.query("INSERT INTO " + - this.name + "(" + keys.join(", ") + ")" + - " VALUES (" + values.join(", ") + ");" + - "SELECT last_insert_rowid() AS rowid" - ).addCallback(function (result) { - data._id = parseInt(result[0].rowid, 10); - promise.emitSuccess(data._id); - }); + this.do_insert(promise, data, keys, values); } return promise; }, @@ -185,7 +198,7 @@ Store.prototype = { data = {_id: data}; } this.conn.execute("DELETE FROM " + this.name + - " WHERE rowid = " + sql_escape(data._id) + " WHERE " + this.index_col + " = " + sql_escape(data._id) ).addCallback(function () { delete data._id; promise.emitSuccess(); diff --git a/lib/persistence/postgres.js b/lib/persistence/postgres.js index 57edfc2..904b857 100644 --- a/lib/persistence/postgres.js +++ b/lib/persistence/postgres.js @@ -371,7 +371,20 @@ function Connection(args) { } Connection.prototype = new process.EventEmitter(); Connection.prototype.get_store = function (name, columns) { - return new sqllib.Store(this, name, columns); + return new sqllib.Store(this, name, columns, { + do_insert: function (promise, data, keys, values) { + this.conn.query("INSERT INTO " + + this.name + "(" + keys.join(", ") + ")" + + " VALUES (" + values.join(", ") + ")" + + " RETURNING _id" + ).addCallback(function (result) { + data._id = parseInt(result[0]._id, 10); + promise.emitSuccess(data._id); + }); + }, + index_col: '_id', + types: ["_id SERIAL"] + }); }; exports.new_connection = function (connection_parameters) { diff --git a/lib/persistence/sqlite.js b/lib/persistence/sqlite.js index 9b55d75..9ff40e5 100644 --- a/lib/persistence/sqlite.js +++ b/lib/persistence/sqlite.js @@ -196,7 +196,20 @@ function Connection(path) { } Connection.prototype = new process.EventEmitter(); Connection.prototype.get_store = function (name, columns) { - return new sqllib.Store(this, name, columns); + return new sqllib.Store(this, name, columns, { + do_insert: function (promise, data, keys, values) { + this.conn.query("INSERT INTO " + + this.name + "(" + keys.join(", ") + ")" + + " VALUES (" + values.join(", ") + ");" + + "SELECT last_insert_rowid() AS _id" + ).addCallback(function (result) { + data._id = parseInt(result[0]._id, 10); + promise.emitSuccess(data._id); + }); + }, + + index_col: 'rowid' + }); }; exports.new_connection = function (connection_parameters) {