YoucanusetheCorC++#pragmamapincdirectivewith the#includedirectivetoincludeexternal file descriptionsinyourprogram.
WhenusedwithSQL,onlya particularformatofthe#pragmamapinc directiveisrecognizedbytheSQL precompiler.Ifalloftherequiredelementsarenotspecified,theprecompilerignoresthedirectiveand doesnotgeneratehostvariable structures.Therequiredelementsare:
v Includename
v Externallydescribedfilename
v Formatnameor alistofformatnames
v Options
v Conversionoptions
Thelibraryname, unionname,conversionoptions,andprefixnameareoptional.Althoughtypedef statementscodedbytheuser arenotrecognizedbytheprecompiler,those createdbythe#pragma mapincand#includedirectives arerecognized.SQLsupportsinput,output,both,and keyvaluesforthe optionsparameter.For theconversionoptions,thesupportedvaluesareD,p,z,_P,and 1BYTE_CHAR. Theseoptionsmaybespecified inanyorderexceptthatbothDand pcannotbe specified.Unions declaredusingthetypedefunioncreatedbythe#pragmamapincand #includedirectivecannotbeused ashostvariablesin SQLstatements;themembers oftheunions canbeused.Structures thatcontainthe typedefstructure cannotbe usedin SQLstatements;thestructure declaredusingthetypedefcanbe used. Toretrievethedefinitionofthesampletable DEPARTMENTdescribedinDB2 UDBforiSeriessample tablesintheDB2UDB foriSeriesSQLProgrammingtopiccollection, youcancodethefollowing:
#pragma mapinc ("dept","CORPDATA/DEPARTMENT(*ALL)","both") #include "dept"
CORPDATA_DEPARTMENT_DEPARTMENT_both_t Dept_Structure;
AhoststructurenamedDept_Structureisdefinedwith thefollowingelements:DEPTNO,DEPTNAME, MGRNO,andADMRDEPT.Thesefieldnamescanbe usedashostvariablesinSQLstatements.
Note: DATE,TIME, andTIMESTAMPcolumnsgeneratecharacterhostvariable definitions.Theyare
treated bySQLwith thesamecomparisonandassignmentrulesasa DATE,TIME,and
TIMESTAMPcolumn.Forexample,a datehost variablecanbecompared onlyagainstaDATE columnora characterstringthatisavalidrepresentationof adate.
IftheGRAPHICorVARGRAPHICcolumnhasaUCS-2CCSID,thegeneratedhostvariable will havetheUCS-2CCSIDassignedtoit.IftheGRAPHICorVARGRAPHICcolumnhasaUTF-16 CCSID, thegeneratedhostvariable willhavetheUTF-16CCSIDassignedtoit.
Althoughzoned,binary(with nonzeroscalefields),and,optionally,decimalare mappedto characterfieldsinILE C,SQLwilltreatthesefieldsasnumeric.By usingtheextendedprogram model(EPM) routines,youcanmanipulatethesefieldstoconvertzonedandpackeddecimaldata. For moreinformation,seetheILEC/C++ LanguageReference
topic.
Determining
equivalent
SQL
and
C
or
C++
data
types
TheprecompilerdeterminesthebaseSQLTYPEand SQLLENofhostvariablesbased onthetable.Ifa hostvariableappears withanindicatorvariable,theSQLTYPEisthebase SQLTYPEplusone.
Table1.CorC++declarationsmappedtotypicalSQLdatatypes
CorC++datatype SQLTYPEofhostvariable SQLLENofhostvariable SQLdatatype
shortint 500 2 SMALLINT
longint 496 4 INTEGER
longlongint 492 8 BIGINT
decimal(p,s) 484 pinbyte1,sinbyte2 DECIMAL(p,s)
float 480 4 FLOAT(singleprecision)
double 480 8 FLOAT(doubleprecision)
single-characterform 452 1 CHAR(1)
NUL-terminatedcharacter form
460 length VARCHAR(length-1)
VARCHARstructuredform 448 length VARCHAR(length)
single-graphicform 468 1 GRAPHIC(1)
NUL-terminated single-graphicform
400 length VARGRAPHIC(length-1)
VARGRAPHICstructured form
464 length VARGRAPHIC(length)
YoucanusethefollowingtabletodeterminetheCorC++datatypethatisequivalenttoagivenSQL datatype.
Table2.SQLdatatypesmappedtotypicalCorC++declarations
SQLdatatype CorC++datatype Notes
SMALLINT shortint
INTEGER longint
BIGINT longlongint
DECIMAL(p,s) decimal(p,s) pisapositiveintegerfrom1to63,
andsisapositiveintegerfrom0to 63.
NUMERIC(p,s)ornonzeroscale binary
Noexactequivalent UseDECIMAL(p,s). FLOAT(singleprecision) float
FLOAT(doubleprecision) double
CHAR(1) single-characterform
CHAR(n) Noexactequivalent Ifn>1,useNUL-terminatedcharacter form.
VARCHAR(n) NUL-terminatedcharacterform Allowatleastn+1toaccommodate theNUL-terminator.Ifdatacan containcharacterNULs(\0),use VARCHARstructuredformorSQL VARCHAR.
nisapositiveinteger.Themaximum valueofnis32740.
VARCHARstructuredform Themaximumvalueofnis32740. TheSQLVARCHARformmayalso beused.
Table2.SQLdatatypesmappedtotypicalCorC++declarations (continued)
SQLdatatype CorC++datatype Notes
CLOB None UseSQLTYPEIStodeclareaCLOB
inCorC++.
GRAPHIC(1) single-graphicform
GRAPHIC(n) Noexactequivalent
VARGRAPHIC(n) NUL-terminatedgraphicform Ifn>1,useNUL-terminatedgraphic form.
VARGRAPHICstructuredform IfdatacancontaingraphicNUL values(/0/0),useVARGRAPHIC structuredform.Allowatleastn+1 toaccommodatetheNUL-terminator.
nisapositiveinteger.Themaximum valueofnis16370.
DBCLOB None UseSQLTYPEIStodeclarea
DBCLOBinCorC++.
BINARY None UseSQLTYPEIStodeclarea
BINARYinCorC++.
VARBINARY None UseSQLTYPEIStodeclarea
VARBINARYinCorC++.
BLOB None UseSQLTYPEIStodeclareaBLOB
inCorC++.
DATE NUL-terminatedcharacterform Iftheformatis*USA,*ISO,*JIS,or *EUR,allowatleast11charactersto accommodatetheNUL-terminator.If theformatis*MDY,*YMD,or*DMY, allowatleast9charactersto
accommodatetheNUL-terminator.If theformatis*JUL,allowatleast7 characterstoaccommodatethe NUL-terminator.
VARCHARstructuredform Iftheformatis*USA,*ISO,*JIS,or *EUR,allowatleast10characters.If theformatis*MDY,*YMD,or*DMY, allowatleast8characters.Ifthe formatis*JUL,allowatleast6 characters.
TIME NUL-terminatedcharacterform Allowatleast7characters(9to includeseconds)toaccommodatethe NUL-terminator.
VARCHARstructuredform Allowatleast6characters;8to includeseconds.
Table2.SQLdatatypesmappedtotypicalCorC++declarations (continued)
SQLdatatype CorC++datatype Notes
TIMESTAMP NUL-terminatedcharacterform Allowatleast20characters(27to includemicrosecondsatfull precision)toaccommodatethe NUL-terminator.Ifnislessthan27, truncationoccursonthe
microsecondspart.
VARCHARstructuredform Allowatleast19characters.To includemicrosecondsatfullprecision, allow26characters.Ifthenumberof charactersislessthan26,truncation occursonthemicrosecondspart.
DATALINK Notsupported
ROWID None UseSQLTYPEIStodeclarea
ROWIDinCorC++.