selectrow.cc

This naive example performs a select statement on an SQL database and prints the returned value (one column only). The example shows the so called 'Runtime selectable' database driver. This allows the user to specify the database type to be used at runtime very much like with java JDBC, perl DBI or similar.

The program starts with some includes and typedefs. Those two lines are required to specify which resource type to use. The type fatalmind::ResourceType::SQL is the typename of the Runtime selectable SQL database driver.

After checking if there were enough arguments passed, the Factory is created. Please note that using RP::Factory makes sure that you don't need to adopt you source code if you use another PoolType.

Next, the ResourcePool itself is created by just passing the factory object. Again we are using the RP typedef to avoid portability issues.

The next block prepares the command to be executed. We want to select a single row, therefore we are using the SQLSelectRow command. The first argument to this command is the ResourcePool you plan to issue the command against, the second argument to this command is the actual SQL string (second argument to the program). Then we create a string object which will be used to store the result. In the end we tell the command to put the output of the first column into the result variable.

Finally we execute the command within the ResourcePool. Actually everything is happening within this line. First the ResourcePool will notice that there is no free resource available and create one using the provided Factory. After this there will be an open connection to the database. If this succeeds the ResourcePool locks this resource and passes it to the Commands execute method. The command will then to the required work to execute this select (for oracle this means the a lot of handles needs to be opened, the statement needs to be prepared, there must be memory provided to fetch the result to, ... but all of this is transparent to the user). After the command has done it's work the resource will be freed again (so the open connection remains in the ResourcePool for later use) and the execution returns to the caller. Everything left to do is to print the result.

In the very end, the destructor of the ResourcePool will close the open database connection.

In the very end, we have the error handler to print the error message if there was an exception.

You can compile this example with the following command:

 c++ -I../ -L../.libs -lResourcePool -lrt -o selectrow selectrow.cc 
in the examples directory of the distribution (after building the library).

As next step, you may wish to have a have a look into the select.cc examples.

#include <iostream>
#include "ResourcePool/SQL/ResourcePool.hh"
                                          /* define the resource type to use */
typedef fatalmind::ResourcePool<fatalmind::ResourceType::SQL> RP;

int main(int argc, char* argv[]) {
    try {
        if (argc < 3) {                   /* just some minimalistic checking */
            throw fatalmind::Exception("arguments missing: driver:logon SQL");
        }
        RP::Factory f(argv[1]);           /* pass the arguments to the factory */
        RP p(f);                          /* create a resource pool */
    
        RP::Command::SQLSelectRow select(p, argv[2]);
        std::string result;               /* prepare the command object to be executed */
        select.bindout(0, result);        /* provide the place were we expect the result */
    
        p.execute(select);                /* execute the SQL */
    
        std::cout << "Result: " << result << std::endl; /* print the result */
    } catch (fatalmind::Exception e) {    /* if you provide missing/wrong data */
        std::cout << "An error happend: " << e.message() << std::endl;
    }
}

Generated on Mon Jun 9 11:27:00 2008 for ResourcePool by  doxygen 1.5.5