<commit_before>#include <string.h>
#include "connection.h"
#include "executeBaton.h"
#include "commitBaton.h"
#include "rollbackBaton.h"
#include "outParam.h"
#include "node_buffer.h"
#include <vector>
#include <node_version.h>
#include <iostream>
using namespace std;

Persistent<FunctionTemplate> Connection::constructorTemplate;

void Connection::Init(Handle<Object> target) {
  HandleScope scope;

  Local<FunctionTemplate> t = FunctionTemplate::New(New);
  constructorTemplate = Persistent<FunctionTemplate>::New(t);
  constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
  constructorTemplate->SetClassName(String::NewSymbol("Connection"));

  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "execute", Execute);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "close", Close);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "isConnected", IsConnected);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "setAutoCommit", SetAutoCommit);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "commit", Commit);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "rollback", Rollback);

  target->Set(String::NewSymbol("Connection"), constructorTemplate->GetFunction());
}

Handle<Value> Connection::New(const Arguments& args) {
  HandleScope scope;

  Connection *connection = new Connection();
  connection->Wrap(args.This());
  return args.This();
}

Connection::Connection() {
}

Connection::~Connection() {
  closeConnection();
}

Handle<Value> Connection::Execute(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_STRING_ARG(0, sql);
  REQ_ARRAY_ARG(1, values);
  REQ_FUN_ARG(2, callback);

  String::AsciiValue sqlVal(sql);

  ExecuteBaton* baton;
  try {
    baton = new ExecuteBaton(connection, *sqlVal, &values, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Execute, (uv_after_work_cb)EIO_AfterExecute);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::Close(const Arguments& args) {
  try {
	  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
	  connection->closeConnection();

	  return Undefined();
  } catch (const std::exception& ex) {
	  printf("Exception: %s\n", ex.what());
	  return Undefined();
  }
}

Handle<Value> Connection::IsConnected(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  if(connection && connection->m_connection) {
    return Boolean::New(true);
  } else {
    return Boolean::New(false);
  }
}

Handle<Value> Connection::Commit(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_FUN_ARG(0, callback);

  CommitBaton* baton;
  try {
    baton = new CommitBaton(connection, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Commit, (uv_after_work_cb)EIO_AfterCommit);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::Rollback(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_FUN_ARG(0, callback);

  RollbackBaton* baton;
  try {
    baton = new RollbackBaton(connection, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Rollback, (uv_after_work_cb)EIO_AfterRollback);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::SetAutoCommit(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
  REQ_BOOL_ARG(0, autoCommit);
  connection->m_autoCommit = autoCommit;
  return Undefined();
}

void Connection::closeConnection() {
  if(m_environment && m_connection) {
    m_environment->terminateConnection(m_connection);
    m_connection = NULL;
  }
}

void RandomBytesFree(char* data, void* hint) {
  delete[] data;
}

int Connection::SetValuesOnStatement(oracle::occi::Statement* stmt, std::vector<value_t*> &values) {
  uint32_t index = 1;
  int outputParam = -1;
  for (std::vector<value_t*>::iterator iterator = values.begin(), end = values.end(); iterator != end; ++iterator, index++) {
    value_t* val = *iterator;
    int outParamType;

    switch(val->type) {
      case VALUE_TYPE_NULL:
        stmt->setNull(index, oracle::occi::OCCISTRING);
        break;
      case VALUE_TYPE_STRING:
        stmt->setString(index, *((std::string*)val->value));
        break;
      case VALUE_TYPE_NUMBER:
        stmt->setNumber(index, *((oracle::occi::Number*)val->value));
        break;
      case VALUE_TYPE_DATE:
        stmt->setDate(index, *((oracle::occi::Date*)val->value));
        break;
      case VALUE_TYPE_OUTPUT:        
        outParamType = ((OutParam*)val->value)->type();
        switch(outParamType) {
          case OutParam::OCCIINT:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setInt(index, ((OutParam*)val->value)->_inOut.intVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIINT);
            }
            break;
          case OutParam::OCCISTRING:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setString(index, ((OutParam*)val->value)->_inOut.stringVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCISTRING, ((OutParam*)val->value)->size());
            }
            break;
          case OutParam::OCCIDOUBLE:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setDouble(index, ((OutParam*)val->value)->_inOut.doubleVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIDOUBLE);
            }
            break;
          case OutParam::OCCIFLOAT:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setFloat(index, ((OutParam*)val->value)->_inOut.floatVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIFLOAT);
            }
            break;
          case OutParam::OCCICURSOR:
            stmt->registerOutParam(index, oracle::occi::OCCICURSOR);
            break;
          case OutParam::OCCICLOB:
            stmt->registerOutParam(index, oracle::occi::OCCICLOB);
            break;
          case OutParam::OCCIDATE:
            stmt->registerOutParam(index, oracle::occi::OCCIDATE);
            break;
          case OutParam::OCCITIMESTAMP:
            stmt->registerOutParam(index, oracle::occi::OCCITIMESTAMP);
            break;
          case OutParam::OCCINUMBER:
            {
              if (((OutParam*)val->value)->_inOut.hasInParam) {
                stmt->setNumber(index, ((OutParam*)val->value)->_inOut.numberVal);
              } else {
                stmt->registerOutParam(index, oracle::occi::OCCINUMBER);
              }
            break;
            }
          case OutParam::OCCIBLOB:
            stmt->registerOutParam(index, oracle::occi::OCCIBLOB);
            break;
          default:
            throw NodeOracleException("SetValuesOnStatement: Unknown OutParam type: " + outParamType);
        }
        outputParam = index;
        break;
      default:
        throw NodeOracleException("SetValuesOnStatement: Unhandled value type");
    }
  }
  return outputParam;
}

void Connection::CreateColumnsFromResultSet(oracle::occi::ResultSet* rs, std::vector<column_t*> &columns) {
  std::vector<oracle::occi::MetaData> metadata = rs->getColumnListMetaData();
  for (std::vector<oracle::occi::MetaData>::iterator iterator = metadata.begin(), end = metadata.end(); iterator != end; ++iterator) {
    oracle::occi::MetaData metadata = *iterator;
    column_t* col = new column_t();
    col->name = metadata.getString(oracle::occi::MetaData::ATTR_NAME);
    int type = metadata.getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
    col->charForm = metadata.getInt(oracle::occi::MetaData::ATTR_CHARSET_FORM);
    switch(type) {
      case oracle::occi::OCCI_TYPECODE_NUMBER:
      case oracle::occi::OCCI_TYPECODE_FLOAT:
      case oracle::occi::OCCI_TYPECODE_DOUBLE:
      case oracle::occi::OCCI_TYPECODE_REAL:
      case oracle::occi::OCCI_TYPECODE_DECIMAL:
      case oracle::occi::OCCI_TYPECODE_INTEGER:
      case oracle::occi::OCCI_TYPECODE_SMALLINT:
        col->type = VALUE_TYPE_NUMBER;
        break;
      case oracle::occi::OCCI_TYPECODE_VARCHAR2:
      case oracle::occi::OCCI_TYPECODE_VARCHAR:
      case oracle::occi::OCCI_TYPECODE_CHAR:
        col->type = VALUE_TYPE_STRING;
        break;
      case oracle::occi::OCCI_TYPECODE_CLOB:
        col->type = VALUE_TYPE_CLOB;
        break;
      case oracle::occi::OCCI_TYPECODE_DATE:
        col->type = VALUE_TYPE_DATE;
        break;
      case OCI_TYPECODE_TIMESTAMP:
        col->type = VALUE_TYPE_TIMESTAMP;
        break;
      case oracle::occi::OCCI_TYPECODE_BLOB:
        col->type = VALUE_TYPE_BLOB;
        break;<commit_msg>Handle timestamp with timezone data in the result set. Done by using additional OCI_TYPECODE constants from oro.h.  See the occi programmer's guide ch. 5 on datatypes: http://docs.oracle.com/cd/B19306_01/appdev.102/b14294/types.htm#sthref434<commit_after>#include <string.h>
#include "connection.h"
#include "executeBaton.h"
#include "commitBaton.h"
#include "rollbackBaton.h"
#include "outParam.h"
#include "node_buffer.h"
#include <vector>
#include <node_version.h>
#include <iostream>
using namespace std;

Persistent<FunctionTemplate> Connection::constructorTemplate;

void Connection::Init(Handle<Object> target) {
  HandleScope scope;

  Local<FunctionTemplate> t = FunctionTemplate::New(New);
  constructorTemplate = Persistent<FunctionTemplate>::New(t);
  constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
  constructorTemplate->SetClassName(String::NewSymbol("Connection"));

  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "execute", Execute);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "close", Close);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "isConnected", IsConnected);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "setAutoCommit", SetAutoCommit);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "commit", Commit);
  NODE_SET_PROTOTYPE_METHOD(constructorTemplate, "rollback", Rollback);

  target->Set(String::NewSymbol("Connection"), constructorTemplate->GetFunction());
}

