14#include "AModelMeta.h"
15#include <AUI/Data/ASqlBuilder.h>
25template<
typename Model>
49 id = table(Meta::getSqlTable()).insertORM((Model&)*
this);
51 table(Meta::getSqlTable()).updateORM((Model&)*
this);
60 table(Meta::getSqlTable()).removeORM((Model&)*
this);
63 class IncompleteSelectRequest {
71 mSql(std::move(sql)) {
76 IncompleteSelectRequest(
const IncompleteSelectRequest&) =
delete;
77 ~IncompleteSelectRequest() =
default;
80 auto[exprString, whereParams] = ASqlBuilder::WhereStatement::WhereExpr::unpack(w);
81 if (!exprString.empty()) {
82 if (mWhereExpr.empty()) {
83 mWhereExpr =
"WHERE " + exprString;
84 mWhereParams = std::move(whereParams);
86 mWhereExpr +=
"AND " + exprString;
87 mWhereParams.insertAll(whereParams);
98 auto idField = AField<ASqlModel<Model>>
::make(&ASqlModel<Model>::id);
100 result.reserve(0x100);
104 auto dbResult = Autumn::get<ASqlDatabase>()->query(mSql, mWhereParams);
108 fields << AModelMeta<Model>::getFields().valueVector();
110 for (
auto& row : dbResult) {
112 idField->set(m, row->getValue(0));
113 for (
size_t columnIndex = 1; columnIndex < dbResult->getColumns().size(); ++columnIndex) {
114 fields[columnIndex - 1]->set(m, row->getValue(columnIndex));
116 result << std::move(m);
130 if (result.size() == 0)
132 if (result.size() != 1)
134 return result.first();
140 columnNames << Meta::getFields().keyVector();
141 return aui::ptr::manage(
new IncompleteSelectRequest(
"SELECT " + (columnNames.empty() ?
'*'
142 : columnNames.join(
',')) +
" FROM " + AModelMeta<Model>::getSqlTable(), expression));
154 columns << Meta::getFields().keyVector();
155 auto result = table(Meta::getSqlTable()).select(columns).where(col(
"id") ==
id)
156 .template as<Model>();
161 return result.first();
167 columnNames << Meta::getFields().keyVector();
168 return aui::ptr::manage(
new IncompleteSelectRequest(
"SELECT " + (columnNames.empty() ?
'*'
169 : columnNames.join(
',')) +
" FROM " + AModelMeta<Model>::getSqlTable(), {}));
179 template<
typename ... Args>
180 static Model
make(Args&&... args) {
181 Model m = {0, std::forward<Args>(args)...};
194 AString tableName = AModelMeta<Model>::getSqlTable();
195 if (tableName.endsWith(
"s")) {
196 tableName = tableName.substr(0, tableName.length() - 1);
217 template<
typename Other>
220 return Other::where(col(columnName) ==
id);
223 template<
typename Other>
224 Other belongsTo(id_t desiredId) {
225 return Other::where(col(
"id") == desiredId)->first();
Definition ASqlBuilder.h:79
AVector< Model > get()
Get query result in ORM.
Definition ASqlModel.h:97
Model first()
Do query and get first row in ORM.
Definition ASqlModel.h:128
Thrown when a single row is expected to be received, but the database did not return any rows.
Definition ASqlModel.h:30
Thrown when one row is expected to be received, but the database returned more than one row.
Definition ASqlModel.h:35
An AVector with string-related functions.
Definition AStringVector.h:22
Represents a Unicode character string.
Definition AString.h:38
A std::vector with AUI extensions.
Definition AVector.h:39
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
#define AUI_ASSERT(condition)
Asserts that the passed condition evaluates to true.
Definition Assert.h:55
Defines a model that can be stored in an SQL database. Implements queries for this type to the databa...
Definition ASqlModel.h:26
void remove()
Removes row from the table by ID.
Definition ASqlModel.h:58
void save()
Saves this model in DB. If id = 0 then a new row will be created in the table, and the id of the crea...
Definition ASqlModel.h:47
static Model byId(id_t id)
Get a row from the table by ID.
Definition ASqlModel.h:151
_< typename Other::IncompleteSelectRequest > hasMany()
Implementation of one-to-many relation between ORM structures. Used with belongsTo.
Definition ASqlModel.h:218
static Model make(Args &&... args)
Creates a model and saves it to the database.
Definition ASqlModel.h:180
static AString getIdColumnNameInOtherTables()
Definition ASqlModel.h:193
static _< T > manage(T *raw)
Delegates memory management of the raw pointer T* raw to the shared pointer, which is returned.
Definition SharedPtrTypes.h:424