Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano committed Mar 21, 2011
0 parents commit 4363849
Show file tree
Hide file tree
Showing 16 changed files with 1,100 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
/build/*
/mysql-test.js
*.node
*.sh
*.swp
.lock*
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
build.sh
drizzle-test.js
22 changes: 22 additions & 0 deletions README.md
@@ -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
13 changes: 13 additions & 0 deletions db-mysql.js
@@ -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;
24 changes: 24 additions & 0 deletions package.json
@@ -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"
}
}
84 changes: 84 additions & 0 deletions src/connection.cc
@@ -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);
}
26 changes: 26 additions & 0 deletions src/connection.h
@@ -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_
55 changes: 55 additions & 0 deletions src/mysql.cc
@@ -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;
}
25 changes: 25 additions & 0 deletions src/mysql.h
@@ -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_
13 changes: 13 additions & 0 deletions src/mysql_bindings.cc
@@ -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);
}
38 changes: 38 additions & 0 deletions src/query.cc
@@ -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());
}
19 changes: 19 additions & 0 deletions src/query.h
@@ -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_

0 comments on commit 4363849

Please sign in to comment.