Listing 1 Database programming

#include <ospace/std/iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
#include <ospace/database.h>


/**
 * Usage {program} [dbserver_hostname [dsn [user [auth]]]]
 * @param argc - Command line argument count
 * @param argv - Command line arguments
 */
int main(int argc, char** argv)
  {

  /*
   * Library initialization
   */
  os_init_network_toolkit();   // Initialize dependency
  os_init_streaming_toolkit(); // Initialize dependency
  os_init_database_toolkit();  // Initialize Database<ToolKit>

  /*
   * Connecting to the Database
   *
   * Establishing a database connection from the Database<Toolkit>
   * Client consists of connecting to the Database<Toolkit> Server 
   * machine, identifying a Data Source Name (DSN) that an
   * administrator has configured within the remote machine's ODBC
   * environment, and providing any necessary authentication
   * information
   *
   */

  os_string target_hostname;
  os_string dsn;      // dsn name
  os_string user;     // user name
  os_string auth;     // auth info

  // Use local host if none specified on command line
  (argc > 1) ? target_hostname = argv[1] : target_hostname = os_host::my_host().name();
  (argc > 2) ? dsn  = argv[2] : dsn  = "dbtoolkit";
  (argc > 3) ? user = argv[3] : user = "guest";
  (argc > 4) ? auth = argv[4] : auth = "";

  try
    {
    os_ip_address iaddr( target_hostname );
    os_db_connection connection( os_socket_address(iaddr, 3006),
                                 dsn,
                                 user,
                                 auth );


    /*
     * Executing SQL Statements
     *
     * 1. Construct an os_db_statement object for an SQL statement
     * 2. Attach the os_db_statement object to an existing database
     *    connection
     * 3. Execute the statement
     */

    os_db_statement stmt( "SELECT * FROM Supplier" );

    stmt.attach( connection );
    stmt.execute();

    const int NAME_LEN = 50+1;
    const int PHONE_LEN = 20+1;
    char supplier_name[NAME_LEN], supplier_phone[PHONE_LEN];
    unsigned long supplier_id;

    /*
     * Binding data buffers with bind() before calling fetch()
     * when a successful call to fetch() is made the data
     * will automatically be placed in all of the bound buffers
     *
     */

    stmt.bind(1, supplier_id);
    stmt.bind(2, (char *)supplier_name, sizeof(supplier_name));
    stmt.bind(9, (char *)supplier_phone, sizeof(supplier_phone));

    while (stmt.fetch())    // Get each row
      {
      os_stringstream oss;
      oss << supplier_id
          << ":"
          << supplier_name
          << ":"
          << supplier_phone;
      cout << oss.str() << endl;
      }
    }
 catch ( os_db_error& error )
    {
    cout << "Caught database error:" << endl;
    cout << "\t" << error.description( error.get_code() ) << endl;
    cout << "\t" << error.what() << endl;
    cout << "Native Error Code: " << error.get_native() << endl;
    }
 catch ( os_streaming_toolkit_error& error )
    {
    cout << "Caught streaming error:" << endl;
    cout << "\t" << error.description( error.code() ) << endl;
    cout << "\t" << error.what() << endl;
    cout << "Native Error Code: " << error.native() << endl;
    }
  catch ( os_network_toolkit_error& error )
    {
    cout << "Caught network toolkit error:" << endl;
    cout << "\t" << error.description( error.code() ) << endl;
    cout << "\t" << error.what() << endl;
    cout << "Native Error Code: " << error.native() << endl;
    }

  return 0;
  }

Listing 2 An alternative way of making a database connection

// ...

/*
 * Instead of using
 * os_db_connection connection1( os_socket_address( iaddr, 3006),    // target machine, port 3006
 *                                 dsn,          // ODBC Data Source Name
 *                                 user,         // user
 *                                auth );       // authentication
 * use a driver specific connection attribute
 *
 */
os_string connect_info =
      "DSN=" + dsn +
      ";UID=" + user +
      ";PWD=" + auth +
      ";";

os_db_connection connection2( os_socket_address(iaddr, 3006),
                                  connect_info );

// ...

Listing 3 Retrieve env_handle and dbc_handle

// ...

    //
    // Build a connection to the database server, delaying DSN connection.
    //
    os_ip_address iaddr( target_hostname );
    os_db_connection connect( os_socket_address( iaddr, 3006) );

    OSSQLHANDLE env_handle = connect.get_env_handle();
    //
    // Can perform raw ODBC API calls on this env_handle now.
    // No dbc_handles exist yet for this env_handle.
    //

    // ...

    OSSQLHANDLE dbc_handle = connect.get_dbc_handle();
    //
    // Can perform raw ODBC API calls on this dbc_handle now.
    // No DSN connection exists.
    //

    // ...

    // Make the DSN connection
    os_string connect_info =
      "DSN=" + dsn +
      ";UID=" + user +
      ";PWD=" + auth +
      ";";

    if (connect.dsn_connect(connect_info))
      {
      cout << "connect succeeded" << endl;
      }
  
// ...