Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 806d7b47fe
Fetching contributors…

Cannot retrieve contributors at this time

file 243 lines (184 sloc) 8.216 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
#include "./binding.h"

node_db::Binding::Binding(): node_db::EventEmitter(), connection(NULL), cbConnect(NULL) {
}

node_db::Binding::~Binding() {
    if (this->cbConnect != NULL) {
        node::cb_destroy(this->cbConnect);
    }
}

void node_db::Binding::Init(v8::Handle<v8::Object> target, v8::Persistent<v8::FunctionTemplate> constructorTemplate) {
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_STRING, node_db::Result::Column::STRING);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_BOOL, node_db::Result::Column::BOOL);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_INT, node_db::Result::Column::INT);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_NUMBER, node_db::Result::Column::NUMBER);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_DATE, node_db::Result::Column::DATE);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_TIME, node_db::Result::Column::TIME);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_DATETIME, node_db::Result::Column::DATETIME);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_TEXT, node_db::Result::Column::TEXT);
    NODE_ADD_CONSTANT(constructorTemplate, COLUMN_TYPE_SET, node_db::Result::Column::SET);

    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "connect", Connect);
    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "disconnect", Disconnect);
    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "isConnected", IsConnected);
    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "escape", Escape);
    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "name", Name);
    NODE_ADD_PROTOTYPE_METHOD(constructorTemplate, "query", Query);
}

v8::Handle<v8::Value> node_db::Binding::Connect(const v8::Arguments& args) {
    v8::HandleScope scope;

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    bool async = true;
    int optionsIndex = -1, callbackIndex = -1;

    if (args.Length() > 0) {
        if (args.Length() > 1) {
            ARG_CHECK_OBJECT(0, options);
            ARG_CHECK_FUNCTION(1, callback);
            optionsIndex = 0;
            callbackIndex = 1;
        } else if (args[0]->IsFunction()) {
            ARG_CHECK_FUNCTION(0, callback);
            callbackIndex = 0;
        } else {
            ARG_CHECK_OBJECT(0, options);
            optionsIndex = 0;
        }

        if (optionsIndex >= 0) {
            v8::Local<v8::Object> options = args[optionsIndex]->ToObject();

            v8::Handle<v8::Value> set = binding->set(options);
            if (!set.IsEmpty()) {
                return scope.Close(set);
            }

            ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(options, async);

            if (options->Has(async_key) && options->Get(async_key)->IsFalse()) {
                async = false;
            }
        }

        if (callbackIndex >= 0) {
            binding->cbConnect = node::cb_persist(args[callbackIndex]);
        }
    }

    connect_request_t* request = new connect_request_t();
    if (request == NULL) {
        THROW_EXCEPTION("Could not create EIO request")
    }

    request->context = v8::Persistent<v8::Object>::New(args.This());
    request->binding = binding;
    request->error = NULL;

    if (async) {
        request->binding->Ref();

        uv_work_t* req = new uv_work_t();
        req->data = request;
        uv_queue_work(uv_default_loop(), req, uvConnect, uvConnectFinished);

        uv_ref(uv_default_loop());
    } else {
        connect(request);
        connectFinished(request);
    }

    return scope.Close(v8::Undefined());
}

void node_db::Binding::connect(connect_request_t* request) {
    try {
        request->binding->connection->open();
    } catch(const node_db::Exception& exception) {
        request->error = exception.what();
    }
}

void node_db::Binding::connectFinished(connect_request_t* request) {
    bool connected = request->binding->connection->isAlive();
    v8::Local<v8::Value> argv[2];

    if (connected) {
        v8::Local<v8::Object> server = v8::Object::New();
        server->Set(v8::String::New("version"), v8::String::New(request->binding->connection->version().c_str()));
        server->Set(v8::String::New("hostname"), v8::String::New(request->binding->connection->getHostname().c_str()));
        server->Set(v8::String::New("user"), v8::String::New(request->binding->connection->getUser().c_str()));
        server->Set(v8::String::New("database"), v8::String::New(request->binding->connection->getDatabase().c_str()));

        argv[0] = v8::Local<v8::Value>::New(v8::Null());
        argv[1] = server;

        request->binding->Emit("ready", 1, &argv[1]);
    } else {
        argv[0] = v8::String::New(request->error != NULL ? request->error : "(unknown error)");

        request->binding->Emit("error", 1, argv);
    }

    if (request->binding->cbConnect != NULL && !request->binding->cbConnect->IsEmpty()) {
        v8::TryCatch tryCatch;
        (*(request->binding->cbConnect))->Call(request->context, connected ? 2 : 1, argv);
        if (tryCatch.HasCaught()) {
            node::FatalException(tryCatch);
        }
    }

    request->context.Dispose();

    delete request;
}

void node_db::Binding::uvConnect(uv_work_t* uvRequest) {
    connect_request_t* request = static_cast<connect_request_t*>(uvRequest->data);
    assert(request);

    connect(request);
}

void node_db::Binding::uvConnectFinished(uv_work_t* uvRequest) {
    v8::HandleScope scope;

    connect_request_t* request = static_cast<connect_request_t*>(uvRequest->data);
    assert(request);

    uv_unref(uv_default_loop());
    request->binding->Unref();

    connectFinished(request);
}

v8::Handle<v8::Value> node_db::Binding::Disconnect(const v8::Arguments& args) {
    v8::HandleScope scope;

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    binding->connection->close();

    return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> node_db::Binding::IsConnected(const v8::Arguments& args) {
    v8::HandleScope scope;

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    return scope.Close(binding->connection->isAlive(true) ? v8::True() : v8::False());
}

v8::Handle<v8::Value> node_db::Binding::Escape(const v8::Arguments& args) {
    v8::HandleScope scope;

    ARG_CHECK_STRING(0, string);

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    std::string escaped;

    try {
        v8::String::Utf8Value string(args[0]->ToString());
        std::string unescaped(*string);
        escaped = binding->connection->escape(unescaped);
    } catch(const node_db::Exception& exception) {
        THROW_EXCEPTION(exception.what())
    }

    return scope.Close(v8::String::New(escaped.c_str()));
}

v8::Handle<v8::Value> node_db::Binding::Name(const v8::Arguments& args) {
    v8::HandleScope scope;

    ARG_CHECK_STRING(0, table);

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    std::ostringstream escaped;

    try {
        v8::String::Utf8Value string(args[0]->ToString());
        std::string unescaped(*string);
        escaped << binding->connection->escapeName(unescaped);
    } catch(const node_db::Exception& exception) {
        THROW_EXCEPTION(exception.what())
    }

    return scope.Close(v8::String::New(escaped.str().c_str()));
}

v8::Handle<v8::Value> node_db::Binding::Query(const v8::Arguments& args) {
    v8::HandleScope scope;

    node_db::Binding* binding = node::ObjectWrap::Unwrap<node_db::Binding>(args.This());
    assert(binding);

    v8::Persistent<v8::Object> query = binding->createQuery();
    if (query.IsEmpty()) {
        THROW_EXCEPTION("Could not create query");
    }

    node_db::Query* queryInstance = node::ObjectWrap::Unwrap<node_db::Query>(query);
    queryInstance->setConnection(binding->connection);

    v8::Handle<v8::Value> set = queryInstance->set(args);
    if (!set.IsEmpty()) {
        return scope.Close(set);
    }

    return scope.Close(query);
}
Something went wrong with that request. Please try again.