125 #ifndef __UT_SQLORM_H__
126 #define __UT_SQLORM_H__
151 #include <functional>
172 template <
typename T>
186 bool force_insert =
false,
187 bool force_update =
false)
189 return myModelMeta.get().save(*
this, ec, force_insert, force_update);
199 const std::initializer_list<UT::orm::FilterArg>&
args,
203 return meta.template fetch<T>(
args, ec);
206 template <
typename PK>
210 return meta.template fetch<T>(pk, ec);
217 return meta.template fetchAll<T>(ec);
221 std::initializer_list<UT::orm::FilterArg>&& args,
226 std::forward<std::initializer_list<UT::orm::FilterArg>>(
args),
231 template <
typename C>
237 return meta.template findByForeignKey<T, C>(fk_name, obj);
242 const std::initializer_list<UT::orm::FilterArg>& args,
246 return meta.template getOrCreate<T>(
args, ec);
250 const std::initializer_list<UT::orm::FilterArg>& args,
254 return meta.template create<T>(
args, ec);
257 template <
typename ArrayT>
262 return meta.template bulkFetch<T, ArrayT>(items, ec, col_name);
270 return meta.template bulkSave<T>(items, ec);
273 template <
typename... Args>
276 auto&& meta_info = T::metaInfo();
277 return fromDB(meta_info, std::forward<Args>(args)...);
280 template <
typename... Args>
283 return meta.template
fromDB<
T, Args...>(
284 std::forward<Args>(
args)...);
288 const std::initializer_list<UT::orm::FilterArg>& args,
292 return meta.template exists<T>(
args, ec);
324 return getDatabase_();
329 bool savepoint =
true)
333 std::forward<UT_AutoSqlTransaction::commit_callback_t>(
342 template <
typename T>
345 auto&& meta_info = T::metaInfo();
346 for (
auto&& m : myRegisteredMetas)
348 if (m->tableName() == meta_info.tableName())
358 ptr->resetInternals();
359 myBuildStack.emplace_back(ptr);
360 myRegisteredMetas.emplace_back(ptr);
366 for (
auto&& m : myRegisteredMetas)
368 if (m->tableName() == meta->tableName())
373 myBuildStack.emplace_back(meta);
374 myRegisteredMetas.emplace_back(meta);
380 for (
auto&& m : myRegisteredMetas)
390 return myRegisteredMetas;
394 friend class UT_SqlOrmTable;
409 void collectAppliedMigrations_(
413 void createMigrationTableIfNotExist_(
430 #define UT_DECLARE_MODEL() \
433 friend class UT_ORMModelMeta; \
434 static const Meta& metaInfo();
436 #define UT_DEFINE_MODEL(_model_) \
437 const _model_::Meta& _model_::metaInfo() \
439 static Meta _instance; \
443 #endif // __UT_SQLORM_H__
GLuint GLsizei const GLchar * message
UT_ORMModel & operator=(const UT_ORMModel &)=default
UT_Array< UT_SharedPtr< UT_ORMModelMeta > > myRegisteredMetas
std::reference_wrapper< const UT_ORMModelMeta > myModelMeta
static T fromDB(Args &&...args)
static UT_Optional< T > create(const std::initializer_list< UT::orm::FilterArg > &args, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
static T fromDB(const UT_ORMModelMeta &meta, Args &&...args)
static UT_Optional< T > filter(std::initializer_list< UT::orm::FilterArg > &&args, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
const UT_Array< UT_SharedPtr< UT_ORMModelMeta > > & metas() const
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
static UT_Optional< T > fetch(const PK &pk, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
UT_ORMModel(const UT_ORMModelMeta &meta=T::metaInfo())
**But if you need a result
UT_ThreadSpecificValue< UT_SqlDatabase > myDB
UT_AutoSqlTransaction transaction(UT_AutoSqlTransaction::commit_callback_t &&callback, bool savepoint=true)
std::optional< T > UT_Optional
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
UT_SharedPtr< UT_ORMModelMeta > lookupMeta(const UT_StringView &name)
A utility class to do read-only operations on a subset of an existing string.
static std::pair< T, bool > getOrCreate(const std::initializer_list< UT::orm::FilterArg > &args, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
GLint GLint GLsizei GLint GLenum GLenum type
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
const UT_ORMModelMeta & meta() const
static UT_Array< T > fetch(const std::initializer_list< UT::orm::FilterArg > &args, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
static const UT_StringHolder theEmptyString
static bool exists(const std::initializer_list< UT::orm::FilterArg > &args, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLuint const GLchar * name
std::function< T > UT_Function
UT_Function< void(const UT_StringHolder &, const UT_ErrorCode &)> error_callback_t
UT_SqlDatabase & database()
bool save(UT_ErrorCode &ec, bool force_insert=false, bool force_update=false)
std::error_code UT_ErrorCode
UT_SqlStatement cursor() const
error_callback_t myErrorCallback
A map of string to various well defined value types.
UT_Array< UT_SharedPtr< UT_ORMModelMeta > > myBuildStack
const UT_SqlDatabase & database() const
**If you just want to fire and args
UT_ORMModelMeta * registerModelMeta(const UT_SharedPtr< UT_ORMModelMeta > &meta)
SYS_AtomicInt32 myHasConfiguredInternals
static UT_Array< T > bulkFetch(const ArrayT &items, UT_ErrorCode &ec, const UT_StringRef &col_name=UT_StringHolder::theEmptyString, const UT_ORMModelMeta &meta=T::metaInfo())
UT_Function< bool()> commit_callback_t
static void bulkSave(const UT_Array< T > &items, UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
static UT_Array< T > fetchAll(UT_ErrorCode &ec, const UT_ORMModelMeta &meta=T::metaInfo())
virtual ~UT_ORMModel()=default
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter