Handle<Value> Session::Request(const Arguments& args) { HandleScope scope; if (args.Length() < 1 || !args[0]->IsString()) { return ThrowException(Exception::Error(String::New( "Service URI string must be provided as first parameter."))); } if (args.Length() < 2 || !args[1]->IsString()) { return ThrowException(Exception::Error(String::New( "String request name must be provided as second parameter."))); } if (args.Length() < 3 || !args[2]->IsObject()) { return ThrowException(Exception::Error(String::New( "Object containing request parameters must be provided " "as third parameter."))); } if (args.Length() < 4 || !args[3]->IsInt32()) { return ThrowException(Exception::Error(String::New( "Integer correlation identifier must be provided " "as fourth parameter."))); } if (args.Length() >= 5 && !args[4]->IsUndefined() && !args[4]->IsString()) { return ThrowException(Exception::Error(String::New( "Optional request label must be a string."))); } if (args.Length() > 5) { return ThrowException(Exception::Error(String::New( "Function expects at most five arguments."))); } int cidi = args[3]->Int32Value(); Session* session = ObjectWrap::Unwrap<Session>(args.This()); BLPAPI_EXCEPTION_TRY Local<String> uri = args[0]->ToString(); String::AsciiValue uriv(uri); blpapi::Service service = session->d_session->getService(*uriv); Local<String> name = args[1]->ToString(); String::Utf8Value namev(name); blpapi::Request request = service.createRequest(*namev); // Loop over object properties, appending/setting into the request. Local<Object> obj = args[2]->ToObject(); Local<Array> props = obj->GetPropertyNames(); for (std::size_t i = 0; i < props->Length(); ++i) { // Process the key. Local<Value> keyval = props->Get(i); Local<String> key = keyval->ToString(); String::Utf8Value keyv(key); // Process the value. // // The values present on the outer object are marshalled into the // blpapi::Request by setting values using 'set'. Arrays indicate // values which should be marshalled using 'append'. Local<Value> val = obj->Get(keyval); if (val->IsString()) { Local<String> s = val->ToString(); String::Utf8Value valv(s); request.set(*keyv, *valv); } else if (val->IsBoolean()) { request.set(*keyv, val->BooleanValue()); } else if (val->IsNumber()) { request.set(*keyv, val->NumberValue()); } else if (val->IsInt32()) { request.set(*keyv, val->Int32Value()); } else if (val->IsUint32()) { request.set(*keyv, static_cast<blpapi::Int64>(val->Uint32Value())); } else if (val->IsDate()) { blpapi::Datetime dt; mkdatetime(&dt, val); request.set(*keyv, dt); } else if (val->IsArray()) { // Arrays are marshalled into the blpapi::Request by appending // value types using the key of the array in the outer object. Local<Object> subarray = val->ToObject(); int jmax = Array::Cast(*val)->Length(); for (int j = 0; j < jmax; ++j) { Local<Value> subval = subarray->Get(j); // Only strings, booleans, and numbers are marshalled. if (subval->IsString()) { Local<String> s = subval->ToString(); String::Utf8Value subvalv(s); request.append(*keyv, *subvalv); } else if (subval->IsBoolean()) { request.append(*keyv, subval->BooleanValue()); } else if (subval->IsNumber()) { request.append(*keyv, subval->NumberValue()); } else if (subval->IsInt32()) { request.append(*keyv, subval->Int32Value()); } else if (subval->IsUint32()) { request.append(*keyv, static_cast<blpapi::Int64>(subval->Uint32Value())); } else if (subval->IsDate()) { blpapi::Datetime dt; mkdatetime(&dt, subval); request.append(*keyv, dt); } else { return ThrowException(Exception::Error(String::New( "Array contains invalid value type."))); } } } else { return ThrowException(Exception::Error(String::New( "Object contains invalid value type."))); } } blpapi::CorrelationId cid(cidi); if (args.Length() == 5) { String::Utf8Value labelv(args[4]->ToString()); session->d_session->sendRequest(request, session->d_identity, cid, 0, *labelv, labelv.length()); } else { session->d_session->sendRequest(request, session->d_identity, cid); } BLPAPI_EXCEPTION_CATCH_RETURN return scope.Close(Integer::New(cidi)); }
bool QueryOperation::BindParameters( Handle<Array> node_params ) { uint32_t count = node_params->Length(); if( count > 0 ) { for( uint32_t i = 0; i < count; ++i ) { Local<Value> p = node_params->Get( i ); ParamBinding binding; if( p->IsNull() ) { binding.js_type = ParamBinding::JS_NULL; binding.c_type = SQL_C_CHAR; binding.sql_type = SQL_CHAR; binding.param_size = 1; binding.digits = 0; binding.buffer = NULL; binding.buffer_len = 0; binding.indptr = SQL_NULL_DATA; } else if( p->IsString() ) { binding.js_type = ParamBinding::JS_STRING; binding.c_type = SQL_C_WCHAR; binding.sql_type = SQL_WVARCHAR; Local<String> str_param = p->ToString(); int str_len = str_param->Length(); binding.buffer = new uint16_t[ str_len + 1 ]; // null terminator str_param->Write( static_cast<uint16_t*>( binding.buffer )); if( str_len > 4000 ) { binding.param_size = 0; // max types require 0 precision } else { binding.param_size = str_len; } binding.buffer_len = str_len * sizeof( uint16_t ); binding.digits = 0; binding.indptr = binding.buffer_len; } else if( p->IsBoolean() ) { binding.js_type = ParamBinding::JS_BOOLEAN; binding.c_type = SQL_C_BIT; binding.sql_type = SQL_BIT; binding.buffer = new uint16_t; binding.buffer_len = sizeof( uint16_t ); *static_cast<uint16_t*>( binding.buffer ) = p->BooleanValue(); binding.param_size = 1; binding.digits = 0; binding.indptr = binding.buffer_len; } else if( p->IsInt32()) { binding.js_type = ParamBinding::JS_INT; binding.c_type = SQL_C_SLONG; binding.sql_type = SQL_INTEGER; binding.buffer = new int32_t; binding.buffer_len = sizeof( int32_t ); *static_cast<int32_t*>( binding.buffer ) = p->Int32Value(); binding.param_size = sizeof( int32_t ); binding.digits = 0; binding.indptr = binding.buffer_len; } else if( p->IsUint32()) { binding.js_type = ParamBinding::JS_UINT; binding.c_type = SQL_C_ULONG; binding.sql_type = SQL_BIGINT; binding.buffer = new uint32_t; binding.buffer_len = sizeof( uint32_t ); *static_cast<int32_t*>( binding.buffer ) = p->Uint32Value(); binding.param_size = sizeof( uint32_t ); binding.digits = 0; binding.indptr = binding.buffer_len; } else if( p->IsNumber()) { // numbers can be either integers or doubles. We attempt to determine which it is through a simple // cast and equality check double d = p->NumberValue(); if( _isnan( d ) || !_finite( d ) ) { return ParameterErrorToUserCallback( i, "Invalid number parameter" ); } else if( d == floor( d ) && d >= std::numeric_limits<int64_t>::min() && d <= std::numeric_limits<int64_t>::max() ) { binding.js_type = ParamBinding::JS_NUMBER; binding.c_type = SQL_C_SBIGINT; binding.sql_type = SQL_BIGINT; binding.buffer = new int64_t; binding.buffer_len = sizeof( int64_t ); *static_cast<int64_t*>( binding.buffer ) = p->IntegerValue(); binding.param_size = sizeof( int64_t ); binding.digits = 0; binding.indptr = binding.buffer_len; } else { binding.js_type = ParamBinding::JS_NUMBER; binding.c_type = SQL_C_DOUBLE; binding.sql_type = SQL_DOUBLE; binding.buffer = new double; binding.buffer_len = sizeof( double ); *static_cast<double*>( binding.buffer ) = p->NumberValue(); binding.param_size = sizeof( double ); binding.digits = 0; binding.indptr = binding.buffer_len; } } else if( p->IsDate() ) { // Since JS dates have no timezone context, all dates are assumed to be UTC Handle<Date> dateObject = Handle<Date>::Cast<Value>( p ); assert( !dateObject.IsEmpty() ); // dates in JS are stored internally as ms count from Jan 1, 1970 double d = dateObject->NumberValue(); SQL_SS_TIMESTAMPOFFSET_STRUCT* sql_tm = new SQL_SS_TIMESTAMPOFFSET_STRUCT; TimestampColumn sql_date( d ); sql_date.ToTimestampOffset( *sql_tm ); binding.js_type = ParamBinding::JS_DATE; binding.c_type = SQL_C_BINARY; // TODO: Determine proper SQL type based on version of server we're talking to binding.sql_type = SQL_SS_TIMESTAMPOFFSET; binding.buffer = sql_tm; binding.buffer_len = sizeof( SQL_SS_TIMESTAMPOFFSET_STRUCT ); // TODO: Determine proper precision and size based on version of server we're talking to binding.param_size = SQL_SERVER_2008_DEFAULT_DATETIME_PRECISION; binding.digits = SQL_SERVER_2008_DEFAULT_DATETIME_SCALE; binding.indptr = binding.buffer_len; } else if( p->IsObject() && node::Buffer::HasInstance( p )) { // TODO: Determine if we need something to keep the Buffer object from going // away while we use it we could just copy the data, but with buffers being // potentially very large, that could be problematic Local<Object> o = p.As<Object>(); binding.js_type = ParamBinding::JS_BUFFER; binding.c_type = SQL_C_BINARY; binding.sql_type = SQL_VARBINARY; binding.buffer = node::Buffer::Data( o ); binding.buffer_len = node::Buffer::Length( o ); binding.param_size = binding.buffer_len; binding.digits = 0; binding.indptr = binding.buffer_len; } else { return ParameterErrorToUserCallback( i, "Invalid parameter type" ); } params.push_back( move( binding )); } } return true; }
bool GNUtil::isDictionaryObject(Local<Value> obj) { return obj->IsObject() && !(obj->IsRegExp() || obj->IsDate() || obj->IsFunction() || obj->IsArray()); }
bool CQueryParameter::TransformParameter( Isolate* isolate, Local< Value > value, sBindParameter** pOutParam ) { HandleScope scope( isolate ); Local< Context > context = isolate->GetCurrentContext( ); sBindParameter* pParameter = *pOutParam; if( value->IsNull( ) ) { pParameter->SetNull( ); } else if( value->IsString( ) ) { pParameter->SetString( value.As<String>( ) ); } else if( value->IsBoolean( ) ) { pParameter->SetBool( value->BooleanValue( context ).FromJust( ) ); } else if( value->IsInt32( ) ) { pParameter->SetInt( value->Int32Value( context ).FromJust( ) ); } else if( value->IsUint32( ) ) { pParameter->SetUInt( value->Uint32Value( context ).FromJust( ) ); } else if( value->IsNumber( ) ) { double d = value->NumberValue( context ).FromJust( ); if( _isnan( d ) || !_finite( d ) ) { return false; } else if( d == floor( d ) && d >= std::numeric_limits<int64_t>::min( ) && d <= std::numeric_limits<int64_t>::max( ) ) { pParameter->SetInt64( value->IntegerValue( context ).FromJust( ) ); } else { pParameter->SetDouble( value->NumberValue( context ).FromJust( ) ); } } else if( value->IsDate( ) ) { Local<Date> dateObject = value.As<Date>( ); pParameter->SetDate( dateObject->NumberValue( context ).FromJust( ) ); } else if( value->IsObject( ) ) { Local<Object> objValue = value.As<Object>( ); if( node::Buffer::HasInstance( value ) ) { pParameter->SetBuffer( isolate, objValue ); } else { Local< String > strId = String::NewFromUtf8( isolate, "_objId", NewStringType::kNormal ).ToLocalChecked( ); if( objValue->HasRealNamedProperty( context, strId ).FromJust( ) ) { Local< Uint32 > numTypeId = objValue->Get( context, strId ).ToLocalChecked( ).As< Uint32 >( ); uint32_t nType = numTypeId->Int32Value( context ).FromJust( ); switch( nType ) { case ID_INPUT_STREAM: { Local<Value> type = objValue->Get( context, String::NewFromUtf8( isolate, "type", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local<Value> stream = objValue->Get( context, String::NewFromUtf8( isolate, "stream", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local<Value> length = objValue->Get( context, String::NewFromUtf8( isolate, "length", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); pParameter->SetStream( isolate, type->Uint32Value( context ).FromJust( ), stream, static_cast< SQLUINTEGER >( length->IntegerValue( context ).FromJust( ) ) ); break; } case ID_NUMERIC_VALUE: { Local< Value > precision = objValue->Get( context, String::NewFromUtf8( isolate, "precision", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local<Value> scale = objValue->Get( context, String::NewFromUtf8( isolate, "scale", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local<Value> sign = objValue->Get( context, String::NewFromUtf8( isolate, "sign", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local<Value> value = objValue->Get( context, String::NewFromUtf8( isolate, "value", NewStringType::kNormal ).ToLocalChecked( ) ).ToLocalChecked( ); Local< Uint32 > numPrecision = precision.As<Uint32>( ); Local< Uint32 > numScale = scale.As<Uint32>( ); Local< Boolean > boolSign = sign.As<Boolean>( ); Local< ArrayBuffer > arrBuffer = ( value.As<Uint8Array>( ) )->Buffer( ); ArrayBuffer::Contents contents = arrBuffer->GetContents( ); if( contents.ByteLength( ) > SQL_MAX_NUMERIC_LEN ) { return false; } pParameter->SetNumeric( numPrecision->Uint32Value( context ).FromJust( ), numScale->Uint32Value( context ).FromJust( ), boolSign->BooleanValue( context ).FromJust( ), contents.Data( ), contents.ByteLength( ) ); break; } default: { return false; } } } else { return false; } } } else { return false; } return true; }