odb--an ORM mapping framework based on C + + (using)

Source: Internet
Author: User
Tags sqlite

Summary: 2, use first, you need to define an object that corresponds to the database field: [CCE lang= "CPP] #ifndef volume_h #define VOLUME_H #include #include #pragma db object cl

2. Use
First, you need to define an object that corresponds to the database field:
[CCE lang= "CPP"]
#ifndef Volume_h
#define Volume_h

#include <string>
#include <odb/core.hxx>

#pragma db object
Class Volume
{
Public
Volume (const std::string &name, const std::string &location, const std::string &cover_path, int trackcount)
: _name (name), _location (location), _cover_path (Cover_path), _trackercount (Trackcount)
{}

unsigned long long ID () {return _id;}
void ID (unsigned long long i) {_id = i;}

Const std::string &name () {return _name;}
void name (const std::string &n) {_name = n;}

Const std::string &location () {return _location;}
void location (const std::string &l) {_location = l;}

Const std::string &cover_path () {return _cover_path;}
void Cover_path (const std::string &c) {_cover_path = C;}

int Trackcount () {return _trackercount;}
void Trackcount (int c) {_trackercount = C;}

Private
Friend class Odb::access;
Volume () {}

#pragma db ID Auto
unsigned long long _id;
std::string _name;
Std::string _location;
Std::string _cover_path;
int _trackercount;
};
[/cce]
The first is to introduce the CORE.HXX header file, which contains the access class. Add the #pragma DB object macro above the class to identify that this is a database object. On the primary key, add the macro #pragma DB ID auto, which identifies this as the primary key and is incremented. Both macros provide information to ODB, which is used to generate the final C + + code. Because the database correspondence fields are private types, you need to declare odb::access as friends.

For convenience, the connection database here uses SQLite, so you need to introduce SQLite-related packages. Create a database connection (for SQLite, open the database file):
[CCE lang= "CPP"]
Std::shared_ptr<odb::d atabase> sqlitedb (new odb::sqlite::d atabase ("mycppweb.db", Sqlite_open_readwrite | Sqlite_open_create));
[/cce]
Note: You need to introduce the header file Odb/sqlite/database.hxx, the first parameter to create a database connection (SQLite only) is the database file name, followed by the open flag, here is the main hint if the database file does not exist, it is created by default. Additionally, the c++11 shared_ptr,g++ is used here to add parameters-std=c++0x

Insert object:
[CCE lang= "CPP"]
{
Odb::transaction T (Db->begin ());
Volumeid = db->persist (volume);
T.commit ();
}
[/cce]
When inserting, the transaction is used, the ODB/TRANSACTION.HXX header file needs to be introduced, and the life cycle of the transaction is minimized.

Generate the corresponding code with the ODB command:
[CCE lang= "bash"]
Odb–database sqlite \
–hxx-suffix. Hpp–ixx-suffix. ipp–cxx-suffix. cpp \
–OUTPUT-DIR/TMP \
–generate-query–generate-schema–schema-format Embedded Volume.h
[/cce]
The database specified here is SQLite, and the statement that created the schema is embedded in the code.
After execution, three files of Volume-odb.hpp, Volume-odb.cpp, and Volume-odb.ipp are generated. Looking at Volume-odb.cpp, you will find that it contains the relationships between the classes specified in the Volume.h and the database tables. If you want to create a database through code (it seems like you need to determine if you already exist, the second run will re-create the table, causing data loss), you can:
[CCE lang= "CPP"]
{
Odb::transaction T (Sqlitedb->begin ());
Odb::schema_catalog::create_schema (*sqlitedb);
T.commit ();
}
[/cce]

ODB's query, not yet tried, specific documents in http://www.codesynthesis.com/products/odb/doc/manual.xhtml

odb--an ORM mapping framework based on C + + (using)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.