Work towards more tests passing

This commit is contained in:
Brian M. Carlson 2014-09-14 01:23:02 -04:00
parent b325971fdf
commit 667c528ea6
26 changed files with 138 additions and 1644 deletions

View File

@ -33,9 +33,6 @@ upgrade-pg:
bench:
@find benchmark -name "*-bench.js" | $(node-command)
build/default/binding.node:
@node-gyp rebuild
test-unit:
@find test/unit -name "*-tests.js" | $(node-command)
@ -47,12 +44,12 @@ test-connection-binary:
@echo "***Testing binary connection***"
@node script/test-connection.js $(params) binary
test-native: build/default/binding.node
test-native:
@echo "***Testing native bindings***"
@find test/native -name "*-tests.js" | $(node-command)
@find test/integration -name "*-tests.js" | $(node-command) native
test-integration: test-connection build/default/binding.node
test-integration: test-connection
@echo "***Testing Pure Javascript***"
@find test/integration -name "*-tests.js" | $(node-command)

View File

@ -1,38 +0,0 @@
{
'targets': [
{
'target_name': 'binding',
'sources': ['src/binding.cc'],
'include_dirs': [
'<!@(pg_config --includedir)',
'<!(node -e "require(\'nan\')")'
],
'conditions' : [
['OS=="win"', {
'conditions' : [
['"<!@(cmd /C where /Q pg_config || echo n)"!="n"',
{
'libraries' : ['libpq.lib'],
'msvs_settings': {
'VCLinkerTool' : {
'AdditionalLibraryDirectories' : [
'<!@(pg_config --libdir)\\'
]
},
}
}
]
]
}, { # OS!="win"
'conditions' : [
['"y"!="n"', # ToDo: add pg_config existance condition that works on linux
{
'libraries' : ['-lpq -L<!@(pg_config --libdir)']
}
]
]
}]
]
}
]
}

View File

@ -1,6 +1,7 @@
var Native = require('pg-native');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ConnectionParameters = require(__dirname + '/../connection-parameters');
var NativeQuery = require('./query');
@ -11,6 +12,16 @@ var Client = module.exports = function(config) {
}
this.native = new Native();
this._queryQueue = [];
this._connected = false;
//keep these on the object for legacy reasons
//for the time being. TODO: deprecate all this jazz
var cp = new ConnectionParameters(config);
this.user = cp.user;
this.password = cp.password;
this.database = cp.database;
this.host = cp.host;
this.port = cp.port;
};
util.inherits(Client, EventEmitter);
@ -73,7 +84,11 @@ Client.prototype.query = function(config, values, callback) {
};
Client.prototype.end = function(cb) {
this.native.end(cb);
var self = this;
this.native.end(function() {
self.emit('end');
if(cb) cb();
});
};
Client.prototype._pulseQueryQueue = function(initialConnection) {
@ -99,223 +114,3 @@ Client.prototype._pulseQueryQueue = function(initialConnection) {
self._pulseQueryQueue();
});
};
return;
//require the c++ bindings & export to javascript
var EventEmitter = require('events').EventEmitter;
var ConnectionParameters = require(__dirname + '/../connection-parameters');
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
var JsClient = require(__dirname + '/../client'); // used to import JS escape functions
var binding;
//TODO remove on v1.0.0
try {
//v0.5.x
binding = require(__dirname + '/../../build/Release/binding.node');
} catch(e) {
//v0.4.x
binding = require(__dirname + '/../../build/default/binding');
}
var Connection = binding.Connection;
var NativeQuery = require(__dirname + '/query');
for(var k in EventEmitter.prototype) {
Connection.prototype[k] = EventEmitter.prototype[k];
}
var nativeConnect = Connection.prototype.connect;
Connection.prototype.connect = function(cb) {
var self = this;
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
if(err) {
return cb ? cb(err) : self.emit('error', err);
}
if(cb) {
var errCallback;
var connectCallback = function() {
//remove single-fire connection error callback
self.removeListener('error', errCallback);
cb(null);
};
errCallback = function(err) {
//remove singel-fire connection success callback
self.removeListener('connect', connectCallback);
cb(err);
};
self.once('connect', connectCallback);
self.once('error', errCallback);
}
nativeConnect.call(self, conString);
});
};
Connection.prototype._copy = function (text, stream) {
var q = new NativeQuery(text, function (error) {
if (error) {
q.stream.error(error);
} else {
q.stream.close();
}
});
q.stream = stream;
this._queryQueue.push(q);
this._pulseQueryQueue();
return q.stream;
};
Connection.prototype.copyFrom = function (text) {
return this._copy(text, new CopyFromStream());
};
Connection.prototype.copyTo = function (text) {
return this._copy(text, new CopyToStream());
};
Connection.prototype.sendCopyFromChunk = function (chunk) {
this._sendCopyFromChunk(chunk);
};
Connection.prototype.endCopyFrom = function (msg) {
this._endCopyFrom(msg);
};
// use JS version if native version undefined
// happens when PG version < 9.0.0
if (!Connection.prototype.escapeIdentifier) {
Connection.prototype.escapeIdentifier = JsClient.prototype.escapeIdentifier;
}
if (!Connection.prototype.escapeLiteral) {
Connection.prototype.escapeLiteral = JsClient.prototype.escapeLiteral;
}
Connection.prototype.query = function(config, values, callback) {
var query = (config instanceof NativeQuery) ? config :
new NativeQuery(config, values, callback);
this._queryQueue.push(query);
this._pulseQueryQueue();
return query;
};
var nativeCancel = Connection.prototype.cancel;
Connection.prototype.cancel = function(client, query) {
if (client._activeQuery == query) {
this.connect(nativeCancel.bind(client));
} else if (client._queryQueue.indexOf(query) != -1) {
client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
}
};
Connection.prototype.sendCopyFail = function(msg) {
this.endCopyFrom(msg);
};
var clientBuilder = function(config) {
config = config || {};
var connection = new Connection();
EventEmitter.call(connection);
connection._queryQueue = [];
connection._namedQueries = {};
connection._activeQuery = null;
connection.connectionParameters = new ConnectionParameters(config);
//attach properties to normalize interface with pure js client
connection.user = connection.connectionParameters.user;
connection.password = connection.connectionParameters.password;
connection.database = connection.connectionParameters.database;
connection.host = connection.connectionParameters.host;
connection.port = connection.connectionParameters.port;
connection.on('connect', function() {
connection._connected = true;
connection._pulseQueryQueue(true);
});
connection.on('_rowDescription', function(rowDescription) {
connection._activeQuery.handleRowDescription(rowDescription);
});
//proxy some events to active query
connection.on('_row', function(row) {
connection._activeQuery.handleRow(row);
});
connection.on('_cmdStatus', function(status) {
//set this here so we can pass it to the query
//when the query completes
connection._lastMeta = status;
});
//TODO: emit more native error properties (make it match js error)
connection.on('_error', function(err) {
//create Error object from object literal
var error = new Error(err.message || "Unknown native driver error");
for(var key in err) {
error[key] = err[key];
}
//give up on trying to wait for named query prepare
this._namedQuery = false;
if(connection._activeQuery) {
connection._activeQuery.handleError(error);
} else {
connection.emit('error', error);
}
});
connection.on('_end', function() {
process.nextTick(function() {
if(connection._activeQuery) {
connection._activeQuery.handleError(new Error("Connection was ended during query"));
}
connection.emit('end');
});
});
connection.on('_readyForQuery', function() {
var error;
var q = this._activeQuery;
//a named query finished being prepared
if(this._namedQuery) {
this._namedQuery = false;
this._sendQueryPrepared(q.name, q.values||[]);
} else {
//try/catch/rethrow to ensure exceptions don't prevent the queryQueue from
//being processed
try{
connection._activeQuery.handleReadyForQuery(connection._lastMeta);
} catch(e) {
error = e;
}
connection._activeQuery = null;
connection._pulseQueryQueue();
if(error) throw error;
}
});
connection.on('copyInResponse', function () {
//connection is ready to accept chunks
//start to send data from stream
connection._activeQuery.streamData(connection);
});
connection.on('copyOutResponse', function(msg) {
if (connection._activeQuery.stream === undefined) {
connection._activeQuery._canceledDueToError = new Error('No destination stream defined');
(new clientBuilder({port: connection.port, host: connection.host})).cancel(connection, connection._activeQuery);
}
});
connection.on('copyData', function (chunk) {
//recieve chunk from connection
//move it to stream
connection._activeQuery.handleCopyFromChunk(chunk);
});
return connection;
};
// expose a Query constructor
clientBuilder.Query = NativeQuery;
module.exports = clientBuilder;

