以前に書いた QxOrm の記事が(日本においてはおそらく)最初の解説記事だったらしく、ちょいちょい反応があった。
ネット上でも gitlove さんという方が、簡単なサンプルを書いてくれた。
クラスの定義や main 関数での QxOrm の操作はそこに書かれているコードほぼそのままでいいと思います。
ただ、(チュートリアルでもあまり強調されていないが)QxOrm を使う際のオマジナイみたいなもので、export.h というファイルが必要なようです。
target = test としたとき、まず .pro ファイルで _BUILDING_TEST を定義しておき、export.h で各種マクロを定義します。
具体的には以下の通り。
//export.h
#ifndef _QX_TEST_EXPORT_H_
#define _QX_TEST_EXPORT_H_
#ifdef _BUILDING_QX_TEST
#define QX_TEST_DLL_EXPORT QX_DLL_EXPORT_HELPER
#else
#define QX_TEST_DLL_EXPORT QX_DLL_IMPORT_HELPER
#endif
#ifdef _BUILDING_QX_TEST
#define QX_REGISTER_HPP_QX_TEST QX_REGISTER_HPP_EXPORT_DLL
#define QX_REGISTER_CPP_QX_TEST QX_REGISTER_CPP_EXPORT_DLL
#else
#define QX_REGISTER_HPP_QX_TEST QX_REGISTER_HPP_IMPORT_DLL
#define QX_REGISTER_CPP_QX_TEST QX_REGISTER_CPP_IMPORT_DLL
#endif
#endif
このファイルを precompiled.h に include しておき、main から使えるようにしておく、という構成です。
マクロを定義したので、ソースも適宜変更。
//person.h
#ifndef _CLASS_PERSON_H_
#define _CLASS_PERSON_H_
class person
{
public:
long id;
QString name;
person() :id(0) { ; }
virtual ~person() { ; }
};
QX_REGISTER_HPP_QX_TEST(person, qx::trait::no_base_class_defined,1)//←これを追加
#endif // _CLASS_PERSON_H
person.cpp も以下のように変更。
#include "../include/precompiled.h"
#include "../include/person.h"
#include <QxOrm_Impl.h>
QX_REGISTER_CPP_QX_TEST(person)//←追加
namespace qx {
template &lt;> void register_class(QxClass<person> & t)
{
t.id(& person::id, "id");
t.data(& person::name, "name");
}}
main.cpp
#include "../include/precompiled.h"
#include <QtCore/qcoreapplication.h>
#include "../include/person.h"
#include <QxOrm_Impl.h>
int main(int argc, char * argv[])
{
// Qt application
QCoreApplication app(argc, argv);
QFile::remove("./person.db");
typedef std::shared_ptr<person> person_ptr;
person_ptr d1; d1.reset(new person()); d1->name = "name1";
typedef std::vector<person_ptr> type_lst_person;
type_lst_person lst_person;
lst_person.push_back(d1);
// Parameters to connect to database
qx::QxSqlDatabase::getSingleton()->setDriverName("QSQLITE");
qx::QxSqlDatabase::getSingleton()->setDatabaseName("./person.db");
qx::QxSqlDatabase::getSingleton()->setHostName("localhost");
qx::QxSqlDatabase::getSingleton()->setUserName("root");
qx::QxSqlDatabase::getSingleton()->setPassword("");
qx::QxSqlDatabase::getSingleton()->setFormatSqlQueryBeforeLogging(true);
qx::QxSqlDatabase::getSingleton()->setDisplayTimerDetails(true);
// Only for debug purpose : assert if invalid offset detected fetching a relation
qx::QxSqlDatabase::getSingleton()->setVerifyOffsetRelation(true);
// Create all tables in database
QSqlError daoError = qx::dao::create_table<person>();
person_ptr person_1; person_1.reset(new person());
person_1->id = 1; person_1->name = "秋葉 太郎";
daoError = qx::dao::insert(person_1);
return 0;
}
これで、ビルドして、実行すると出力先に test(d) という実行ファイルと person.db という sqlite のデータベースができているはずです。
DB Browser for SQLite などで覗いてみると…
できてますね!
注意点
①上では、従来の .pro ファイルを使う qmake でビルドしています。
なのですが、cmake でビルドしようとすると QxOrm のライブラリが見つからない場合があるようです。
これは QxOrm の cmake ビルドシステム自体が未整備のためのようです。関連の PR がありました。
開発者さん自身は、しばらくは qmake メインでいきたいようです。
ワイは、こんなことを提案してきました。
cmake が他のパッケージをどうやって探しているかまっっったく理解してませんが。
参考:
https://qiita.com/shohirose/items/d9bda00a39a113965c5c
②他には postgreSQL が繋がらないかな。
qx::QxSqlDatabase::getSingleton()->setDriverName("QPSQL");
としたでのは、ドライバーがロードできない云々というエラーが出る。