Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit. Connect() and close() functions implemented.

  • Loading branch information...
commit 8f8815484d2ff7b5a8e297455e35ff047463166f 0 parents
@Sannis authored
3  .gitignore
@@ -0,0 +1,3 @@
+*~
+.lock-wscript
+build/
22 LICENSE
@@ -0,0 +1,22 @@
+MIT License
+----
+
+Copyright (C) 2010 Oleg Efimov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
19 README
@@ -0,0 +1,19 @@
+MySQL syncronous binding for Node.js
+====================================
+
+Documentation isn't exists now.
+The code lives at http://github.com/sannis/node-mysql/
+
+The two files required to use these bindings are mysql-sync.js and
+build/default/mysql_sync_bindings.node. Put this directory in your
+NODE_PATH or copy those two files where you need them.
+
+Tested with Node version v0.1.30-15-g4e16e38
+
+== License & Copyright
+
+MIT License. See license text in file LICENSE.
+
+Copyright (C) 2010 Oleg Efimov
+E-mail: <efimovov@gmail.com>
+Site: http://sannis.ru>
20 mysql-sync.js
@@ -0,0 +1,20 @@
+/*
+Copyright (C) 2010, Oleg Efimov <efimovov@gmail.com>
+
+See license text in LICENSE file
+*/
+
+var binding = require("./mysql_sync_binding");
+var MysqlDbSync = binding.MysqlDbSync;
+var sys = require("sys");
+
+function createConnection (servername, user, password, database, port, socket)
+{
+ var db = new MysqlDbSync;
+ db.connect(servername, user, password, database, port, socket);
+
+ return db;
+}
+
+exports.createConnection = createConnection;
+
136 mysql_sync_binding.cc
@@ -0,0 +1,136 @@
+/*
+Copyright (C) 2010, Oleg Efimov <efimovov@gmail.com>
+
+See license text in LICENSE file
+*/
+#include <mysql/mysql.h>
+
+#include <v8.h>
+#include <node.h>
+#include <node_events.h>
+
+using namespace v8;
+using namespace node;
+
+//static Persistent<String> connect_symbol;
+//static Persistent<String> close_symbol;
+
+class MysqlDbSync : public EventEmitter
+{
+ public:
+ static void Init(v8::Handle<v8::Object> target)
+ {
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+
+ t->Inherit(EventEmitter::constructor_template);
+ t->InstanceTemplate()->SetInternalFieldCount(1);
+
+ //connect_symbol = NODE_PSYMBOL("connect");
+ //close_symbol = NODE_PSYMBOL("close");
+
+ NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
+ NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
+
+ target->Set(v8::String::NewSymbol("MysqlDbSync"), t->GetFunction());
+ }
+
+ bool Connect(const char* servername, const char* user, const char* password, const char* database, unsigned int port, const char* socket)
+ {printf("bool Connect()\n");
+ if (_connection) return false;
+
+ _connection = mysql_init(NULL);
+
+ if(!_connection)
+ {
+ return false;
+ }
+
+ if(!mysql_real_connect(_connection, servername, user, password, database, port, socket, 0))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ void Close()
+ {printf("void Close()\n");
+ if (_connection)
+ {printf("Inside if (_connection)\n");
+ mysql_close(_connection);
+ _connection = NULL;
+ }
+ }
+
+ protected:
+ MYSQL *_connection;
+
+ MysqlDbSync()
+ {
+ _connection = NULL;
+ }
+
+ ~MysqlDbSync()
+ {
+ if (_connection) mysql_close(_connection);
+ }
+
+ static Handle<Value> New (const Arguments& args)
+ {printf("static Handle<Value> New\n");
+ HandleScope scope;
+
+ MysqlDbSync *connection = new MysqlDbSync();
+ connection->Wrap(args.This());
+
+ return args.This();
+ }
+
+ static Handle<Value> Connect (const Arguments& args)
+ {printf("static Handle<Value> Connect\n");
+ HandleScope scope;
+
+ MysqlDbSync *connection = ObjectWrap::Unwrap<MysqlDbSync>(args.This());
+
+ //TODO: Check arguments
+ /*if (args.Length() == 0 || !args[0]->IsString()) {
+ return ThrowException(String::New("Must give conninfo string as argument"));
+ }*/
+
+ //TODO: Learn v8 types conversions
+ String::Utf8Value servername(args[0]->ToString());
+ String::Utf8Value user(args[1]->ToString());
+ String::Utf8Value password(args[2]->ToString());
+ String::Utf8Value database(args[3]->ToString());
+ //Local<Integer> port(args[4]->ToInteger()); port.Value()
+ String::Utf8Value socket(args[5]->ToString());
+
+ bool r = connection->Connect(*servername, *user, *password, *database, 3306, *socket);
+
+ //TODO:
+ /*if (!r) {
+ return ThrowException(Exception::Error(String::New(connection->ErrorMessage())));
+ }*/
+
+ return Undefined();
+ }
+
+ static Handle<Value> Close (const Arguments& args)
+ {printf("static Handle<Value> Close\n");
+ HandleScope scope;
+
+ MysqlDbSync *connection = ObjectWrap::Unwrap<MysqlDbSync>(args.This());
+
+ connection->Close();
+
+ return Undefined();
+ }
+
+};
+
+extern "C" void init (v8::Handle<Object> target)
+{
+ MysqlDbSync::Init(target);
+}
+
1  mysql_sync_binding.node
15 test-sync.js
@@ -0,0 +1,15 @@
+#!/usr/local/bin/node
+
+var sys = require("sys");
+var mysql_sync = require("./mysql-sync");
+
+var c = mysql_sync.createConnection("localhost", "test", "", "test");
+c.close();
+
+for( i = 0; i < 1000; i++ )
+{
+ c.connect("localhost", "test", "", "test");
+ c.close();
+}
+
+
16 wscript
@@ -0,0 +1,16 @@
+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")
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.target = "mysql_sync_binding"
+ obj.source = "mysql_sync_binding.cc"
+ obj.lib = "mysqlclient"
Please sign in to comment.
Something went wrong with that request. Please try again.