Permalink
Browse files

make Query a public api

  • Loading branch information...
1 parent 312a3dd commit e62eb9339b9cc3a5fd3068747ece3c3ecf3e63e2 Troy Kruthoff committed with Aug 9, 2012
Showing with 20 additions and 16 deletions.
  1. +4 −15 lib/client.js
  2. +1 −0 lib/index.js
  3. +15 −1 lib/query.js
View
@@ -185,23 +185,12 @@ p._pulseQueryQueue = function() {
};
p.query = function(config, values, callback) {
- //can take in strings or config objects
- config = (typeof(config) == 'string') ? { text: config } : config;
- if (this.binary && !('binary' in config)) {
- config.binary = true;
+ //can take in strings, config object or query object
+ var query = (config instanceof Query) ? config : new Query(config, values, callback);
+ if (this.binary && !query.binary) {
+ query.binary = true;
}
- if(values) {
- if(typeof values === 'function') {
- callback = values;
- } else {
- config.values = values;
- }
- }
-
- config.callback = callback;
-
- var query = new Query(config);
this.queryQueue.push(query);
this._pulseQueryQueue();
return query;
View
@@ -13,6 +13,7 @@ var PG = function(clientConstructor) {
EventEmitter.call(this);
this.Client = clientConstructor;
this.Connection = require(__dirname + '/connection');
+ this.Query = require(__dirname + '/query');
this.defaults = defaults;
};
View
@@ -5,7 +5,21 @@ var Result = require(__dirname + '/result');
var Types = require(__dirname + '/types');
var utils = require(__dirname + '/utils');
-var Query = function(config) {
+var Query = function(config, values, callback) {
+ // use of "new" optional
+ if (!(this instanceof Query)) return new Query(config, values, callback);
+
+ //can take in strings or config objects
+ config = (typeof(config) == 'string') ? { text: config } : config;
+ if(values) {
+ if(typeof values === 'function') {
+ callback = values;
+ } else {
+ config.values = values;
+ }
+ }
+ config.callback = callback;
+
this.text = config.text;
this.values = config.values;
this.rows = config.rows;

0 comments on commit e62eb93

Please sign in to comment.