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

Styling #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
129 changes: 64 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
var PQ = module.exports = require('bindings')('addon.node').PQ;
var PQ = module.exports = require('bindings')('addon.node').PQ,
EventEmitter = require('events').EventEmitter,
assert = require('assert');

var EventEmitter = require('events').EventEmitter;
var assert = require('assert');

for(var key in EventEmitter.prototype) {
for (var key in EventEmitter.prototype) {
PQ.prototype[key] = EventEmitter.prototype[key];
}

//SYNC connects to the server
//throws an exception in the event of a connection error
PQ.prototype.connectSync = function(paramString) {
if(!paramString) {
PQ.prototype.connectSync = function (paramString) {
if (!paramString) {
paramString = '';
}
var connected = this.$connectSync(paramString);
if(!connected) {
if (!connected) {
this.finish();
throw new Error(this.errorMessage());
}
};

//connects async using a background thread
//calls the callback with an error if there was one
PQ.prototype.connect = function(paramString, cb) {
if(typeof paramString == 'function') {
PQ.prototype.connect = function (paramString, cb) {
if (typeof paramString == 'function') {
cb = paramString;
paramString = '';
}
if(!paramString) {
if (!paramString) {
paramString = '';
}
assert(cb, 'Must provide a connection callback');
this.$connect(paramString, cb);
};

PQ.prototype.errorMessage = function() {
PQ.prototype.errorMessage = function () {
return this.$getLastErrorMessage();
};

//returns an int for the fd of the socket
PQ.prototype.socket = function() {
PQ.prototype.socket = function () {
return this.$socket();
};

//finishes the connection & closes it
PQ.prototype.finish = function() {
PQ.prototype.finish = function () {
this.$finish();
};

Expand All @@ -53,8 +52,8 @@ PQ.prototype.finish = function() {
//ntuples, getvalue, etc...
//returns false if there was an error
//consume additional error details via PQ#errorMessage & friends
PQ.prototype.exec = function(commandText) {
if(!commandText) {
PQ.prototype.exec = function (commandText) {
if (!commandText) {
commandText = '';
}
this.$exec(commandText);
Expand All @@ -65,11 +64,11 @@ PQ.prototype.exec = function(commandText) {
//ntuples, getvalue, etc...
//returns false if there was an error
//consume additional error details via PQ#errorMessage & friends
PQ.prototype.execParams = function(commandText, parameters) {
if(!commandText) {
PQ.prototype.execParams = function (commandText, parameters) {
if (!commandText) {
commandText = '';
}
if(!parameters) {
if (!parameters) {
parameters = [];
}
this.$execParams(commandText, parameters);
Expand All @@ -80,12 +79,12 @@ PQ.prototype.execParams = function(commandText, parameters) {
//ntuples, getvalue, etc...
//returns false if there was an error
//consume additional error details via PQ#errorMessage & friends
PQ.prototype.prepare = function(statementName, commandText, nParams) {
PQ.prototype.prepare = function (statementName, commandText, nParams) {
assert.equal(arguments.length, 3, 'Must supply 3 arguments');
if(!statementName) {
if (!statementName) {
statementName = '';
}
if(!commandText) {
if (!commandText) {
commandText = '';
}
nParams = Number(nParams) || 0;
Expand All @@ -97,45 +96,45 @@ PQ.prototype.prepare = function(statementName, commandText, nParams) {
//ntuples, getvalue, etc...
//returns false if there was an error
//consume additional error details via PQ#errorMessage & friends
PQ.prototype.execPrepared = function(statementName, parameters) {
if(!statementName) {
PQ.prototype.execPrepared = function (statementName, parameters) {
if (!statementName) {
statementName = '';
}
if(!parameters) {
if (!parameters) {
parameters = [];
}
this.$execPrepared(statementName, parameters);
};

//send a command to begin executing a query in async mode
//returns true if sent, or false if there was a send failure
PQ.prototype.sendQuery = function(commandText) {
if(!commandText) {
PQ.prototype.sendQuery = function (commandText) {
if (!commandText) {
commandText = '';
}
return this.$sendQuery(commandText);
};

//send a command to begin executing a query with parameters in async mode
//returns true if sent, or false if there was a send failure
PQ.prototype.sendQueryParams = function(commandText, parameters) {
if(!commandText) {
PQ.prototype.sendQueryParams = function (commandText, parameters) {
if (!commandText) {
commandText = '';
}
if(!parameters) {
if (!parameters) {
parameters = [];
}
return this.$sendQueryParams(commandText, parameters);
};

//send a command to prepare a named query in async mode
//returns true if sent, or false if there was a send failure
PQ.prototype.sendPrepare = function(statementName, commandText, nParams) {
PQ.prototype.sendPrepare = function (statementName, commandText, nParams) {
assert.equal(arguments.length, 3, 'Must supply 3 arguments');
if(!statementName) {
if (!statementName) {
statementName = '';
}
if(!commandText) {
if (!commandText) {
commandText = '';
}
nParams = Number(nParams) || 0;
Expand All @@ -144,11 +143,11 @@ PQ.prototype.sendPrepare = function(statementName, commandText, nParams) {

//send a command to execute a named query in async mode
//returns true if sent, or false if there was a send failure
PQ.prototype.sendQueryPrepared = function(statementName, parameters) {
if(!statementName) {
PQ.prototype.sendQueryPrepared = function (statementName, parameters) {
if (!statementName) {
statementName = '';
}
if(!parameters) {
if (!parameters) {
parameters = [];
}
return this.$sendQueryPrepared(statementName, parameters);
Expand All @@ -161,67 +160,67 @@ PQ.prototype.sendQueryPrepared = function(statementName, parameters) {
//or false if there was no pending result. if there was no pending result
//the last found result is not overwritten so you can call getResult as many
//times as you want, and you'll always have the last available result for consumption
PQ.prototype.getResult = function() {
PQ.prototype.getResult = function () {
return this.$getResult();
};

//returns a text of the enum associated with the result
//usually just PGRES_COMMAND_OK or PGRES_FATAL_ERROR
PQ.prototype.resultStatus = function() {
PQ.prototype.resultStatus = function () {
return this.$resultStatus();
};

PQ.prototype.resultErrorMessage = function() {
PQ.prototype.resultErrorMessage = function () {
return this.$resultErrorMessage();
};

//free the memory associated with a result
//this is somewhat handled for you within the c/c++ code
//by never allowing the code to 'leak' a result. still,
//if you absolutely want to free it yourself, you can use this.
PQ.prototype.clear = function() {
PQ.prototype.clear = function () {
this.$clear();
};

//returns the number of tuples (rows) in the result set
PQ.prototype.ntuples = function() {
PQ.prototype.ntuples = function () {
return this.$ntuples();
};

//returns the number of fields (columns) in the result set
PQ.prototype.nfields = function() {
PQ.prototype.nfields = function () {
return this.$nfields();
};

//returns the name of the field (column) at the given offset
PQ.prototype.fname = function(offset) {
PQ.prototype.fname = function (offset) {
return this.$fname(offset);
};

//returns the Oid of the type for the given field
PQ.prototype.ftype = function(offset) {
PQ.prototype.ftype = function (offset) {
return this.$ftype(offset);
};

//returns a text value at the given row/col
//if the value is null this still returns empty string
//so you need to use PQ#getisnull to determine
PQ.prototype.getvalue = function(row, col) {
PQ.prototype.getvalue = function (row, col) {
return this.$getvalue(row, col);
};

//returns true/false if the value is null
PQ.prototype.getisnull = function(row, col) {
PQ.prototype.getisnull = function (row, col) {
return this.$getisnull(row, col);
};

//returns the status of the command
PQ.prototype.cmdStatus = function() {
PQ.prototype.cmdStatus = function () {
return this.$cmdStatus();
};

//returns the tuples in the command
PQ.prototype.cmdTuples = function() {
PQ.prototype.cmdTuples = function () {
return this.$cmdTuples();
};

Expand All @@ -231,76 +230,76 @@ PQ.prototype.cmdTuples = function() {
//works except to clear the SELECT() notification you need to call
//PQ#consumeInput instead of letting node pull the data off the socket
//http://www.postgresql.org/docs/9.1/static/libpq-async.html
PQ.prototype.startReader = function() {
PQ.prototype.startReader = function () {
this.$startRead();
};

//suspends the libuv socket 'read ready' listener
PQ.prototype.stopReader = function() {
PQ.prototype.stopReader = function () {
this.$stopRead();
};

PQ.prototype.writable = function(cb) {
PQ.prototype.writable = function (cb) {
this.$startWrite();
return this.once('writable', cb);
};

//returns boolean - false indicates an error condition
//e.g. a failure to consume input
PQ.prototype.consumeInput = function() {
PQ.prototype.consumeInput = function () {
return this.$consumeInput();
};

//returns true if PQ#getResult would cause
//the process to block waiting on results
//false indicates PQ#getResult can be called
//with an assurance of not blocking
PQ.prototype.isBusy = function() {
PQ.prototype.isBusy = function () {
return this.$isBusy();
};

//toggles the socket blocking on outgoing writes
PQ.prototype.setNonBlocking = function(truthy) {
PQ.prototype.setNonBlocking = function (truthy) {
return this.$setNonBlocking(truthy ? 1 : 0);
};

//returns true if the connection is non-blocking on writes, otherwise false
//note: connection is always non-blocking on reads if using the send* methods
PQ.prototype.isNonBlocking = function() {
PQ.prototype.isNonBlocking = function () {
return this.$isNonBlocking();
};

//returns 1 if socket is not write-ready
//returns 0 if all data flushed to socket
//returns -1 if there is an error
PQ.prototype.flush = function() {
PQ.prototype.flush = function () {
return this.$flush();
};

//escapes a literal and returns the escaped string
//I'm not 100% sure this doesn't do any I/O...need to check that
PQ.prototype.escapeLiteral = function(input) {
if(!input) return input;
PQ.prototype.escapeLiteral = function (input) {
if (!input) return input;
return this.$escapeLiteral(input);
};

PQ.prototype.escapeIdentifier = function(input) {
if(!input) return input;
PQ.prototype.escapeIdentifier = function (input) {
if (!input) return input;
return this.$escapeIdentifier(input);
};

//Checks for any notifications which may have arrivied
//and returns them as a javascript object: {relname: 'string', extra: 'string', be_pid: int}
//if there are no pending notifications this returns undefined
PQ.prototype.notifies = function() {
PQ.prototype.notifies = function () {
return this.$notifies();
};

//Sends a buffer of binary data to the server
//returns 1 if the command was sent successfully
//returns 0 if the command would block (use PQ#writable here if so)
//returns -1 if there was an error
PQ.prototype.putCopyData = function(buffer) {
PQ.prototype.putCopyData = function (buffer) {
assert(buffer instanceof Buffer);
return this.$putCopyData(buffer);
};
Expand All @@ -311,8 +310,8 @@ PQ.prototype.putCopyData = function(buffer) {
//returns 1 if sent succesfully
//returns 0 if the command would block
//returns -1 if there was an error
PQ.prototype.putCopyEnd = function(errorMessage) {
if(errorMessage) {
PQ.prototype.putCopyEnd = function (errorMessage) {
if (errorMessage) {
return this.$putCopyEnd(errorMessage);
}
return this.$putCopyEnd();
Expand All @@ -325,6 +324,6 @@ PQ.prototype.putCopyEnd = function(errorMessage) {
//returns 0 if copy is still in process (async only)
//returns -1 if the copy is done
//returns -2 if there was an error
PQ.prototype.getCopyData = function(async) {
PQ.prototype.getCopyData = function (async) {
return this.$getCopyData(!!async);
};
Loading