/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
**
** The following sample of source code ("Sample") is owned by International
** Business Machines Corporation or one of its subsidiaries ("IBM") and is
** copyrighted and licensed, not sold. You may use, copy, modify, and
** distribute the Sample in any form without payment to IBM, for the purpose of
** assisting you in the development of your applications.
**
** The Sample code is provided to you on an "AS IS" basis, without warranty of
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
** not allow for the exclusion or limitation of implied warranties, so the above
** limitations or exclusions may not apply to you. IBM shall not be liable for
** any damages you suffer as a result of using, copying, modifying or
** distributing the Sample, even if IBM has been advised of the possibility of
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: tbinfo.sqC
**
** SAMPLE: How to get information at the table level
**
** SQL STATEMENTS USED:
** SELECT
** DECLARE CURSOR
** OPEN
** FETCH
** CLOSE
**
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing C++ applications, see the Application
** Development Guide.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 Information Center at
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
(DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
#include <iomanip>
#include <iostream>
using namespace std;
#else
#include <iomanip.h>
#include <iostream.h>
#endif
EXEC SQL BEGIN DECLARE SECTION;
char tableName[128];
char schemaName[128];
char columnName[20];
char dataType[14];
sqlint32 dataLength;
short dataScale;
EXEC SQL END DECLARE SECTION;
class TbInfo
{
public:
int TbSchemaNameGet();
int TbColumnInfoGet();
};
int TbInfo::TbSchemaNameGet()
{
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENT:" << endl;
cout << " SELECT INTO" << endl;
cout << "TO GET A TABLE SCHEMA NAME." << endl;
// get the table schema name
cout << "\n Execute the statement" << endl;
cout << " SELECT tabschema INTO :schemaName" << endl;
cout << " FROM syscat.tables" << endl;
cout << " WHERE tabname = :tableName" << endl;
cout << " for" << endl;
cout << " tableName = '" << tableName << "'." << endl;
EXEC SQL SELECT tabschema INTO :schemaName
FROM syscat.tables
WHERE tabname = :tableName;
EMB_SQL_CHECK("Table schema name -- Get");
// get rid of spaces from the end of schemaName
strtok(schemaName, " ");
cout << "\n Table schema name is: " << schemaName << endl;
return 0;
} //TbInfo::TbSchemaNameGet
int TbInfo::TbColumnInfoGet()
{
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENTS:" << endl;
cout << " DECLARE CURSOR" << endl;
cout << " OPEN" << endl;
cout << " FETCH" << endl;
cout << " CLOSE" << endl;
cout << "TO GET TABLE COLUMN INFO." << endl;
// get info for table columns
cout << "\n Get info for '" << schemaName << "."
<< tableName << "' table columns:\n" << endl;
cout << " column name data type data size" << endl;
cout << " -------------------- -------------- ----------" << endl;
EXEC SQL DECLARE c1 CURSOR FOR
SELECT colname, typename, length, scale
FROM syscat.columns
WHERE tabschema = :schemaName AND tabname = :tableName;
EXEC SQL OPEN c1;
EMB_SQL_CHECK("Cursor -- Open");
EXEC SQL FETCH c1 INTO :columnName, :dataType, :dataLength, :dataScale;
EMB_SQL_CHECK("Cursor -- Fetch");
if (sqlca.sqlcode == 100)
{
cout << "\n Data not found." << endl;
}
cout.setf(ios::left, ios::adjustfield);
while (sqlca.sqlcode != 100)
{
cout << " " << setw(20) << columnName << " "
<< setw(14) << dataType << " " << dataLength;
if (dataScale != 0)
{
cout << "," << dataScale << endl;
}
else
{
cout << endl;
}
EXEC SQL FETCH c1 INTO :columnName, :dataType, :dataLength, :dataScale;
EMB_SQL_CHECK("Cursor -- Fetch");
}
EXEC SQL CLOSE c1;
EMB_SQL_CHECK("Cursor -- Close");
return 0;
} //TbInfo::TbColumnInfoGet
int main(int argc, char *argv[])
{
int rc = 0;
CmdLineArgs check;
TbInfo info;
DbEmb db;
// check the command line arguments
rc = check.CmdLineArgsCheck1(argc, argv, db);
if (rc != 0)
{
return rc;
}
cout << "\nTHIS SAMPLE SHOWS ";
cout << "HOW TO GET INFORMATION AT THE TABLE LEVEL." << endl;
// connect to the database
rc = db.Connect();
if (rc != 0)
{
return rc;
}
strcpy(tableName, "STAFF");
rc = info.TbSchemaNameGet();
rc = info.TbColumnInfoGet();
// disconnect from the database
rc = db.Disconnect();
if (rc != 0)
{
return rc;
}
return 0;
} //main