Skip to content

Commit cf07a4f

Browse files
author
rpedela
committed
brianc#403 Only use native escape functions if PG version >= 9.0.0. Otherwise use the JS functions.
1 parent baaacd2 commit cf07a4f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#define LOG(msg) printf("%s\n",msg);
99
#define TRACE(msg) //printf("%s\n", msg);
1010

11+
#if PG_VERSION_NUM > 90000
12+
#define ESCAPE_SUPPORTED
13+
#endif
1114

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

@@ -67,8 +70,10 @@ class Connection : public ObjectWrap {
6770
command_symbol = NODE_PSYMBOL("command");
6871

6972
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
73+
#ifdef ESCAPE_SUPPORTED
7074
NODE_SET_PROTOTYPE_METHOD(t, "escapeIdentifier", EscapeIdentifier);
7175
NODE_SET_PROTOTYPE_METHOD(t, "escapeLiteral", EscapeLiteral);
76+
#endif
7277
NODE_SET_PROTOTYPE_METHOD(t, "_sendQuery", SendQuery);
7378
NODE_SET_PROTOTYPE_METHOD(t, "_sendQueryWithParams", SendQueryWithParams);
7479
NODE_SET_PROTOTYPE_METHOD(t, "_sendPrepare", SendPrepare);
@@ -132,6 +137,7 @@ class Connection : public ObjectWrap {
132137
return Undefined();
133138
}
134139

140+
#ifdef ESCAPE_SUPPORTED
135141
//v8 entry point into Connection#escapeIdentifier
136142
static Handle<Value>
137143
EscapeIdentifier(const Arguments& args)
@@ -183,6 +189,7 @@ class Connection : public ObjectWrap {
183189

184190
return scope.Close(jsStr);
185191
}
192+
#endif
186193

187194
//v8 entry point into Connection#_sendQuery
188195
static Handle<Value>
@@ -361,6 +368,7 @@ class Connection : public ObjectWrap {
361368
return args.This();
362369
}
363370

371+
#ifdef ESCAPE_SUPPORTED
364372
char * EscapeIdentifier(const char *str)
365373
{
366374
TRACE("js::EscapeIdentifier")
@@ -372,6 +380,7 @@ class Connection : public ObjectWrap {
372380
TRACE("js::EscapeLiteral")
373381
return PQescapeLiteral(connection_, str, strlen(str));
374382
}
383+
#endif
375384

376385
int Send(const char *queryText)
377386
{

0 commit comments

Comments
 (0)