[odb-users] About Use of C++ ORM ODB
zhigang lee
micrsosft at live.com
Sat Dec 10 11:55:24 EST 2016
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
From: zhigang lee<mailto:micrsosft at live.com>
Sent: Sunday, December 11, 2016 00:43
To: announcements at codesynthesis.com<mailto:announcements at codesynthesis.com>
Subject: About Use of C++ ORM ODB
Excuse me!
Having problems using the ODB.Problem may be small, but I do not know how to solve.
No.1:
Multiple dynamic libraries using ODB. Correspond to different database engine create dll module.
Example: in odb_mssqlhelper.dll dealing with all the odb code for mssql.
in odb_mysqlhelper.dll dealing with all the odb code for mysql.
………
These modules use the boost date and UUID
Compile odb_mysqlhelper.dll and odb_mssqlhelper.dll now all goes well
No.2:
In order to switch the database engine at run time, we created odb_databasehelper.dll. According to configured using different dbhelper class. But odb_databasehelper.dll at compile-time error occurs.
No3:
Error details and code examples
Example odb header code:
// file : System_Country.hxx
// copyright : not copyrighted - public domain
#ifndef LNSF_SYSDB_COUNTRY_OBJ_MSSQL_HXX
#define LNSF_SYSDB_COUNTRY_OBJ_MSSQL_HXX
#include "stdafx.h"
//#include <guiddef.h>
#include <string>
#include <cstddef> // std::size_t
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/unordered_set.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <odb/core.hxx>
#include <odb/boost/lazy-ptr.hxx>
using boost::shared_ptr;
using boost::weak_ptr;
using odb::boost::lazy_shared_ptr;
using odb::boost::lazy_weak_ptr;
using boost::uuids::uuid;
using boost::gregorian::date;
#include <odb/core.hxx>
#include "Lnsf_MssqlDbHelper_Config.hpp"
#pragma db object
namespace LNSF_DBHELP_MSSQL
{
class LNSF_MSSQLDBHELPER_API Lnsf_System_Country
{
public:
Lnsf_System_Country(){}
private:
friend class odb::access;
#pragma db id
uuid Cid;
uuid Pid;
std::wstring SystemName;
std::wstring LocalName;
unsigned int AreaCode;
time_t Founding;
date Foundtime;
#pragma db type("NVARCHAR(1024)") null
std::wstring Flag;
#pragma db type("NVARCHAR(512)") null
std::wstring Capital;
#pragma db type("NVARCHAR(1024)") null
std::wstring Description;
public:
uuid GetCid(){ return Cid; }
void SetCid(uuid gCid){ Cid = gCid; }
uuid GetPid(){ return Pid; }
void SetPid(uuid gPid){ Pid = gPid; }
std::wstring GetSystemName(){ return SystemName; }
void SetSystemName(const std::wstring&strSystemName){ SystemName = strSystemName; }
std::wstring GetLocalName(){ return LocalName; }
void SetLocalName(const std::wstring&strLocalName){ LocalName = strLocalName; }
unsigned int GetAreaCode(){ return AreaCode; }
void SetAreaCode(unsigned int nAreaCode){ AreaCode = nAreaCode; }
time_t GetFounding(){ return Founding; }
void SetFounding(time_t tFounding){ Founding = tFounding; }
date GetFoundTime(){ return Foundtime; }
void SetFoundTime(date dTime){ Foundtime = dTime; }
std::wstring GetFlag(){ return Flag; }
void SetFlag(const std::wstring&strFlag){ Flag = strFlag; }
std::wstring GetCapital(){ return Capital; }
void SetCapital(const std::wstring&strCapital){ Capital = strCapital; }
std::wstring GetDescription(){ return Description; }
void SetDescription(const std::wstring&strDescription){ Description = strDescription; }
};
}
#endif
This header file custom build tool command line: odb.exe --std c++11 --database mssql --profile boost --generate-query --generate-schema --generate-session --table-prefix boost_ System_Country.hxx
This header files include all odb code build success.
Inermediate module header code:
Config header files
/*
*Lnsf Databse Helper Config Header Files
*@Micrsosft at live.com
*2016-4-4
*/
#ifndef __LNSF_DBHELP_CONFIG__
#define __LNSF_DBHELP_CONFIG__
#include <string>
#include <memory> // std::auto_ptr
#include <cstdlib> // std::exit
#include <iostream>
#include <cstddef> // std::size_t
#include <stdio.h>
#include <string>
#include <odb/database.hxx>
#include <odb/boost/exception.hxx>
//import Database Helper Library
#pragma comment(lib, "Lnsf_MssqlDbHelper.lib")
#pragma comment(lib, "Lnsf_MysqlDbHelper.lib")
#pragma comment(lib, "Lnsf_OracleDbHelper.lib")
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/date.hpp>
#include <boost/date_time/special_defs.hpp>
#include <boost/date_time/gregorian/greg_calendar.hpp>
#include <boost/date_time/gregorian/greg_duration.hpp>
#include <boost/date_time/gregorian/conversion.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian/parsers.hpp>
using namespace boost;
using boost::uuids::uuid;
#include <boost/weak_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/unordered_set.hpp>
#include <odb/core.hxx>
#include <odb/boost/lazy-ptr.hxx>
using boost::shared_ptr;
using boost::weak_ptr;
using odb::boost::lazy_shared_ptr;
using odb::boost::lazy_weak_ptr;
using boost::gregorian::date;
#include "Lnsf_DbConvertHelper.hpp"
static int DATA_ENVIRONMENT = LNSF::GetDatabaseEnvironment();
using namespace LNSF;
using namespace std;
using namespace odb::core;
#endif
Handle Header Files Code
#pragma once
#include <memory> // std::auto_ptr
#include <string>
#include <iostream>
#include <cstdlib> // std::exit
#include <vector>
#include <odb/database.hxx>
#include <odb/session.hxx>
#include <odb/transaction.hxx>
#include <odb/mssql/database.hxx>
#include <odb/oracle/database.hxx>
#include <odb/mysql/database.hxx>
#include <odb/boost/exception.hxx>
#include <odb/boost/uuid/mysql/uuid-mapping.hxx>
#include <odb/boost/uuid/mssql/uuid-mapping.hxx>
#include <odb/boost/date-time/exceptions.hxx>
#include <odb/boost/date-time/mssql/gregorian-mapping.hxx>
#include "Lnsf_DatabaseConfig.hpp"
#define LNSF_SYSTEM_DB L"Lnsf_System"
#define LNSF_USEBAS_DB L"Lnsf_Userbase"
#define LNSF_RELATE_DB L"Lnsf_Relate"
#define LNSF_ENTERP_DB L"Lnsf_Enterprise"
using namespace boost::gregorian;
using namespace odb::core;
namespace LNSF_DBHELP
{
template <class T> class System_DataManager
{
public:
System_DataManager();
typedef odb::query<T> Query;
bool insert(T item, __int64 &nRowID);
bool insert(T item, GUID &gID);
bool insert(T item, uuid &uID);
bool insert(T item, std::string &strID);
~System_DataManager(){}
};
template <class T> System_DataManager<T>::System_DataManager()
{
}
template <class T> bool System_DataManager<T>::insert(T item, __int64 &nRowID)
{
auto_ptr<database> db(Connect_LnsfSystem());
try
{
transaction t(db->begin());
nRowID = db->persist(item);
t.commit();
}
catch (const odb::exception& e)
{
cerr << e.what() << endl;
AddinException(LNSF_SYSTEM_DB, e.what());
return false;
}
return true;
}
template <class T> bool System_DataManager<T>::insert(T item, GUID &gID)
{
auto_ptr<database> db(Connect_LnsfSystem());
try
{
transaction t(db->begin());
gID = db->persist(item);
t.commit();
}
catch (const odb::exception& e)
{
cerr << e.what() << endl;
//如果插入失败,将失败信息写入日志
AddinException(LNSF_SYSTEM_DB,e.what());
return false;
}
return true;
}
template <class T> bool System_DataManager<T>::insert(T item, uuid &uID)
{
auto_ptr<database> db(Connect_LnsfSystem());
try
{
transaction t(db->begin());
uID = db->persist(item);
t.commit();
}
catch (const odb::exception& e)
{
cerr << e.what() << endl;
//如果插入失败,将失败信息写入日志
AddinException(LNSF_SYSTEM_DB, e.what());
return false;
}
return true;
}
template <class T> bool System_DataManager<T>::insert(T item, std::string &strID)
{
auto_ptr<database> db(Connect_LnsfSystem());
try
{
transaction t(db->begin());
strID = db->persist(item);
t.commit();
}
catch (const odb::exception& e)
{
//transaction t(db->begin());
cerr << e.what() << endl;
AddinException(LNSF_SYSTEM_DB, e.what());
//t.rollback();
return false;
}
return true;
}
Template class and query define header files code
#ifndef __LNSF_DBSYSTEM_TEMPLATE__
#define __LNSF_DBSYSTEM_TEMPLATE__
#include "Lnsf_DatabaseConfig.hpp"
#include "Lnsf_DatabaseHandle.hpp"
#include <System_Country.hxx>
#include <System_Country-odb.hxx>
typedef odb::query<LNSF_DBHELP_MSSQL::Lnsf_System_Country> countryQuery;
typedef std::vector<LNSF_DBHELP_MSSQL::Lnsf_System_Country>vCountryList;
class CDb_System_Country_Handle :public LNSF_DBHELP::System_DataManager<LNSF_DBHELP_MSSQL::Lnsf_System_Country>
{
public:
CDb_System_Country_Handle(){}
~CDb_System_Country_Handle(){};
};
Database table operate class header file code
#pragma once
#include "Lnsf_DatabaseFamily.hpp"
namespace LNSF_DBHELP
{
class CLnsf_SystemCountry_Handle
{
public:
CLnsf_SystemCountry_Handle();
~CLnsf_SystemCountry_Handle();
public:
bool System_Country_Add(LNSF_SYSDB_COUNTRY tagCountry);
bool System_Country_BatchAdd(vDataCountryList vCountryList);
bool System_Country_Update(LNSF_SYSDB_COUNTRY tagCountry);
bool System_Country_CheckExist(LNSF_SYSDB_COUNTRY tagCountry);
bool System_Country_Find(GUID gID, LNSF_SYSDB_COUNTRY &tagCountry);
bool System_Country_Erase(GUID gID);
bool System_Country_GetByID(GUID gID, LNSF_SYSDB_COUNTRY &tagItems);
bool System_Country_QueryItem(int nCondition, LNSF_SYSDB_COUNTRY tagCountry, vDataCountryList& vQueryList); bool System_Country_Clear();
long long System_Country_Exec(std::string &strSql);
std::wstring GetFullCountryAddr(GUID gID, bool bSysname,bool bSpace);
};
No.4:Build error info:
Output info:
>------ Build started: Project: Lnsf_DatabaseHelper, Configuration: Debug x64 ------
1> stdafx.cpp
1> SystemSpace_Handle.cpp
1> SystemCountry_Handle.cpp
1>f:\livenetframework\lnsf_framework\lnsf_framework_source\lnsf_databasehelper\Lnsf_DatabaseHandle.hpp(524): error C2664: 'std::auto_ptr<T>::auto_ptr(std::auto_ptr_ref<_Ty>) throw()' : cannot convert argument 1 from 'boost::shared_ptr<LNSF_DBHELP_MSSQL::Lnsf_System_Country>' to 'LNSF_DBHELP_MSSQL::Lnsf_System_Country *'
1> with
1> [
1> T=LNSF_DBHELP_MSSQL::Lnsf_System_Country
1> , _Ty=LNSF_DBHELP_MSSQL::Lnsf_System_Country
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> f:\livenetframework\lnsf_framework\lnsf_framework_source\lnsf_databasehelper\Lnsf_DatabaseHandle.hpp(519) : while compiling class template member function 'LNSF_DBHELP_MSSQL::Lnsf_System_Country LNSF_DBHELP::System_DataManager<LNSF_DBHELP_MSSQL::Lnsf_System_Country>::GetById(boost::uuids::uuid)'
1> SystemCountry_Handle.cpp(384) : see reference to function template instantiation 'LNSF_DBHELP_MSSQL::Lnsf_System_Country LNSF_DBHELP::System_DataManager<LNSF_DBHELP_MSSQL::Lnsf_System_Country>::GetById(boost::uuids::uuid)' being compiled
1> f:\livenetframework\lnsf_framework\lnsf_framework_source\lnsf_databasehelper\Lnsf_TemplateSystem.hpp(511) : see reference to class template instantiation 'LNSF_DBHELP::System_DataManager<LNSF_DBHELP_MSSQL::Lnsf_System_Country>' being compiled
1>F:\LiveNetFramework\Lnsf_Framework\Lnsf_Include\Lnsf_DataBaseDrive\Odb_BaseSdks\odb/database.ixx(180): error C2440: 'initializing' : cannot convert from 'LNSF_DBHELP_MSSQL::Lnsf_System_Country *' to 'const object_pointer &'
1> Reason: cannot convert from 'LNSF_DBHELP_MSSQL::Lnsf_System_Country *' to 'const object_pointer'
1> Constructor for class 'boost::shared_ptr<LNSF_DBHELP_MSSQL::Lnsf_System_Country>' is declared 'explicit'
1> SystemCountry_Handle.cpp(107) : see reference to function template instantiation 'odb::access::object_traits<LNSF_DBHELP_MSSQL::Lnsf_System_Country>::id_type odb::database::persist<LNSF_DBHELP_MSSQL::Lnsf_System_Country>(T *)' being compiled
1> with
1> [
1> T=LNSF_DBHELP_MSSQL::Lnsf_System_Country
1> ]
Error list info:
1、Error 2 error C2440: 'initializing' : cannot convert from 'LNSF_DBHELP_MSSQL::Lnsf_System_Country *' to 'const object_pointer &' F:\LiveNetFramework\Lnsf_Framework\Lnsf_Include\Lnsf_DataBaseDrive\Odb_BaseSdks\odb\database.ixx 180
2、Error 1 error C2664: 'std::auto_ptr<T>::auto_ptr(std::auto_ptr_ref<_Ty>) throw()' : cannot convert argument 1 from 'boost::shared_ptr<LNSF_DBHELP_MSSQL::Lnsf_System_Country>' to 'LNSF_DBHELP_MSSQL::Lnsf_System_Country *' f:\livenetframework\lnsf_framework\lnsf_framework_source\lnsf_databasehelper\Lnsf_DatabaseHandle.hpp 524
If you have time please answer this question. Thank you so much
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
More information about the odb-users
mailing list