Билдим с odb из redkitty
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
Application {
|
||||
PSApplication {
|
||||
// CppApplication {
|
||||
cpp.defines: [
|
||||
// You can make your code fail to compile if it uses deprecated APIs.
|
||||
@@ -16,9 +16,10 @@ Application {
|
||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "database" }
|
||||
Depends { name: "odbModule"}
|
||||
|
||||
// Depends { name: "redkit_gen" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "sqlite3" }
|
||||
Depends { name: "restapi" }
|
||||
Depends { name: "redkit_gen" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
@@ -29,6 +30,12 @@ Application {
|
||||
]
|
||||
}
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"sqlite3"
|
||||
]
|
||||
|
||||
// }
|
||||
} // Project
|
||||
|
||||
@@ -4,24 +4,22 @@
|
||||
#ifndef AUTHOR_S_H
|
||||
#define AUTHOR_S_H
|
||||
|
||||
// #include <QtCore/QObject>
|
||||
#include "database_global.h"
|
||||
|
||||
#include <cstddef> // std::size_t
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <QString>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
|
||||
#pragma db object
|
||||
class __attribute__((visibility("default"))) Author_S
|
||||
class DATABASE_EXPORT Author_S
|
||||
{
|
||||
public:
|
||||
Author_S() = default;
|
||||
|
||||
Author_S(const std::string& first,
|
||||
const std::string& last,
|
||||
Author_S(const QString& first,
|
||||
const QString& last,
|
||||
const unsigned short age) :
|
||||
m_first(first),
|
||||
m_last(last),
|
||||
@@ -30,11 +28,11 @@ public:
|
||||
}
|
||||
|
||||
unsigned long long id() const { return m_id; }
|
||||
std::string first() const { return m_first; }
|
||||
std::string last() const { return m_last; }
|
||||
QString first() const { return m_first; }
|
||||
QString last() const { return m_last; }
|
||||
unsigned short age() const { return m_age; }
|
||||
|
||||
std::string full_name() const { return m_first + " " + m_last; }
|
||||
QString full_name() const { return m_first + " " + m_last; }
|
||||
|
||||
void age(unsigned short age) { m_age = age; }
|
||||
|
||||
@@ -44,8 +42,8 @@ private:
|
||||
#pragma db id auto
|
||||
unsigned long long m_id;
|
||||
|
||||
std::string m_first;
|
||||
std::string m_last;
|
||||
QString m_first;
|
||||
QString m_last;
|
||||
unsigned short m_age;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,23 +4,23 @@
|
||||
#ifndef BOOK_S_H
|
||||
#define BOOK_S_H
|
||||
|
||||
// #include <QtCore/QObject>
|
||||
#include "database_global.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <QString>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
|
||||
#include "author_s.h"
|
||||
|
||||
#pragma db object
|
||||
class __attribute__((visibility("default"))) Book_S
|
||||
class DATABASE_EXPORT Book_S
|
||||
{
|
||||
public:
|
||||
Book_S() = default;
|
||||
|
||||
Book_S(const std::string& name,
|
||||
const std::shared_ptr<Author_S>& author,
|
||||
Book_S(const QString & name,
|
||||
const QSharedPointer<Author_S>& author,
|
||||
const int year)
|
||||
{
|
||||
m_author = author;
|
||||
@@ -29,8 +29,8 @@ public:
|
||||
};
|
||||
|
||||
unsigned long long id() const { return m_id; }
|
||||
std::shared_ptr<Author_S> author() const { return m_author; }
|
||||
std::string name() const { return m_name; }
|
||||
QSharedPointer<Author_S> author() const { return m_author; }
|
||||
QString name() const { return m_name; }
|
||||
int year() const { return m_year; }
|
||||
|
||||
private:
|
||||
@@ -40,8 +40,8 @@ private:
|
||||
#pragma db id auto
|
||||
unsigned long long m_id;
|
||||
|
||||
std::shared_ptr<Author_S> m_author;
|
||||
std::string m_name;
|
||||
QSharedPointer<Author_S> m_author;
|
||||
QString m_name;
|
||||
int m_year;
|
||||
};
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@
|
||||
#include <filesystem>
|
||||
#include <random>
|
||||
|
||||
using odatabase = odb::sqlite::database;
|
||||
using unidb = std::unique_ptr<odatabase>;
|
||||
|
||||
struct testData
|
||||
{
|
||||
std::string first;
|
||||
@@ -91,21 +94,23 @@ std::vector<testData> fillDB()
|
||||
return vecTest;
|
||||
}
|
||||
|
||||
inline std::unique_ptr<odb::database> openDB(std::string path_db)
|
||||
inline unidb openDB(const std::string path_db)
|
||||
{
|
||||
// Открыть БД, если она существует
|
||||
auto db_ptr = new odatabase(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
|
||||
|
||||
if (std::filesystem::is_regular_file(path_db))
|
||||
return std::unique_ptr<odb::core::database>(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
||||
return unidb(db_ptr);
|
||||
|
||||
// И создать новую, если не существует
|
||||
std::unique_ptr<odb::core::database> db(new odb::sqlite::database(path_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
|
||||
unidb db(db_ptr);
|
||||
|
||||
odb::core::connection_ptr connect_ptr(db->connection());
|
||||
odb::connection_ptr connect_ptr(db->connection());
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=OFF");
|
||||
|
||||
odb::core::transaction tans(db->begin());
|
||||
odb::core::schema_catalog::create_schema(*db);
|
||||
odb::transaction tans(db->begin());
|
||||
odb::schema_catalog::create_schema(*db);
|
||||
tans.commit();
|
||||
|
||||
connect_ptr->execute("PRAGMA foreign_keys=ON");
|
||||
|
||||
@@ -8,45 +8,45 @@ PSLibrary {
|
||||
// You can make your code fail to compile if it uses deprecated APIs.
|
||||
// In order to do so, uncomment the following line.
|
||||
//"QT_DISABLE_DEPRECATED_BEFORE=0x060000" // disables all the APIs deprecated before Qt 6.0.0
|
||||
"DATABASE_SQLITE"
|
||||
"DATABASE_SQLITE",
|
||||
"DATABASE_LIBRARY"
|
||||
]
|
||||
consoleApplication: true
|
||||
|
||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "odbModule"}
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "redkit_gen" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
odb.gen.databases: "sqlite"
|
||||
cpp.cxxLanguageVersion: "c++17"
|
||||
|
||||
Group {
|
||||
name: "cpp"
|
||||
files: [
|
||||
"books.*",
|
||||
"databasemanager.*",
|
||||
"*.cpp",
|
||||
"*.h",
|
||||
"*.hxx",
|
||||
]
|
||||
excludeFiles: odbs.files
|
||||
}
|
||||
|
||||
Group {
|
||||
id: odbs
|
||||
name: "odb"
|
||||
files: [
|
||||
"author_s.h",
|
||||
"book_s.h",
|
||||
]
|
||||
fileTags: ["odb"]
|
||||
fileTags: ["hpp", "odbxx", "odb"]
|
||||
}
|
||||
|
||||
cpp.includePaths: [
|
||||
"/usr/include", // Общие заголовки
|
||||
"/usr/include/odb" // Заголовки ODB
|
||||
]
|
||||
|
||||
// Подключаем библиотеки ODB
|
||||
cpp.libraryPaths: [
|
||||
"/usr/lib" // Путь к библиотекам
|
||||
]
|
||||
|
||||
cpp.dynamicLibraries: [
|
||||
"odb-sqlite",
|
||||
"odb-qt",
|
||||
"odb",
|
||||
"odb-sqlite"
|
||||
"sqlite3"
|
||||
]
|
||||
}
|
||||
|
||||
12
src/database/database_global.h
Normal file
12
src/database/database_global.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef DATABASE_GLOBAL_H
|
||||
#define DATABASE_GLOBAL_H
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#if defined(DATABASE_LIBRARY)
|
||||
#define DATABASE_EXPORT Q_DECL_EXPORT
|
||||
#else
|
||||
#define DATABASE_EXPORT Q_DECL_IMPORT
|
||||
#endif
|
||||
|
||||
#endif // DATABASE_GLOBAL_H
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef DATABASEMANAGER_H
|
||||
#define DATABASEMANAGER_H
|
||||
|
||||
#include "database_global.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QtSql>
|
||||
|
||||
@@ -35,7 +37,7 @@ struct SelectBuilder
|
||||
}
|
||||
};
|
||||
|
||||
class Q_DECL_EXPORT DatabaseManager
|
||||
class DATABASE_EXPORT DatabaseManager
|
||||
{
|
||||
public:
|
||||
static DatabaseManager& instance(); // Singleton
|
||||
|
||||
53
src/main.cpp
53
src/main.cpp
@@ -1,5 +1,8 @@
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include <QPointer>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <restapi/restapiserver.h>
|
||||
|
||||
/* Опыты с odb */
|
||||
@@ -12,36 +15,34 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using unidb = std::unique_ptr<odb::database>;
|
||||
|
||||
void fillBooksBD(unidb& db)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<std::shared_ptr<Author_S>> authors = {
|
||||
std::make_shared<Author_S>("George", "Orwell", 142),
|
||||
std::make_shared<Author_S>("J.K.", "Rowling", 56),
|
||||
std::make_shared<Author_S>("J.R.R.", "Tolkien", 81),
|
||||
std::make_shared<Author_S>("Leo", "Tolstoy", 189),
|
||||
std::make_shared<Author_S>("Fyodor", "Dostoevsky", 174),
|
||||
std::make_shared<Author_S>("Mark", "Twain", 183),
|
||||
std::make_shared<Author_S>("Charles", "Dickens", 208),
|
||||
std::make_shared<Author_S>("Virginia", "Woolf", 134),
|
||||
std::make_shared<Author_S>("Ernest", "Hemingway", 122),
|
||||
std::make_shared<Author_S>("Gabriel", "García Márquez", 98),
|
||||
std::make_shared<Author_S>("Franz", "Kafka", 100),
|
||||
std::make_shared<Author_S>("Harper", "Lee", 64),
|
||||
std::make_shared<Author_S>("William", "Shakespeare", 459),
|
||||
std::make_shared<Author_S>("Oscar", "Wilde", 155),
|
||||
std::make_shared<Author_S>("Aldous", "Huxley", 123),
|
||||
std::make_shared<Author_S>("Jane", "Austen", 210),
|
||||
std::make_shared<Author_S>("John", "Steinbeck", 116),
|
||||
std::make_shared<Author_S>("Agatha", "Christie", 124),
|
||||
std::make_shared<Author_S>("Isaac", "Asimov", 105)
|
||||
std::vector<QSharedPointer<Author_S>> authors = {
|
||||
QSharedPointer<Author_S>::create("George", "Orwell", 142),
|
||||
QSharedPointer<Author_S>::create("J.K.", "Rowling", 56),
|
||||
QSharedPointer<Author_S>::create("J.R.R.", "Tolkien", 81),
|
||||
QSharedPointer<Author_S>::create("Leo", "Tolstoy", 189),
|
||||
QSharedPointer<Author_S>::create("Fyodor", "Dostoevsky", 174),
|
||||
QSharedPointer<Author_S>::create("Mark", "Twain", 183),
|
||||
QSharedPointer<Author_S>::create("Charles", "Dickens", 208),
|
||||
QSharedPointer<Author_S>::create("Virginia", "Woolf", 134),
|
||||
QSharedPointer<Author_S>::create("Ernest", "Hemingway", 122),
|
||||
QSharedPointer<Author_S>::create("Gabriel", "García Márquez", 98),
|
||||
QSharedPointer<Author_S>::create("Franz", "Kafka", 100),
|
||||
QSharedPointer<Author_S>::create("Harper", "Lee", 64),
|
||||
QSharedPointer<Author_S>::create("William", "Shakespeare", 459),
|
||||
QSharedPointer<Author_S>::create("Oscar", "Wilde", 155),
|
||||
QSharedPointer<Author_S>::create("Aldous", "Huxley", 123),
|
||||
QSharedPointer<Author_S>::create("Jane", "Austen", 210),
|
||||
QSharedPointer<Author_S>::create("John", "Steinbeck", 116),
|
||||
QSharedPointer<Author_S>::create("Agatha", "Christie", 124),
|
||||
QSharedPointer<Author_S>::create("Isaac", "Asimov", 105)
|
||||
};
|
||||
|
||||
// Список книг
|
||||
std::vector<Book_S> books = {
|
||||
QVector<Book_S> books = {
|
||||
Book_S("1984", authors[0], 1949),
|
||||
Book_S("Harry Potter and the Philosopher's Stone", authors[1], 1997),
|
||||
Book_S("The Hobbit", authors[2], 1937),
|
||||
@@ -98,7 +99,7 @@ void fillBooksBD(unidb& db)
|
||||
{
|
||||
odb::core::transaction t(db->begin());
|
||||
for (auto& author : authors)
|
||||
db->persist(author.get()); // Используем get() для получения сырого указателя
|
||||
db->persist(author); // Используем get() для получения сырого указателя
|
||||
t.commit();
|
||||
}
|
||||
|
||||
@@ -123,9 +124,9 @@ int main(int argc, char* argv[])
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
const std::string dbPath = "test_db.sqlite";
|
||||
std::unique_ptr<odb::database> db(openDB(dbPath));
|
||||
unidb db(openDB(dbPath));
|
||||
|
||||
// fillBooksBD(db);
|
||||
fillBooksBD(db);
|
||||
|
||||
RestApiServer server(*db);
|
||||
server.start(8080);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
\inherits Project
|
||||
\brief Описание
|
||||
*/
|
||||
PSLibrary {
|
||||
WPSLibrary {
|
||||
cpp.defines: [
|
||||
// You can make your code fail to compile if it uses deprecated APIs.
|
||||
// In order to do so, uncomment the following line.
|
||||
@@ -15,10 +15,8 @@ PSLibrary {
|
||||
Depends { name: "Qt"; submodules: [ "core", "sql", "network" ] }
|
||||
Depends { name: "cpp" }
|
||||
Depends { name: "database" }
|
||||
Depends { name: "odbModule"}
|
||||
|
||||
|
||||
// Depends { name: "redkit_gen" }
|
||||
Depends { name: "odb.gen" }
|
||||
Depends { name: "redkit_gen" }
|
||||
|
||||
cpp.cxxLanguageVersion: "c++20"
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
|
||||
#include <database/author_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/author_s.h>
|
||||
#include <database/book_s-odb.hxx> // Должен быть здесь
|
||||
#include <database/book_s.h>
|
||||
|
||||
#include "database/book_s-odb.hxx"
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/query.hxx>
|
||||
@@ -57,6 +58,7 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
||||
{
|
||||
QString author = request.section(' ', 1, 1).section('/', 3, 3).replace("%20", " ");
|
||||
|
||||
qWarning() << "THIS";
|
||||
// Books books;
|
||||
// QList<BookRecord> results = books.getBooksByAuthor(author);
|
||||
|
||||
@@ -72,7 +74,7 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
||||
// }
|
||||
// if (results.size() > 0)
|
||||
// jsonResponse.chop(1); // Убираем последнюю запятую
|
||||
// jsonResponse += "] }";
|
||||
jsonResponse += "] }";
|
||||
|
||||
return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n" + jsonResponse;
|
||||
}
|
||||
@@ -90,8 +92,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
||||
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(book.id());
|
||||
j["title"] = QString::fromStdString(book.name());
|
||||
j["author"] = QString::fromStdString(book.author()->full_name());
|
||||
j["title"] = book.name();
|
||||
j["author"] = book.author()->full_name();
|
||||
j["year"] = book.year();
|
||||
jArray.push_back(j);
|
||||
}
|
||||
@@ -115,8 +117,8 @@ QByteArray RestApiServer::processRequest(const QString& request)
|
||||
|
||||
QJsonObject j;
|
||||
j["id"] = QString::number(author.id());
|
||||
j["first"] = QString::fromStdString(author.first());
|
||||
j["last"] = QString::fromStdString(author.last());
|
||||
j["first"] = author.first();
|
||||
j["last"] = author.last();
|
||||
j["age"] = author.age();
|
||||
jArray.push_back(j);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user