OTL 4.0, Example 647 (#define's OTL_DEFAULT_XXX_NULL_TO_VAL)

This example demonstrates how to use #define OTL_DEFAULT_NUMERIC_NULL_TO_VAL,OTL_DEFAULT_DATETIME_NULL_TO_VAL, OTL_DEFAULT_STRING_NULL_TO_VAL,OTL_DEFAULT_CHAR_NULL_TO_VAL., which allow the user to default NULLs to some values.

Source Code

#include <iostream>
using namespace std;

#include <stdio.h>
// Compile OTL 4.0/ODBC in Windows. Informix CLI uses the  
// regular ODBC header files in Windows
#define OTL_ODBC

// Uncomment the #defines below in Linux / Unix
//#define OTL_ODBC_UNIX
//#define OTL_INFORMIX_CLI

// Default "numeric" NULLs to (-1)
#define OTL_DEFAULT_NUMERIC_NULL_TO_VAL (-1)

// Default "datetime" NULLs to otl_datetime(2000,1,1,0,0,0)
#define OTL_DEFAULT_DATETIME_NULL_TO_VAL otl_datetime(2000,1,1,0,0,0)

// Default "string" NULLs to "***NULL***"
#define OTL_DEFAULT_STRING_NULL_TO_VAL "***NULL***"

// Default "char" NULLs to '*'
#define OTL_DEFAULT_CHAR_NULL_TO_VAL '*'

#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object

void insert()
// insert rows into table

 otl_stream o(50, // buffer size
              "insert into test_tab "
              "values(:f1<float>,:f2<char[31]>,NULL,:f4<timestamp>)",  
                 // SQL statement
              db // connect object
             );
 char tmp[32];
 otl_datetime f4(2001,2,24,13,21,11);

 for(int i=1;i<=100;++i){
  sprintf(tmp,"Name%d",i);
  if(i==10)
   o<<(float)i<<otl_null()<<otl_null();
  else
   o<<(float)i<<tmp<<f4;
 }

}

void select()

 otl_stream i(50, // buffer size
              "select f1,f2,f2 f22,f3,f4 "
               "from test_tab "
               "where f1>=:f11<int> and f1<=:f12<int>*2",
                 // SELECT statement
              db // connect object
             ); 
   // create select stream
 
 float f1;
 char f2[31];
 char f22;
 int f3;
 otl_datetime f4;


 
 i<<8<<8; // assigning :f11 = 8, :f12=8
   // SELECT automatically executes when all input variables are
   // assigned. First portion of output rows is fetched to the buffer

 cout<<"=====> Fetching rows and printing out default values instead of NULLs"<<endl;
 while(!i.eof()){ // while not end-of-data
  i>>f1;
  cout<<"f1="<<f1<<", ";
  i>>f2;
  cout<<"f2="<<f2<<", ";
  i>>f22;
  cout<<"f22="<<f22<<", ";
  i>>f3;
  cout<<"f3="<<f3<<", ";
  i>>f4;
  cout<<"f4="<<f4.month<<"/"
      <<f4.day<<"/"
      <<f4.year<<" "
      <<f4.hour<<":"
      <<f4.minute<<":"
      <<f4.second;
  cout<<endl;
 }

 i<<8<<8; // assigning :f11 = 8, :f12=8
 cout<<"=====> Fetching rows and printing out NULLs"<<endl;
 while(!i.eof()){ // while not end-of-data
  i>>f1;
  if(i.is_null())
   cout<<"f1=NULL, ";
  else
   cout<<"f1="<<f1<<", ";
  i>>f2;
  if(i.is_null())
   cout<<"f2=NULL, ";
  else
   cout<<"f2="<<f2<<", ";
  i>>f22;
  if(i.is_null())
   cout<<"f22=NULL, ";
  else
   cout<<"f22="<<f22<<", ";
  i>>f3;
  if(i.is_null())
   cout<<"f3=NULL, ";
  else
   cout<<"f3="<<f3<<", ";
  i>>f4;
  if(i.is_null())
   cout<<"f4=NULL,";
  else
   cout<<"f4="<<f4.month<<"/"
       <<f4.day<<"/"
       <<f4.year<<" "
       <<f4.hour<<":"
       <<f4.minute<<":"
       <<f4.second;
  cout<<endl;
 }

}

int main()
{
 otl_connect::otl_initialize(); // initialize Informix CLI environment
 try{

  db.rlogon("informix/tigger@informixsql"); // connect to Informix

  otl_cursor::direct_exec
   (db,
    "drop table test_tab",
    otl_exception::disabled // disable OTL exceptions
   ); // drop table

  otl_cursor::direct_exec
   (db,
    "create table test_tab(f1 int, f2 varchar(30), f3 int, f4 datetime year to second)"
    );  // create table

  insert(); // insert records into table
  select(); // select records from table

 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from Informix

 return 0;

}

Output

=====> Fetching rows and printing out default values instead of NULLs
f1=8, f2=Name8, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=9, f2=Name9, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=10, f2=***NULL***, f22=*, f3=-1, f4=1/1/2000 0:0:0
f1=11, f2=Name11, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=12, f2=Name12, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=13, f2=Name13, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=14, f2=Name14, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=15, f2=Name15, f22=N, f3=-1, f4=2/24/2001 13:21:11
f1=16, f2=Name16, f22=N, f3=-1, f4=2/24/2001 13:21:11
=====> Fetching rows and printing out NULLs
f1=8, f2=Name8, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=9, f2=Name9, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=10, f2=NULL, f22=NULL, f3=NULL, f4=NULL,
f1=11, f2=Name11, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=12, f2=Name12, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=13, f2=Name13, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=14, f2=Name14, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=15, f2=Name15, f22=N, f3=NULL, f4=2/24/2001 13:21:11
f1=16, f2=Name16, f22=N, f3=NULL, f4=2/24/2001 13:21:11


Examples ContentsGo Home

Copyright © 1996-2023, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com .

Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.