Handle<Value> Connection::New(const Arguments& args) {
  HandleScope scope;

  Connection *connection = new Connection();
  connection->Wrap(args.This());
  return args.This();
}

Connection::Connection() {
}

Connection::~Connection() {
  closeConnection();
}

Handle<Value> Connection::Execute(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_STRING_ARG(0, sql);
  REQ_ARRAY_ARG(1, values);
  REQ_FUN_ARG(2, callback);

  String::AsciiValue sqlVal(sql);

  ExecuteBaton* baton;
  try {
    baton = new ExecuteBaton(connection, *sqlVal, &values, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Execute, (uv_after_work_cb)EIO_AfterExecute);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::Close(const Arguments& args) {
  try {
	  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
	  connection->closeConnection();

	  return Undefined();
  } catch (const std::exception& ex) {
	  printf("Exception: %s\n", ex.what());
	  return Undefined();
  }
}

Handle<Value> Connection::IsConnected(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  if(connection && connection->m_connection) {
    return Boolean::New(true);
  } else {
    return Boolean::New(false);
  }
}

Handle<Value> Connection::Commit(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_FUN_ARG(0, callback);

  CommitBaton* baton;
  try {
    baton = new CommitBaton(connection, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Commit, (uv_after_work_cb)EIO_AfterCommit);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::Rollback(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());

  REQ_FUN_ARG(0, callback);

  RollbackBaton* baton;
  try {
    baton = new RollbackBaton(connection, &callback);
  } catch(NodeOracleException &ex) {
    Handle<Value> argv[2];
    argv[0] = Exception::Error(String::New(ex.getMessage().c_str()));
    argv[1] = Undefined();
    callback->Call(Context::GetCurrent()->Global(), 2, argv);
    return Undefined();
  }

  uv_work_t* req = new uv_work_t();
  req->data = baton;
  uv_queue_work(uv_default_loop(), req, EIO_Rollback, (uv_after_work_cb)EIO_AfterRollback);

  connection->Ref();

  return Undefined();
}

Handle<Value> Connection::SetAutoCommit(const Arguments& args) {
  Connection* connection = ObjectWrap::Unwrap<Connection>(args.This());
  REQ_BOOL_ARG(0, autoCommit);
  connection->m_autoCommit = autoCommit;
  return Undefined();
}

void Connection::closeConnection() {
  if(m_environment && m_connection) {
    m_environment->terminateConnection(m_connection);
    m_connection = NULL;
  }
}

void RandomBytesFree(char* data, void* hint) {
  delete[] data;
}

int Connection::SetValuesOnStatement(oracle::occi::Statement* stmt, std::vector<value_t*> &values) {
  uint32_t index = 1;
  int outputParam = -1;
  for (std::vector<value_t*>::iterator iterator = values.begin(), end = values.end(); iterator != end; ++iterator, index++) {
    value_t* val = *iterator;
    int outParamType;

    switch(val->type) {
      case VALUE_TYPE_NULL:
        stmt->setNull(index, oracle::occi::OCCISTRING);
        break;
      case VALUE_TYPE_STRING:
        stmt->setString(index, *((std::string*)val->value));
        break;
      case VALUE_TYPE_NUMBER:
        stmt->setNumber(index, *((oracle::occi::Number*)val->value));
        break;
      case VALUE_TYPE_DATE:
        stmt->setDate(index, *((oracle::occi::Date*)val->value));
        break;
      case VALUE_TYPE_OUTPUT:        
        outParamType = ((OutParam*)val->value)->type();
        switch(outParamType) {
          case OutParam::OCCIINT:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setInt(index, ((OutParam*)val->value)->_inOut.intVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIINT);
            }
            break;
          case OutParam::OCCISTRING:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setString(index, ((OutParam*)val->value)->_inOut.stringVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCISTRING, ((OutParam*)val->value)->size());
            }
            break;
          case OutParam::OCCIDOUBLE:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setDouble(index, ((OutParam*)val->value)->_inOut.doubleVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIDOUBLE);
            }
            break;
          case OutParam::OCCIFLOAT:
            if (((OutParam*)val->value)->_inOut.hasInParam) {
              stmt->setFloat(index, ((OutParam*)val->value)->_inOut.floatVal);
            }else {
              stmt->registerOutParam(index, oracle::occi::OCCIFLOAT);
            }
            break;
          case OutParam::OCCICURSOR:
            stmt->registerOutParam(index, oracle::occi::OCCICURSOR);
            break;
          case OutParam::OCCICLOB:
            stmt->registerOutParam(index, oracle::occi::OCCICLOB);
            break;
          case OutParam::OCCIDATE:
            stmt->registerOutParam(index, oracle::occi::OCCIDATE);
            break;
          case OutParam::OCCITIMESTAMP:
            stmt->registerOutParam(index, oracle::occi::OCCITIMESTAMP);
            break;
          case OutParam::OCCINUMBER:
            {
              if (((OutParam*)val->value)->_inOut.hasInParam) {
                stmt->setNumber(index, ((OutParam*)val->value)->_inOut.numberVal);
              } else {
                stmt->registerOutParam(index, oracle::occi::OCCINUMBER);
              }
            break;
            }
          case OutParam::OCCIBLOB:
            stmt->registerOutParam(index, oracle::occi::OCCIBLOB);
            break;
          default:
            throw NodeOracleException("SetValuesOnStatement: Unknown OutParam type: " + outParamType);
        }
        outputParam = index;
        break;
      default:
        throw NodeOracleException("SetValuesOnStatement: Unhandled value type");
    }
  }
  return outputParam;
}

