Object-oriented Database Access in C++
The C APIs work great for procedural C development. They do not, however, fit into the object-oriented world of C++ all that well. In order to demonstrate how these two APIs work in real code, we will spend the rest of the chapter using them to create a C++ API for object-oriented database development.
Because we are trying to illustrate MySQL and mSQL database access, we will focus on issues specific to MySQL and mSQL and not try to create the perfect general C++ API. In the MySQL and mSQL world, there are three basic concepts: the connection, the result set, and the rows in the result set. We will use these concepts as the core of the object model on which our library will be based. Figure 13.1 shows these objects in a UML diagram.[20]
Figure 13-1. Object-oriented database access library
The Database Connection
Database access in any environment starts with the connection. As you
saw in the first two examples, MySQL and mSQL have two different ways
of representing the same concept—a connection to the database.
We will start our object-oriented library by abstracting on that
concept and creating a
Connection
object. A Connection
object should be able to establish a connection to the server, select
the appropriate database, send queries, and return results. Example 13.3 is the header file that declares the interface
for the Connection
object.
Get MySQL and mSQL now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.