Skip to content

Commit a17f7fc

Browse files
committed
Merge pull request brianc#409 from rpedela/master
Fix build when escape functions are not supported in libpq
2 parents c3dcf28 + 6dffc0c commit a17f7fc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/native/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter;
44
var ConnectionParameters = require(__dirname + '/../connection-parameters');
55
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
66
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
7+
var JsClient = require(__dirname + '/../client'); // used to import JS escape functions
78

89
var binding;
910

@@ -80,6 +81,15 @@ Connection.prototype.endCopyFrom = function (msg) {
8081
this._endCopyFrom(msg);
8182
};
8283

84+
// use JS version if native version undefined
85+
// happens when PG version < 9.0.0
86+
if (!Connection.prototype.escapeIdentifier) {
87+
Connection.prototype.escapeIdentifier = JsClient.prototype.escapeIdentifier;
88+
}
89+
if (!Connection.prototype.escapeLiteral) {
90+
Connection.prototype.escapeLiteral = JsClient.prototype.escapeLiteral;
91+
}
92+
8393
Connection.prototype.query = function(config, values, callback) {
8494
var query = (config instanceof NativeQuery) ? config :
8595
new NativeQuery(config, values, callback);

src/binding.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <pg_config.h>
12
#include <libpq-fe.h>
23
#include <node.h>
34
#include <node_buffer.h>
@@ -8,6 +9,9 @@
89
#define LOG(msg) printf("%s\n",msg);
910
#define TRACE(msg) //printf("%s\n", msg);
1011

12+
#if PG_VERSION_NUM >= 90000
13+
#define ESCAPE_SUPPORTED
14+
#endif
1115

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

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

6973
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
74+
#ifdef ESCAPE_SUPPORTED
7075
NODE_SET_PROTOTYPE_METHOD(t, "escapeIdentifier", EscapeIdentifier);
7176
NODE_SET_PROTOTYPE_METHOD(t, "escapeLiteral", EscapeLiteral);
77+
#endif
7278
NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery);
7379
NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams);
7480
NODE_SET_PROTOTYPE_METHOD(t, "_sendPrepare", SendPrepare);
@@ -132,6 +138,7 @@ class Connection : public ObjectWrap {
132138
return Undefined();
133139
}
134140

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

184191
return scope.Close(jsStr);
185192
}
193+
#endif
186194

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

372+
#ifdef ESCAPE_SUPPORTED
364373
char * EscapeIdentifier(const char *str)
365374
{
366375
TRACE("js::EscapeIdentifier")
@@ -372,6 +381,7 @@ class Connection : public ObjectWrap {
372381
TRACE("js::EscapeLiteral")
373382
return PQescapeLiteral(connection_, str, strlen(str));
374383
}
384+
#endif
375385

376386
int Send(const char *queryText)
377387
{

0 commit comments

Comments
 (0)