Permalink
Browse files

Initial implementation of db-oracle, still placeholder -> parameter b…

…inding work to be done
  • Loading branch information...
0 parents commit 6a78fdcc8a6e2f29044ba7ef0e41e76acb195ee0 @mariano committed Jun 3, 2011
Showing with 825 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +3 −0 .gitmodules
  3. +50 −0 README.md
  4. +14 −0 db-oracle.js
  5. +1 −0 lib/node-db
  6. +30 −0 package.json
  7. +56 −0 src/connection.cc
  8. +29 −0 src/connection.h
  9. +94 −0 src/oracle.cc
  10. +26 −0 src/oracle.h
  11. +13 −0 src/oracle_bindings.cc
  12. +76 −0 src/query.cc
  13. +20 −0 src/query.h
  14. +271 −0 src/result.cc
  15. +63 −0 src/result.h
  16. +10 −0 tests.js
  17. +64 −0 wscript
@@ -0,0 +1,5 @@
+/build/*
+*.node
+*.sh
+*.swp
+.lock*
@@ -0,0 +1,3 @@
+[submodule "lib/node-db"]
+ path = lib/node-db
+ url = git://github.com/mariano/node-db.git
@@ -0,0 +1,50 @@
+# db-oracle: Oracle database bindings for Node.js #
+
+For detailed information about this and other Node.js
+database bindings visit the [Node.js db-mysql homepage] [homepage].
+
+## INSTALL ##
+
+Before proceeding with installation, you need to have the
+[OCI instant client] [oci] [libraries] [oci-lib] and [include files] [oci-inc].
+In order for the installation script to locate them properly, you'll
+need to set the OCI_INCLUDE_DIR and OCI_LIB_DIR environment variables.
+For example:
+
+ $ export OCI_INCLUDE_DIR=/opt/instantclient/sdk/include/
+ $ export OCI_LIB_DIR=/opt/instantclient
+
+Once the environment variables are set, install with npm:
+
+ $ npm install db-oracle
+
+## QUICK START ##
+
+ var oracle = require('db-oracle');
+ new oracle.Database({
+ hostname: 'localhost',
+ user: 'root',
+ password: 'password',
+ database: 'node'
+ }).connect(function(error) {
+ if (error) {
+ return console.log("CONNECTION ERROR: " + error);
+ }
+
+ this.query().select('*').from('users').execute(function(error, rows) {
+ if (error) {
+ return console.log('ERROR: ' + error);
+ }
+ console.log(rows.length + ' ROWS');
+ });
+ });
+
+## LICENSE ##
+
+This module is released under the [MIT License] [license].
+
+[homepage]: http://nodejsdb.org/db-mysql
+[license]: http://www.opensource.org/licenses/mit-license.php
+[oci]: http://www.oracle.com/technetwork/database/features/oci/index.html
+[oci-lib]: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
+[oci-inc]: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
@@ -0,0 +1,14 @@
+/*!
+ * Copyright by Mariano Iglesias
+ *
+ * See license text in LICENSE file
+ */
+
+/**
+ * Require bindings native binary
+ *
+ * @ignore
+ */
+var binding = require("./build/default/oracle_bindings");
+exports.Database = binding.Oracle;
+exports.Query = binding.Query;
Submodule node-db added at 3d2d08
@@ -0,0 +1,30 @@
+{ "name" : "db-oracle"
+, "description" : "Oracle database bindings for Node.JS"
+, "keywords": ["database","db","native","binding","library","plugin","client","oracle","oci","occi"]
+, "homepage" : "http://nodejsdb.org/db-oracle"
+, "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-oracle/issues" }
+, "licenses" : [ { "type" : "MIT" } ]
+, "repositories" :
+ [ { "type" : "git"
+ , "url" : "http://github.com/mariano/node-db-oracle.git"
+ }
+ ]
+, "main" : "./db-oracle"
+, "scripts" :
+ { "install": "node-waf configure build"
+ , "preuninstall": "rm -rf build/*"
+ , "test" : "node-waf test"
+ , "doc" : "node-waf doc"
+ }
+, "devDependencies" :
+ { "nodeunit" : "*"
+ , "nodelint" : "*"
+ }
+}
@@ -0,0 +1,56 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./connection.h"
+
+node_db_oracle::Connection::Connection()
+ : environment(NULL),
+ connection(NULL) {
+ this->port = 1521;
+ this->quoteName = '"';
+ this->environment = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::THREADED_MUTEXED);
+ if (this->environment == NULL) {
+ throw node_db::Exception("Cannot create environment");
+ }
+}
+
+node_db_oracle::Connection::~Connection() {
+ this->close();
+ oracle::occi::Environment::terminateEnvironment(this->environment);
+}
+
+bool node_db_oracle::Connection::isAlive(bool ping) throw() {
+ if (ping && this->alive) {
+ }
+ return this->alive;
+}
+
+void node_db_oracle::Connection::open() throw(node_db::Exception&) {
+ this->close();
+
+ std::ostringstream connection;
+ connection << "//" << this->hostname << ":" << this->port << "/" << this->database;
+ try {
+ this->connection = this->environment->createConnection(this->user, this->password, connection.str());
+ this->alive = true;
+ } catch(oracle::occi::SQLException& exception) {
+ throw node_db::Exception(exception.getMessage());
+ }
+}
+
+void node_db_oracle::Connection::close() {
+ if (this->alive) {
+ this->environment->terminateConnection(this->connection);
+ }
+ this->alive = false;
+}
+
+std::string node_db_oracle::Connection::escape(const std::string& string) const throw(node_db::Exception&) {
+ throw node_db::Exception("This binding does not implement escape()");
+}
+
+std::string node_db_oracle::Connection::version() const {
+ return this->connection->getServerVersion();
+}
+
+node_db::Result* node_db_oracle::Connection::query(const std::string& query) const throw(node_db::Exception&) {
+ return new node_db_oracle::Result(this->connection->createStatement(query));
+}
@@ -0,0 +1,29 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#ifndef SRC_CONNECTION_H_
+#define SRC_CONNECTION_H_
+
+#include <occi.h>
+#include <string>
+#include <sstream>
+#include "./node-db/connection.h"
+#include "./result.h"
+
+namespace node_db_oracle {
+class Connection : public node_db::Connection {
+ public:
+ Connection();
+ ~Connection();
+ bool isAlive(bool ping) throw();
+ 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:
+ oracle::occi::Environment* environment;
+ oracle::occi::Connection* connection;
+};
+}
+
+#endif // SRC_CONNECTION_H_
@@ -0,0 +1,94 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./oracle.h"
+
+v8::Persistent<v8::FunctionTemplate> node_db_oracle::Oracle::constructorTemplate;
+
+node_db_oracle::Oracle::Oracle(): node_db::Binding() {
+ this->connection = new node_db_oracle::Connection();
+ assert(this->connection);
+}
+
+node_db_oracle::Oracle::~Oracle() {
+ if (this->connection != NULL) {
+ delete this->connection;
+ }
+}
+
+void node_db_oracle::Oracle::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("Oracle"), constructorTemplate->GetFunction());
+}
+
+v8::Handle<v8::Value> node_db_oracle::Oracle::New(const v8::Arguments& args) {
+ v8::HandleScope scope;
+
+ node_db_oracle::Oracle* binding = new node_db_oracle::Oracle();
+ if (binding == NULL) {
+ THROW_EXCEPTION("Can't create client object")
+ }
+
+ if (args.Length() > 0) {
+ ARG_CHECK_OBJECT(0, options);
+
+ v8::Handle<v8::Value> set = binding->set(args[0]->ToObject());
+ if (!set.IsEmpty()) {
+ return scope.Close(set);
+ }
+ }
+
+ binding->Wrap(args.This());
+
+ return scope.Close(args.This());
+}
+
+v8::Handle<v8::Value> node_db_oracle::Oracle::set(const v8::Local<v8::Object> options) {
+ ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, hostname);
+ ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, user);
+ ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, password);
+ ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(options, database);
+ ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(options, port);
+
+ node_db_oracle::Connection* connection = static_cast<node_db_oracle::Connection*>(this->connection);
+
+ v8::String::Utf8Value hostname(options->Get(hostname_key)->ToString());
+ v8::String::Utf8Value user(options->Get(user_key)->ToString());
+ v8::String::Utf8Value password(options->Get(password_key)->ToString());
+ v8::String::Utf8Value database(options->Get(database_key)->ToString());
+
+ if (options->Has(hostname_key)) {
+ connection->setHostname(*hostname);
+ }
+
+ if (options->Has(user_key)) {
+ connection->setUser(*user);
+ }
+
+ if (options->Has(password_key)) {
+ connection->setPassword(*password);
+ }
+
+ if (options->Has(database_key)) {
+ connection->setDatabase(*database);
+ }
+
+ if (options->Has(port_key)) {
+ connection->setPort(options->Get(port_key)->ToInt32()->Value());
+ }
+
+ return v8::Handle<v8::Value>();
+}
+
+v8::Persistent<v8::Object> node_db_oracle::Oracle::createQuery() const {
+ v8::Persistent<v8::Object> query(
+ node_db_oracle::Query::constructorTemplate->GetFunction()->NewInstance());
+ return query;
+}
@@ -0,0 +1,26 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#ifndef SRC_ORACLE_H_
+#define SRC_ORACLE_H_
+
+#include "./node-db/node_defs.h"
+#include "./node-db/binding.h"
+#include "./connection.h"
+#include "./query.h"
+
+namespace node_db_oracle {
+class Oracle : public node_db::Binding {
+ public:
+ static void Init(v8::Handle<v8::Object> target);
+
+ protected:
+ static v8::Persistent<v8::FunctionTemplate> constructorTemplate;
+
+ Oracle();
+ ~Oracle();
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ v8::Handle<v8::Value> set(const v8::Local<v8::Object> options);
+ v8::Persistent<v8::Object> createQuery() const;
+};
+}
+
+#endif // SRC_ORACLE_H_
@@ -0,0 +1,13 @@
+// Copyright 2011 Mariano Iglesias <mgiglesias@gmail.com>
+#include "./node-db/binding.h"
+#include "./oracle.h"
+#include "./query.h"
+
+extern "C" {
+ void init(v8::Handle<v8::Object> target) {
+ node_db_oracle::Oracle::Init(target);
+ node_db_oracle::Query::Init(target);
+ }
+
+ NODE_MODULE(oracle_bindings, init);
+}
Oops, something went wrong.

0 comments on commit 6a78fdc

Please sign in to comment.