Skip to content

Commit

Permalink
manual table map time to timeoffset let server truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
TimelordUK committed Mar 13, 2022
1 parent 52a6bd4 commit 66572a3
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 102 deletions.
12 changes: 7 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"request": "launch",
"program": "C:/Program Files/nodejs/node.exe",
"args": [
"simple-demo"
// "runtest", "-k", "linux",
// "simple-demo"
// "runtest", "-k", "linux",
// "runtest", "-k", "linux",
"runtest",
"-t", "table-builder",
// "-t", "querycancel",
// "-t", "pause"
// "-t", "prepared",
Expand All @@ -44,11 +46,11 @@
// "-t", "bulk",
// "-t", "bcp"
//"-t", "json"
"-t", "promises"
//"-t", "promises"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/samples/javascript",
// "cwd": "${workspaceFolder}",
// "cwd": "${workspaceFolder}/samples/javascript",
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
},
Expand Down
13 changes: 11 additions & 2 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ const tableModule = (() => {

const twoParam =
c.type === 'numeric' ||
c.type === 'decimal'
c.type === 'decimal' ||
c.type === 'time'

const timeParam =
c.type === 'time'

let maxLength = c.max_length
if (c.type === 'nvarchar') {
Expand All @@ -77,6 +81,8 @@ const tableModule = (() => {

if (c.is_computed) {
return `${c.decorator}`
} else if (timeParam) {
return `${c.type} (${c.scale}) ${c.decorator}`
} else if (oneParam) {
return `${c.type} (${maxLength}) ${c.decorator}`
} else if (twoParam) {
Expand Down Expand Up @@ -817,15 +823,18 @@ const tableModule = (() => {
columns.forEach(col => {
let declaration = `${col.name} ${col.type}`
let length = 0
const withDeclaration = col.type === 'nvarchar' || col.type === 'varbinary'
if (col.max_length > 0) {
if (col.type === 'nvarchar') {
length = col.max_length / 2
} else if (col.type === 'varbinary') {
length = col.max_length
} else {
length = col.max_length
}
}

if (length > 0) {
if (withDeclaration) {
declaration += `(${length})`
}
let offset = 0
Expand Down
7 changes: 6 additions & 1 deletion src/BoundDatum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,16 @@ namespace mssql
const auto elem = Nan::Get(arr, i).ToLocalChecked();
if (!elem->IsNullOrUndefined())
{
// rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_SS_TIME2, 16, 7, &time2, 0, &cbtime2);
// dates in JS are stored internally as ms count from Jan 1, 1970
const auto d = Local<Date>::Cast<Value>(elem);
auto& time2 = vec[i];
const auto local = Nan::To<Number>(d).ToLocalChecked() ;
const auto ms = local->Value() - offset * 60000;
const TimestampColumn sql_date(-1, ms);
sql_date.ToTime2Struct(time2);
sql_type = SQL_SS_TIME2;
c_type = SQL_TIME;
_indvec[i] = sizeof(SQL_SS_TIME2_STRUCT);
}
}
Expand All @@ -894,6 +897,7 @@ namespace mssql
const TimestampColumn sql_date(-1, ms);
sql_date.ToTime2Struct(time2);
_indvec[0] = sizeof(SQL_SS_TIME2_STRUCT);
param_size = sizeof(SQL_SS_TIME2_STRUCT);
}
}

Expand Down Expand Up @@ -1704,7 +1708,8 @@ namespace mssql
{
if (pp->IsArray())
{
bind_time_array(pp);
// bind_time_array(pp);
bind_time_stamp_offset_array(pp);
}
else
{
Expand Down
Loading

0 comments on commit 66572a3

Please sign in to comment.