-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
116 lines (92 loc) · 2.87 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <QCoreApplication>
#include <QDebug>
#include <QLibraryInfo>
#ifdef _WIN32
# include <qt_windows.h>
#endif
#include <iostream>
#include <orm/db.hpp>
#include <orm/libraryinfo.hpp>
#include <orm/tiny/model.hpp>
using Qt::StringLiterals::operator""_s;
using Orm::Constants::ID;
using Orm::Constants::NAME;
using Orm::Constants::NEWLINE;
using Orm::Constants::QSQLITE;
using Orm::Constants::QUOTE;
using Orm::Constants::SPACE;
using Orm::Constants::check_database_exists;
using Orm::Constants::database_;
using Orm::Constants::driver_;
using Orm::DB;
using Orm::LibraryInfo;
using Orm::Tiny::Model;
using Orm::Utils::Helpers;
namespace
{
class Post final : public Model<Post> // NOLINT(bugprone-exception-escape)
{
friend Model;
using Model::Model;
};
std::ostream &operator<<(std::ostream &os, const QString &value)
{
return os << value.toUtf8().constData();
}
void run()
{
// Create the SQLite database connection
// Ownership of a unique_ptr()
auto manager = DB::create({
{driver_, QSQLITE},
{database_, qEnvironmentVariable("TINYORM_HELLOWORLD_DB_SQLITE_DATABASE",
u"../../HelloWorld.sqlite3"_s)},
{check_database_exists, true},
});
// Print Qt and TinyORM builds information
std::cout << QLibraryInfo::build() << NEWLINE
<< LibraryInfo::build() << NEWLINE << NEWLINE;
// Select using QueryBuilder
{
auto posts = DB::select(u"select * from posts"_s);
while(posts.next())
std::cout << posts.value(ID).toULongLong() << SPACE << QUOTE
<< posts.value(NAME).toString() << QUOTE << NEWLINE;
}
// Select using ORM (Post model)
{
const auto posts = Post::all();
for (const auto &post : posts)
std::cout << post.getAttribute<quint64>(ID) << SPACE << QUOTE
<< post.getAttribute<QString>(NAME) << QUOTE << NEWLINE;
}
}
} // namespace
int main(int argc, char *argv[])
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
// SetConsoleOutputCP(1250);
#endif
try {
/* Needed from Qt v6.5.3 to avoid:
qt.core.qobject.connect: QObject::connect(QObject, Unknown): invalid nullptr parameter */
const QCoreApplication app(argc, argv);
run();
} catch (const std::exception &e) {
Helpers::logException(e);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Expected output:
Qt 6.8.0 (x86_64-little_endian-llp64 shared (dynamic) debug build; by MSVC 2022)
TinyORM 0.37.3 (x86_64-little_endian-llp64 shared debug build; by MSVC 2022 (1940))
Executed prepared query (8ms, -1 results, 0 affected, tinyorm_default) : select * from posts
1 "First Post"
2 "Second Post"
Executed prepared query (0ms, -1 results, 0 affected, tinyorm_default) : select * from "posts"
1 "First Post"
2 "Second Post"
*/