IBM / nodejs-idb-connector

A JavaScript (Node.js) library for communicating with Db2 for IBM i, with support for queries, procedures, and much more. Uses traditional callback-style syntax
MIT License
37 stars 23 forks source link

How to insert a null value? #23

Closed abmusse closed 6 years ago

abmusse commented 6 years ago

Original report by Kerim Gueney (Bitbucket: KerimG, GitHub: KerimG).


I have a database table that has nullable columns and I want to multiple rows in one go. The data contains null values in various places. As in, I cannot just leave out an entire column in my insert statement but rather need an explicit way to insert null.

Is that currently possible? If yes, how would I do that?

abmusse commented 6 years ago

Original comment by Xu Meng (Bitbucket: mengxumx, GitHub: dmabupt).


Yes, indicator 3 tells idb-connector the parameter is null -->

[null, db.SQL_PARAM_INPUT, 3]

This is the old way to set the data type, but from v1.0.9, it can automatically detect the data types. Considering compatibility, I still keep the indicator 0(CLOB), 1(null-terminated-string), 2(integer), 3(null) and added two new types 4(decimal), 5(boolean) --

https://bitbucket.org/litmis/nodejs-idb-connector/src/318f12eecda1120839054d76b805a927cd172c20/src/db2ia/dbstmt.cc#lines-707

abmusse commented 6 years ago

Original comment by Jesse G (Bitbucket: ThePrez, GitHub: ThePrez).


Looking at dbstmt.cc, I see:


    else if(bindIndicator == 3) { //Parameter is NULL
      SQLINTEGER nullLen = SQL_NULL_DATA;
      DEBUG("SQLBindParameter [%d] = NULL \n", i + 1)
      rc = SQLBindParameter(obj->stmth, i + 1, SQL_PARAM_INPUT, SQL_C_DEFAULT, dataType, paramSize, decDigits, buf, 0, &nullLen);
    }

So, I'd expect passing a '3' in as the bind indicator (instead of a 1 for character, 2 for numeric) would do the trick. @KerimG, can you verify?

If so, we should define and document const values for the possible bind indicators or 0, 1, 2, and 3.