From 5e0bafdee272b5e1a60d0d69278d045e9dbf804e Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Thu, 31 Oct 2019 13:30:59 +0100 Subject: [PATCH 1/5] Add node v12 compatibility --- package-lock.json | 18 +++ package.json | 4 +- src/h/sqlany_utils.h | 2 +- src/sqlanywhere.cpp | 16 +-- src/utils.cpp | 256 ++++++++++++++++++++++--------------------- 5 files changed, 163 insertions(+), 133 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7b6a8e3 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,18 @@ +{ + "name": "sqlanywhere", + "version": "1.0.24", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "help": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/help/-/help-3.0.2.tgz", + "integrity": "sha1-luGQ1KCkU7icLLSwWrOOOo+f2t0=" + }, + "nan": { + "version": "2.14.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz", + "integrity": "sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==" + } + } +} diff --git a/package.json b/package.json index 088d5d2..faf4836 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "SQL ANYWHERE", "name": "sqlanywhere", "description": "SQL Anywhere JavaScript Driver.", - "version": "1.0.24", + "version": "2.0.0", "repository": { "url": "https://github.com/sqlanywhere/node-sqlanywhere" }, @@ -15,6 +15,6 @@ }, "dependencies": { "help": "^3.0.2", - "nan": "2.11.1" + "nan": "^2.14.0" } } diff --git a/src/h/sqlany_utils.h b/src/h/sqlany_utils.h index 928b1cc..908b5d2 100644 --- a/src/h/sqlany_utils.h +++ b/src/h/sqlany_utils.h @@ -157,7 +157,7 @@ void callBack( std::string * str, #endif bool getBindParameters( std::vector &execData - , Handle arg + , Local arg , std::vector ¶ms , unsigned &num_rows ); diff --git a/src/sqlanywhere.cpp b/src/sqlanywhere.cpp index df1018a..10416e7 100644 --- a/src/sqlanywhere.cpp +++ b/src/sqlanywhere.cpp @@ -3,6 +3,7 @@ // *************************************************************************** #include "nodever_cover.h" #include "sqlany_utils.h" +#include "nan.h" #if !v010 @@ -454,7 +455,7 @@ NODE_API_FUNC( Connection::exec ) return; } - String::Utf8Value param0( args[0]->ToString() ); + Nan::Utf8String param0( args[0]->ToString(isolate) ); executeBaton *baton = new executeBaton(); baton->obj = obj; @@ -641,7 +642,7 @@ NODE_API_FUNC( Connection::prepare ) return; } - String::Utf8Value param0( args[0]->ToString() ); + Nan::Utf8String param0( args[0]->ToString(isolate) ); prepareBaton *baton = new prepareBaton(); baton->obj = obj; @@ -797,6 +798,7 @@ NODE_API_FUNC( Connection::connect ) /**********************************/ { Isolate *isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); HandleScope scope( isolate ); int num_args = args.Length(); Connection *obj; @@ -856,26 +858,26 @@ NODE_API_FUNC( Connection::connect ) baton->sqlca_connection = sqlca_connection; if( sqlca_connection ) { - baton->sqlca = (void *)(long)args[0]->NumberValue(); + baton->sqlca = (void *)(long)args[0]->NumberValue(context).ToChecked(); } else { Local localArg = Local::New( isolate, obj->_arg ); if( localArg->Length() > 0 ) { - String::Utf8Value param0( localArg ); + Nan::Utf8String param0( localArg ); baton->conn_string = std::string(*param0); } else { baton->conn_string = std::string(); } if( arg_is_string ) { - String::Utf8Value param0( args[0]->ToString() ); + Nan::Utf8String param0( args[0]->ToString(isolate) ); baton->conn_string.append( ";" ); baton->conn_string.append(*param0); } else if( arg_is_object ) { Persistent arg_string; - HashToString( args[0]->ToObject(), arg_string ); + HashToString( args[0]->ToObject(context).ToLocalChecked(), arg_string ); Local local_arg_string = Local::New( isolate, arg_string ); - String::Utf8Value param0( local_arg_string ); + Nan::Utf8String param0( local_arg_string ); baton->conn_string.append( ";" ); baton->conn_string.append(*param0); arg_string.Reset(); diff --git a/src/utils.cpp b/src/utils.cpp index 319ddd6..616a012 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -82,6 +82,7 @@ void throwError( int code ) { Isolate *isolate = Isolate::GetCurrent(); std::string message; + getErrorMsg( code, message ); isolate->ThrowException( Exception::Error( String::NewFromUtf8( isolate, message.c_str() ) ) ); @@ -194,126 +195,131 @@ void callBack( std::string * str, } static bool getWideBindParameters( std::vector &execData, - Handle arg, + Local arg, std::vector & params, unsigned &num_rows ) /*********************************************************************************/ { - Handle rows = Handle::Cast( arg ); - num_rows = rows->Length(); - - Handle row0 = Handle::Cast( rows->Get(0) ); - unsigned num_cols = row0->Length(); - unsigned c; - - if( num_cols == 0 ) { - // if an empty array was passed in, we still need ExecuteData - ExecuteData *ex = new ExecuteData; - execData.push_back( ex ); - return true; - } - - // Make sure that each array in the list has the same number and types - // of values - for( unsigned int r = 1; r < num_rows; r++ ) { - Handle row = Handle::Cast( rows->Get(r) ); - for( c = 0; c < num_cols; c++ ) { - Handle val0 = row0->Get(c); - Handle val = row->Get(c); - - if( ( val0->IsInt32() || val0->IsNumber() ) && - ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { - return false; - } - if( val0->IsString() && - !val->IsString() && !val->IsNull() ) { - return false; - } - if( Buffer::HasInstance( val0 ) && - !Buffer::HasInstance( val ) && !val->IsNull() ) { - return false; - } - } - } - - for( c = 0; c < num_cols; c++ ) { - a_sqlany_bind_param param; - memset( ¶m, 0, sizeof( param ) ); - - ExecuteData *ex = new ExecuteData; - execData.push_back( ex ); - - double * param_double = new double[num_rows]; - ex->addNum( param_double ); - char ** char_arr = new char *[num_rows]; - size_t * len = new size_t[num_rows]; - ex->addStrings( char_arr, len ); - sacapi_bool * is_null = new sacapi_bool[num_rows]; - ex->addNull( is_null ); - param.value.is_null = is_null; - param.value.is_address = false; - - if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { - param.value.type = A_DOUBLE; - param.value.buffer = (char *)( param_double ); - - } else if( row0->Get(c)->IsString() ) { - param.value.type = A_STRING; - param.value.buffer = (char *)char_arr; - param.value.length = len; - param.value.is_address = true; - - } else if( Buffer::HasInstance( row0->Get(c) ) ) { - param.value.type = A_BINARY; - param.value.buffer = (char *)char_arr; - param.value.length = len; - param.value.is_address = true; - - } else if( row0->Get(c)->IsNull() ) { - - } else{ - return false; - } - - for( unsigned int r = 0; r < num_rows; r++ ) { - Handle bind_params = Handle::Cast( rows->Get(r) ); - - is_null[r] = false; - if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { - param_double[r] = bind_params->Get(c)->NumberValue(); - - } else if( bind_params->Get(c)->IsString() ) { - String::Utf8Value paramValue( bind_params->Get(c)->ToString() ); - const char* param_string = (*paramValue); - len[r] = (size_t)paramValue.length(); - char *param_char = new char[len[r] + 1]; - char_arr[r] = param_char; - memcpy( param_char, param_string, len[r] + 1 ); - - } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { - len[r] = Buffer::Length( bind_params->Get(c) ); - char *param_char = new char[len[r]]; - char_arr[r] = param_char; - memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); - - } else if( bind_params->Get(c)->IsNull() ) { - is_null[r] = true; - } - } - - params.push_back( param ); - } + //Isolate* isolate = Isolate::GetCurrent(); + //Local context = isolate->GetCurrentContext(); + //Local rows = Local::Cast( arg ); + // num_rows = rows->Length(); + + //Local row0 = Local::Cast( rows->Get(0) ); + // unsigned num_cols = row0->Length(); + // unsigned c; + + // if( num_cols == 0 ) { + //// if an empty array was passed in, we still need ExecuteData + //ExecuteData *ex = new ExecuteData; + //execData.push_back( ex ); + //return true; + // } + + // // Make sure that each array in the list has the same number and types + // // of values + // for( unsigned int r = 1; r < num_rows; r++ ) { + //Local row = Local::Cast( rows->Get(r) ); + //for( c = 0; c < num_cols; c++ ) { + // Local val0 = row0->Get(c); + // Local val = row->Get(c); + + // if( ( val0->IsInt32() || val0->IsNumber() ) && + // ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { + // return false; + // } + // if( val0->IsString() && + // !val->IsString() && !val->IsNull() ) { + // return false; + // } + // if( Buffer::HasInstance( val0 ) && + // !Buffer::HasInstance( val ) && !val->IsNull() ) { + // return false; + // } + //} + // } + + // for( c = 0; c < num_cols; c++ ) { + //a_sqlany_bind_param param; + //memset( ¶m, 0, sizeof( param ) ); + + //ExecuteData *ex = new ExecuteData; + //execData.push_back( ex ); + + //double * param_double = new double[num_rows]; + //ex->addNum( param_double ); + //char ** char_arr = new char *[num_rows]; + //size_t * len = new size_t[num_rows]; + //ex->addStrings( char_arr, len ); + //sacapi_bool * is_null = new sacapi_bool[num_rows]; + //ex->addNull( is_null ); + //param.value.is_null = is_null; + //param.value.is_address = false; + + //if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { + // param.value.type = A_DOUBLE; + // param.value.buffer = (char *)( param_double ); + + //} else if( row0->Get(c)->IsString() ) { + // param.value.type = A_STRING; + // param.value.buffer = (char *)char_arr; + // param.value.length = len; + // param.value.is_address = true; + + //} else if( Buffer::HasInstance( row0->Get(c) ) ) { + // param.value.type = A_BINARY; + // param.value.buffer = (char *)char_arr; + // param.value.length = len; + // param.value.is_address = true; + // + //} else if( row0->Get(c)->IsNull() ) { + + //} else{ + // return false; + //} + + //for( unsigned int r = 0; r < num_rows; r++ ) { + // Local bind_params = Local::Cast( rows->Get(r) ); + // + // is_null[r] = false; + // if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { + // param_double[r] = bind_params->Get(c)->NumberValue(context).ToChecked(); + // + // } else if( bind_params->Get(c)->IsString() ) { + // Nan::Utf8String paramValue( bind_params->Get(c)->ToString(isolate) ); + // const char* param_string = (*paramValue); + // len[r] = (size_t)paramValue.length(); + // char *param_char = new char[len[r] + 1]; + // char_arr[r] = param_char; + // memcpy( param_char, param_string, len[r] + 1 ); + // + // } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { + // len[r] = Buffer::Length( bind_params->Get(c) ); + // char *param_char = new char[len[r]]; + // char_arr[r] = param_char; + // memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); + + // } else if( bind_params->Get(c)->IsNull() ) { + // is_null[r] = true; + // } + //} + // + //params.push_back( param ); + // } return true; } bool getBindParameters( std::vector &execData, - Handle arg, + Local arg, std::vector & params, unsigned &num_rows ) /*************************************************************************/ { - Handle bind_params = Handle::Cast( arg ); + + Isolate* isolate = Isolate::GetCurrent(); + Local context = isolate->GetCurrentContext(); + Local bind_params = Local::Cast( arg ); if( bind_params->Length() == 0 ) { // if an empty array was passed in, we still need ExecuteData @@ -336,20 +342,20 @@ bool getBindParameters( std::vector &execData, if( bind_params->Get(i)->IsInt32() ) { int *param_int = new int; - *param_int = bind_params->Get(i)->Int32Value(); + *param_int = bind_params->Get(i)->Int32Value(context).ToChecked(); ex->addInt( param_int ); param.value.buffer = (char *)( param_int ); param.value.type = A_VAL32; } else if( bind_params->Get(i)->IsNumber() ) { double *param_double = new double; - *param_double = bind_params->Get(i)->NumberValue(); // Remove Round off Error + *param_double = bind_params->Get(i)->NumberValue(context).ToChecked(); // Remove Round off Error ex->addNum( param_double ); param.value.buffer = (char *)( param_double ); param.value.type = A_DOUBLE; } else if( bind_params->Get(i)->IsString() ) { - String::Utf8Value paramValue( bind_params->Get(i)->ToString() ); + Nan::Utf8String paramValue( bind_params->Get(i)->ToString(context).ToLocalChecked() ); const char* param_string = (*paramValue); size_t *len = new size_t; *len = (size_t)paramValue.length(); @@ -755,6 +761,7 @@ void StmtObject::Init( Isolate *isolate ) /***************************************/ { HandleScope scope(isolate); + Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); tpl->SetClassName( String::NewFromUtf8( isolate, "StmtObject" ) ); @@ -764,7 +771,7 @@ void StmtObject::Init( Isolate *isolate ) NODE_SET_PROTOTYPE_METHOD( tpl, "exec", exec ); NODE_SET_PROTOTYPE_METHOD( tpl, "drop", drop ); NODE_SET_PROTOTYPE_METHOD( tpl, "getMoreResults", getMoreResults ); - constructor.Reset( isolate, tpl->GetFunction() ); + constructor.Reset( isolate, tpl->GetFunction(context).ToLocalChecked() ); } void StmtObject::New( const FunctionCallbackInfo &args ) @@ -791,7 +798,7 @@ void StmtObject::CreateNewInstance( const FunctionCallbackInfo & args, Isolate *isolate = args.GetIsolate(); HandleScope scope(isolate); const unsigned argc = 1; - Handle argv[argc] = { args[0] }; + Local argv[argc] = { args[0] }; Localcons = Local::New( isolate, constructor ); #if NODE_MAJOR_VERSION >= 10 Local env = isolate->GetCurrentContext(); @@ -829,15 +836,16 @@ void HashToString( Local obj, Persistent &ret ) { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope(isolate); - Local props = obj->GetOwnPropertyNames(); - int length = props->Length(); + Local context = isolate->GetCurrentContext(); + MaybeLocal props = obj->GetOwnPropertyNames(context); + int length = props.ToLocalChecked()->Length(); std::string params = ""; bool first = true; for( int i = 0; i < length; i++ ) { - Local key = props->Get(i).As(); + Local key = props.ToLocalChecked()->Get(i).As(); Local val = obj->Get(key).As(); - String::Utf8Value key_utf8( key ); - String::Utf8Value val_utf8( val ); + String::Utf8Value key_utf8( isolate, key ); + String::Utf8Value val_utf8( isolate, val ); if( !first ) { params += ";"; } @@ -899,6 +907,7 @@ Connection::Connection( const FunctionCallbackInfo &args ) /***************************************************************/ { Isolate *isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); HandleScope scope( isolate ); uv_mutex_init(&conn_mutex); conn = NULL; @@ -911,14 +920,14 @@ Connection::Connection( const FunctionCallbackInfo &args ) if( args.Length() == 1 ) { //CheckArgType( args[0] ); if( args[0]->IsString() ) { - Local str = args[0]->ToString(); - int string_len = str->Utf8Length(); + MaybeLocal str = args[0]->ToString(context); + int string_len = str.ToLocalChecked()->Utf8Length(isolate); char *buf = new char[string_len+1]; - str->WriteUtf8( buf ); + str.ToLocalChecked()->WriteUtf8(isolate, buf ); _arg.Reset( isolate, String::NewFromUtf8( isolate, buf ) ); delete [] buf; } else if( args[0]->IsObject() ) { - HashToString( args[0]->ToObject(), _arg ); + HashToString(args[0]->ToObject(context).ToLocalChecked(), _arg); } else if( !args[0]->IsUndefined() && !args[0]->IsNull() ) { throwError( JS_ERR_INVALID_ARGUMENTS ); } else { @@ -977,6 +986,7 @@ void Connection::Init( Isolate *isolate ) /***************************************/ { HandleScope scope( isolate ); + Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); tpl->SetClassName( String::NewFromUtf8( isolate, "Connection" ) ); @@ -992,7 +1002,7 @@ void Connection::Init( Isolate *isolate ) NODE_SET_PROTOTYPE_METHOD( tpl, "rollback", rollback ); NODE_SET_PROTOTYPE_METHOD( tpl, "connected", connected ); - constructor.Reset( isolate, tpl->GetFunction() ); + constructor.Reset( isolate, tpl->GetFunction(context).ToLocalChecked() ); } void Connection::New( const FunctionCallbackInfo &args ) @@ -1026,7 +1036,7 @@ void Connection::NewInstance( const FunctionCallbackInfo &args ) Isolate *isolate = args.GetIsolate(); HandleScope scope( isolate ); const unsigned argc = 1; - Handle argv[argc] = { args[0] }; + Local argv[argc] = { args[0] }; Local cons = Local::New( isolate, constructor ); #if NODE_MAJOR_VERSION >= 10 Local env = isolate->GetCurrentContext(); From 3b4e0d18a336d17166782ba6f3c9a53a1dea21ad Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Thu, 31 Oct 2019 13:31:33 +0100 Subject: [PATCH 2/5] Add gitignore file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7dab5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +build \ No newline at end of file From a2089b6ca246e46361214f302f15c737065b83db Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Thu, 31 Oct 2019 13:47:58 +0100 Subject: [PATCH 3/5] Uncomment wrongly commented code --- src/utils.cpp | 212 +++++++++++++++++++++++++------------------------- 1 file changed, 106 insertions(+), 106 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index 616a012..b378f56 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -200,112 +200,112 @@ static bool getWideBindParameters( std::vector &execData, unsigned &num_rows ) /*********************************************************************************/ { - //Isolate* isolate = Isolate::GetCurrent(); - //Local context = isolate->GetCurrentContext(); - //Local rows = Local::Cast( arg ); - // num_rows = rows->Length(); - - //Local row0 = Local::Cast( rows->Get(0) ); - // unsigned num_cols = row0->Length(); - // unsigned c; - - // if( num_cols == 0 ) { - //// if an empty array was passed in, we still need ExecuteData - //ExecuteData *ex = new ExecuteData; - //execData.push_back( ex ); - //return true; - // } - - // // Make sure that each array in the list has the same number and types - // // of values - // for( unsigned int r = 1; r < num_rows; r++ ) { - //Local row = Local::Cast( rows->Get(r) ); - //for( c = 0; c < num_cols; c++ ) { - // Local val0 = row0->Get(c); - // Local val = row->Get(c); - - // if( ( val0->IsInt32() || val0->IsNumber() ) && - // ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { - // return false; - // } - // if( val0->IsString() && - // !val->IsString() && !val->IsNull() ) { - // return false; - // } - // if( Buffer::HasInstance( val0 ) && - // !Buffer::HasInstance( val ) && !val->IsNull() ) { - // return false; - // } - //} - // } - - // for( c = 0; c < num_cols; c++ ) { - //a_sqlany_bind_param param; - //memset( ¶m, 0, sizeof( param ) ); - - //ExecuteData *ex = new ExecuteData; - //execData.push_back( ex ); - - //double * param_double = new double[num_rows]; - //ex->addNum( param_double ); - //char ** char_arr = new char *[num_rows]; - //size_t * len = new size_t[num_rows]; - //ex->addStrings( char_arr, len ); - //sacapi_bool * is_null = new sacapi_bool[num_rows]; - //ex->addNull( is_null ); - //param.value.is_null = is_null; - //param.value.is_address = false; - - //if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { - // param.value.type = A_DOUBLE; - // param.value.buffer = (char *)( param_double ); - - //} else if( row0->Get(c)->IsString() ) { - // param.value.type = A_STRING; - // param.value.buffer = (char *)char_arr; - // param.value.length = len; - // param.value.is_address = true; - - //} else if( Buffer::HasInstance( row0->Get(c) ) ) { - // param.value.type = A_BINARY; - // param.value.buffer = (char *)char_arr; - // param.value.length = len; - // param.value.is_address = true; - // - //} else if( row0->Get(c)->IsNull() ) { - - //} else{ - // return false; - //} - - //for( unsigned int r = 0; r < num_rows; r++ ) { - // Local bind_params = Local::Cast( rows->Get(r) ); - // - // is_null[r] = false; - // if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { - // param_double[r] = bind_params->Get(c)->NumberValue(context).ToChecked(); - // - // } else if( bind_params->Get(c)->IsString() ) { - // Nan::Utf8String paramValue( bind_params->Get(c)->ToString(isolate) ); - // const char* param_string = (*paramValue); - // len[r] = (size_t)paramValue.length(); - // char *param_char = new char[len[r] + 1]; - // char_arr[r] = param_char; - // memcpy( param_char, param_string, len[r] + 1 ); - // - // } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { - // len[r] = Buffer::Length( bind_params->Get(c) ); - // char *param_char = new char[len[r]]; - // char_arr[r] = param_char; - // memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); - - // } else if( bind_params->Get(c)->IsNull() ) { - // is_null[r] = true; - // } - //} - // - //params.push_back( param ); - // } + Isolate* isolate = Isolate::GetCurrent(); + Local context = isolate->GetCurrentContext(); + Local rows = Local::Cast( arg ); + num_rows = rows->Length(); + + Local row0 = Local::Cast( rows->Get(0) ); + unsigned num_cols = row0->Length(); + unsigned c; + + if( num_cols == 0 ) { + // if an empty array was passed in, we still need ExecuteData + ExecuteData *ex = new ExecuteData; + execData.push_back( ex ); + return true; + } + + // Make sure that each array in the list has the same number and types + // of values + for( unsigned int r = 1; r < num_rows; r++ ) { + Local row = Local::Cast( rows->Get(r) ); + for( c = 0; c < num_cols; c++ ) { + Local val0 = row0->Get(c); + Local val = row->Get(c); + + if( ( val0->IsInt32() || val0->IsNumber() ) && + ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { + return false; + } + if( val0->IsString() && + !val->IsString() && !val->IsNull() ) { + return false; + } + if( Buffer::HasInstance( val0 ) && + !Buffer::HasInstance( val ) && !val->IsNull() ) { + return false; + } + } + } + + for( c = 0; c < num_cols; c++ ) { + a_sqlany_bind_param param; + memset( ¶m, 0, sizeof( param ) ); + + ExecuteData *ex = new ExecuteData; + execData.push_back( ex ); + + double * param_double = new double[num_rows]; + ex->addNum( param_double ); + char ** char_arr = new char *[num_rows]; + size_t * len = new size_t[num_rows]; + ex->addStrings( char_arr, len ); + sacapi_bool * is_null = new sacapi_bool[num_rows]; + ex->addNull( is_null ); + param.value.is_null = is_null; + param.value.is_address = false; + + if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { + param.value.type = A_DOUBLE; + param.value.buffer = (char *)( param_double ); + + } else if( row0->Get(c)->IsString() ) { + param.value.type = A_STRING; + param.value.buffer = (char *)char_arr; + param.value.length = len; + param.value.is_address = true; + + } else if( Buffer::HasInstance( row0->Get(c) ) ) { + param.value.type = A_BINARY; + param.value.buffer = (char *)char_arr; + param.value.length = len; + param.value.is_address = true; + + } else if( row0->Get(c)->IsNull() ) { + + } else{ + return false; + } + + for( unsigned int r = 0; r < num_rows; r++ ) { + Local bind_params = Local::Cast( rows->Get(r) ); + + is_null[r] = false; + if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { + param_double[r] = bind_params->Get(c)->NumberValue(context).ToChecked(); + + } else if( bind_params->Get(c)->IsString() ) { + Nan::Utf8String paramValue( bind_params->Get(c)->ToString(isolate) ); + const char* param_string = (*paramValue); + len[r] = (size_t)paramValue.length(); + char *param_char = new char[len[r] + 1]; + char_arr[r] = param_char; + memcpy( param_char, param_string, len[r] + 1 ); + + } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { + len[r] = Buffer::Length( bind_params->Get(c) ); + char *param_char = new char[len[r]]; + char_arr[r] = param_char; + memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); + + } else if( bind_params->Get(c)->IsNull() ) { + is_null[r] = true; + } + } + + params.push_back( param ); + } return true; } From d47cd258affb4436da2989e39b0e0b2571f621c5 Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Wed, 22 Jul 2020 12:58:11 +0200 Subject: [PATCH 4/5] Updated to work with nodev14 --- package.json | 2 +- src/sqlanywhere.cpp | 300 ++++++++++++++++++++++---------------------- src/utils.cpp | 247 ++++++++++++++++++------------------ 3 files changed, 276 insertions(+), 273 deletions(-) diff --git a/package.json b/package.json index faf4836..04ac017 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "SQL ANYWHERE", "name": "sqlanywhere", "description": "SQL Anywhere JavaScript Driver.", - "version": "2.0.0", + "version": "2.0.1", "repository": { "url": "https://github.com/sqlanywhere/node-sqlanywhere" }, diff --git a/src/sqlanywhere.cpp b/src/sqlanywhere.cpp index 10416e7..ea4be30 100644 --- a/src/sqlanywhere.cpp +++ b/src/sqlanywhere.cpp @@ -27,7 +27,7 @@ struct executeBaton { std::string stmt; std::vector execData; std::vector params; - + std::vector colNames; int rows_affected; std::vector col_types; @@ -94,7 +94,7 @@ void executeWork( uv_work_t *req ) getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); return; } - + a_sqlany_stmt *sqlany_stmt = NULL; if( baton->stmt_obj == NULL ) { baton->stmt_obj = new StmtObject(); @@ -113,13 +113,13 @@ void executeWork( uv_work_t *req ) return; } baton->stmt_obj->sqlany_stmt = sqlany_stmt; - + } else if( sqlany_stmt == NULL ) { baton->err = true; getErrorMsg( JS_ERR_INVALID_OBJECT, baton->error_msg ); return; } - + if( !api.sqlany_reset( sqlany_stmt ) ) { baton->err = true; getErrorMsg( baton->obj->conn, baton->error_msg ); @@ -128,7 +128,7 @@ void executeWork( uv_work_t *req ) for( unsigned int i = 0; i < baton->params.size(); i++ ) { a_sqlany_bind_param param; - + if( !api.sqlany_describe_bind_param( sqlany_stmt, i, ¶m ) ) { baton->err = true; getErrorMsg( baton->obj->conn, baton->error_msg ); @@ -147,7 +147,7 @@ void executeWork( uv_work_t *req ) if( baton->num_rows > 1 ) { api.sqlany_set_batch_size( sqlany_stmt, baton->num_rows ); } - + sacapi_bool success_execute = api.sqlany_execute( sqlany_stmt ); baton->execData[0]->clear(); @@ -156,7 +156,7 @@ void executeWork( uv_work_t *req ) getErrorMsg( baton->obj->conn, baton->error_msg ); return; } - + if( !fetchResultSet( sqlany_stmt, baton->rows_affected, baton->colNames, baton->execData[0], baton->col_types ) ) { baton->err = true; @@ -189,9 +189,9 @@ NODE_API_FUNC( StmtObject::exec ) bool callback_required = false, bind_required = false; int cbfunc_arg = -1; Local undef = Local::New( isolate, Undefined( isolate ) ); - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsArray() ) { bind_required = true; @@ -209,7 +209,7 @@ NODE_API_FUNC( StmtObject::exec ) args.GetReturnValue().SetUndefined(); return; } - + if( obj == NULL || obj->connection == NULL || obj->connection->conn == NULL || obj->sqlany_stmt == NULL ) { std::string error_msg; @@ -218,7 +218,7 @@ NODE_API_FUNC( StmtObject::exec ) args.GetReturnValue().SetUndefined(); return; } - + executeBaton *baton = new executeBaton(); baton->obj = obj->connection; baton->stmt_obj = obj; @@ -252,32 +252,32 @@ NODE_API_FUNC( StmtObject::exec ) uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; status = uv_queue_work( uv_default_loop(), req, executeWork, (uv_after_work_cb)executeAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + Persistent ResultSet; - + executeWork( req ); bool success = fillResult( baton, ResultSet ); delete baton; delete req; - + if( !success ) { args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( ResultSet ); + args.GetReturnValue().Set( ResultSet.Get(isolate) ); ResultSet.Reset(); } @@ -347,9 +347,9 @@ NODE_API_FUNC( StmtObject::getMoreResults ) bool callback_required = false; int cbfunc_arg = -1; Local undef = Local::New( isolate, Undefined( isolate ) ); - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsFunction() ) { callback_required = true; cbfunc_arg = 0; @@ -359,7 +359,7 @@ NODE_API_FUNC( StmtObject::getMoreResults ) args.GetReturnValue().SetUndefined(); return; } - + if( obj == NULL || obj->connection == NULL || obj->connection->conn == NULL || obj->sqlany_stmt == NULL ) { std::string error_msg; @@ -380,32 +380,32 @@ NODE_API_FUNC( StmtObject::getMoreResults ) uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; status = uv_queue_work( uv_default_loop(), req, getMoreResultsWork, (uv_after_work_cb)getMoreResultsAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + Persistent ResultSet; - + getMoreResultsWork( req ); bool success = fillResult( baton, ResultSet ); delete baton; delete req; - + if( !success ) { args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( ResultSet ); + args.GetReturnValue().Set( ResultSet.Get(isolate)); ResultSet.Reset(); } @@ -424,16 +424,16 @@ NODE_API_FUNC( Connection::exec ) } else if( num_args == 2 && args[1]->IsArray() ) { bind_required = true; - + } else if( num_args == 2 && args[1]->IsFunction() ) { callback_required = true; cbfunc_arg = 1; - + } else if( num_args == 3 && args[1]->IsArray() && args[2]->IsFunction() ) { callback_required = true; bind_required = true; cbfunc_arg = 2; - + } else { throwError( JS_ERR_INVALID_ARGUMENTS ); args.GetReturnValue().SetUndefined(); @@ -446,7 +446,7 @@ NODE_API_FUNC( Connection::exec ) } Connection *obj = ObjectWrap::Unwrap( args.This() ); - + if( obj == NULL || obj->conn == NULL ) { std::string error_msg; getErrorMsg( JS_ERR_INVALID_OBJECT, error_msg ); @@ -454,9 +454,9 @@ NODE_API_FUNC( Connection::exec ) args.GetReturnValue().SetUndefined(); return; } - - Nan::Utf8String param0( args[0]->ToString(isolate) ); - + + Nan::Utf8String param0( args[0] ); + executeBaton *baton = new executeBaton(); baton->obj = obj; baton->callback_required = callback_required; @@ -488,10 +488,10 @@ NODE_API_FUNC( Connection::exec ) baton->execData.push_back( new ExecuteData ); baton->num_rows = 1; } - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); @@ -499,11 +499,11 @@ NODE_API_FUNC( Connection::exec ) status = uv_queue_work( uv_default_loop(), req, executeWork, (uv_after_work_cb)executeAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + Persistent ResultSet; executeWork( req ); @@ -511,7 +511,7 @@ NODE_API_FUNC( Connection::exec ) delete baton; delete req; - + if( !success ) { args.GetReturnValue().SetUndefined(); return; @@ -526,17 +526,17 @@ struct prepareBaton { bool err; std::string error_msg; bool callback_required; - + StmtObject *obj; std::string stmt; Persistent StmtObj; - + prepareBaton() { err = false; callback_required = false; obj = NULL; } - + ~prepareBaton() { obj = NULL; callback.Reset(); @@ -544,22 +544,22 @@ struct prepareBaton { } }; -void Connection::prepareWork( uv_work_t *req ) +void Connection::prepareWork( uv_work_t *req ) /*********************************************/ { prepareBaton *baton = static_cast(req->data); - if( baton->obj == NULL || baton->obj->connection == NULL || + if( baton->obj == NULL || baton->obj->connection == NULL || baton->obj->connection->conn == NULL ) { baton->err = true; getErrorMsg( JS_ERR_INVALID_OBJECT, baton->error_msg ); return; } - + scoped_lock lock( baton->obj->connection->conn_mutex ); baton->obj->sqlany_stmt = api.sqlany_prepare( baton->obj->connection->conn, baton->stmt.c_str() ); - + if( baton->obj->sqlany_stmt == NULL ) { baton->err = true; getErrorMsg( baton->obj->connection->conn, baton->error_msg ); @@ -567,14 +567,14 @@ void Connection::prepareWork( uv_work_t *req ) } } -void Connection::prepareAfter( uv_work_t *req ) +void Connection::prepareAfter( uv_work_t *req ) /**********************************************/ { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope( isolate ); prepareBaton *baton = static_cast(req->data); Local undef = Local::New( isolate, Undefined( isolate ) ); - + if( baton->err ) { callBack( &( baton->error_msg ), baton->callback, undef, baton->callback_required ); @@ -582,13 +582,13 @@ void Connection::prepareAfter( uv_work_t *req ) delete req; return; } - + if( baton->callback_required ) { Local StmtObj = Local::New( isolate, baton->StmtObj ); callBack( NULL, baton->callback, StmtObj, baton->callback_required ); baton->StmtObj.Reset(); } - + delete baton; delete req; } @@ -612,9 +612,9 @@ NODE_API_FUNC( Connection::prepare ) args.GetReturnValue().SetUndefined(); return; } - + Connection *db = ObjectWrap::Unwrap( args.This() ); - + if( db == NULL || db->conn == NULL ) { std::string error_msg; getErrorMsg( JS_ERR_NOT_CONNECTED, error_msg ); @@ -641,17 +641,17 @@ NODE_API_FUNC( Connection::prepare ) p_stmt.Reset(); return; } - - Nan::Utf8String param0( args[0]->ToString(isolate) ); - + + Nan::Utf8String param0( args[0] ); + prepareBaton *baton = new prepareBaton(); baton->obj = obj; - baton->callback_required = callback_required; + baton->callback_required = callback_required; baton->stmt = std::string(*param0); - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); @@ -660,21 +660,21 @@ NODE_API_FUNC( Connection::prepare ) status = uv_queue_work( uv_default_loop(), req, prepareWork, (uv_after_work_cb)prepareAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); p_stmt.Reset(); return; } - + prepareWork( req ); bool err = baton->err; prepareAfter( req ); - + if( err ) { args.GetReturnValue().SetUndefined(); return; } - args.GetReturnValue().Set( p_stmt ); + args.GetReturnValue().Set( p_stmt.Get(isolate)); p_stmt.Reset(); } @@ -686,12 +686,12 @@ struct connectBaton { bool err; std::string error_msg; bool callback_required; - + Connection *obj; bool sqlca_connection; std::string conn_string; void *sqlca; - + connectBaton() { obj = NULL; sqlca = NULL; @@ -699,36 +699,36 @@ struct connectBaton { err = false; callback_required = false; } - + ~connectBaton() { obj = NULL; sqlca = NULL; callback.Reset(); } - + }; -void Connection::connectWork( uv_work_t *req ) +void Connection::connectWork( uv_work_t *req ) /*********************************************/ { connectBaton *baton = static_cast(req->data); scoped_lock api_lock( api_mutex ); scoped_lock lock( baton->obj->conn_mutex ); - + if( baton->obj->conn != NULL ) { baton->err = true; getErrorMsg( JS_ERR_CONNECTION_ALREADY_EXISTS, baton->error_msg ); return; } - + if( api.initialized == false) { - + if( !sqlany_initialize_interface( &api, NULL ) ) { baton->err = true; getErrorMsg( JS_ERR_INITIALIZING_DBCAPI, baton->error_msg ); return; } - + if( !api.sqlany_init( "Node.js", SQLANY_API_VERSION_4, &(baton->obj->max_api_ver) )) { // As long as the version is >= 2, we're OK. We just have to disable @@ -747,7 +747,7 @@ void Connection::connectWork( uv_work_t *req ) } } } - + if( !baton->sqlca_connection ) { baton->obj->conn = api.sqlany_new_connection(); if( !api.sqlany_connect( baton->obj->conn, baton->conn_string.c_str() ) ) { @@ -758,7 +758,7 @@ void Connection::connectWork( uv_work_t *req ) cleanAPI(); return; } - + } else { baton->obj->conn = api.sqlany_make_connection( baton->sqlca ); if( baton->obj->conn == NULL ) { @@ -766,20 +766,20 @@ void Connection::connectWork( uv_work_t *req ) cleanAPI(); return; } - } - + } + baton->obj->sqlca_connection = baton->sqlca_connection; openConnections++; } -void Connection::connectAfter( uv_work_t *req ) +void Connection::connectAfter( uv_work_t *req ) /**********************************************/ { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope( isolate ); connectBaton *baton = static_cast(req->data); Local undef = Local::New( isolate, Undefined( isolate ) ); - + if( baton->err ) { callBack( &( baton->error_msg ), baton->callback, undef, baton->callback_required ); @@ -787,9 +787,9 @@ void Connection::connectAfter( uv_work_t *req ) delete req; return; } - + callBack( NULL, baton->callback, undef, baton->callback_required ); - + delete baton; delete req; } @@ -802,7 +802,7 @@ NODE_API_FUNC( Connection::connect ) HandleScope scope( isolate ); int num_args = args.Length(); Connection *obj; - obj = ObjectWrap::Unwrap( args.This() ); + obj = ObjectWrap::Unwrap( args.This() ); bool sqlca_connection = false; bool callback_required = false; int cbfunc_arg = -1; @@ -819,25 +819,25 @@ NODE_API_FUNC( Connection::connect ) } else if( num_args == 1 && args[0]->IsNumber() ){ sqlca_connection = true; - + } else if( num_args == 1 && args[0]->IsString() ) { sqlca_connection = false; - + } else if( num_args == 1 && args[0]->IsObject() ) { sqlca_connection = false; arg_is_string = false; arg_is_object = true; - + } else if( num_args == 2 && args[0]->IsNumber() && args[1]->IsFunction() ) { sqlca_connection = true; callback_required = true; cbfunc_arg = 1; - + } else if( num_args == 2 && args[0]->IsString() && args[1]->IsFunction() ) { sqlca_connection = false; callback_required = true; cbfunc_arg = 1; - + } else if( num_args == 2 && args[0]->IsObject() && args[1]->IsFunction() ) { sqlca_connection = false; callback_required = true; @@ -850,16 +850,16 @@ NODE_API_FUNC( Connection::connect ) args.GetReturnValue().SetUndefined(); return; } - + connectBaton *baton = new connectBaton(); baton->obj = obj; baton->callback_required = callback_required; baton->sqlca_connection = sqlca_connection; - + if( sqlca_connection ) { baton->sqlca = (void *)(long)args[0]->NumberValue(context).ToChecked(); - + } else { Local localArg = Local::New( isolate, obj->_arg ); if( localArg->Length() > 0 ) { @@ -869,13 +869,13 @@ NODE_API_FUNC( Connection::connect ) baton->conn_string = std::string(); } if( arg_is_string ) { - Nan::Utf8String param0( args[0]->ToString(isolate) ); + Nan::Utf8String param0( args[0] ); baton->conn_string.append( ";" ); baton->conn_string.append(*param0); } else if( arg_is_object ) { Persistent arg_string; HashToString( args[0]->ToObject(context).ToLocalChecked(), arg_string ); - Local local_arg_string = + Local local_arg_string = Local::New( isolate, arg_string ); Nan::Utf8String param0( local_arg_string ); baton->conn_string.append( ";" ); @@ -884,10 +884,10 @@ NODE_API_FUNC( Connection::connect ) } baton->conn_string.append( ";CHARSET='UTF-8'" ); } - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); @@ -899,7 +899,7 @@ NODE_API_FUNC( Connection::connect ) args.GetReturnValue().SetUndefined(); return; } - + connectWork( req ); connectAfter( req ); args.GetReturnValue().SetUndefined(); @@ -907,13 +907,13 @@ NODE_API_FUNC( Connection::connect ) } // Disconnect Function -void Connection::disconnectWork( uv_work_t *req ) +void Connection::disconnectWork( uv_work_t *req ) /************************************************/ { noParamBaton *baton = static_cast(req->data); scoped_lock api_lock(api_mutex ); scoped_lock lock( baton->obj->conn_mutex ); - + if( baton->obj->conn == NULL ) { getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); return; @@ -924,16 +924,16 @@ void Connection::disconnectWork( uv_work_t *req ) if( !baton->obj->sqlca_connection ) { api.sqlany_disconnect( baton->obj->conn ); } - // Must free the connection object or there will be a memory leak + // Must free the connection object or there will be a memory leak api.sqlany_free_connection( baton->obj->conn ); baton->obj->conn = NULL; openConnections--; - if( openConnections <= 0 ) { + if( openConnections <= 0 ) { openConnections = 0; cleanAPI(); } - + return; } @@ -945,53 +945,53 @@ NODE_API_FUNC( Connection::disconnect ) int num_args = args.Length(); bool callback_required = false; int cbfunc_arg = -1; - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsFunction() ) { callback_required = true; cbfunc_arg = 0; - + } else { throwError( JS_ERR_INVALID_ARGUMENTS ); args.GetReturnValue().SetUndefined(); return; } - + Connection *obj = ObjectWrap::Unwrap( args.This() ); noParamBaton *baton = new noParamBaton(); - + baton->callback_required = callback_required; baton->obj = obj; - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; status = uv_queue_work( uv_default_loop(), req, disconnectWork, (uv_after_work_cb)noParamAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + disconnectWork( req ); noParamAfter( req ); args.GetReturnValue().SetUndefined(); return; } -void Connection::commitWork( uv_work_t *req ) +void Connection::commitWork( uv_work_t *req ) /********************************************/ { noParamBaton *baton = static_cast(req->data); scoped_lock lock( baton->obj->conn_mutex ); - + if( baton->obj->conn == NULL ) { baton->err = true; getErrorMsg( JS_ERR_NOT_CONNECTED, baton->error_msg ); @@ -1013,48 +1013,48 @@ NODE_API_FUNC( Connection::commit ) int num_args = args.Length(); bool callback_required = false; int cbfunc_arg = -1; - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsFunction() ) { callback_required = true; cbfunc_arg = 0; - + } else { throwError( JS_ERR_INVALID_ARGUMENTS ); args.GetReturnValue().SetUndefined(); return; } - + Connection *obj = ObjectWrap::Unwrap( args.This() ); - + noParamBaton *baton = new noParamBaton(); baton->obj = obj; baton->callback_required = callback_required; - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; - status = uv_queue_work( uv_default_loop(), req, commitWork, + status = uv_queue_work( uv_default_loop(), req, commitWork, (uv_after_work_cb)noParamAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + commitWork( req ); noParamAfter( req ); args.GetReturnValue().SetUndefined(); return; } -void Connection::rollbackWork( uv_work_t *req ) +void Connection::rollbackWork( uv_work_t *req ) /**********************************************/ { noParamBaton *baton = static_cast(req->data); @@ -1081,41 +1081,41 @@ NODE_API_FUNC( Connection::rollback ) int num_args = args.Length(); bool callback_required = false; int cbfunc_arg = -1; - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsFunction() ) { callback_required = true; cbfunc_arg = 0; - + } else { throwError( JS_ERR_INVALID_ARGUMENTS ); args.GetReturnValue().SetUndefined(); return; } - + Connection *obj = ObjectWrap::Unwrap( args.This() ); noParamBaton *baton = new noParamBaton(); baton->obj = obj; baton->callback_required = callback_required; - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; status = uv_queue_work( uv_default_loop(), req, rollbackWork, (uv_after_work_cb)noParamAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + rollbackWork( req ); noParamAfter( req ); args.GetReturnValue().SetUndefined(); @@ -1134,29 +1134,29 @@ struct dropBaton { bool err; std::string error_msg; bool callback_required; - - StmtObject *obj; - + + StmtObject *obj; + dropBaton() { err = false; callback_required = false; obj = NULL; } - + ~dropBaton() { obj = NULL; callback.Reset(); } }; -void StmtObject::dropAfter( uv_work_t *req ) +void StmtObject::dropAfter( uv_work_t *req ) /*******************************************/ { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope( isolate ); dropBaton *baton = static_cast(req->data); Local undef = Local::New( isolate, Undefined( isolate ) ); - + if( baton->err ) { callBack( &( baton->error_msg ), baton->callback, undef, baton->callback_required ); @@ -1164,14 +1164,14 @@ void StmtObject::dropAfter( uv_work_t *req ) delete req; return; } - + callBack( NULL, baton->callback, undef, baton->callback_required ); - + delete baton; delete req; } -void StmtObject::dropWork( uv_work_t *req ) +void StmtObject::dropWork( uv_work_t *req ) /******************************************/ { dropBaton *baton = static_cast(req->data); @@ -1189,41 +1189,41 @@ NODE_API_FUNC( StmtObject::drop ) int num_args = args.Length(); bool callback_required = false; int cbfunc_arg = -1; - + if( num_args == 0 ) { - + } else if( num_args == 1 && args[0]->IsFunction() ) { callback_required = true; cbfunc_arg = 0; - + } else { throwError( JS_ERR_INVALID_ARGUMENTS ); args.GetReturnValue().SetUndefined(); return; } - + StmtObject *obj = ObjectWrap::Unwrap( args.This() ); dropBaton *baton = new dropBaton(); baton->obj = obj; baton->callback_required = callback_required; - + uv_work_t *req = new uv_work_t(); req->data = baton; - + if( callback_required ) { Local callback = Local::Cast(args[cbfunc_arg]); baton->callback.Reset( isolate, callback ); - + int status; status = uv_queue_work( uv_default_loop(), req, dropWork, (uv_after_work_cb)dropAfter ); assert(status == 0); - + args.GetReturnValue().SetUndefined(); return; } - + dropWork( req ); dropAfter( req ); args.GetReturnValue().SetUndefined(); diff --git a/src/utils.cpp b/src/utils.cpp index b378f56..baad637 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -17,7 +17,7 @@ void getErrorMsg( int code, std::string &str ) message << "Code: "; message << code; message << " Msg: "; - + switch( code ) { case JS_ERR_INVALID_OBJECT: message << "Invalid Object"; @@ -73,8 +73,8 @@ void throwError( a_sqlany_connection *conn ) Isolate *isolate = Isolate::GetCurrent(); std::string message; getErrorMsg( conn, message ); - isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, message.c_str() ) ) ); + isolate->ThrowException( + Exception::Error( String::NewFromUtf8( isolate, message.c_str() ).ToLocalChecked()) ); } void throwError( int code ) @@ -82,10 +82,10 @@ void throwError( int code ) { Isolate *isolate = Isolate::GetCurrent(); std::string message; - + getErrorMsg( code, message ); - isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, message.c_str() ) ) ); + isolate->ThrowException( + Exception::Error( String::NewFromUtf8( isolate, message.c_str() ).ToLocalChecked()) ); } void callBack( std::string * str, @@ -104,18 +104,18 @@ void callBack( std::string * str, throwError( JS_ERR_INVALID_ARGUMENTS ); return; } - + Local Err; if( str == NULL ) { Err = Local::New( isolate, Undefined( isolate ) ); - + } else { - Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ); + Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked() ); } - + int argc = 2; Local argv[2] = { Err, Result }; - + #if v012 TryCatch try_catch; #else @@ -129,7 +129,7 @@ void callBack( std::string * str, } else { if( str != NULL ) { isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ) ); + Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked()) ); } } } @@ -163,18 +163,18 @@ void callBack( std::string * str, return; } Local callback = Local::Cast(arg); - + Local Err; if( str == NULL ) { Err = Local::New( isolate, Undefined( isolate ) ); - + } else { - Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ); + Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked() ); } - + int argc = 2; Local argv[2] = { Err, Result }; - + #if v012 TryCatch try_catch; #else @@ -185,11 +185,11 @@ void callBack( std::string * str, if( try_catch.HasCaught()) { node::FatalException( isolate, try_catch ); } - + } else { if( str != NULL ) { - isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, str->c_str() ) ) ); + isolate->ThrowException( + Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked()) ); } } } @@ -205,7 +205,7 @@ static bool getWideBindParameters( std::vector &execData, Local rows = Local::Cast( arg ); num_rows = rows->Length(); - Local row0 = Local::Cast( rows->Get(0) ); + Local row0 = Local::Cast( rows->Get(context, 0).ToLocalChecked() ); unsigned num_cols = row0->Length(); unsigned c; @@ -219,10 +219,10 @@ static bool getWideBindParameters( std::vector &execData, // Make sure that each array in the list has the same number and types // of values for( unsigned int r = 1; r < num_rows; r++ ) { - Local row = Local::Cast( rows->Get(r) ); + Local row = Local::Cast( rows->Get(context, r).ToLocalChecked() ); for( c = 0; c < num_cols; c++ ) { - Local val0 = row0->Get(c); - Local val = row->Get(c); + Local val0 = row0->Get(context, c).ToLocalChecked(); + Local val = row->Get(context, c).ToLocalChecked(); if( ( val0->IsInt32() || val0->IsNumber() ) && ( !val->IsInt32() && !val->IsNumber() && !val->IsNull() ) ) { @@ -256,58 +256,58 @@ static bool getWideBindParameters( std::vector &execData, param.value.is_null = is_null; param.value.is_address = false; - if( row0->Get(c)->IsInt32() || row0->Get(c)->IsNumber() ) { + if( row0->Get(context, c).ToLocalChecked()->IsInt32() || row0->Get(context, c).ToLocalChecked()->IsNumber() ) { param.value.type = A_DOUBLE; param.value.buffer = (char *)( param_double ); - } else if( row0->Get(c)->IsString() ) { + } else if( row0->Get(context, c).ToLocalChecked()->IsString() ) { param.value.type = A_STRING; param.value.buffer = (char *)char_arr; param.value.length = len; param.value.is_address = true; - } else if( Buffer::HasInstance( row0->Get(c) ) ) { + } else if( Buffer::HasInstance( row0->Get(context, c).ToLocalChecked() ) ) { param.value.type = A_BINARY; param.value.buffer = (char *)char_arr; param.value.length = len; param.value.is_address = true; - - } else if( row0->Get(c)->IsNull() ) { + + } else if( row0->Get(context, c).ToLocalChecked()->IsNull() ) { } else{ return false; } for( unsigned int r = 0; r < num_rows; r++ ) { - Local bind_params = Local::Cast( rows->Get(r) ); - + Local bind_params = Local::Cast( rows->Get(context, r).ToLocalChecked()); + is_null[r] = false; - if( bind_params->Get(c)->IsInt32() || bind_params->Get(c)->IsNumber() ) { - param_double[r] = bind_params->Get(c)->NumberValue(context).ToChecked(); - - } else if( bind_params->Get(c)->IsString() ) { - Nan::Utf8String paramValue( bind_params->Get(c)->ToString(isolate) ); + if( bind_params->Get(context, c).ToLocalChecked()->IsInt32() || bind_params->Get(context, c).ToLocalChecked()->IsNumber() ) { + param_double[r] = bind_params->Get(context, c).ToLocalChecked()->NumberValue(context).ToChecked(); + + } else if( bind_params->Get(context, c).ToLocalChecked()->IsString() ) { + Nan::Utf8String paramValue( bind_params->Get(context, c).ToLocalChecked() ); const char* param_string = (*paramValue); len[r] = (size_t)paramValue.length(); char *param_char = new char[len[r] + 1]; char_arr[r] = param_char; memcpy( param_char, param_string, len[r] + 1 ); - - } else if( Buffer::HasInstance( bind_params->Get(c) ) ) { - len[r] = Buffer::Length( bind_params->Get(c) ); + + } else if( Buffer::HasInstance( bind_params->Get(context, c).ToLocalChecked()) ) { + len[r] = Buffer::Length( bind_params->Get(context, c).ToLocalChecked()); char *param_char = new char[len[r]]; char_arr[r] = param_char; - memcpy( param_char, Buffer::Data( bind_params->Get(c) ), len[r] ); + memcpy( param_char, Buffer::Data( bind_params->Get(context, c).ToLocalChecked()), len[r] ); - } else if( bind_params->Get(c)->IsNull() ) { + } else if( bind_params->Get(context, c).ToLocalChecked()->IsNull() ) { is_null[r] = true; } } - + params.push_back( param ); } - return true; + return true; } bool getBindParameters( std::vector &execData, @@ -328,7 +328,7 @@ bool getBindParameters( std::vector &execData, return true; } - if( bind_params->Get(0)->IsArray() ) { + if( bind_params->Get(context, 0).ToLocalChecked()->IsArray() ) { return getWideBindParameters( execData, arg, params, num_rows ); } num_rows = 1; @@ -340,68 +340,68 @@ bool getBindParameters( std::vector &execData, a_sqlany_bind_param param; memset( ¶m, 0, sizeof( param ) ); - if( bind_params->Get(i)->IsInt32() ) { + if( bind_params->Get(context, i).ToLocalChecked()->IsInt32() ) { int *param_int = new int; - *param_int = bind_params->Get(i)->Int32Value(context).ToChecked(); + *param_int = bind_params->Get(context, i).ToLocalChecked()->Int32Value(context).ToChecked(); ex->addInt( param_int ); param.value.buffer = (char *)( param_int ); param.value.type = A_VAL32; - - } else if( bind_params->Get(i)->IsNumber() ) { + + } else if( bind_params->Get(context, i).ToLocalChecked()->IsNumber() ) { double *param_double = new double; - *param_double = bind_params->Get(i)->NumberValue(context).ToChecked(); // Remove Round off Error + *param_double = bind_params->Get(context, i).ToLocalChecked()->NumberValue(context).ToChecked(); // Remove Round off Error ex->addNum( param_double ); param.value.buffer = (char *)( param_double ); param.value.type = A_DOUBLE; - - } else if( bind_params->Get(i)->IsString() ) { - Nan::Utf8String paramValue( bind_params->Get(i)->ToString(context).ToLocalChecked() ); + + } else if( bind_params->Get(context, i).ToLocalChecked()->IsString() ) { + Nan::Utf8String paramValue( bind_params->Get(context, i).ToLocalChecked() ); const char* param_string = (*paramValue); size_t *len = new size_t; *len = (size_t)paramValue.length(); - + char **char_arr = new char *; char *param_char = new char[*len + 1]; *char_arr = param_char; memcpy( param_char, param_string, ( *len ) + 1 ); ex->addStrings( char_arr, len ); - + param.value.type = A_STRING; param.value.buffer = param_char; param.value.length = len; param.value.buffer_size = *len + 1; - - } else if( Buffer::HasInstance( bind_params->Get(i) ) ) { + + } else if( Buffer::HasInstance( bind_params->Get(context, i).ToLocalChecked() ) ) { size_t *len = new size_t; - *len = Buffer::Length( bind_params->Get(i) ); + *len = Buffer::Length( bind_params->Get(context, i).ToLocalChecked() ); char **char_arr = new char *; char *param_char = new char[*len]; *char_arr = param_char; - - memcpy( param_char, Buffer::Data( bind_params->Get(i) ), *len ); + + memcpy( param_char, Buffer::Data( bind_params->Get(context, i).ToLocalChecked() ), *len ); ex->addStrings( char_arr, len ); - + param.value.type = A_BINARY; param.value.buffer = param_char; param.value.length = len; param.value.buffer_size = sizeof( param_char ); - - } else if( bind_params->Get(i)->IsNull() ) { + + } else if( bind_params->Get(context, i).ToLocalChecked()->IsNull() ) { param.value.type = A_STRING; sacapi_bool *is_null = new sacapi_bool; param.value.is_null = is_null; ex->addNull( is_null ); is_null[0] = true; - + } else{ return false; } - + params.push_back( param ); } - return true; + return true; } bool getResultSet( Persistent & Result, @@ -412,6 +412,7 @@ bool getResultSet( Persistent & Result, /*****************************************************************/ { Isolate *isolate = Isolate::GetCurrent(); + Local context = isolate->GetCurrentContext(); HandleScope scope( isolate ); int num_rows = 0; size_t num_cols = colNames.size(); @@ -420,7 +421,7 @@ bool getResultSet( Persistent & Result, Result.Reset( isolate, Integer::New( isolate, rows_affected ) ); return true; } - + if( num_cols > 0 ) { size_t count = 0; size_t count_int = 0, count_num = 0, count_string = 0; @@ -433,7 +434,7 @@ bool getResultSet( Persistent & Result, for( size_t i = 0; i < num_cols; i++ ) { switch( col_types[count] ) { case A_INVALID_TYPE: - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Null( isolate ) ); break; @@ -443,61 +444,61 @@ bool getResultSet( Persistent & Result, case A_VAL8: case A_UVAL8: if( execData->intIsNull( count_int ) ) { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Null( isolate ) ); } else { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Integer::New( isolate, execData->getInt( count_int ) ) ); } count_int++; break; - + case A_UVAL32: case A_UVAL64: case A_VAL64: case A_DOUBLE: if( execData->numIsNull( count_num ) ) { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Null( isolate ) ); } else { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Number::New( isolate, execData->getNum( count_num ) ) ); } count_num++; break; - + case A_BINARY: if( execData->stringIsNull( count_string ) ) { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Null( isolate ) ); } else { #if v012 - Local buf = node::Buffer::New( + Local buf = node::Buffer::New( isolate, execData->getString( count_string ), - execData->getLen( count_string ) ); + execData->getLen( count_string ) ); curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), buf ); #else - MaybeLocal mbuf = node::Buffer::Copy( + MaybeLocal mbuf = node::Buffer::Copy( isolate, execData->getString( count_string ), - execData->getLen( count_string ) ); + execData->getLen( count_string ) ); Local buf = mbuf.ToLocalChecked(); #endif - curr_row->Set( String::NewFromUtf8( isolate, - colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, + colNames[i] ).ToLocalChecked(), buf ); } count_string++; break; - - case A_STRING: + + case A_STRING: if( execData->stringIsNull( count_string ) ) { - curr_row->Set( String::NewFromUtf8( isolate, colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), Null( isolate ) ); } else { - curr_row->Set( String::NewFromUtf8( isolate, - colNames[i] ), + curr_row->Set(context, String::NewFromUtf8( isolate, + colNames[i] ).ToLocalChecked(), #if v012 String::NewFromUtf8( isolate, execData->getString( count_string ), @@ -513,20 +514,20 @@ bool getResultSet( Persistent & Result, } count_string++; break; - + default: return false; } count++; } - ResultSet->Set( num_rows - 1, curr_row ); + ResultSet->Set(context, num_rows - 1, curr_row ); } Result.Reset( isolate, ResultSet ); } else { - Result.Reset( isolate, Local::New( isolate, + Result.Reset( isolate, Local::New( isolate, Undefined( isolate ) ) ); } - + return true; } @@ -537,21 +538,21 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, std::vector & col_types ) /*****************************************************************/ { - + a_sqlany_data_value value; int num_cols = 0; - + rows_affected = api.sqlany_affected_rows( sqlany_stmt ); num_cols = api.sqlany_num_cols( sqlany_stmt ); - - + + if( rows_affected > 0 && num_cols < 1 ) { return true; } - + rows_affected = -1; if( num_cols > 0 ) { - + for( int i = 0; i < num_cols; i++ ) { a_sqlany_column_info info; api.sqlany_get_column_info( sqlany_stmt, i, &info ); @@ -560,7 +561,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, memcpy( name, info.name, size ); colNames.push_back( name ); } - + int count_string = 0, count_num = 0, count_int = 0; while( api.sqlany_fetch_next( sqlany_stmt ) ) { @@ -570,12 +571,12 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, return false; break; } - + if( *(value.is_null) ) { col_types.push_back( A_INVALID_TYPE ); continue; } - + switch( value.type ) { case A_BINARY: { @@ -587,7 +588,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_string++; break; } - + case A_STRING: { size_t *size = new size_t; @@ -598,7 +599,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_string++; break; } - + case A_VAL64: { double *val = new double; @@ -607,7 +608,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_num++; break; } - + case A_UVAL64: { double *val = new double; @@ -616,7 +617,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_num++; break; } - + case A_VAL32: { int *val = new int; @@ -634,7 +635,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_num++; break; } - + case A_VAL16: { int *val = new int; @@ -652,7 +653,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_int++; break; } - + case A_VAL8: { int *val = new int; @@ -661,7 +662,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_int++; break; } - + case A_UVAL8: { int *val = new int; @@ -670,7 +671,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_int++; break; } - + case A_DOUBLE: { double *val = new double; @@ -679,7 +680,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, count_num++; break; } - + default: return false; } @@ -687,7 +688,7 @@ bool fetchResultSet( a_sqlany_stmt * sqlany_stmt, } } } - + return true; } @@ -706,7 +707,7 @@ bool cleanAPI() // Generic Baton and Callback (After) Function // Use this if the function does not have any return values and -// Does not take any parameters. +// Does not take any parameters. // Create custom Baton and Callback (After) functions otherwise void Connection::noParamAfter( uv_work_t *req ) @@ -722,9 +723,9 @@ void Connection::noParamAfter( uv_work_t *req ) baton->callback_required ); return; } - + callBack( NULL, baton->callback, undef, baton->callback_required ); - + delete baton; delete req; } @@ -764,7 +765,7 @@ void StmtObject::Init( Isolate *isolate ) Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); - tpl->SetClassName( String::NewFromUtf8( isolate, "StmtObject" ) ); + tpl->SetClassName( String::NewFromUtf8( isolate, "StmtObject" ).ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); // Prototype @@ -786,9 +787,11 @@ void StmtObject::New( const FunctionCallbackInfo &args ) void StmtObject::NewInstance( const FunctionCallbackInfo &args ) /*********************************************************************/ { + Isolate* isolate = Isolate::GetCurrent(); + Local context = isolate->GetCurrentContext(); Persistent obj; CreateNewInstance( args, obj ); - args.GetReturnValue().Set( obj ); + args.GetReturnValue().Set( obj.Get(isolate) ); } void StmtObject::CreateNewInstance( const FunctionCallbackInfo & args, @@ -842,8 +845,8 @@ void HashToString( Local obj, Persistent &ret ) std::string params = ""; bool first = true; for( int i = 0; i < length; i++ ) { - Local key = props.ToLocalChecked()->Get(i).As(); - Local val = obj->Get(key).As(); + Local key = props.ToLocalChecked()->Get(context, i).ToLocalChecked().As(); + Local val = obj->Get(context, key).ToLocalChecked().As(); String::Utf8Value key_utf8( isolate, key ); String::Utf8Value val_utf8( isolate, val ); if( !first ) { @@ -854,7 +857,7 @@ void HashToString( Local obj, Persistent &ret ) params += "="; params += std::string(*val_utf8); } - ret.Reset( isolate, String::NewFromUtf8( isolate, params.c_str() ) ); + ret.Reset( isolate, String::NewFromUtf8( isolate, params.c_str() ).ToLocalChecked()); } #if 0 @@ -924,17 +927,17 @@ Connection::Connection( const FunctionCallbackInfo &args ) int string_len = str.ToLocalChecked()->Utf8Length(isolate); char *buf = new char[string_len+1]; str.ToLocalChecked()->WriteUtf8(isolate, buf ); - _arg.Reset( isolate, String::NewFromUtf8( isolate, buf ) ); + _arg.Reset( isolate, String::NewFromUtf8( isolate, buf ).ToLocalChecked()); delete [] buf; } else if( args[0]->IsObject() ) { HashToString(args[0]->ToObject(context).ToLocalChecked(), _arg); } else if( !args[0]->IsUndefined() && !args[0]->IsNull() ) { throwError( JS_ERR_INVALID_ARGUMENTS ); } else { - _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ) ); + _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ).ToLocalChecked()); } } else { - _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ) ); + _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ).ToLocalChecked()); } } @@ -953,7 +956,7 @@ Connection::~Connection() conn = NULL; openConnections--; } - + cleanAPI(); }; @@ -989,9 +992,9 @@ void Connection::Init( Isolate *isolate ) Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); - tpl->SetClassName( String::NewFromUtf8( isolate, "Connection" ) ); + tpl->SetClassName( String::NewFromUtf8( isolate, "Connection" ).ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); - + // Prototype NODE_SET_PROTOTYPE_METHOD( tpl, "exec", exec ); NODE_SET_PROTOTYPE_METHOD( tpl, "prepare", prepare ); From 717931297115aab671d75754abfe5e063b59f2b5 Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Thu, 23 Jul 2020 14:27:00 +0200 Subject: [PATCH 5/5] Removed old code and work with node 12 and 14 --- src/h/connection.h | 8 ----- src/h/nodever_cover.h | 15 -------- src/h/sqlany_utils.h | 20 +---------- src/h/stmt.h | 6 ---- src/sqlanywhere.cpp | 8 ++--- src/utils.cpp | 81 +++++++++++++++++-------------------------- 6 files changed, 35 insertions(+), 103 deletions(-) diff --git a/src/h/connection.h b/src/h/connection.h index e9aeb92..46106c0 100644 --- a/src/h/connection.h +++ b/src/h/connection.h @@ -138,22 +138,14 @@ class Connection : public ObjectWrap { public: /// @internal -#if v010 - static void Init(); -#else static void Init( Isolate * ); -#endif /// @internal static NODE_API_FUNC( NewInstance ); private: /// @internal -#if v010 - Connection( const Arguments &args ); -#else Connection( const FunctionCallbackInfo &args ); -#endif /// @internal ~Connection(); diff --git a/src/h/nodever_cover.h b/src/h/nodever_cover.h index e179bde..88c069b 100644 --- a/src/h/nodever_cover.h +++ b/src/h/nodever_cover.h @@ -3,19 +3,4 @@ // *************************************************************************** #include -#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION == 10 -#define v010 1 -#define v012 0 -#elif NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION == 12 -#define v010 0 -#define v012 1 -#else -#define v010 0 -#define v012 0 -#endif - -#if v010 -#define NODE_API_FUNC( name ) Handle name ( const Arguments &args ) -#else #define NODE_API_FUNC( name ) void name ( const FunctionCallbackInfo &args ) -#endif diff --git a/src/h/sqlany_utils.h b/src/h/sqlany_utils.h index 908b5d2..c24f511 100644 --- a/src/h/sqlany_utils.h +++ b/src/h/sqlany_utils.h @@ -132,16 +132,6 @@ void getErrorMsg( int code, std::string &str ); void throwError( a_sqlany_connection *conn ); void throwError( int code ); -#if v010 -void callBack( std::string * str, - Persistent callback, - Local Result, - bool callback_required = true ); -void callBack( std::string * str, - Local callback, - Local Result, - bool callback_required = true ); -#else void callBack( std::string * str, Persistent & callback, Local & Result, @@ -154,7 +144,6 @@ void callBack( std::string * str, Persistent & callback, Persistent & Result, bool callback_required = true ); -#endif bool getBindParameters( std::vector &execData , Local arg @@ -162,19 +151,12 @@ bool getBindParameters( std::vector &execData , unsigned &num_rows ); -#if v010 -bool getResultSet( Local &Result - , int &rows_affected - , std::vector &colNames - , ExecuteData *execData - , std::vector &col_types ); -#else + bool getResultSet( Persistent &Result , int &rows_affected , std::vector &colNames , ExecuteData *execData , std::vector &col_types ); -#endif bool fetchResultSet( a_sqlany_stmt *sqlany_stmt , int &rows_affected diff --git a/src/h/stmt.h b/src/h/stmt.h index a4106aa..756c5d0 100644 --- a/src/h/stmt.h +++ b/src/h/stmt.h @@ -16,20 +16,14 @@ class StmtObject : public node::ObjectWrap { public: /// @internal -#if v010 - static void Init(); -#else static void Init( Isolate * ); -#endif /// @internal static NODE_API_FUNC( NewInstance ); /// @internal -#if !v010 static void CreateNewInstance( const FunctionCallbackInfo &args, Persistent &obj ); -#endif /// @internal StmtObject(); diff --git a/src/sqlanywhere.cpp b/src/sqlanywhere.cpp index ea4be30..730ceff 100644 --- a/src/sqlanywhere.cpp +++ b/src/sqlanywhere.cpp @@ -5,7 +5,7 @@ #include "sqlany_utils.h" #include "nan.h" -#if !v010 +#if NODE_MAJOR_VERSION >= 12 using namespace v8; @@ -1234,11 +1234,7 @@ void init( Local exports ) /********************************/ { uv_mutex_init(&api_mutex); -#if v012 - Isolate *isolate = Isolate::GetCurrent(); -#else Isolate *isolate = exports->GetIsolate(); -#endif StmtObject::Init( isolate ); Connection::Init( isolate ); NODE_SET_METHOD( exports, "createConnection", Connection::NewInstance ); @@ -1246,4 +1242,4 @@ void init( Local exports ) NODE_MODULE( DRIVER_NAME, init ) -#endif // !v010 +#endif //NODE_MAJOR_VERSION >= 12 \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index baad637..1379d50 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,7 +5,7 @@ #include "sqlany_utils.h" #include "nan.h" -#if !v010 +#if NODE_MAJOR_VERSION >= 12 using namespace v8; using namespace node; @@ -53,6 +53,15 @@ void getErrorMsg( int code, std::string &str ) } +v8::Local GetUtf8String(v8::Isolate *isolate, std::string msg) { + auto ret = String::NewFromUtf8(isolate, msg.c_str()); +#if NODE_MAJOR_VERSION == 14 + return ret.ToLocalChecked(); +#else + return ret; +#endif +} + void getErrorMsg( a_sqlany_connection *conn, std::string &str ) /*************************************************************/ { @@ -74,7 +83,7 @@ void throwError( a_sqlany_connection *conn ) std::string message; getErrorMsg( conn, message ); isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, message.c_str() ).ToLocalChecked()) ); + Exception::Error( GetUtf8String(isolate, message.c_str() )) ); } void throwError( int code ) @@ -85,7 +94,7 @@ void throwError( int code ) getErrorMsg( code, message ); isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, message.c_str() ).ToLocalChecked()) ); + Exception::Error(GetUtf8String(isolate, message.c_str())) ); } void callBack( std::string * str, @@ -110,17 +119,13 @@ void callBack( std::string * str, Err = Local::New( isolate, Undefined( isolate ) ); } else { - Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked() ); + Err = Exception::Error(GetUtf8String(isolate, str->c_str())); } int argc = 2; Local argv[2] = { Err, Result }; -#if v012 - TryCatch try_catch; -#else TryCatch try_catch( isolate ); -#endif Nan::Callback *cb = new Nan::Callback( local_callback ); cb->Call( argc, argv ); if( try_catch.HasCaught()) { @@ -129,7 +134,7 @@ void callBack( std::string * str, } else { if( str != NULL ) { isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked()) ); + Exception::Error(GetUtf8String(isolate, str->c_str()))); } } } @@ -169,17 +174,13 @@ void callBack( std::string * str, Err = Local::New( isolate, Undefined( isolate ) ); } else { - Err = Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked() ); + Err = Exception::Error(GetUtf8String(isolate, str->c_str())); } int argc = 2; Local argv[2] = { Err, Result }; -#if v012 - TryCatch try_catch; -#else TryCatch try_catch( isolate ); -#endif Nan::Callback *cb = new Nan::Callback( callback ); cb->Call( argc, argv ); if( try_catch.HasCaught()) { @@ -189,7 +190,7 @@ void callBack( std::string * str, } else { if( str != NULL ) { isolate->ThrowException( - Exception::Error( String::NewFromUtf8( isolate, str->c_str() ).ToLocalChecked()) ); + Exception::Error(GetUtf8String(isolate, str->c_str()))); } } } @@ -434,7 +435,7 @@ bool getResultSet( Persistent & Result, for( size_t i = 0; i < num_cols; i++ ) { switch( col_types[count] ) { case A_INVALID_TYPE: - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Null( isolate ) ); break; @@ -444,10 +445,10 @@ bool getResultSet( Persistent & Result, case A_VAL8: case A_UVAL8: if( execData->intIsNull( count_int ) ) { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Null( isolate ) ); } else { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Integer::New( isolate, execData->getInt( count_int ) ) ); } count_int++; @@ -458,10 +459,10 @@ bool getResultSet( Persistent & Result, case A_VAL64: case A_DOUBLE: if( execData->numIsNull( count_num ) ) { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Null( isolate ) ); } else { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Number::New( isolate, execData->getNum( count_num ) ) ); } count_num++; @@ -469,24 +470,14 @@ bool getResultSet( Persistent & Result, case A_BINARY: if( execData->stringIsNull( count_string ) ) { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Null( isolate ) ); } else { -#if v012 - Local buf = node::Buffer::New( - isolate, execData->getString( count_string ), - execData->getLen( count_string ) ); - curr_row->Set( String::NewFromUtf8( isolate, - colNames[i] ), - buf ); -#else MaybeLocal mbuf = node::Buffer::Copy( isolate, execData->getString( count_string ), execData->getLen( count_string ) ); Local buf = mbuf.ToLocalChecked(); -#endif - curr_row->Set(context, String::NewFromUtf8( isolate, - colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), buf ); } count_string++; @@ -494,22 +485,14 @@ bool getResultSet( Persistent & Result, case A_STRING: if( execData->stringIsNull( count_string ) ) { - curr_row->Set(context, String::NewFromUtf8( isolate, colNames[i] ).ToLocalChecked(), + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), Null( isolate ) ); } else { - curr_row->Set(context, String::NewFromUtf8( isolate, - colNames[i] ).ToLocalChecked(), -#if v012 - String::NewFromUtf8( isolate, - execData->getString( count_string ), - String::NewStringType::kNormalString, - (int)execData->getLen( count_string ) ) -#else + curr_row->Set(context, GetUtf8String(isolate, colNames[i]), String::NewFromUtf8( isolate, execData->getString( count_string ), NewStringType::kNormal, (int)execData->getLen( count_string ) ).ToLocalChecked() -#endif ); } count_string++; @@ -765,7 +748,7 @@ void StmtObject::Init( Isolate *isolate ) Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); - tpl->SetClassName( String::NewFromUtf8( isolate, "StmtObject" ).ToLocalChecked()); + tpl->SetClassName(GetUtf8String(isolate, "StmtObject")); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); // Prototype @@ -857,7 +840,7 @@ void HashToString( Local obj, Persistent &ret ) params += "="; params += std::string(*val_utf8); } - ret.Reset( isolate, String::NewFromUtf8( isolate, params.c_str() ).ToLocalChecked()); + ret.Reset(isolate, GetUtf8String(isolate, params.c_str())); } #if 0 @@ -927,17 +910,17 @@ Connection::Connection( const FunctionCallbackInfo &args ) int string_len = str.ToLocalChecked()->Utf8Length(isolate); char *buf = new char[string_len+1]; str.ToLocalChecked()->WriteUtf8(isolate, buf ); - _arg.Reset( isolate, String::NewFromUtf8( isolate, buf ).ToLocalChecked()); + _arg.Reset( isolate, GetUtf8String(isolate, buf)); delete [] buf; } else if( args[0]->IsObject() ) { HashToString(args[0]->ToObject(context).ToLocalChecked(), _arg); } else if( !args[0]->IsUndefined() && !args[0]->IsNull() ) { throwError( JS_ERR_INVALID_ARGUMENTS ); } else { - _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ).ToLocalChecked()); + _arg.Reset( isolate, GetUtf8String(isolate, "")); } } else { - _arg.Reset( isolate, String::NewFromUtf8( isolate, "" ).ToLocalChecked()); + _arg.Reset( isolate, GetUtf8String(isolate, "")); } } @@ -992,7 +975,7 @@ void Connection::Init( Isolate *isolate ) Local context = isolate->GetCurrentContext(); // Prepare constructor template Local tpl = FunctionTemplate::New( isolate, New ); - tpl->SetClassName( String::NewFromUtf8( isolate, "Connection" ).ToLocalChecked()); + tpl->SetClassName(GetUtf8String(isolate, "Connection")); tpl->InstanceTemplate()->SetInternalFieldCount( 1 ); // Prototype @@ -1051,4 +1034,4 @@ void Connection::NewInstance( const FunctionCallbackInfo &args ) args.GetReturnValue().Set( instance ); } -#endif // !v010 +#endif //NODE_MAJOR_VERSION >= 12