Node-drizzle APIDrizzle/MySQL bindings for Node.js using libdrizzle. Check out the Github repo for the source and installation guide. | |
connection.h | ./src/drizzle/connection.h |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #ifndef SRCDRIZZLECONNECTIONH #define SRCDRIZZLECONNECTIONH include <libdrizzle/drizzle.h>include <libdrizzle/drizzle_client.h>include <string>include "./exception.h"include "./result.h"namespace drizzle { class Connection { public: Connection(); ~Connection(); std::string getHostname() const; void setHostname(const std::string& hostname); std::string getUser() const; void setUser(const std::string& user); std::string getPassword() const; void setPassword(const std::string& password); std::string getDatabase() const; void setDatabase(const std::string& database); uint32t getPort() const; void setPort(uint32t port); bool isMysql() const; void setMysql(bool mysql); bool isOpened() const; void open() throw(Exception&); void close(); std::string escape(const std::string& string) const throw(Exception&); std::string version() const; Result* query(const std::string& query) const throw(Exception&);
}; } endif // SRC_DRIZZLE_CONNECTION_H_ |
|
connection.cc | ./src/drizzle/connection.cc |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #include "./connection.h" drizzle::Connection::Connection() :port(3306), mysql(true), opened(false), drizzle(NULL), connection(NULL) { } drizzle::Connection::~Connection() { this->close(); if (this->drizzle != NULL) { drizzle_free(this->drizzle); } } std::string drizzle::Connection::getHostname() const { return this->hostname; } void drizzle::Connection::setHostname(const std::string& hostname) { this->hostname = hostname; } std::string drizzle::Connection::getUser() const { return this->user; } void drizzle::Connection::setUser(const std::string& user) { this->user = user; } std::string drizzle::Connection::getPassword() const { return this->password; } void drizzle::Connection::setPassword(const std::string& password) { this->password = password; } std::string drizzle::Connection::getDatabase() const { return this->database; } void drizzle::Connection::setDatabase(const std::string& database) { this->database = database; } uint32_t drizzle::Connection::getPort() const { return this->port; } void drizzle::Connection::setPort(uint32_t port) { this->port = port; } bool drizzle::Connection::isMysql() const { return this->mysql; } void drizzle::Connection::setMysql(bool mysql) { this->mysql = mysql; } bool drizzle::Connection::isOpened() const { return this->opened; } void drizzle::Connection::open() throw(drizzle::Exception&) { this->close();
} void drizzle::Connection::close() { if (this->connection != NULL) { drizzleconclose(this->connection); drizzleconfree(this->connection); this->connection = NULL; } this->opened = false; } std::string drizzle::Connection::escape(const std::string& string) const throw(drizzle::Exception&) { char* buffer = new char[string.length() * 2 + 1]; if (buffer == NULL) { throw drizzle::Exception("Can\'t create buffer to escape string"); }
} std::string drizzle::Connection::version() const { std::string version; if (this->opened) { version = drizzleconserver_version(this->connection); } return version; } drizzle::Result* drizzle::Connection::query(const std::string& query) const throw(drizzle::Exception&) { if (!this->opened) { throw drizzle::Exception("Can't execute query without an opened connection"); }
} |
|
exception.h | ./src/drizzle/exception.h |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #ifndef SRCDRIZZLEEXCEPTIONH #define SRCDRIZZLEEXCEPTIONH include <exception>namespace drizzle { class Exception : public std::exception { public: explicit Exception(const char message); const char what() const throw(); protected: const char* message; }; } endif // SRC_DRIZZLE_EXCEPTION_H_ |
|
exception.cc | ./src/drizzle/exception.cc |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #include "./exception.h" drizzle::Exception::Exception(const char* message) : exception(), message(message) { } const char* drizzle::Exception::what() const throw() { return this->message; } |
|
result.h | ./src/drizzle/result.h |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #ifndef SRCDRIZZLERESULTH #define SRCDRIZZLERESULTH include <libdrizzle/drizzle.h>include <libdrizzle/drizzle_client.h>include <string>include <stdexcept>include "./exception.h"namespace drizzle { class Result { public: class Column { public: typedef enum { STRING, TEXT, INT, NUMBER, DATE, TIME, DATETIME, BOOL, SET } type_t;
}; } endif // SRC_DRIZZLE_RESULT_H_ |
|
drizzle.h | ./src/drizzle.h |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #ifndef SRCDRIZZLEH #define SRCDRIZZLEH include <stdlib.h>include <node.h>include <node_buffer.h>include <node_events.h>include <string>include <sstream>include <vector>include "./drizzle/connection.h"include "./drizzle/exception.h"include "./drizzle/result.h"include "./drizzle_bindings.h"namespace node_drizzle { class Drizzle : public node::EventEmitter { public: static void Init(v8::Handle<v8::Object> target);
}; } endif // SRC_DRIZZLE_H_ |
|
result.cc | ./src/drizzle/result.cc |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #include "./result.h" drizzle::Result::Column::Column(drizzlecolumnst *column) { this->name = drizzlecolumnname(column);
} drizzle::Result::Column::~Column() { } std::string drizzle::Result::Column::getName() const { return this->name; } drizzle::Result::Column::type_t drizzle::Result::Column::getType() const { return this->type; } drizzle::Result::Result(drizzlest* drizzle, drizzleresult_st* result) throw(drizzle::Exception&) : columns(NULL), totalColumns(0), rowNumber(0), drizzle(drizzle), result(result), previousRow(NULL), nextRow(NULL) { if (this->result == NULL) { throw drizzle::Exception("Invalid result"); }
} drizzle::Result::~Result() { if (this->columns != NULL) { for (uint16t i = 0; i < this->totalColumns; i++) { delete this->columns[i]; } delete [] this->columns; } if (this->result != NULL) { if (this->previousRow != NULL) { drizzlerowfree(this->result, this->previousRow); } if (this->nextRow != NULL) { drizzlerowfree(this->result, this->nextRow); } drizzleresult_free(this->result); } } bool drizzle::Result::hasNext() const { return (this->nextRow != NULL); } const char** drizzle::Result::next() throw(drizzle::Exception&) { if (this->previousRow != NULL) { drizzlerowfree(this->result, this->previousRow); }
} char drizzle::Result::row() throw(drizzle::Exception&) { drizzlereturnt result = DRIZZLERETURNIO_WAIT; char row = NULL;
} uint64t drizzle::Result::index() const throw(std::outofrange&) { if (this->rowNumber == 0) { throw std::outof_range("Not standing on a row"); } return (this->rowNumber - 1); } drizzle::Result::Column* drizzle::Result::column(uint16t i) const throw(std::outofrange&) { if (i < 0 || i >= this->totalColumns) { throw std::outof_range("Wrong column index"); } return this->columns[i]; } uint64t drizzle::Result::insertId() const { return drizzleresultinsertid(this->result); } uint64t drizzle::Result::affectedCount() const { return drizzleresultaffectedrows(this->result); } uint16t drizzle::Result::warningCount() const { return drizzleresultwarningcount(this->result); } uint16_t drizzle::Result::columnCount() const { return this->totalColumns; } |
|
drizzle.cc | ./src/drizzle.cc |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #include "./drizzle.h" void node_drizzle::Drizzle::Init(v8::Handle<v8::Object> target) { v8::HandleScope scope;
} node_drizzle::Drizzle::Drizzle(): node::EventEmitter(), connection(NULL) { } node_drizzle::Drizzle::~Drizzle() { if (this->connection != NULL) { delete this->connection; } } v8::Handle<v8::Value> node_drizzle::Drizzle::New(const v8::Arguments& args) { v8::HandleScope scope;
} v8::Handle<v8::Value> node_drizzle::Drizzle::Connect(const v8::Arguments& args) { v8::HandleScope scope;
} void nodedrizzle::Drizzle::connect(connectrequest_t* request) { try { request->drizzle->connection->open(); } catch(const drizzle::Exception& exception) { request->error = exception.what(); } } void nodedrizzle::Drizzle::connectFinished(connectrequest_t* request) { bool connected = request->drizzle->connection->isOpened();
} int nodedrizzle::Drizzle::eioConnect(eioreq* eioRequest) { connectrequestt *request = staticcast<connectrequest_t *>(eioRequest->data); assert(request);
} int nodedrizzle::Drizzle::eioConnectFinished(eioreq* eioRequest) { v8::HandleScope scope;
} v8::Handle<v8::Value> node_drizzle::Drizzle::Disconnect(const v8::Arguments& args) { v8::HandleScope scope;
} v8::Handle<v8::Value> node_drizzle::Drizzle::Escape(const v8::Arguments& args) { v8::HandleScope scope;
} v8::Handle<v8::Value> node_drizzle::Drizzle::Query(const v8::Arguments& args) { v8::HandleScope scope;
} int nodedrizzle::Drizzle::eioQuery(eioreq* eioRequest) { queryrequestt *request = staticcast<queryrequest_t *>(eioRequest->data); assert(request);
} int nodedrizzle::Drizzle::eioQueryFinished(eioreq* eioRequest) { v8::HandleScope scope;
} int nodedrizzle::Drizzle::eioQueryEach(eioreq* eioRequest) { queryrequestt *request = staticcast<queryrequest_t *>(eioRequest->data); assert(request);
} int nodedrizzle::Drizzle::eioQueryEachFinished(eioreq* eioRequest) { v8::HandleScope scope;
} void nodedrizzle::Drizzle::eioQueryCleanup(queryrequestt* request) { evunref(EVDEFAULTUC); request->drizzle->Unref();
} void nodedrizzle::Drizzle::eioQueryRequestFree(queryrequestt* request) { if (request->result != NULL) { if (request->rows != NULL) { uint16t columnCount = request->result->columnCount(); for (std::vector<std::string>::iterator iterator = request->rows->begin(), end = request->rows->end(); iterator != end; ++iterator) { std::string row = *iterator; for (uint16_t i = 0; i < columnCount; i++) { if (row[i] != NULL) { delete row[i]; } } delete [] row; }
} v8::Local<v8::Object> node_drizzle::Drizzle::row(drizzle::Result* result, std::string** currentRow, bool cast) const { v8::Local<v8::Object> row = v8::Object::New();
} std::string nodedrizzle::Drizzle::parseQuery(const std::string& query, v8::Local<v8::Array> values) const throw(drizzle::Exception&) { std::string parsed(query); std::vector<std::string::sizetype> positions; char quote = 0; bool escaped = false; uint32_t delta = 0;
} std::string node_drizzle::Drizzle::value(v8::Local<v8::Value> value, bool inArray) const throw(drizzle::Exception&) { std::ostringstream currentStream;
} uint64t nodedrizzle::Drizzle::toDate(const std::string& value, bool hasTime) const throw(drizzle::Exception&) { int day, month, year, hour, min, sec; char sep; std::istringstream stream(value, std::istringstream::in);
} std::string nodedrizzle::Drizzle::fromDate(const uint64t timeStamp) const throw(drizzle::Exception&) { char* buffer = new char[20]; if (buffer == NULL) { throw drizzle::Exception("Can\'t create buffer to write parsed date"); }
} int nodedrizzle::Drizzle::gmtDelta() const throw(drizzle::Exception&) { int localHour, gmtHour, localMin, gmtMin; timet rawtime; struct tm timeinfo;
} uint64t nodedrizzle::Drizzle::toTime(const std::string& value) const { int hour, min, sec; char sep; std::istringstream stream(value, std::istringstream::in);
} |
|
drizzle_bindings.h | ./src/drizzle_bindings.h |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #ifndef SRCDRIZZLEBINDINGSH #define SRCDRIZZLEBINDINGSH include <node.h>define NODE_CONSTANT(constant) v8::Integer::New(constant)define NODE_ADD_PROTOTYPE_METHOD(templ, name, callback) \do { \ v8::Local<v8::Signature> callback##_SIG = v8::Signature::New(templ); \ v8::Local<v8::FunctionTemplate> callback##TEM = \ v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \ __callback##SIG); \ templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \ __callback##_TEM); \ } while (0) define ARG_CHECK_OPTIONAL_STRING(I, VAR) \
define ARG_CHECK_STRING(I, VAR) \
define ARG_CHECK_OPTIONAL_OBJECT(I, VAR) \
define ARG_CHECK_OBJECT(I, VAR) \
define ARG_CHECK_OPTIONAL_FUNCTION(I, VAR) \
define ARG_CHECK_FUNCTION(I, VAR) \
define ARG_CHECK_OPTIONAL_ARRAY(I, VAR) \
define ARG_CHECK_ARRAY(I, VAR) \
define ARG_CHECK_OBJECT_ATTR_STRING(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_UINT32(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_BOOL(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_FUNCTION(VAR, KEY) \
define ARG_CHECK_OBJECT_ATTR_OPTIONAL_FUNCTION(VAR, KEY) \
endif // SRC_DRIZZLE_BINDINGS_H_ |
|
drizzle_bindings.cc | ./src/drizzle_bindings.cc |
// Copyright 2011 Mariano Iglesias mgiglesias@gmail.com #include "./drizzle_bindings.h" #include "./drizzle.h" extern "C" { void init(v8::Handle<v8::Object> target) { node_drizzle::Drizzle::Init(target); }
} |
|
drizzle | ./drizzle.js |