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
As next step, you may wish to have a have a look into the select.cc examples.
00001 #include <iostream> 00002 #include "ResourcePool/SQL/ResourcePool.hh" 00003 /* define the resource type to use */ 00004 typedef fatalmind::ResourcePool<fatalmind::ResourceType::SQL> RP; 00005 00006 int main(int argc, char* argv[]) { 00007 try { 00008 if (argc < 3) { /* just some minimalistic checking */ 00009 throw fatalmind::Exception("arguments missing: driver:logon SQL"); 00010 } 00011 RP::Factory f(argv[1]); /* pass the arguments to the factory */ 00012 RP p(f); /* create a resource pool */ 00013 00014 RP::Commands::SQLSelectRow select(p, argv[2]); 00015 std::string result; /* prepare the command object to be executed */ 00016 select.bindout(0, result); /* provide the place were we expect the result */ 00017 00018 p.execute(select); /* execute the SQL */ 00019 00020 std::cout << "Result: " << result << std::endl; /* print the result */ 00021 } catch (fatalmind::Exception e) { /* if you provide missing/wrong data */ 00022 std::cout << "An error happend: " << e.message() << std::endl; 00023 } 00024 }