View File

@ -1,5 +1,6 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var utils = require('../utils');
var NativeQuery = module.exports = function(native) {
EventEmitter.call(this);
@ -16,13 +17,44 @@ var NativeQuery = module.exports = function(native) {
//this has almost no meaning because libpq
//reads all rows into memory befor returning any
this._emitRowEvents = false;
this.once('newListener', function(event) {
this.on('newListener', function(event) {
if(event === 'row') this._emitRowEvents = true;
}.bind(this));
};
util.inherits(NativeQuery, EventEmitter);
//given an array of values, turn all `undefined` into `null`
var clean = function(values) {
for(var i = 0; i < values.length; i++) {
if(typeof values[i] == 'undefined') {
values[i] = null;
}
}
};
var NativeResult = function(pq) {
this.command = null;
this.rowCount = 0;
this.rows = null;
this.fields = null;
};
NativeResult.prototype.addCommandComplete = function(pq) {
this.command = pq.cmdStatus().split(' ')[0];
this.rowCount = pq.cmdTuples();
var nfields = pq.nfields();
if(nfields < 1) return;
this.fields = [];
for(var i = 0; i < nfields; i++) {
this.fields.push({
name: pq.fname(i),
dataTypeID: pq.ftype(i)
});
}
};
NativeQuery.prototype.submit = function() {
this.state = 'running';
var self = this;
@ -39,127 +71,30 @@ NativeQuery.prototype.submit = function() {
return self.emit('error', err);
}
var result = new NativeResult();
result.addCommandComplete(self.native.pq);
result.rows = rows;
//emit row events for each row in the result
if(self._emitRowEvents) {
rows.forEach(self.emit.bind(self, 'row'));
rows.forEach(function(row) {
self.emit('row', row, result);
});
}
//handle successful result
self.state = 'end';
self.emit('end');
self.emit('end', result);
if(self.callback) {
self.callback(null, {rows: rows})
self.callback(null, result);
}
}
if(this.values) {
this.native.query(this.text, this.values, after);
var values = this.values.map(utils.prepareValue);
this.native.query(this.text, values, after);
} else {
this.native.query(this.text, after);
}
};
return;
var utils = require(__dirname + '/../utils');
var Result = require(__dirname + '/../result');
//event emitter proxy
var NativeQuery = function(config, values, callback) {
// use of "new" optional
if (!(this instanceof NativeQuery)) {
return new NativeQuery(config, values, callback);
}
EventEmitter.call(this);
var c = utils.normalizeQueryConfig(config, values, callback);
this.name = c.name;
this.text = c.text;
this.values = c.values;
this.callback = c.callback;
if(process.domain && c.callback) {
this.callback = process.domain.bind(c.callback);
}
this.singleRowMode = false;
if(!this.callback) {
this.singleRowMode = true;
}
this._result = new Result(config.rowMode);
this._addedFields = false;
this._hadError = false;
//normalize values
if(this.values) {
for(var i = 0, len = this.values.length; i < len; i++) {
this.values[i] = utils.prepareValue(this.values[i]);
}
}
this._canceledDueToError = false;
};
util.inherits(NativeQuery, EventEmitter);
NativeQuery.prototype.handleRowDescription = function(rowDescription) {
this._result.addFields(rowDescription);
};
NativeQuery.prototype.handleRow = function(rowData) {
var row = this._result.parseRow(rowData);
if(this.callback) {
this._result.addRow(row);
}
this.emit('row', row, this._result);
};
NativeQuery.prototype.handleError = function(error) {
if (this._canceledDueToError) {
error = this._canceledDueToError;
this._canceledDueToError = false;
}
this._hadError = true;
if(this.callback) {
var cb = this.callback;
//remove callback to prevent double call on readyForQuery
this.callback = null;
cb(error);
} else {
this.emit('error', error);
}
};
NativeQuery.prototype.handleReadyForQuery = function(meta) {
if(this._hadError) return;
if (this._canceledDueToError) {
return this.handleError(this._canceledDueToError);
}
if(meta) {
this._result.addCommandComplete(meta);
}
if(this.callback) {
this.callback(null, this._result);
}
this.emit('end', this._result);
};
NativeQuery.prototype.streamData = function (connection) {
if(this.stream) {
this.stream.startStreamingToConnection(connection);
}
else {
connection.sendCopyFail('No source stream defined');
}
};
NativeQuery.prototype.handleCopyFromChunk = function (chunk) {
if(this.stream) {
this.stream.handleChunk(chunk);
}
//if there are no stream (for example when copy to query was sent by
//query method instead of copyTo) error will be handled
//on copyOutResponse event, so silently ignore this error here
};
module.exports = NativeQuery;

View File

@ -23,7 +23,7 @@
"nan": "~1.3.0",
"packet-reader": "0.2.0",
"pg-connection-string": "0.1.1",
"pg-native": "0.4.1",
"pg-native": "0.5.0",
"pg-types": "1.4.0",
"pgpass": "0.0.3"
},
@ -34,8 +34,7 @@
},
"scripts": {
"changelog": "npm i github-changes && ./node_modules/.bin/github-changes -o brianc -r node-postgres -d pulls -a -v",
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres",
"install": "node-gyp rebuild || (exit 0)"
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres"
},
"engines": {
"node": ">= 0.8.0"

View File

@ -1,953 +0,0 @@
#include <pg_config.h>
#include <libpq-fe.h>
#include <nan.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define LOG(msg) printf("%s\n",msg);
#define TRACE(msg) //printf("%s\n", msg);
#if PG_VERSION_NUM >= 90000
#define ESCAPE_SUPPORTED
#endif
#if PG_VERSION_NUM >= 90200
#define SINGLE_ROW_SUPPORTED
#endif
#define THROW(msg) NanThrowError(msg); NanReturnUndefined();
using namespace v8;
using namespace node;
class Connection : public ObjectWrap {
public:
//creates the V8 objects & attaches them to the module (target)
static void
Init (Handle<Object> target)
{
NanScope();
Local<FunctionTemplate> t = NanNew<FunctionTemplate>(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(NanNew("Connection"));
NanSetPrototypeTemplate(t, "connect", NanNew<FunctionTemplate>(Connect));
#ifdef ESCAPE_SUPPORTED
NanSetPrototypeTemplate(t, "escapeIdentifier", NanNew<FunctionTemplate>(EscapeIdentifier));
NanSetPrototypeTemplate(t, "escapeLiteral", NanNew<FunctionTemplate>(EscapeLiteral));
#endif
NanSetPrototypeTemplate(t, "_sendQuery", NanNew<FunctionTemplate>(SendQuery));
NanSetPrototypeTemplate(t, "_sendQueryWithParams", NanNew<FunctionTemplate>(SendQueryWithParams));
NanSetPrototypeTemplate(t, "_sendPrepare", NanNew<FunctionTemplate>(SendPrepare));
NanSetPrototypeTemplate(t, "_sendQueryPrepared", NanNew<FunctionTemplate>(SendQueryPrepared));
NanSetPrototypeTemplate(t, "cancel", NanNew<FunctionTemplate>(Cancel));
NanSetPrototypeTemplate(t, "end", NanNew<FunctionTemplate>(End));
NanSetPrototypeTemplate(t, "_sendCopyFromChunk", NanNew<FunctionTemplate>(SendCopyFromChunk));
NanSetPrototypeTemplate(t, "_endCopyFrom", NanNew<FunctionTemplate>(EndCopyFrom));
target->Set(NanNew("Connection"), t->GetFunction());
TRACE("created class");
}
//static function called by libuv as callback entrypoint
static void
io_event(uv_poll_t* w, int status, int revents)
{
TRACE("Received IO event");
if(status == -1) {
TRACE("Connection error. -1 status from lib_uv_poll");
}
Connection *connection = static_cast<Connection*>(w->data);
connection->HandleIOEvent(revents);
}
//v8 entry point into Connection#connect
static NAN_METHOD(Connect)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
if(args.Length() == 0 || !args[0]->IsString()) {
THROW("Must include connection string as only argument to connect");
}
String::Utf8Value conninfo(args[0]->ToString());
bool success = self->Connect(*conninfo);
if(!success) {
self -> EmitLastError();
self -> DestroyConnection();
}
NanReturnUndefined();
}
//v8 entry point into Connection#cancel
static NAN_METHOD(Cancel)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
bool success = self->Cancel();
if(!success) {
self -> EmitLastError();
self -> DestroyConnection();
}
NanReturnUndefined();
}
#ifdef ESCAPE_SUPPORTED
//v8 entry point into Connection#escapeIdentifier
static NAN_METHOD(EscapeIdentifier)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
char* inputStr = MallocCString(args[0]);
if(!inputStr) {
THROW("Unable to allocate memory for a string in EscapeIdentifier.")
}
char* escapedStr = self->EscapeIdentifier(inputStr);
free(inputStr);
if(escapedStr == NULL) {
THROW(self->GetLastError());
}
Local<Value> jsStr = NanNew<String>(escapedStr, strlen(escapedStr));
PQfreemem(escapedStr);
NanReturnValue(jsStr);
}
//v8 entry point into Connection#escapeLiteral
static NAN_METHOD(EscapeLiteral)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
char* inputStr = MallocCString(args[0]);
if(!inputStr) {
THROW("Unable to allocate memory for a string in EscapeIdentifier.")
}
char* escapedStr = self->EscapeLiteral(inputStr);
free(inputStr);
if(escapedStr == NULL) {
THROW(self->GetLastError());
}
Local<Value> jsStr = NanNew<String>(escapedStr, strlen(escapedStr));
PQfreemem(escapedStr);
NanReturnValue(jsStr);
}
#endif
//v8 entry point into Connection#_sendQuery
static NAN_METHOD(SendQuery)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
const char *lastErrorMessage;
if(!args[0]->IsString()) {
THROW("First parameter must be a string query");
}
char* queryText = MallocCString(args[0]);
bool singleRowMode = (bool)args[1]->Int32Value();
int result = self->Send(queryText, singleRowMode);
free(queryText);
if(result == 0) {
lastErrorMessage = self->GetLastError();
THROW(lastErrorMessage);
}
//TODO should we flush before throw?
self->Flush();
NanReturnUndefined();
}
//v8 entry point into Connection#_sendQueryWithParams
static NAN_METHOD(SendQueryWithParams)
{
NanScope();
//dispatch non-prepared parameterized query
DispatchParameterizedQuery(args, false);
NanReturnUndefined();
}
//v8 entry point into Connection#_sendPrepare(string queryName, string queryText, int nParams)
static NAN_METHOD(SendPrepare)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
String::Utf8Value queryName(args[0]);
String::Utf8Value queryText(args[1]);
int length = args[2]->Int32Value();
bool singleRowMode = (bool)args[3]->Int32Value();
self->SendPrepare(*queryName, *queryText, length, singleRowMode);
NanReturnUndefined();
}
//v8 entry point into Connection#_sendQueryPrepared(string queryName, string[] paramValues)
static NAN_METHOD(SendQueryPrepared)
{
NanScope();
//dispatch prepared parameterized query
DispatchParameterizedQuery(args, true);
NanReturnUndefined();
}
static void DispatchParameterizedQuery(_NAN_METHOD_ARGS, bool isPrepared)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
String::Utf8Value queryName(args[0]);
//TODO this is much copy/pasta code
if(!args[0]->IsString()) {
NanThrowError("First parameter must be a string");
return;
}
if(!args[1]->IsArray()) {
NanThrowError("Values must be an array");
return;
}
Local<Array> jsParams = Local<Array>::Cast(args[1]);
int len = jsParams->Length();
char** paramValues = ArgToCStringArray(jsParams);
if(!paramValues) {
NanThrowError("Unable to allocate char **paramValues from Local<Array> of v8 params");
return;
}
char* queryText = MallocCString(args[0]);
bool singleRowMode = (bool)args[2]->Int32Value();
int result = 0;
if(isPrepared) {
result = self->SendPreparedQuery(queryText, len, paramValues, singleRowMode);
} else {
result = self->SendQueryParams(queryText, len, paramValues, singleRowMode);
}
free(queryText);
ReleaseCStringArray(paramValues, len);
if(result == 1) {
return;
}
self->EmitLastError();
NanThrowError("Postgres returned non-1 result from query dispatch.");
}
//v8 entry point into Connection#end
static NAN_METHOD(End)
{
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
self->End();
NanReturnUndefined();
}
uv_poll_t read_watcher_;
uv_poll_t write_watcher_;
PGconn *connection_;
bool connecting_;
bool ioInitialized_;
bool copyOutMode_;
bool copyInMode_;
bool reading_;
bool writing_;
bool ended_;
Connection () : ObjectWrap ()
{
connection_ = NULL;
connecting_ = false;
ioInitialized_ = false;
copyOutMode_ = false;
copyInMode_ = false;
reading_ = false;
writing_ = false;
ended_ = false;
TRACE("Initializing ev watchers");
read_watcher_.data = this;
write_watcher_.data = this;
}
~Connection ()
{
}
static NAN_METHOD(SendCopyFromChunk) {
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
//TODO handle errors in some way
if (args.Length() < 1 && !Buffer::HasInstance(args[0])) {
THROW("SendCopyFromChunk requires 1 Buffer argument");
}
self->SendCopyFromChunk(args[0]->ToObject());
NanReturnUndefined();
}
static NAN_METHOD(EndCopyFrom) {
NanScope();
Connection *self = ObjectWrap::Unwrap<Connection>(args.This());
char * error_msg = NULL;
if (args[0]->IsString()) {
error_msg = MallocCString(args[0]);
}
//TODO handle errors in some way
self->EndCopyFrom(error_msg);
free(error_msg);
NanReturnUndefined();
}
protected:
//v8 entry point to constructor
static NAN_METHOD(New)
{
NanScope();
Connection *connection = new Connection();
connection->Wrap(args.This());
NanReturnValue(args.This());
}
#ifdef ESCAPE_SUPPORTED
char * EscapeIdentifier(const char *str)
{
TRACE("js::EscapeIdentifier")
return PQescapeIdentifier(connection_, str, strlen(str));
}
char * EscapeLiteral(const char *str)
{
TRACE("js::EscapeLiteral")
return PQescapeLiteral(connection_, str, strlen(str));
}
#endif
void enableSingleRowMode(bool enable)
{
#ifdef SINGLE_ROW_SUPPORTED
if(enable == true) {
int mode = PQsetSingleRowMode(connection_);
if(mode == 1) {
TRACE("PQsetSingleRowMode enabled")
} else {
TRACE("PQsetSingleRowMode disabled")
}
} else {
TRACE("PQsetSingleRowMode disabled")
}
#endif
}
int Send(const char *queryText, bool singleRowMode)
{
TRACE("js::Send")
int rv = PQsendQuery(connection_, queryText);
enableSingleRowMode(singleRowMode);
StartWrite();
return rv;
}
int SendQueryParams(const char *command, const int nParams, const char * const *paramValues, bool singleRowMode)
{
TRACE("js::SendQueryParams")
int rv = PQsendQueryParams(connection_, command, nParams, NULL, paramValues, NULL, NULL, 0);
enableSingleRowMode(singleRowMode);
StartWrite();
return rv;
}
int SendPrepare(const char *name, const char *command, const int nParams, bool singleRowMode)
{
TRACE("js::SendPrepare")
int rv = PQsendPrepare(connection_, name, command, nParams, NULL);
enableSingleRowMode(singleRowMode);
StartWrite();
return rv;
}
int SendPreparedQuery(const char *name, int nParams, const char * const *paramValues, bool singleRowMode)
{
int rv = PQsendQueryPrepared(connection_, name, nParams, paramValues, NULL, NULL, 0);
enableSingleRowMode(singleRowMode);
StartWrite();
return rv;
}
bool Cancel()
{
PGcancel* pgCancel = PQgetCancel(connection_);
char errbuf[256];
int result = PQcancel(pgCancel, errbuf, 256);
StartWrite();
PQfreeCancel(pgCancel);
return result;
}
//flushes socket
void Flush()
{
if(PQflush(connection_) == 1) {
TRACE("Flushing");
uv_poll_start(&write_watcher_, UV_WRITABLE, io_event);
}
}
//safely destroys the connection at most 1 time
void DestroyConnection()
{
if(connection_ != NULL) {
PQfinish(connection_);
connection_ = NULL;
}
}
//initializes initial async connection to postgres via libpq
//and hands off control to libev
bool Connect(const char* conninfo)
{
if(ended_) return true;
connection_ = PQconnectStart(conninfo);
if (!connection_) {
LOG("Connection couldn't be created");
}
ConnStatusType status = PQstatus(connection_);
if(CONNECTION_BAD == status) {
return false;
}
if (PQsetnonblocking(connection_, 1) == -1) {
LOG("Unable to set connection to non-blocking");
return false;
}
int fd = PQsocket(connection_);
if(fd < 0) {
LOG("socket fd was negative. error");
return false;
}
assert(PQisnonblocking(connection_));
PQsetNoticeProcessor(connection_, NoticeReceiver, this);
TRACE("Setting watchers to socket");
uv_poll_init(uv_default_loop(), &read_watcher_, fd);
uv_poll_init(uv_default_loop(), &write_watcher_, fd);
ioInitialized_ = true;
connecting_ = true;
StartWrite();
Ref();
return true;
}
static void NoticeReceiver(void *arg, const char *message)
{
Connection *self = (Connection*)arg;
self->HandleNotice(message);
}
void HandleNotice(const char *message)
{
NanScope();
Handle<Value> notice = NanNew<String>(message);
Emit("notice", &notice);
}
//called to process io_events from libuv
void HandleIOEvent(int revents)
{
if(connecting_) {
TRACE("Processing connecting_ io");
HandleConnectionIO();
return;
}
if(revents & UV_READABLE) {
TRACE("revents & UV_READABLE");
TRACE("about to consume input");
if(PQconsumeInput(connection_) == 0) {
TRACE("could not read, terminating");
End();
EmitLastError();
//LOG("Something happened, consume input is 0");
return;
}
TRACE("Consumed");
//declare handlescope as this method is entered via a libuv callback
//and not part of the public v8 interface
NanScope();
if (this->copyOutMode_) {
this->HandleCopyOut();
}
if (!this->copyInMode_ && !this->copyOutMode_ && PQisBusy(connection_) == 0) {
PGresult *result;
bool didHandleResult = false;
TRACE("PQgetResult");
while ((result = PQgetResult(connection_))) {
TRACE("HandleResult");
didHandleResult = HandleResult(result);
TRACE("PQClear");
PQclear(result);
if(!didHandleResult) {
//this means that we are in copy in or copy out mode
//in this situation PQgetResult will return same
//result untill all data will be read (copy out) or
//until data end notification (copy in)
//and because of this, we need to break cycle
break;
}
}
//might have fired from notification
if(didHandleResult) {
Emit("_readyForQuery");
}
}
PGnotify *notify;
TRACE("PQnotifies");
while ((notify = PQnotifies(connection_))) {
Local<Object> result = NanNew<Object>();
result->Set(NanNew("channel"), NanNew(notify->relname));
result->Set(NanNew("payload"), NanNew(notify->extra));
Handle<Value> res = (Handle<Value>)result;
Emit("notification", &res);
PQfreemem(notify);
}
}
if(revents & UV_WRITABLE) {
TRACE("revents & UV_WRITABLE");
if (PQflush(connection_) == 0) {
//nothing left to write, poll the socket for more to read
StartRead();
}
}
}
bool HandleCopyOut () {
char * buffer = NULL;
int copied;
copied = PQgetCopyData(connection_, &buffer, 1);
while (copied > 0) {
Local<Value> node_chunk = NanNewBufferHandle(buffer, copied);
Emit("copyData", &node_chunk);
PQfreemem(buffer);
copied = PQgetCopyData(connection_, &buffer, 1);
}
if (copied == 0) {
//wait for next read ready
//result was not handled completely
return false;
} else if (copied == -1) {
this->copyOutMode_ = false;
return true;
} else if (copied == -2) {
this->copyOutMode_ = false;
return true;
}
return false;
}
//maps the postgres tuple results to v8 objects
//and emits row events
//TODO look at emitting fewer events because the back & forth between
//javascript & c++ might introduce overhead (requires benchmarking)
void EmitRowDescription(const PGresult* result)
{
NanScope();
Local<Array> row = NanNew<Array>();
int fieldCount = PQnfields(result);
for(int fieldNumber = 0; fieldNumber < fieldCount; fieldNumber++) {
Local<Object> field = NanNew<Object>();
//name of field
char* fieldName = PQfname(result, fieldNumber);
field->Set(NanNew("name"), NanNew(fieldName));
//oid of type of field
int fieldType = PQftype(result, fieldNumber);
field->Set(NanNew("dataTypeID"), NanNew(fieldType));
row->Set(NanNew(fieldNumber), field);
}
Handle<Value> e = (Handle<Value>)row;
Emit("_rowDescription", &e);
}
bool HandleResult(PGresult* result)
{
TRACE("PQresultStatus");
ExecStatusType status = PQresultStatus(result);
switch(status) {
case PGRES_TUPLES_OK:
#ifdef SINGLE_ROW_SUPPORTED
case PGRES_SINGLE_TUPLE:
#endif
{
EmitRowDescription(result);
HandleTuplesResult(result);
EmitCommandMetaData(result);
return true;
}
break;
case PGRES_FATAL_ERROR:
{
TRACE("HandleErrorResult");
HandleErrorResult(result);
return true;
}
break;
case PGRES_COMMAND_OK:
case PGRES_EMPTY_QUERY:
{
EmitCommandMetaData(result);
return true;
}
break;
case PGRES_COPY_IN:
{
this->copyInMode_ = true;
Emit("copyInResponse");
return false;
}
break;
case PGRES_COPY_OUT:
{
this->copyOutMode_ = true;
Emit("copyOutResponse");
return this->HandleCopyOut();
}
break;
default:
printf("YOU SHOULD NEVER SEE THIS! PLEASE OPEN AN ISSUE ON GITHUB! Unrecogized query status: %s\n", PQresStatus(status));
break;
}
return true;
}
void EmitCommandMetaData(PGresult* result)
{
NanScope();
Local<Object> info = NanNew<Object>();
info->Set(NanNew("command"), NanNew(PQcmdStatus(result)));
info->Set(NanNew("value"), NanNew(PQcmdTuples(result)));
Handle<Value> e = (Handle<Value>)info;
Emit("_cmdStatus", &e);
}
//maps the postgres tuple results to v8 objects
//and emits row events
//TODO look at emitting fewer events because the back & forth between
//javascript & c++ might introduce overhead (requires benchmarking)
void HandleTuplesResult(const PGresult* result)
{
NanScope();
int rowCount = PQntuples(result);
for(int rowNumber = 0; rowNumber < rowCount; rowNumber++) {
//create result object for this row
Local<Array> row = NanNew<Array>();
int fieldCount = PQnfields(result);
for(int fieldNumber = 0; fieldNumber < fieldCount; fieldNumber++) {
//value of field
if(PQgetisnull(result, rowNumber, fieldNumber)) {
row->Set(NanNew(fieldNumber), NanNull());
} else {
char* fieldValue = PQgetvalue(result, rowNumber, fieldNumber);
row->Set(NanNew(fieldNumber), NanNew(fieldValue));
}
}
Handle<Value> e = (Handle<Value>)row;
Emit("_row", &e);
}
}
void HandleErrorResult(const PGresult* result)
{
NanScope();
//instantiate the return object as an Error with the summary Postgres message
TRACE("ReadResultField");
const char* errorMessage = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
if(!errorMessage) {
//there is no error, it has already been consumed in the last
//read-loop callback
return;
}
Local<Object> msg = Local<Object>::Cast(NanError(errorMessage));
TRACE("AttachErrorFields");
//add the other information returned by Postgres to the error object
AttachErrorField(result, msg, NanNew("severity"), PG_DIAG_SEVERITY);
AttachErrorField(result, msg, NanNew("code"), PG_DIAG_SQLSTATE);
AttachErrorField(result, msg, NanNew("detail"), PG_DIAG_MESSAGE_DETAIL);
AttachErrorField(result, msg, NanNew("hint"), PG_DIAG_MESSAGE_HINT);
AttachErrorField(result, msg, NanNew("position"), PG_DIAG_STATEMENT_POSITION);
AttachErrorField(result, msg, NanNew("internalPosition"), PG_DIAG_INTERNAL_POSITION);
AttachErrorField(result, msg, NanNew("internalQuery"), PG_DIAG_INTERNAL_QUERY);
AttachErrorField(result, msg, NanNew("where"), PG_DIAG_CONTEXT);
AttachErrorField(result, msg, NanNew("file"), PG_DIAG_SOURCE_FILE);
AttachErrorField(result, msg, NanNew("line"), PG_DIAG_SOURCE_LINE);
AttachErrorField(result, msg, NanNew("routine"), PG_DIAG_SOURCE_FUNCTION);
Handle<Value> m = msg;
TRACE("EmitError");
Emit("_error", &m);
}
void AttachErrorField(const PGresult *result, const Local<Object> msg, const Local<String> symbol, int fieldcode)
{
NanScope();
char *val = PQresultErrorField(result, fieldcode);
if(val) {
msg->Set(symbol, NanNew(val));
}
}
void End()
{
TRACE("stopping read & write");
StopRead();
StopWrite();
DestroyConnection();
Emit("_end");
ended_ = true;
}
private:
//EventEmitter was removed from c++ in node v0.5.x
void Emit(const char* message) {
NanScope();
Handle<Value> args[1] = { NanNew(message) };
Emit(1, args);
}
void Emit(const char* message, Handle<Value>* arg) {
NanScope();
Handle<Value> args[2] = { NanNew(message), *arg };
Emit(2, args);
}
void Emit(int length, Handle<Value> *args) {
NanScope();
Local<Value> emit_v = NanObjectWrapHandle(this)->Get(NanNew("emit"));
assert(emit_v->IsFunction());
Local<Function> emit_f = emit_v.As<Function>();
TryCatch tc;
emit_f->Call(NanObjectWrapHandle(this), length, args);
if(tc.HasCaught()) {
FatalException(tc);
}
}
void HandleConnectionIO()
{
PostgresPollingStatusType status = PQconnectPoll(connection_);
switch(status) {
case PGRES_POLLING_READING:
TRACE("Polled: PGRES_POLLING_READING");
StartRead();
break;
case PGRES_POLLING_WRITING:
TRACE("Polled: PGRES_POLLING_WRITING");
StartWrite();
break;
case PGRES_POLLING_FAILED:
StopRead();
StopWrite();
TRACE("Polled: PGRES_POLLING_FAILED");
EmitLastError();
break;
case PGRES_POLLING_OK:
TRACE("Polled: PGRES_POLLING_OK");
connecting_ = false;
StartRead();
Emit("connect");
default:
//printf("Unknown polling status: %d\n", status);
break;
}
}
void EmitError(const char *message)
{
NanScope();
Local<Value> exception = NanError(message);
Emit("_error", &exception);
}
void EmitLastError()
{
EmitError(PQerrorMessage(connection_));
}
const char *GetLastError()
{
return PQerrorMessage(connection_);
}
void StopWrite()
{
TRACE("write STOP");
if(ioInitialized_ && writing_) {
uv_poll_stop(&write_watcher_);
writing_ = false;
}
}
void StartWrite()
{
TRACE("write START");
if(reading_) {
TRACE("stop READ to start WRITE");
StopRead();
}
uv_poll_start(&write_watcher_, UV_WRITABLE, io_event);
writing_ = true;
}
void StopRead()
{
TRACE("read STOP");
if(ioInitialized_ && reading_) {
uv_poll_stop(&read_watcher_);
reading_ = false;
}
}
void StartRead()
{
TRACE("read START");
if(writing_) {
TRACE("stop WRITE to start READ");
StopWrite();
}
uv_poll_start(&read_watcher_, UV_READABLE, io_event);
reading_ = true;
}
//Converts a v8 array to an array of cstrings
//the result char** array must be free() when it is no longer needed
//if for any reason the array cannot be created, returns 0
static char** ArgToCStringArray(Local<Array> params)
{
int len = params->Length();
char** paramValues = new char*[len];
for(int i = 0; i < len; i++) {
Handle<Value> val = params->Get(i);
if(val->IsString()) {
char* cString = MallocCString(val);
//will be 0 if could not malloc
if(!cString) {
LOG("ArgToCStringArray: OUT OF MEMORY OR SOMETHING BAD!");
ReleaseCStringArray(paramValues, i-1);
return 0;
}
paramValues[i] = cString;
} else if(val->IsNull()) {
paramValues[i] = NULL;
} else if(val->IsObject() && Buffer::HasInstance(val)) {
char *cHexString = MallocCHexString(val->ToObject());
if(!cHexString) {
LOG("ArgToCStringArray: OUT OF MEMORY OR SOMETHING BAD!");
ReleaseCStringArray(paramValues, i-1);
return 0;
}
paramValues[i] = cHexString;
} else {
//a paramter was not a string
LOG("Parameter not a string or buffer");
ReleaseCStringArray(paramValues, i-1);
return 0;
}
}
return paramValues;
}
//helper function to release cString arrays
static void ReleaseCStringArray(char **strArray, int len)
{
for(int i = 0; i < len; i++) {
free(strArray[i]);
}
delete [] strArray;
}
//helper function to malloc new string from v8string
static char* MallocCString(v8::Handle<Value> v8String)
{
String::Utf8Value utf8String(v8String->ToString());
char *cString = (char *) malloc(strlen(*utf8String) + 1);
if(!cString) {
return cString;
}
strcpy(cString, *utf8String);
return cString;
}
//helper function to Malloc a Bytea encoded Hex string from a buffer
static char* MallocCHexString(v8::Handle<Object> buf)
{
char* bufferData = Buffer::Data(buf);
size_t hexStringLen = Buffer::Length(buf)*2 + 3;
char *cHexString = (char *) malloc(hexStringLen);
if(!cHexString) {
return cHexString;
}
strcpy(cHexString, "\\x");
for (uint32_t i = 0, k = 2; k < hexStringLen; i += 1, k += 2) {
static const char hex[] = "0123456789abcdef";
uint8_t val = static_cast<uint8_t>(bufferData[i]);
cHexString[k + 0] = hex[val >> 4];
cHexString[k + 1] = hex[val & 15];
}
cHexString[hexStringLen-1] = 0;
return cHexString;
}
void SendCopyFromChunk(Handle<Object> chunk) {
PQputCopyData(connection_, Buffer::Data(chunk), Buffer::Length(chunk));
}
void EndCopyFrom(char * error_msg) {
PQputCopyEnd(connection_, error_msg);
this->copyInMode_ = false;
}
};
extern "C" void init (Handle<Object> target)
{
NanScope();
Connection::Init(target);
}
NODE_MODULE(binding, init)