void Connection::CreateColumnsFromResultSet(oracle::occi::ResultSet* rs, std::vector<column_t*> &columns) {
  std::vector<oracle::occi::MetaData> metadata = rs->getColumnListMetaData();
  for (std::vector<oracle::occi::MetaData>::iterator iterator = metadata.begin(), end = metadata.end(); iterator != end; ++iterator) {
    oracle::occi::MetaData metadata = *iterator;
    column_t* col = new column_t();
    col->name = metadata.getString(oracle::occi::MetaData::ATTR_NAME);
    int type = metadata.getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
    col->charForm = metadata.getInt(oracle::occi::MetaData::ATTR_CHARSET_FORM);
    switch(type) {
      case oracle::occi::OCCI_TYPECODE_NUMBER:
      case oracle::occi::OCCI_TYPECODE_FLOAT:
      case oracle::occi::OCCI_TYPECODE_DOUBLE:
      case oracle::occi::OCCI_TYPECODE_REAL:
      case oracle::occi::OCCI_TYPECODE_DECIMAL:
      case oracle::occi::OCCI_TYPECODE_INTEGER:
      case oracle::occi::OCCI_TYPECODE_SMALLINT:
        col->type = VALUE_TYPE_NUMBER;
        break;
      case oracle::occi::OCCI_TYPECODE_VARCHAR2:
      case oracle::occi::OCCI_TYPECODE_VARCHAR:
      case oracle::occi::OCCI_TYPECODE_CHAR:
        col->type = VALUE_TYPE_STRING;
        break;
      case oracle::occi::OCCI_TYPECODE_CLOB:
        col->type = VALUE_TYPE_CLOB;
        break;
      case oracle::occi::OCCI_TYPECODE_DATE:
        col->type = VALUE_TYPE_DATE;
        break;
      //Use OCI_TYPECODE from oro.h because occiCommon.h does not re-export these in the TypeCode enum
      case OCI_TYPECODE_TIMESTAMP:
      case OCI_TYPECODE_TIMESTAMP_TZ: //Timezone
      case OCI_TYPECODE_TIMESTAMP_LTZ: //Local Timezone
        col->type = VALUE_TYPE_TIMESTAMP;
        break;
      case oracle::occi::OCCI_TYPECODE_BLOB:
        col->type = VALUE_TYPE_BLOB;
        break;
{'fraction_non_alphanumeric': 0.1426985568674942, 'fraction_numerical': 0.0033807649611464325, 'mean_word_length': 3.7064830206601758, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 217, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5032763', 'n_tokens_mistral': 6460, 'n_tokens_neox': 6263, 'n_words': 1030}