From af2f6402be16269ea054603e1fd92817f44da179 Mon Sep 17 00:00:00 2001 From: Diogo Resende Date: Mon, 4 Mar 2013 20:11:35 +0000 Subject: [PATCH] Adds db.ping() (#57) --- lib/Drivers/DML/mysql.js | 5 +++++ lib/Drivers/DML/postgres.js | 7 +++++++ lib/Drivers/DML/postgresaxomic.js | 8 ++++++++ lib/Drivers/DML/sqlite.js | 5 +++++ lib/ORM.js | 5 +++++ test/integration/test-ping.js | 10 ++++++++++ 6 files changed, 40 insertions(+) create mode 100644 test/integration/test-ping.js diff --git a/lib/Drivers/DML/mysql.js b/lib/Drivers/DML/mysql.js index 290f985d..9f5a0403 100644 --- a/lib/Drivers/DML/mysql.js +++ b/lib/Drivers/DML/mysql.js @@ -35,6 +35,11 @@ Driver.prototype.drop = function (opts, cb) { return require("../DDL/mysql").drop(this, opts, cb); }; +Driver.prototype.ping = function (cb) { + this.db.ping(cb); + return this; +}; + Driver.prototype.on = function (ev, cb) { if (ev == "error") { this.db.on("error", cb); diff --git a/lib/Drivers/DML/postgres.js b/lib/Drivers/DML/postgres.js index 1fd188dd..11d6fe88 100644 --- a/lib/Drivers/DML/postgres.js +++ b/lib/Drivers/DML/postgres.js @@ -26,6 +26,13 @@ Driver.prototype.drop = function (opts, cb) { return require("../DDL/postgres").drop(this, opts, cb); }; +Driver.prototype.ping = function (cb) { + this.db.query("SELECT * FROM pg_stat_activity LIMIT 1", function () { + return cb(); + }); + return this; +}; + Driver.prototype.on = function (ev, cb) { if (ev == "error") { this.db.on("error", cb); diff --git a/lib/Drivers/DML/postgresaxomic.js b/lib/Drivers/DML/postgresaxomic.js index c7dbcadf..828e70e4 100644 --- a/lib/Drivers/DML/postgresaxomic.js +++ b/lib/Drivers/DML/postgresaxomic.js @@ -25,6 +25,14 @@ Driver.prototype.sync = function (opts, cb) { Driver.prototype.drop = function (opts, cb) { return require("../DDL/postgres").drop(this, opts, cb); }; + +Driver.prototype.ping = function (cb) { + this.db.query("SELECT * FROM pg_stat_activity LIMIT 1", function () { + return cb(); + }); + return this; +}; + Driver.prototype.connect = function (cb) { //console.log("connect called"); cb(null); diff --git a/lib/Drivers/DML/sqlite.js b/lib/Drivers/DML/sqlite.js index f47ed49d..3cff0fd3 100644 --- a/lib/Drivers/DML/sqlite.js +++ b/lib/Drivers/DML/sqlite.js @@ -33,6 +33,11 @@ Driver.prototype.drop = function (opts, cb) { return require("../DDL/sqlite").drop(this, opts, cb); }; +Driver.prototype.ping = function (cb) { + process.nextTick(cb); + return this; +}; + Driver.prototype.on = function (ev, cb) { if (ev == "error") { this.db.on("error", cb); diff --git a/lib/ORM.js b/lib/ORM.js index 8fe70d5c..3aae621b 100644 --- a/lib/ORM.js +++ b/lib/ORM.js @@ -118,6 +118,11 @@ ORM.prototype.define = function (name, properties, opts) { }); return this.models[name]; }; +ORM.prototype.ping = function (cb) { + this.driver.ping(cb); + + return this; +}; ORM.prototype.close = function (cb) { this.driver.close(cb); diff --git a/test/integration/test-ping.js b/test/integration/test-ping.js new file mode 100644 index 00000000..7fe6d3dc --- /dev/null +++ b/test/integration/test-ping.js @@ -0,0 +1,10 @@ +var common = require('../common'); +var assert = require('assert'); + +common.createConnection(function (err, db) { + db.ping(function (err) { + assert.equal(err, null); + + db.close(); + }); +});