Zen :: Enterprise :: Database
The Zen Enterprise Database abstraction layer provides a great interface
Types used in the example
This code block shows the typedefs and member variables used for the database service and the database connection.
typedef Zen::Database::I_DatabaseManager::pDatabaseService_type pDatabaseService_type; typedef Zen::Database::I_DatabaseService::pDatabaseConnection_type pDatabaseConnection_type; pDatabaseService_type m_pDatabase; pDatabaseConnection_type m_pConnection;
Loading a Database Driver plugin
This code segment shows verifying that the database exists and then loads the SQLite database driver plugin.
Valid values are "sqlite", "mysql", and "postgresql".
Please note that if you use the mysql GPL driver, your code must be licensed through the GPL license. The ZLib license that ZOSS uses is compatible with the GPL license, but it will restrict the way you can distribute your code.
boost::filesystem::path dbPath = "database.sqlite"; if (!boost::filesystem::exists(dbPath)) { // TODO Create it. return false; } // The sqlite database driver doesn't take any parameters for config. Zen::Database::I_DatabaseManager::config_type config; m_pDatabase = Zen::Database::I_DatabaseManager::getSingleton().createDatabaseService("sqlite", config); if (!m_pDatabase.isValid()) { return false; }
Connecting to a Database
This next code segment creates a single database connection to an SQLite database.
Note that this example is for SQLite.
TODO: Document all of the parameters required for each database driver.
// Connect to the SQLite database. // We pass the database name using config. Each database driver // has its own set of parameters required. config["fileName"] = dbPath.string(); m_pConnection = m_pDatabase->connect("workbench", config); // Make sure the connection is valid if (!m_pConnection.isValid()) { return false; }