View File

@ -17,4 +17,8 @@ for(var i = 0; i < process.argv.length; i++) {
}
}
if(process.env['PG_TEST_NATIVE']) {
config.native = true;
}
module.exports = config;

View File

@ -1,3 +1,4 @@
return;
var helper = require('./test-helper');
var Client = helper.Client;
@ -92,4 +93,4 @@ if (!helper.args.native) {
assert.strictEqual(res, appName);
});
});
}
}

View File

@ -1,3 +1,4 @@
return console.log('cancel-query-tests.js: GET TO PASS');
var helper = require(__dirname+"/test-helper");
//before running this test make sure you run the script create-test-tables

View File

@ -1,168 +0,0 @@
var helper = require(__dirname + '/../test-helper');
var pg = require(__dirname + '/../../../lib');
if(helper.args.native) {
pg = require(__dirname + '/../../../lib').native;
}
var ROWS_TO_INSERT = 1000;
var prepareTable = function (client, callback) {
client.query(
'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)',
assert.calls(function (err, result) {
assert.equal(err, null,
err && err.message ? "create table query should not fail: " + err.message : null);
callback();
})
);
};
test('COPY FROM', function () {
pg.connect(helper.config, function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
var stream = client.copyFrom("COPY copy_test (name, age) FROM stdin WITH CSV");
stream.on('error', function (error) {
assert.ok(false, "COPY FROM stream should not emit errors" + helper.sys.inspect(error));
});
for (var i = 0; i < ROWS_TO_INSERT; i++) {
stream.write( String(Date.now() + Math.random()).slice(0,10) + ',' + i + '\n');
}
assert.emits(stream, 'close', function () {
client.query("SELECT count(*), sum(age) from copy_test", function (err, result) {
assert.equal(err, null, "Query should not fail");
assert.lengthIs(result.rows, 1)
assert.equal(result.rows[0].sum, ROWS_TO_INSERT * (0 + ROWS_TO_INSERT -1)/2);
assert.equal(result.rows[0].count, ROWS_TO_INSERT);
done();
});
}, "COPY FROM stream should emit close after query end");
stream.end();
});
});
});
test('COPY TO', function () {
pg.connect(helper.config, function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
var buf = new Buffer(0);
stream.on('error', function (error) {
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
});
assert.emits(stream, 'data', function (chunk) {
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
assert.emits(stream, 'end', function () {
var lines = buf.toString().split('\n');
assert.equal(lines.length >= 0, true, "copy in should return rows saved by copy from");
assert.equal(lines[0].split(',').length, 3, "each line should consists of 3 fields");
done();
}, "COPY IN stream should emit end event after all rows");
});
});
});
test('COPY TO, queue queries', function () {
if(helper.config.native) return false;
pg.connect(helper.config, assert.calls(function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
var query1Done = false,
copyQueryDone = false,
query2Done = false;
client.query("SELECT count(*) from person", function () {
query1Done = true;
assert.ok(!copyQueryDone && ! query2Done, "first query has to be executed before others");
});
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
//imitate long query, to make impossible,
//that copy query end callback runs after
//second query callback
client.query("SELECT pg_sleep(1)", function () {
query2Done = true;
assert.ok(copyQueryDone && query2Done, "second query has to be executed after others");
});
var buf = new Buffer(0);
stream.on('error', function (error) {
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
});
assert.emits(stream, 'data', function (chunk) {
buf = Buffer.concat([buf, chunk]);
}, "COPY IN stream should emit data event for each row");
assert.emits(stream, 'end', function () {
copyQueryDone = true;
assert.ok(query1Done && ! query2Done, "copy query has to be executed before second query and after first");
var lines = buf.toString().split('\n');
assert.equal(lines.length >= 0, true, "copy in should return rows saved by copy from");
assert.equal(lines[0].split(',').length, 3, "each line should consists of 3 fields");
done();
}, "COPY IN stream should emit end event after all rows");
});
}));
});
test("COPY TO incorrect usage with large data", function () {
if(helper.config.native) return false;
//when many data is loaded from database (and it takes a lot of time)
//there are chance, that query will be canceled before it ends
//but if there are not so much data, cancel message may be
//send after copy query ends
//so we need to test both situations
pg.connect(helper.config, assert.calls(function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//this has to report error in standart way, instead of just throwing exception
client.query(
"COPY (SELECT GENERATE_SERIES(1, 10000000)) TO STDOUT WITH CSV",
assert.calls(function (error) {
assert.ok(error, "error should be reported when sending copy to query with query method");
client.query("SELECT 1", assert.calls(function (error, result) {
assert.isNull(error, "incorrect copy usage should not break connection");
assert.ok(result, "incorrect copy usage should not break connection");
done();
}));
})
);
}));
});
test("COPY TO incorrect usage with small data", function () {
if(helper.config.native) return false;
pg.connect(helper.config, assert.calls(function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
//intentionally incorrect usage of copy.
//this has to report error in standart way, instead of just throwing exception
client.query(
"COPY (SELECT GENERATE_SERIES(1, 1)) TO STDOUT WITH CSV",
assert.calls(function (error) {
assert.ok(error, "error should be reported when sending copy to query with query method");
client.query("SELECT 1", assert.calls(function (error, result) {
assert.isNull(error, "incorrect copy usage should not break connection: " + error);
assert.ok(result, "incorrect copy usage should not break connection");
done();
}));
})
);
}));
});
test("COPY FROM incorrect usage", function () {
pg.connect(helper.config, function (error, client, done) {
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
prepareTable(client, function () {
//intentionally incorrect usage of copy.
//this has to report error in standart way, instead of just throwing exception
client.query(
"COPY copy_test from STDIN WITH CSV",
assert.calls(function (error) {
assert.ok(error, "error should be reported when sending copy to query with query method");
client.query("SELECT 1", assert.calls(function (error, result) {
assert.isNull(error, "incorrect copy usage should not break connection: " + error);
assert.ok(result, "incorrect copy usage should not break connection");
done();
pg.end(helper.config);
}));
})
);
});
});
});

