c++occi

Exporting an oracle table dynamically to a flat file


I am trying to build a C++ program using occi libraries that will take a select statement or a table name as an input and turn it into a delimited file. However, looking at the documentation, I can't find a way to export all columns of a query result into a file. Almost all examples I found were along the following lines

string query = "SELECT col1 FROM table1";

stmt = con->createStatement(query);
res = stmt->executeQuery();

while (res->next())
{
    outfile<<res->getInt(1)<<endl;
}

What i want to do is: Do a select * and then export the full row to the file in one go without specifying the type for each column, but I haven't been able to find something that does this.

I know that row-by-row exports are not really efficient for large sets, but I want to make this work before optimizing it.

Does anyone have any ideas around how to do this efficiently?


Solution

  • I found that there is no way to do this without iterating over the metadata object at least once. Since I only need to do this once per query execution I ended up writing the attribute types and column positions to a map and using that map within the result set loop to read data. Here's the code I used:

        res = stmt->executeQuery();
        vector<oracle::occi::MetaData> meta = res->getColumnListMetaData();
        map<int, int> mapper;
    
        for (int i=0; i < meta.size(); i++) {
                mapper[i] = meta[i].getInt(oracle::occi::MetaData::ATTR_DATA_TYPE);
        }