Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 4363849c76a6c1bf0cd645e33403aa57acba3299 @mariano committed Mar 21, 2011
Showing with 1,100 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 .npmignore
  3. +22 −0 README.md
  4. +13 −0 db-mysql.js
  5. +24 −0 package.json
  6. +84 −0 src/connection.cc
  7. +26 −0 src/connection.h
  8. +55 −0 src/mysql.cc
  9. +25 −0 src/mysql.h
  10. +13 −0 src/mysql_bindings.cc
  11. +38 −0 src/query.cc
  12. +19 −0 src/query.h
  13. +112 −0 src/result.cc
  14. +53 −0 src/result.h
  15. +542 −0 tests.js
  16. +66 −0 wscript
@@ -0,0 +1,6 @@
+/build/*
+/mysql-test.js
+*.node
+*.sh
+*.swp
+.lock*
@@ -0,0 +1,2 @@
+build.sh
+drizzle-test.js
@@ -0,0 +1,22 @@
+# node-db-mysql: MySQL bindings for Node.js #
+
+## LICENSE ##
+
+node-db-drizzle is released under the [MIT License] [license].
+
+## INSTALLATION ##
+
+### Install node-db-mysql ###
+
+#### Using npm ####
+
+ $ npm install db-mysql
+
+#### Using GIT ####
+
+ $ git clone https://github.com/mariano/node-db-mysql.git
+ $ git submodule update --init
+ $ cd node-db-mysql
+ $ npm install
+
+[license]: http://www.opensource.org/licenses/mit-license.php
@@ -0,0 +1,13 @@
+/*!
+ * Copyright by Mariano Iglesias
+ *
+ * See license text in LICENSE file
+ */
+
+/**
+ * Require bindings native binary
+ *
+ * @ignore
+ */
+var binding = require("./build/default/mysql_bindings");
+Mysql = binding.Mysql;
@@ -0,0 +1,24 @@
+{ "name" : "db-mysql"
+, "description" : "MySQL bindings for Node.JS"
+, "homepage" : "http://github.com/mariano/node-db-mysql"
+, "version" : "0.1.0"
+, "engines" : { "node" : ">=0.4.1" }
+, "maintainers" :
+ [ { "name": "Mariano Iglesias"
+ , "email": "mgiglesias@gmail.com"
+ }
+ ]
+, "bugs" : { "web" : "http://github.com/mariano/node-db-mysql/issues" }
+, "licenses" : [ { "type" : "MIT" } ]
+, "repositories" :
+ [ { "type" : "git"
+ , "url" : "http://github.com/mariano/node-db-mysql.git"
+ }
+ ]
+, "main" : "./db-mysql"
+, "scripts" :
+ { "install" : "node-waf configure build"
+ , "test" : "node-waf test"
+ , "doc" : "node-waf doc"
+ }
+}
@@ -0,0 +1,84 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./connection.h"
+
+node_db_mysql::Connection::Connection():
+ connection(NULL) {
+ this->quoteString = '\'';
+ this->quoteField = '`';
+ this->quoteTable = '`';
+ this->connection = new MYSQL();
+ if (this->connection == NULL) {
+ throw node_db::Exception("Cannot create MYSQL handle");
+ }
+ mysql_init(this->connection);
+}
+
+node_db_mysql::Connection::~Connection() {
+ this->close();
+ if (this->connection != NULL) {
+ delete this->connection;
+ }
+}
+
+void node_db_mysql::Connection::open() throw(node_db::Exception&) {
+ this->close();
+
+ this->opened = mysql_real_connect(
+ this->connection,
+ this->hostname.c_str(),
+ this->user.c_str(),
+ this->password.c_str(),
+ this->database.c_str(),
+ this->port,
+ NULL,
+ 0
+ );
+ if (!this->opened) {
+ throw node_db::Exception(mysql_error(this->connection));
+ }
+}
+
+void node_db_mysql::Connection::close() {
+ if (this->opened) {
+ mysql_close(this->connection);
+ }
+ this->opened = false;
+}
+
+std::string node_db_mysql::Connection::escape(const std::string& string) const throw(node_db::Exception&) {
+ char* buffer = new char[string.length() * 2 + 1];
+ if (buffer == NULL) {
+ throw node_db::Exception("Can\'t create buffer to escape string");
+ }
+
+ mysql_real_escape_string(this->connection, buffer, string.c_str(), string.length());
+
+ std::string escaped = buffer;
+ delete [] buffer;
+ return escaped;
+}
+
+std::string node_db_mysql::Connection::version() const {
+ std::string version;
+ if (this->opened) {
+ version = mysql_get_server_info(this->connection);
+ }
+ return version;
+}
+
+node_db::Result* node_db_mysql::Connection::query(const std::string& query) const throw(node_db::Exception&) {
+ if (!this->opened) {
+ throw node_db::Exception("Can't execute query without an opened connection");
+ }
+
+ if (mysql_query(this->connection, query.c_str()) != 0) {
+ throw node_db::Exception(mysql_error(this->connection));
+ }
+
+ MYSQL_RES* result = mysql_use_result(this->connection);
+ if (result == NULL) {
+ throw node_db::Exception("Could not fetch result of query");
+ }
+
+ return new node_db_mysql::Result(this->connection, result);
+}
@@ -0,0 +1,26 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#ifndef SRC_CONNECTION_H_
+#define SRC_CONNECTION_H_
+
+#include <mysql/mysql.h>
+#include <string>
+#include "./node-db/connection.h"
+#include "./result.h"
+
+namespace node_db_mysql {
+class Connection : public node_db::Connection {
+ public:
+ Connection();
+ ~Connection();
+ void open() throw(node_db::Exception&);
+ void close();
+ std::string escape(const std::string& string) const throw(node_db::Exception&);
+ std::string version() const;
+ node_db::Result* query(const std::string& query) const throw(node_db::Exception&);
+
+ private:
+ MYSQL* connection;
+};
+}
+
+#endif // SRC_CONNECTION_H_
@@ -0,0 +1,55 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./mysql.h"
+
+v8::Persistent<v8::FunctionTemplate> node_db_mysql::Mysql::constructorTemplate;
+
+node_db_mysql::Mysql::Mysql(): node_db::Binding() {
+ this->connection = new node_db_mysql::Connection();
+ assert(this->connection);
+}
+
+node_db_mysql::Mysql::~Mysql() {
+ if (this->connection != NULL) {
+ delete this->connection;
+ }
+}
+
+void node_db_mysql::Mysql::Init(v8::Handle<v8::Object> target) {
+ v8::HandleScope scope;
+
+ v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
+
+ constructorTemplate = v8::Persistent<v8::FunctionTemplate>::New(t);
+ constructorTemplate->Inherit(node::EventEmitter::constructor_template);
+ constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
+
+ node_db::Binding::Init(target, constructorTemplate);
+
+ target->Set(v8::String::NewSymbol("Mysql"), constructorTemplate->GetFunction());
+}
+
+v8::Handle<v8::Value> node_db_mysql::Mysql::New(const v8::Arguments& args) {
+ v8::HandleScope scope;
+
+ node_db_mysql::Mysql* binding = new node_db_mysql::Mysql();
+ if (binding == NULL) {
+ THROW_EXCEPTION("Can't create client object")
+ }
+
+ if (args.Length() > 0) {
+ v8::Handle<v8::Value> set = binding->set(args);
+ if (!set.IsEmpty()) {
+ return scope.Close(set);
+ }
+ }
+
+ binding->Wrap(args.This());
+
+ return scope.Close(args.This());
+}
+
+v8::Persistent<v8::Object> node_db_mysql::Mysql::createQuery() const {
+ v8::Persistent<v8::Object> query(
+ node_db_mysql::Query::constructorTemplate->GetFunction()->NewInstance());
+ return query;
+}
@@ -0,0 +1,25 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#ifndef SRC_DRIZZLE_H_
+#define SRC_DRIZZLE_H_
+
+#include "./node-db/node_defs.h"
+#include "./node-db/binding.h"
+#include "./connection.h"
+#include "./query.h"
+
+namespace node_db_mysql {
+class Mysql : public node_db::Binding {
+ public:
+ static void Init(v8::Handle<v8::Object> target);
+
+ protected:
+ static v8::Persistent<v8::FunctionTemplate> constructorTemplate;
+
+ Mysql();
+ ~Mysql();
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ v8::Persistent<v8::Object> createQuery() const;
+};
+}
+
+#endif // SRC_DRIZZLE_H_
@@ -0,0 +1,13 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./node-db/binding.h"
+#include "./mysql.h"
+#include "./query.h"
+
+extern "C" {
+ void init(v8::Handle<v8::Object> target) {
+ node_db_mysql::Mysql::Init(target);
+ node_db_mysql::Query::Init(target);
+ }
+
+ NODE_MODULE(mysql_bindings, init);
+}
@@ -0,0 +1,38 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./query.h"
+
+v8::Persistent<v8::FunctionTemplate> node_db_mysql::Query::constructorTemplate;
+
+void node_db_mysql::Query::Init(v8::Handle<v8::Object> target) {
+ v8::HandleScope scope;
+
+ v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
+
+ constructorTemplate = v8::Persistent<v8::FunctionTemplate>::New(t);
+ constructorTemplate->Inherit(node::EventEmitter::constructor_template);
+ constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
+
+ node_db::Query::Init(target, constructorTemplate);
+
+ target->Set(v8::String::NewSymbol("Query"), constructorTemplate->GetFunction());
+}
+
+v8::Handle<v8::Value> node_db_mysql::Query::New(const v8::Arguments& args) {
+ v8::HandleScope scope;
+
+ node_db_mysql::Query* query = new node_db_mysql::Query();
+ if (query == NULL) {
+ THROW_EXCEPTION("Can't create query object")
+ }
+
+ if (args.Length() > 0) {
+ v8::Handle<v8::Value> set = query->set(args);
+ if (!set.IsEmpty()) {
+ return scope.Close(set);
+ }
+ }
+
+ query->Wrap(args.This());
+
+ return scope.Close(args.This());
+}
@@ -0,0 +1,19 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#ifndef SRC_QUERY_H_
+#define SRC_QUERY_H_
+
+#include "./node-db/node_defs.h"
+#include "./node-db/query.h"
+
+namespace node_db_mysql {
+class Query : public node_db::Query {
+ public:
+ static v8::Persistent<v8::FunctionTemplate> constructorTemplate;
+ static void Init(v8::Handle<v8::Object> target);
+
+ protected:
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+};
+}
+
+#endif // SRC_QUERY_H_
Oops, something went wrong. Retry.

0 comments on commit 4363849

Please sign in to comment.