initial experiment with libpq bindings

This commit is contained in:
Brian Carlson 2011-02-18 11:38:47 -06:00
parent c79b9e9136
commit 0f0f59c12f
4 changed files with 153 additions and 0 deletions

2
lib/binding.js Normal file
View File

@ -0,0 +1,2 @@
var binding = require(__dirname + '/../build/default/binding');
module.exports = binding.Connection;

113
src/binding.cc Normal file
View File

@ -0,0 +1,113 @@
#include <libpq-fe.h>
#include <node.h>
#include <node_events.h>
#include <assert.h>
#include <stdlib.h>
#define LOG(msg) printf("%s\n",msg)
using namespace v8;
using namespace node;
class Connection : public EventEmitter {
public:
static void
Init (Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("Connection"));
NODE_SET_PROTOTYPE_METHOD(t, "test", Test);
target->Set(String::NewSymbol("Connection"), t->GetFunction());
LOG("created class");
}
static Handle<Value>
Test(const Arguments& args)
{
HandleScope scope;
PGconn *connection_ = PQconnectStart("");
if (!connection_) {
LOG("Connection couldn't be created");
} else {
LOG("Connect created");
}
if (PQsetnonblocking(connection_, 1) == -1) {
LOG("Unable to set connection to non-blocking");
PQfinish(connection_);
connection_ = NULL;
}
ConnStatusType status = PQstatus(connection_);
printf("status: %d\n", status);
if(CONNECTION_BAD == status) {
PQfinish(connection_);
LOG("Bad connection status");
connection_ = NULL;
}
int fd = PQsocket(connection_);
if(fd < 0) {
LOG("socket fd was negative. error");
}
LOG("Initializing ev watchers");
ev_io read_watcher;
ev_io write_watcher;
ev_init(&read_watcher, io_event);
ev_init(&write_watcher, io_event);
ev_io_set(&read_watcher, fd, EV_READ);
ev_io_set(&write_watcher, fd, EV_WRITE);
ev_io_start(EV_DEFAULT_ &write_watcher);
LOG("EV started");
Local<String> result = String::New("Hello world");
return scope.Close(result);
}
static void
io_event(EV_P_ ev_io *w, int revents)
{
LOG("Received IO event");
}
Connection () : EventEmitter ()
{
}
~Connection ()
{
}
protected:
static Handle<Value>
New (const Arguments& args)
{
HandleScope scope;
Connection *connection = new Connection();
connection->Wrap(args.This());
return args.This();
}
private:
ev_io read_watcher_;
ev_io write_watcher_;
};
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
Connection::Init(target);
}

View File

@ -0,0 +1,7 @@
var Connection = require(__dirname + "/../../lib/binding");
var con = new Connection();
console.log(con.test());
setTimeout(function() {
}, 1000)

31
wscript Normal file
View File

@ -0,0 +1,31 @@
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/integration/binding-spike.js")