Skip to content

Fix build when escape functions are not supported in libpq #409

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

Merged
merged 5 commits into from
Aug 1, 2013
Merged
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
10 changes: 10 additions & 0 deletions lib/native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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;

Expand Down Expand Up @@ -80,6 +81,15 @@ 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);
Expand Down
10 changes: 10 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <pg_config.h>
#include <libpq-fe.h>
#include <node.h>
#include <node_buffer.h>
Expand All @@ -8,6 +9,9 @@
#define LOG(msg) printf("%s\n",msg);
#define TRACE(msg) //printf("%s\n", msg);

#if PG_VERSION_NUM >= 90000
#define ESCAPE_SUPPORTED
#endif

#define THROW(msg) return ThrowException(Exception::Error(String::New(msg)));

Expand Down Expand Up @@ -67,8 +71,10 @@ class Connection : public ObjectWrap {
command_symbol = NODE_PSYMBOL("command");

NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
#ifdef ESCAPE_SUPPORTED
NODE_SET_PROTOTYPE_METHOD(t, "escapeIdentifier", EscapeIdentifier);
NODE_SET_PROTOTYPE_METHOD(t, "escapeLiteral", EscapeLiteral);
#endif
NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery);
NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams);
NODE_SET_PROTOTYPE_METHOD(t, "_sendPrepare", SendPrepare);
Expand Down Expand Up @@ -132,6 +138,7 @@ class Connection : public ObjectWrap {
return Undefined();
}

#ifdef ESCAPE_SUPPORTED
//v8 entry point into Connection#escapeIdentifier
static Handle<Value>
EscapeIdentifier(const Arguments& args)
Expand Down Expand Up @@ -183,6 +190,7 @@ class Connection : public ObjectWrap {

return scope.Close(jsStr);
}
#endif

//v8 entry point into Connection#_sendQuery
static Handle<Value>
Expand Down Expand Up @@ -361,6 +369,7 @@ class Connection : public ObjectWrap {
return args.This();
}

#ifdef ESCAPE_SUPPORTED
char * EscapeIdentifier(const char *str)
{
TRACE("js::EscapeIdentifier")
Expand All @@ -372,6 +381,7 @@ class Connection : public ObjectWrap {
TRACE("js::EscapeLiteral")
return PQescapeLiteral(connection_, str, strlen(str));
}
#endif

int Send(const char *queryText)
{
Expand Down