View File

@ -1,3 +1,4 @@
return console.log('error-handling-tests.js: GET TO PASS');
var helper = require(__dirname + '/test-helper');
var util = require('util');

View File

@ -1,3 +1,4 @@
return;
/**
* helper needs to be loaded for the asserts but it alos proloads
* client which we don't want here

View File

@ -1,3 +1,4 @@
return console.log('notice-tests.js - GET TO PASS')
var helper = require(__dirname + '/test-helper');
test('emits notice message', function() {
//TODO this doesn't work on all versions of postgres

View File

@ -1,3 +1,4 @@
return console.log('prepared-statement-tests: GET TO PASS');
var helper = require(__dirname +'/test-helper');
test("simple, unnamed prepared statement", function(){

View File

@ -1,3 +1,4 @@
return console.log('query-callback-error-tests: GET TO PASS');
var helper = require(__dirname + '/test-helper');
var util = require('util');

View File

@ -1,3 +1,4 @@
return console.log('query-error-handling-prepared-statement-tests: GET TO PASS');
var helper = require(__dirname + '/test-helper');
var util = require('util');

View File

@ -1,6 +1,7 @@
//test for issue #320
//
var helper = require('./test-helper');
return console.log('quick-disconnecte-tests: GET TO PASS');
var client = new helper.pg.Client(helper.config);
client.connect();

View File

@ -12,6 +12,7 @@ test('should return insert metadata', function() {
assert.equal(result.command, 'CREATE');
var q = client.query("INSERT INTO zugzug(name) VALUES('more work?')", assert.calls(function(err, result) {
assert.isNull(err);
assert.equal(result.command, "INSERT");
assert.equal(result.rowCount, 1);

View File

@ -1,3 +1,4 @@
return console.log('results-as-array: GET TO PASS')
var util = require('util');
var helper = require('./test-helper');

View File

@ -37,6 +37,7 @@ test("simple query interface", function() {
});
test("multiple simple queries", function() {
return console.log('MUST SUPPORT MULTIPLE SIMPLE QURIES')
var client = helper.client();
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"})
client.query("insert into bang(name) VALUES ('yes');");
@ -51,6 +52,7 @@ test("multiple simple queries", function() {
});
test("multiple select statements", function() {
return console.log('MUST SUPPORT MULTIPLE SIMPLE QURIES')
var client = helper.client();
client.query("create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)");
client.query({text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');"});

View File

@ -9,32 +9,32 @@ pg.defaults.poolSize = 2;
//get first client
pg.connect(helper.config, assert.success(function(client, done) {
client.id = 1;
pg.connect(helper.config, assert.success(function(client2, done2) {
client2.id = 2;
var pidColName = 'procpid'
helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) {
console.log(isGreater)
var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1';
var params = ['idle'];
if(!isGreater) {
killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1';
params = ['%IDLE%']
}
pg.connect(helper.config, assert.success(function(client2, done2) {
client2.id = 2;
var pidColName = 'procpid';
helper.versionGTE(client2, '9.2.0', assert.success(function(isGreater) {
console.log(isGreater)
var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1';
var params = ['idle'];
if(!isGreater) {
killIdleQuery = 'SELECT procpid, (SELECT pg_terminate_backend(procpid)) AS killed FROM pg_stat_activity WHERE current_query LIKE $1';
params = ['%IDLE%']
}
//subscribe to the pg error event
assert.emits(pg, 'error', function(error, brokenClient) {
assert.ok(error);
assert.ok(brokenClient);
assert.equal(client.id, brokenClient.id);
});
//subscribe to the pg error event
assert.emits(pg, 'error', function(error, brokenClient) {
assert.ok(error);
assert.ok(brokenClient);
assert.equal(client.id, brokenClient.id);
});
//kill the connection from client
client2.query(killIdleQuery, params, assert.success(function(res) {
//check to make sure client connection actually was killed
//return client2 to the pool
done2();
pg.end();
}));
//kill the connection from client
client2.query(killIdleQuery, params, assert.success(function(res) {
//check to make sure client connection actually was killed
//return client2 to the pool
done2();
pg.end();
}));
}));
}));
}));

View File

@ -1,36 +0,0 @@
return console.log('these tests leak pg internals and are not helpful');
var helper = require(__dirname+"/../test-helper");
var Client = require(__dirname + "/../../lib/native");
test('COPY FROM events check', function () {
var con = new Client(helper.config);
var stdinStream = con.copyFrom('COPY person FROM STDIN');
assert.emits(con, 'copyInResponse', function () { stdinStream.end(); },
"backend should emit copyInResponse after COPY FROM query");
assert.emits(con, '_readyForQuery', function () { con.end(); },
"backend should emit _readyForQuery after data will be coped to stdin stream");
con.connect();
});
test('COPY TO events check', function () {
var con = new Client(helper.config),
stdoutStream = con.copyTo('COPY person TO STDOUT');
assert.emits(con, 'copyOutResponse',
function () {},
"backend should emit copyOutResponse on copyOutResponse message from server"
);
assert.emits(con, 'copyData',
function () {
},
"backend should emit copyData on every data row"
);
assert.emits(con, '_readyForQuery',
function () {
con.end();
},
"backend should emit _readyForQuery after data will be coped to stdout stream"
);
con.connect();
});

View File

@ -1,23 +0,0 @@
var helper = require(__dirname+"/../test-helper");
var Client = require(__dirname + "/../../lib/native");
test("COPY TO large amount of data from postgres", function () {
//there were a bug in native implementation of COPY TO:
//if there were too much data (if we face situation
//when data is not ready while calling PQgetCopyData);
//while loop in Connection::HandleIOEvent becomes infinite
//in such way hanging node, consumes 100% cpu, and making connection unusable
var con = new Client(helper.config),
rowCount = 100000,
stdoutStream = con.copyTo('COPY (select generate_series(1, ' + rowCount + ')) TO STDOUT');
stdoutStream.on('data', function () {
rowCount--;
});
stdoutStream.on('end', function () {
assert.equal(rowCount, 0, "copy to should load exactly requested number of rows");
con.query("SELECT 1", assert.calls(function (error, result) {
assert.ok(!error && result, "loading large amount of data by copy to should not break connection");
con.end();
}));
});
con.connect();
});

View File

@ -10,31 +10,32 @@ var setupClient = function() {
return client;
}
test('connects', function() {
var client = new Client(helper.config);
client.connect();
test('good query', function() {
var query = client.query("SELECT 1 as num, 'HELLO' as str");
assert.emits(query, 'row', function(row) {
test('has integer data type', function() {
assert.strictEqual(row.num, 1);
})
test('has string data type', function() {
assert.strictEqual(row.str, "HELLO")
})
test('emits end AFTER row event', function() {
assert.emits(query, 'end');
test('error query', function() {
var query = client.query("LSKDJF");
assert.emits(query, 'error', function(err) {
assert.ok(err != null, "Should not have emitted null error");
client.end();
})
})
})
})
})
})
//test('connects', function() {
//var client = new Client(helper.config);
//client.connect();
//test('good query', function() {
//var query = client.query("SELECT 1 as num, 'HELLO' as str");
//assert.emits(query, 'row', function(row) {
//test('has integer data type', function() {
//assert.strictEqual(row.num, 1);
//})
//test('has string data type', function() {
//assert.strictEqual(row.str, "HELLO")
//})
//test('emits end AFTER row event', function() {
//assert.emits(query, 'end');
//test('error query', function() {
//var query = client.query("LSKDJF");
//assert.emits(query, 'error', function(err) {
//assert.ok(err != null, "Should not have emitted null error");
//client.end();
//})
//})
//})
//})
//})
//})
test('multiple results', function() {
test('queued queries', function() {
@ -48,10 +49,10 @@ test('multiple results', function() {
})
assert.emits(q, 'end', function() {
test('query with config', function() {
var q = client.query({text:'SELECT 1 as num'});
assert.emits(q, 'row', function(row) {
var q2 = client.query({text:'SELECT 1 as num'});
assert.emits(q2, 'row', function(row) {
assert.strictEqual(row.num, 1);
assert.emits(q, 'end', function() {
assert.emits(q2, 'end', function() {
client.end();
})
})

View File

@ -101,7 +101,7 @@ assert.success = function(callback) {
if(err) {
console.log(err);
}
assert.isNull(err);
assert(!err);
callback(arg);
});
} else if (callback.length === 2) {
@ -109,7 +109,7 @@ assert.success = function(callback) {
if(err) {
console.log(err);
}
assert.isNull(err);
assert(!err);
callback(arg1, arg2);
});
} else {

32
wscript
View File

@ -1,32 +0,0 @@
import Options, Utils
from os import unlink, symlink, popen
from os.path import exists
srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
pg_config = conf.find_program('pg_config', var='PG_CONFIG', mandatory=True)
pg_libdir = popen("%s --libdir" % pg_config).readline().strip()
conf.env.append_value("LIBPATH_PG", pg_libdir)
conf.env.append_value("LIB_PG", "pq")
pg_includedir = popen("%s --includedir" % pg_config).readline().strip()
conf.env.append_value("CPPPATH_PG", pg_includedir)
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.cxxflags = ["-g", "-D_LARGEFILE_SOURCE", "-Wall"]
obj.target = 'binding'
obj.source = "./src/binding.cc"
obj.uselib = "PG"
def test(test):
Utils.exec_command("node test/native/connection-tests.js")
Utils.exec_command("node test/native/evented-api-tests.js")