Skip to content

Commit

Permalink
fix for TVP where a vector of type numeric is sent to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
TimelordUK committed May 8, 2020
1 parent 31c13f9 commit e5b9c7c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
},
"devDependencies": {
"@types/electron": "^1.4.31",
"@types/node": "^13.13.4",
"@types/node": "^13.13.5",
"@types/sequelize": "^4.28.8",
"asynquence": "^0.10.2",
"asynquence-contrib": "^0.28.2",
"electron": "^8.2.3",
"electron": "^8.2.5",
"minimist": "^1.2.5",
"mocha": "^7.1.2",
"node-gyp": "^4.0.0",
Expand Down
18 changes: 5 additions & 13 deletions src/BoundDatum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ namespace mssql
const auto precision = min(1024, s->Length() + 1);
s->WriteUtf8(fact.isolate, tmp, precision);
const string narrow(tmp);
const auto wide = converter.from_bytes(narrow);
auto wide = converter.from_bytes(narrow);
return wide;
}

Expand Down Expand Up @@ -595,8 +595,8 @@ namespace mssql
const auto elem = MutateJS::get_array_elelemt_at_index(arr, i);
if (!elem->IsNull())
{
const Local<Number> local;
const auto d = local->Value();
const auto num = Local<Number>::Cast<Value>(elem);
const auto d = num->Value();
encode_numeric_struct(d, static_cast<int>(param_size), digits, ns);
param_size = ns.precision;
digits = static_cast<unsigned char>(ns.scale);
Expand Down Expand Up @@ -1287,18 +1287,14 @@ namespace mssql
Local<Value> pval;
const nodeTypeFactory fact;

if (sql_type_s_maps_to_int32(p))
if (sql_type_s_maps_to_int32(p) || sql_type_s_maps_to_boolean(p))
{
pval = fact.new_int32(0);
}
else if (sql_type_s_maps_to_u_int32(p))
{
pval = fact.new_uint32(0);
}
else if (sql_type_s_maps_to_boolean(p))
{
pval = fact.new_int32(0);
}
else if (sql_type_s_maps_to_numeric(p))
{
pval = fact.new_number(0.0);
Expand Down Expand Up @@ -1812,11 +1808,7 @@ namespace mssql
err = static_cast<char*>("Invalid number parameter");
return false;
}
else if (counts.numberCount > 0)
{
bind_double_array(pp);
}
else if (counts.int64Count > 0 && counts.int32Count > 0)
else if (counts.numberCount > 0 || counts.int64Count > 0 && counts.int32Count > 0)
{
bind_double_array(pp);
}
Expand Down
30 changes: 15 additions & 15 deletions src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,31 @@ namespace mssql
void Connection::close(const FunctionCallbackInfo<Value>& info)
{
const auto cb = info[0].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->close(cb);
info.GetReturnValue().Set(ret);
}

void Connection::begin_transaction(const FunctionCallbackInfo<Value>& info)
{
const auto cb = info[0].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->begin_transaction(cb);
info.GetReturnValue().Set(ret);
}

void Connection::commit(const FunctionCallbackInfo<Value>& info)
{
const auto cb = info[0].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->commit(cb);
info.GetReturnValue().Set(ret);
}

void Connection::rollback(const FunctionCallbackInfo<Value>& info)
{
const auto cb = info[0].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->rollback(cb);
info.GetReturnValue().Set(ret);
}
Expand All @@ -134,7 +134,7 @@ namespace mssql
const auto params = info[2].As<Array>();
const auto callback = info[3].As<Object>();

const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->query(query_id, query_object, params, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -145,7 +145,7 @@ namespace mssql
const auto query_object = info[1].As<Object>();
const auto callback = info[2].As<Object>();

const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->prepare(query_id, query_object, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -156,7 +156,7 @@ namespace mssql
const auto params = info[1].As<Array>();
const auto callback = info[2].As<Object>();

const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->query_prepared(query_id, params, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -169,7 +169,7 @@ namespace mssql
const auto params = info[2].As<Array>();
const auto callback = info[3].As<Object>();

const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->call_procedure(query_id, query_object, params, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -178,7 +178,7 @@ namespace mssql
{
const auto query_id = info[0].As<Number>();
const auto callback = info[1].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->unbind_parameters(query_id, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -187,7 +187,7 @@ namespace mssql
{
const auto query_id = info[0].As<Number>();
const auto callback = info[1].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->free_statement(query_id, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -197,7 +197,7 @@ namespace mssql
const auto query_id = info[0].As<Number>();
const auto number_rows = info[1].As<Number>();
const auto cb = info[2].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->read_column(query_id, number_rows, cb);
info.GetReturnValue().Set(ret);
}
Expand All @@ -206,7 +206,7 @@ namespace mssql
{
const auto query_id = info[0].As<Number>();
const auto callback = info[1].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->read_next_result(query_id, callback);
info.GetReturnValue().Set(ret);
}
Expand All @@ -216,7 +216,7 @@ namespace mssql
const auto connection_object = info[0].As<Object>();
const auto callback = info[1].As<Object>();

const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const auto ret = connection->connectionBridge->open(connection_object, callback, info.This());
info.GetReturnValue().Set(ret);
}
Expand All @@ -225,7 +225,7 @@ namespace mssql
{
const auto query_id = info[0].As<Number>();
const auto callback = info[1].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());

const auto ret = connection->connectionBridge->cancel(query_id, callback);
info.GetReturnValue().Set(ret);
Expand All @@ -236,7 +236,7 @@ namespace mssql
const auto query_id = info[0].As<Number>();
const auto v1 = info[1].As<Number>();
const auto callback = info[2].As<Object>();
const auto connection = Unwrap<Connection>(info.This());
auto* const connection = Unwrap<Connection>(info.This());
const nodeTypeFactory fact;
const auto context = fact.isolate->GetCurrentContext();
const auto maybe = v1->Int32Value(context);
Expand Down
4 changes: 2 additions & 2 deletions src/MutateJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace mssql
return val;
}

Local<Value> MutateJS::get_array_elelemt_at_index(const Local<Array> &arr, const int index)
Local<Value> MutateJS::get_array_elelemt_at_index(const Local<Array> &arr, const unsigned int index)
{
const nodeTypeFactory fact;
const auto context = fact.isolate->GetCurrentContext();
Expand All @@ -179,7 +179,7 @@ namespace mssql
return elem;
}

void MutateJS::set_array_elelemt_at_index(const Local<Array>& arr, const int index, const Local<Value>& value)
void MutateJS::set_array_elelemt_at_index(const Local<Array>& arr, const unsigned int index, const Local<Value>& value)
{
const nodeTypeFactory fact;
const auto context = fact.isolate->GetCurrentContext();
Expand Down
4 changes: 2 additions & 2 deletions src/MutateJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace mssql
static Local<Value> get_property_as_value(const Local<Object>& o, const char* v);
static Local<Value> get_property_as_value(const Local<Object>& o, const Local<Value>& v);
static void set_property_value(const Local<Object>& o, const Local<Value>& p, const Local<Value>& v);
static Local<Value> get_array_elelemt_at_index(const Local<Array> & arr, const int index);
static void set_array_elelemt_at_index(const Local<Array>& arr, const int index, const Local<Value> & value);
static Local<Value> get_array_elelemt_at_index(const Local<Array> & arr, const unsigned int index);
static void set_array_elelemt_at_index(const Local<Array>& arr, const unsigned int index, const Local<Value> & value);
static int32_t getint32(Local<Object> query_object, const char* v);
static int64_t getint64(Local<Object> query_object, const char* v);
static int64_t getint64(Local<Number> l);
Expand Down
25 changes: 16 additions & 9 deletions unit.tests/tvp.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ END`
\tdescription varchar(max),
\tusername nvarchar(30),
\tage int,
\tsalary real
\tsalary real,
\tcode numeric(18,0)
)`

const dropTypeSql = `IF TYPE_ID(N'${tableTypeName}') IS not NULL drop type ${tableTypeName}`

const createTypeSql = `CREATE TYPE ${tableTypeName} AS TABLE (description varchar(max), username nvarchar(30), age int, salary real)`
const createTypeSql = `CREATE TYPE ${tableTypeName} AS TABLE (description varchar(max), username nvarchar(30), age int, salary real, code numeric(18,0))`

const insertProcedureSql = `create PROCEDURE ${insertProcedureTypeName}
@tvp ${tableTypeName} READONLY
Expand All @@ -81,13 +82,15 @@ BEGIN
[description],
[username],
[age],
[salary]
[salary],
[code]
)
SELECT
[description],
[username],
[age],
[salary]
[salary],
[code]
n FROM @tvp tvp
END`

Expand Down Expand Up @@ -146,7 +149,7 @@ END`
theConnection.getUserTypeTable(tableTypeName, (err, t) => {
assert.ifError(err)
table = t
assert(table.columns.length === 4)
assert(table.columns.length === 5)
asyncDone()
})
}
Expand All @@ -168,13 +171,15 @@ END`
description: longString,
username: 'santa',
age: 1000,
salary: 0
salary: 0,
code: 123456789012345
},
{
description: 'an entry',
username: 'md',
age: 28,
salary: 100000
salary: 100000,
code: 98765432109876
}
]
}
Expand All @@ -186,13 +191,15 @@ END`
description: longString,
username: 'santa',
age: 1000,
salary: 0
salary: 0,
code: 123456789012345
},
{
description: 'can compound â€',
username: 'md',
age: 28,
salary: 100000
salary: 100000,
code: 98765432109876
}
]
}
Expand Down

0 comments on commit e5b9c7c

Please sign in to comment.