OTL 4.0, Example 743 (Extended/ customized numeric data type support)

This example demonstrates how OTL can be extended with numeric data types that are not supported by the underlying database API.

Source Code

// Suppress VC++ warnings about insecure sprintf / sscanf calls.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4.0/SNAC

// disable private operators >>/<< for unsigned long
#define OTL_STREAM_NO_PRIVATE_UNSIGNED_LONG_OPERATORS

// #defines for extended numeric data types
#define OTL_NUMERIC_TYPE_1_ID "ULONG"
#define OTL_NUMERIC_TYPE_1_STR_SIZE 40
#define OTL_NUMERIC_TYPE_1 unsigned long
#define OTL_STR_TO_NUMERIC_TYPE_1(str,n) \
{ \
sscanf(str,"%lu",&n); \
}

#define OTL_NUMERIC_TYPE_1_TO_STR(n,str) \
{ \
sprintf(str,"%lu",n); \
}

#define OTL_NUMERIC_TYPE_2_ID "UULONG"
#define OTL_NUMERIC_TYPE_2_STR_SIZE 40
#define OTL_NUMERIC_TYPE_2 unsigned long long
#define OTL_STR_TO_NUMERIC_TYPE_2(str,n) \
{ \
sscanf(str,"%llu",&n); \
}

#define OTL_NUMERIC_TYPE_2_TO_STR(n,str) \
{ \
sprintf(str,"%llu",n); \
}

#define OTL_NUMERIC_TYPE_3_ID "LDOUBLE"
#define OTL_NUMERIC_TYPE_3_STR_SIZE 60
#define OTL_NUMERIC_TYPE_3 long double
#define OTL_STR_TO_NUMERIC_TYPE_3(str,n) \
{ \
sscanf(str,"%Lf",&n); \
}

#define OTL_NUMERIC_TYPE_3_TO_STR(n,str) \
{ \
sprintf(str,"%Lf",n); \
}

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

const OTL_NUMERIC_TYPE_1 VAL1=1234567890;
const OTL_NUMERIC_TYPE_2 VAL2=1234567890;
const OTL_NUMERIC_TYPE_3 VAL3=1234567890;

otl_connect db; // connect object

void insert()
// insert rows into table
{
otl_stream o(50, // buffer size
"insert into test_tab values(:f1<uulong>,:f2<char[31]>)",
// SQL statement
db // connect object
);
char tmp[32];

for(OTL_NUMERIC_TYPE_2 i=VAL2;i<=VAL2+100;++i){
int ndx=static_cast<int>(i-VAL2);
sprintf(tmp,"Name%d",ndx);
o<<i;
o<<tmp;
}
}

void select()
{
otl_stream i(50, // buffer size
"select f1 :#1<ulong>, f2 "
"from test_tab "
"where f1>=:f<ulong> "
" and f1<=:ff<ulong>",
db
 );
// create select stream

OTL_NUMERIC_TYPE_1 f1;
char f2[31];

i<<VAL1+8
<<VAL1+16;

while(!i.eof()){ // while not end-of-data
i>>f1>>f2;
cout<<"f1="<<f1<<", f2="<<f2<<endl;
}

}

void select2()
{
otl_stream i(50, // buffer size
"select f1 :#1<ldouble>, f2 "
"from test_tab "
"where f1>=:f<ldouble> "
" and f1<=:ff<ldouble>",
db
);
// create select stream

OTL_NUMERIC_TYPE_3 f1;
char f2[31];

i<<VAL3+8
<<VAL3+16;

while(!i.eof()){ // while not end-of-data
i>>f1>>f2;
cout<<"f1="<<f1<<", f2="<<f2<<endl;
}

}

int main()
{
otl_connect::otl_initialize(); // initialize database API environment
try{

db.rlogon("scott/tiger@mssql2008"); // connect to the database

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 DECIMAL(38), f2 varchar(30))"
); // create table

insert();
select();
select2();
}

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 the databaseS

return 0;

}

Output

f1=1234567898, f2=Name8
f1=1234567899, f2=Name9
f1=1234567900, f2=Name10
f1=1234567901, f2=Name11
f1=1234567902, f2=Name12
f1=1234567903, f2=Name13
f1=1234567904, f2=Name14
f1=1234567905, f2=Name15
f1=1234567906, f2=Name16
f1=1.23457e+009, f2=Name8
f1=1.23457e+009, f2=Name9
f1=1.23457e+009, f2=Name10
f1=1.23457e+009, f2=Name11
f1=1.23457e+009, f2=Name12
f1=1.23457e+009, f2=Name13
f1=1.23457e+009, f2=Name14
f1=1.23457e+009, f2=Name15
f1=1.23457e+009, f2=Name16

Examples Contents Go Home

Copyright © 1996-2017, 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.