Using operation classes

The csql.cpp example is a small application that uses the ITQuery and ITConnection classes to provide a simple command-line interface that accepts SQL commands from the standard input, transmits the commands to the database, and the results are displayed.

The major steps of the program are as follows.
  1. Open the connection.

    Before any database interaction can take place, the connection with the database must be established. Opening the connection without any arguments instructs the interface to use the default system, database, user name, and password. For details about connection defaults, see The ITDBInfo class.

    ITConnection conn;
    conn.Open();
  2. Build an ITQuery object for the connection.
    ITQuery query(conn);

    A query object is used to issue database queries and to access result sets. An operation class is always created in the context of a server connection.

  3. Read lines of input from stdin by using the C++ iostream library methods.
    while (cin.getline(qtext, sizeof(qtext)))
    {
    }
  4. Execute the query read from stdin by using the ExecForIteration method of the query object.
    if (!query.ExecForIteration(qtext))
    {
    }
  5. Loop through the result rows of the query.
    ITRow *comp;
    int rowcount = 0;
    while ((comp = query.NextRow()) != NULL)
    {
    }

    A row is extracted from the result set of a query by using the NextRow method of the query object. The code shows the declaration of a pointer to the row interface for an object that receives the result data, and the loop that reads the result data into the row object.

    This is an example of the use of a value object in the program: The NextRow method returns a pointer to an ITRow interface. The pointer returned by NextRow is not a pointer to an actual object; it is a pointer to an interface that is exposed by an object.

  6. Print the row.
    cout << comp->Printable() << endl;

    Every value object exposing an ITValue or ITValue-derived interface supports the Printable method, which returns the object as a printable string in a constant ITString object. This object can be put directly on the stdout stream. For details about the ITString class, see The ITValue interface.

  7. Release the row.
    comp->Release();

    The value interface returned to the application must be explicitly released by the application. A value object tracks the number of outstanding references to it, and when the last reference is released, deletes itself.

  8. Close the connection.
    conn.Close();

Closing a connection deletes any saved data associated with the connection. Because a value object might hold a reference to this saved data, it must track whether the underlying data has been deletes. For details, see Value object management.