OTL 4.0, Example 573 (OTL/ODBC, accessing the system data dictionary via ODBC functions, Sybase SQL Server)

This example demonstrates how to access the database system data dictionary via ODBC functions, OTL/ODBC, Sybase SQL Server. In theory, any ODBC driver that implements the necessary ODBC functions can be used with OTL/ODBC.

Source Code

#include <iostream>
#include <stdio.h>
using namespace std;

#define OTL_ODBC_UNIX // Compile OTL 4.0/ODBC
#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object


void select()

 otl_stream s;

 s.set_all_column_types(otl_all_num2str|otl_all_date2str); 
    // map all numeric and date/time output columns to strings for simplicity.

db.auto_commit_on(); // setting database connect auto-commit to ON

 s.open(100, // buffer size
         "$SQLColumns $1:'sybsql' $2:'scott' $3:'test_tab'",
         // get a list of all columns of table
// "test_tab" that belongs to schema "dbo",
// catalog "sybsql"
         db // connect object
        ); 

 otl_column_desc* desc;
 int desc_len;
 desc=s.describe_select(desc_len); 
    // describe the structure of the output columns of the result set.

 int rpc=0;
 while(!s.eof()){
   ++rpc;
   char str[512];
   cout<<"ROW#"<<rpc<<" ";
   for(int col_num=0;col_num<desc_len;++col_num){
     s>>str;
     if(s.is_null())
       cout<<desc[col_num].name<<"=NULL ";
     else
       cout<<desc[col_num].name<<"="<<str<<" ";
   }
   cout<<endl;
 }

}

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

  db.rlogon("scott/tigger@freetds_sybsql"); // connect to ODBC
// It's recommended to use auto_commit_off() when OTL is used
// with FreeTDS/ODBC against Sybase in order to set the DB
// session's auto-commit to OFF. otl_connect::rlogon()'s auto-commit
// parameter has not effect. auto_commit_off() is a workaround, until
// a fix becomes available.
db.auto_commit_off();

  select(); // query the system data dictionary via ODBC functions.

 }

 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.sqlstate<<endl; // print out SQLSTATE message
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from ODBC

 return 0;

}

Output

ROW#1 TABLE_CAT=sybsql TABLE_SCHEM=scott TABLE_NAME=test_tab COLUMN_NAME=f1 DATA_TYPE=4 TYPE_NAME=int COLUMN_SIZE=10 BUFFER_LENGTH=4 DECIMAL_DIGITS=0 NUM_PREC_RADIX=10 NULLABLE=1 REMARKS=NULL COLUMN_DEF=NULL SQL_DATA_TYPE=4 SQL_DATETIME_SUB=NULL CHAR_OCTET_LENGTH=0 ORDINAL_POSITION=1 IS_NULLABLE=YES 
ROW#2 TABLE_CAT=sybsql TABLE_SCHEM=scott TABLE_NAME=test_tab COLUMN_NAME=f2 DATA_TYPE=-1 TYPE_NAME=text COLUMN_SIZE=2147483647 BUFFER_LENGTH=16 DECIMAL_DIGITS=NULL NUM_PREC_RADIX=NULL NULLABLE=1 REMARKS=NULL COLUMN_DEF=NULL SQL_DATA_TYPE=-1 SQL_DATETIME_SUB=NULL CHAR_OCTET_LENGTH=2147483647 ORDINAL_POSITION=2 IS_NULLABLE=YES


Examples ContentsGo 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.