ORM for C ORM Objectrelational mapping ORM ORM
ORM for C++
ORM • Object-relational mapping (ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in object-oriented programming languages. • This creates, in effect, a "virtual object database" that can be used from within the programming language.
Idea C++ My. SQL Automated by ORM A lot of code To communicate
Advantages Ease of use. ODB automatically generates database conversion code from your C++ classes and allows you to manipulate persistent objects using a simple, object-oriented database API. Concise code. With ODB hiding the details of the underlying database, the application logic is written using the natural object vocabulary making it simpler and thus easier to read and understand. Safety. The ODB object persistence and query APIs are statically typed. You use C++ identifiers instead of strings to refer to object members and the generated code makes sure database and C++ types are compatible. All this helps catch programming errors at compile-time rather than at runtime. Database portability. Because the database conversion code is automatically generated, it is easy to switch from one database vendor to another. Optimal performance. ODB has been designed for high performance and low memory overhead. All the available optimization techniques, such as prepared statements and extensive connection, statement, and buffer caching, are used to provide the most efficient implementation for each database operation. Persistent classes have zero per-object memory overhead. There are no hidden "database" members that each class must have nor are there per-object data structures allocated by ODB. Maintainability. Automatic code generation minimizes the effort needed to adapt the application to changes in persistent classes. The database conversion code is kept separately from the class declarations and application logic. This makes the application easier to debug and maintain.
ORM workflow 2. Code driven 1. Schema driven C++ code DB Schema C++ code 3. Database driven DB DB C++ code
C++ ORMs
ODB usage My. SQL
Persist objects Create DB Object Create your objects Just say persist
Query objects Query (search the objects): Simple load the objects:
Relationship ODB supports Relationship types: • Unidirectional Relationships – To-One Relationships – To-Many Relationships • Bidirectional Relationships – One-to-One Relationships – One-to-Many Relationships – Many-to-Many Relationships
Relationships example The ODB built-in support for: • • the TR 1 shared_ptr, weak_ptr, std: : auto_ptr raw pointers. Plus, ODB profile libraries, (Boost , Qt …) custom pointers (after some setup) My. SQL
- Slides: 12