-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef38cbd
commit 3ca86c7
Showing
8 changed files
with
543 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#include <orm/schema.hpp> | ||
|
||
#include "BookModel.hpp" | ||
#include "PublisherModel.hpp" | ||
|
||
const QString BookModel::g_property_id = "id"; | ||
const QString BookModel::g_property_publisher("publisher"); | ||
const QString BookModel::g_property_author = "author"; | ||
const QString BookModel::g_property_title = "title"; | ||
const QString BookModel::g_property_releaseDate = "released"; | ||
const QString BookModel::g_property_isbn = "isbn"; | ||
const QString BookModel::g_table("books"); | ||
|
||
QStringList BookModel::u_fillable { | ||
g_property_id, | ||
g_property_author, | ||
g_property_title, | ||
g_property_releaseDate, | ||
g_property_isbn | ||
}; | ||
|
||
const bool BookModel::u_snakeAttributes(false); | ||
|
||
BookModel::BookModel(QString author, QString title, QDate releaseDate, QString isbn) | ||
: BookModel() | ||
{ | ||
setAuthor(author); | ||
setTitle(title); | ||
setReleaseDate(releaseDate); | ||
setIsbn(isbn); | ||
} | ||
|
||
BookModel::BookModel() | ||
: Model() | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
BookModel::~BookModel() | ||
{ | ||
} | ||
|
||
BookModel::BookModel(const BookModel &other) | ||
: Model(other) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
BookModel::BookModel(Orm::Tiny::DontFillDefaultAttributes attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
BookModel::BookModel(const QVector<AttributeItem> &attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
BookModel::BookModel(std::initializer_list<AttributeItem> attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
BookModel &BookModel::operator=(const BookModel &other) | ||
{ | ||
Model::operator=(other); | ||
// do not overwrite u_relations and u_table | ||
return *this; | ||
} | ||
|
||
QHash<QString, BookModel::RelationVisitor> BookModel::relations() const | ||
{ | ||
return { | ||
{PublisherModel::g_table, [](RelationStore& visitor) { visitor(&BookModel::publisher); }} | ||
}; | ||
} | ||
|
||
QString BookModel::table() const | ||
{ | ||
return g_table; | ||
} | ||
|
||
std::unique_ptr<BookModel::BelongsTo<BookModel, PublisherModel>> | ||
BookModel::publisher() | ||
{ | ||
return belongsTo<PublisherModel>(); | ||
} | ||
|
||
void BookModel::createTableCallback(Orm::SchemaNs::Blueprint& table) | ||
{ | ||
table.id(); | ||
table.foreignId(g_property_publisher).nullable().constrained().cascadeOnDelete().cascadeOnUpdate(); | ||
table.string(g_property_author); | ||
table.string(g_property_title); | ||
table.string(g_property_isbn); | ||
table.date(g_property_releaseDate); | ||
table.timestamps(); | ||
} | ||
|
||
void BookModel::createTable() | ||
{ | ||
Orm::Schema::create(g_table, BookModel::createTableCallback); | ||
} | ||
|
||
void BookModel::dropTable() | ||
{ | ||
Orm::Schema::drop(g_table); | ||
} | ||
|
||
qlonglong BookModel::id() const | ||
{ | ||
return getAttribute(g_property_id).toLongLong(); | ||
} | ||
void BookModel::setId(qlonglong id) | ||
{ | ||
setAttribute(g_property_id, id); | ||
} | ||
|
||
QString BookModel::author() const | ||
{ | ||
return getAttribute(g_property_author).toString(); | ||
} | ||
void BookModel::setAuthor(QString author) | ||
{ | ||
setAttribute(g_property_author, author); | ||
} | ||
|
||
QString BookModel::title() const | ||
{ | ||
return getAttribute(g_property_title).toString(); | ||
} | ||
void BookModel::setTitle(QString title) | ||
{ | ||
setAttribute(g_property_title, title); | ||
} | ||
|
||
QString BookModel::isbn() const | ||
{ | ||
return getAttribute(g_property_isbn).toString(); | ||
} | ||
void BookModel::setIsbn(QString isbn) | ||
{ | ||
setAttribute(g_property_isbn, isbn); | ||
} | ||
|
||
QDate BookModel::releaseDate() const | ||
{ | ||
return getAttribute(g_property_releaseDate).toDate(); | ||
} | ||
void BookModel::setReleaseDate(QDate released) | ||
{ | ||
setAttribute(g_property_releaseDate, released); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#pragma once | ||
|
||
#include <orm/tiny/model.hpp> | ||
|
||
#include "PublisherModel.hpp" | ||
|
||
class PublisherModel; | ||
|
||
class BookModel final : public Orm::Tiny::Model<BookModel, PublisherModel> | ||
{ | ||
friend Model; | ||
using Model::Model; | ||
|
||
public: | ||
BookModel(QString author, QString title, QDate releaseDate, QString isbn); | ||
BookModel(); | ||
~BookModel(); | ||
BookModel(const BookModel &other); | ||
BookModel(Orm::Tiny::DontFillDefaultAttributes); | ||
BookModel(const QVector<AttributeItem> &attributes); | ||
BookModel(std::initializer_list<AttributeItem> attributes); | ||
BookModel &operator=(const BookModel &other); | ||
Model &operator=(Model &&) noexcept = delete; | ||
|
||
static void createTable(); | ||
static void dropTable(); | ||
|
||
qlonglong id() const; | ||
void setId(qlonglong id); | ||
|
||
QString author() const; | ||
void setAuthor(QString author); | ||
|
||
QString title() const; | ||
void setTitle(QString title); | ||
|
||
QDate releaseDate() const; | ||
void setReleaseDate(QDate released); | ||
|
||
QString isbn() const; | ||
void setIsbn(QString isbn); | ||
|
||
public: | ||
std::unique_ptr<BelongsTo<BookModel, PublisherModel>> publisher(); | ||
|
||
private: | ||
using RelationStore = Orm::Tiny::Support::Stores::BaseRelationStore<BookModel, PublisherModel>; | ||
|
||
static QStringList u_fillable; | ||
static const bool u_snakeAttributes; | ||
|
||
QHash<QString, RelationVisitor> u_relations; | ||
QString u_table; | ||
|
||
protected: | ||
static void createTableCallback(Orm::SchemaNs::Blueprint& table); | ||
QHash<QString, RelationVisitor> relations() const; | ||
QString table() const; | ||
|
||
public: | ||
static const QString g_property_id; | ||
static const QString g_property_publisher; | ||
static const QString g_property_author; | ||
static const QString g_property_title; | ||
static const QString g_property_releaseDate; | ||
static const QString g_property_isbn; | ||
static const QString g_table; | ||
}; | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
#include <orm/schema.hpp> | ||
|
||
#include "BookModel.hpp" | ||
#include "PublisherModel.hpp" | ||
|
||
const QString PublisherModel::g_property_id("id"); | ||
const QString PublisherModel::g_property_name("name"); | ||
const QString PublisherModel::g_property_founded("founded"); | ||
const QString PublisherModel::g_property_website("website"); | ||
const QString PublisherModel::g_table("publishers"); | ||
|
||
const QVector<QString> PublisherModel::u_with { | ||
BookModel::g_table | ||
}; | ||
|
||
const QStringList PublisherModel::u_fillable { | ||
g_property_id, | ||
g_property_name, | ||
g_property_founded, | ||
g_property_website | ||
}; | ||
|
||
const QVector<PublisherModel::AttributeItem> PublisherModel::u_attributes { | ||
{ g_property_name, "<null>" }, | ||
{ g_property_founded, QDate(1970,1,1).toString(Qt::ISODate) }, | ||
{ g_property_website, "<null>" } | ||
}; | ||
|
||
const bool PublisherModel::u_snakeAttributes(false); | ||
|
||
PublisherModel::PublisherModel() | ||
: Model() | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
PublisherModel::~PublisherModel() | ||
{ | ||
} | ||
|
||
PublisherModel::PublisherModel(const PublisherModel &other) | ||
: Model(other) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
PublisherModel::PublisherModel(QString name, QDate founded, QUrl website) | ||
: PublisherModel() | ||
{ | ||
setName(name); | ||
setFounded(founded); | ||
setWebsite(website); | ||
} | ||
|
||
PublisherModel::PublisherModel(Orm::Tiny::DontFillDefaultAttributes attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
PublisherModel::PublisherModel(const QVector<AttributeItem> &attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
PublisherModel::PublisherModel(std::initializer_list<AttributeItem> attributes) | ||
: Model(attributes) | ||
, u_relations(relations()) | ||
, u_table(table()) | ||
{ | ||
} | ||
|
||
PublisherModel &PublisherModel::operator=(const PublisherModel &other) { | ||
Model::operator=(other); | ||
// do not overwrite u_relations or u_table | ||
return *this; | ||
} | ||
|
||
void PublisherModel::createTableCallback(Orm::SchemaNs::Blueprint& table) | ||
{ | ||
table.id(); | ||
table.string(g_property_name); | ||
table.date(g_property_founded); | ||
table.string(g_property_website); | ||
table.timestamps(); | ||
} | ||
|
||
void PublisherModel::createTable() | ||
{ | ||
Orm::Schema::create(g_table, PublisherModel::createTableCallback); | ||
} | ||
|
||
void PublisherModel::dropTable() | ||
{ | ||
Orm::Schema::drop(g_table); | ||
} | ||
|
||
|
||
qlonglong PublisherModel::id() const | ||
{ | ||
return getAttribute(g_property_id).toLongLong(); | ||
} | ||
void PublisherModel::setId(qlonglong id) | ||
{ | ||
setAttribute(g_property_id, id); | ||
} | ||
|
||
QString PublisherModel::name() const | ||
{ | ||
return getAttribute(g_property_name).toString(); | ||
} | ||
void PublisherModel::setName(QString name) | ||
{ | ||
setAttribute(g_property_name, name); | ||
} | ||
|
||
QDate PublisherModel::founded() const | ||
{ | ||
return getAttribute(g_property_founded).toDate(); | ||
} | ||
void PublisherModel::setFounded(QDate founded) | ||
{ | ||
setAttribute(g_property_founded, founded); | ||
} | ||
|
||
QUrl PublisherModel::website() const | ||
{ | ||
return getAttribute(g_property_website).toUrl(); | ||
} | ||
void PublisherModel::setWebsite(QUrl website) | ||
{ | ||
setAttribute(g_property_website, website.toString()); | ||
} | ||
|
||
std::unique_ptr<PublisherModel::HasMany<PublisherModel, BookModel>> | ||
PublisherModel::books() | ||
{ | ||
return hasMany<BookModel>(BookModel::g_property_publisher); | ||
} | ||
|
||
QHash<QString, PublisherModel::RelationVisitor> PublisherModel::relations() const | ||
{ | ||
return { | ||
{ BookModel::g_table, [](RelationStore& visitor) { visitor(&PublisherModel::books); } } | ||
}; | ||
} | ||
|
||
QString PublisherModel::table() const | ||
{ | ||
return g_table; | ||
} |
Oops, something went wrong.