Commit Graph

55 Commits

Author SHA1 Message Date
silverqx
d9815e9d5a tests test polymorphic Relation instances
Added the test case which tests returning
the std::unique_ptr<Relation<>> instead of the derived relation
eg. std::unique_ptr<HasMany> from the relationship factory methods.
This behavior is considered an additional feature and is possible
thanks to the polymorphism.
2023-05-19 11:19:00 +02:00
silverqx
fe89d9d53f added comment and whitespaces 2023-05-18 15:42:18 +02:00
silverqx
864272d10f models used SIZE_ everywhere
To avoid collisions with Windows header files.
2023-05-07 17:49:20 +02:00
silverqx
4c9bf9c7e9 added note constant 2023-05-05 14:14:15 +02:00
silverqx
7f344a2145 added ModelsCollection 🔥🚀🎉
The Orm::Tiny::Types::ModelsCollection is container class with
additional handy methods like pluck(), whereIn(), filter(), ...
It extends the QVector. The template parameter can be the Model or
Model *, the model must be the Derived model type eg. Torrent, Post, ...

All ORM-related methods return the ModelsCollection so it can be easily
filtered, changed, transformed with one simple line or callback.
All the algorithms are nicely encapsulated in the ModelsCollection
methods.

Currently, there is this pattern, all TinyBuilder or Model methods are
returning the ModelsCollection<Derived> and all relation related
methods are returning the ModelsCollection<Derived *>.

 - added Album and AlbumImage migrations and seeder data (also PHP)
 - added Album and AlbumImage models
 - added functional tests for ModelsCollection<Model> and
   ModelsCollection<Model *>
 - all ModelsCollection methods or algorithms are unit tested
 - replaced all occurences of the QVector<Model> and QVector<Model *>
   with the ModelsCollection<Model> and ModelsCollection<Model *> 😵‍💫
 - tests added Model::findMany()
 - added reserve() on few places
 - removed useless #include-s
2023-05-05 13:06:15 +02:00
silverqx
9bccda590e changed Model timestamps to getter methods
Changed from static data members to static getter methods to avoid Clang
crashed because of static initialization order.

 - updated docs
2023-04-02 11:07:17 +02:00
silverqx
289b688e35 suppressed clang-tidy warning 2023-02-16 16:42:10 +01:00
silverqx
77a8feb60a suppressed clang-tidy warnings in all models 2023-02-16 14:01:11 +01:00
silverqx
8aa059bdfe used QString::fromUtf8(__func__) instead of cast 2023-02-14 21:03:27 +01:00
silverqx
a83dd8ce50 suppressed clang tidy warnings
Primarily in models and in one test case.
2023-01-06 21:22:18 +01:00
silverqx
4145de89a9 wrapped include in macro 2022-12-20 16:15:44 +01:00
silverqx
2f3f3c9552 tests QDateTime ctor instead of fromString
For all UTC and a local time QDateTime instances, except eg. +02:00
QDateTime-s with time zone.
2022-11-19 14:17:42 +01:00
silverqx
6b6b192a82 used QLatin1Char everywhere
Except short parameters for tom because it would make code formatting
and code look much worse. 🫤
2022-11-12 03:19:09 +01:00
silverqx
b4bb44e2a4 removed useless private 2022-11-11 09:49:44 +01:00
silverqx
f6e33f1e67 enhanced and fixed Model instantiation
- primarily fixed instantiation with Default Attribute values with
   the QDateTime
 - revisited ctors and instance methods
 - added instanceHeap methods, create model instance on the heap
 - added instance method overloads with the connection override param.
 - added tests for all these new and old methods and ctor-s
 - used Model::instance() related methods all over the TinyORM to
   correctly support Model instantiation with Default Attribute values
   with the QDateTime
 - added a new Model constructor with the DontFillDefaultAttributes tag
   that instantiates a Model class without fill default attributes, to
   bypass CRTP problem with the QDateTime; this ctor is called from
   the instance()-related methods and the fill() method is called on
   already instantiated Model
   methods
 - updated NOTES.txt where the raw Model ctor-s were called
2022-11-05 18:45:31 +01:00
silverqx
a6fe55ec7e qmake excluded torrent.cpp 2022-10-29 19:20:13 +02:00
silverqx
db348e3a3f renamed to datetimes table 2022-10-27 14:53:45 +02:00
silverqx
1ded27bbc8 QDateTime overhaul 🤯🤐🙃
Fixed buggy behavior of QDateTime values during SELECT, INSERT, and
UPDATE queries for all supported QtSql drivers. Behavior is fixed if
querying the database using Orm::QueryBuilder or TinyBuilder/Model.
Can't be fixed if using raw queries using the QSqlQuery because I don't
have any control over this code.

Every QtSql driver behaves differently in how it works with
the QDateTime and datetime-related database types. It doesn't have only
one problem, it has various kinds of problems, eg. it returns all
QDateTime objects in the local time zone. All issues are summarized
in NOTES.TXT under "QDateTime and database date/time types:"
section.

This buggy behavior can be fixed or corrected using a new "qt_timezone"
database connection configuration option. It accepts the time zone value
in various formats (QTimeZone, Qt::TimeSpec, int number as an offset
from UTC, QString eg. +02:00, Europe/Prague or Orm::QtTimeZoneConfig).
This "qt_timezone" value affects how the  QueryBuilder and TinyBuilder
send and receives datetime-related types to/from database. It should be
set to the same time zone value as the "timezone" connection
configuration option.

During the INSERT and UPDATE statements, it CONVERTS QDateTime's
time zone to the qt_timezone value before the statement is sent
to the database.
And during the SELECT statements it SETS QDateTime's time zone value
after the values are obtained from the database.

So if you set the "timezone" to UTC and "qt_timezone" to eg. Qt::UTC or
QTimeZone::utc(), then QDateTime values will have set the correct
time zone, in this case, the time zone will be UTC.

For the SQLite database was also added the "return_qdatetime" connection
configuration option which default value is true. By default,
the QSQLITE driver returns datetime values as QString.
The "return_qdatetime" controls this behavior and if is set to true then
the QDateTime will be returned instead.

TinyORM QueryBuilder returns the Orm::SqlQuery instead of QSqlQuery
from methods for which the QDateTime's time zone should be corrected.
That are eg. the select(), selectOne(), unprepared(), or chunk(),
chunkById(), each(), eachById(), ...

Orm::SqlQuery is a simple wrapper around the QSqlQuery whose
responsibility is only to fix a QDateTime's time zone.

Also unified the QDate behavior across all QtSql drivers.

 - added a new migration for the datetime table for testing QDateTime
   and datetime-related database types
 - added new Datetime model
 - added functional tests for testing datetime-related queries
   - testing QDateTime for all supported drivers
   - testing with different time zones, UTC, +02:00
   - testing QDate for all supported drivers
 - added two new connection configuration options "qt_timezone" and
   "return_qdatetime" which is for the SQLite database only
 - added returnQDateTime()/setReturnQDateTime() getter/setter
   to the SQLiteConnection class
 - added getQtTimeZone()/setQtTimeZone()/isConvertingTimeZone()
   to the DatabaseConnection class
 - tests, fixed all QDateTime instances, changed time zone to UTC, so
   auto tests are now UTC, they force also MySQL and PostgreSQL server
   time zone session variable to UTC

Others:

 - StringUtils moved from orm/tiny/utils/ to the orm/utils/ folder
   to the ::Orm::Utils namespace
 - enhanced all database connection constructors, used rvalue references
 - added delegated DatabaseConnection() constructor
   for SQLiteConnection() because of m_returnQDateTime
2022-10-27 14:30:00 +02:00
silverqx
89d63d5ba1 added Casting Attributes 🤯🙌
Attribute casting allows converting TinyORM Model attribute values when
you retrieve them on model instances. Conversion is done using
the QVariant::convert(QMetaType) method.

To cast attributes you can provide the Model::u_casts data member
of the type std::unordered_map<QString, CastItem>.
The CastItem type can be initialized by the CastType (first argument)
and optionally a modifier of the QVariant type (second argument).
Currently, only the CastType::Decimal accepts a modifier that rounds
the decimal number to the given number of decimals.

Supported Orm::Tiny::CastType-s are:
Bool, Boolean, Short, UShort, Int, Integer, UInt, UInteger, LongLong,
ULongLong, Real, Float, Double, Decimal, QString, QDate, QDateTime,
Timestamp, QByteArray.

The MySQL, PostgreSQL, and SQLite QSqlDriver-s already do conversions,
but every one of these drivers is doing these conversions in their own
way, only the varchar or text database types are handled uniformly.
The rules of how these Qt drivers are handling or converting these types
are not simple and the explanation will need it's own paragraph in docs.

Attributes casting is especially useful with the QSQLiteDriver. Other
useful use cases are casting of dates or Unix timestamps,
bool (tinyint), and the decimal type (also supports modifier).

 - added functional tests
 - added a new Type model class used by unit testing
 - added a new migration and seeder for the Type model
 - added showcase example to the Torrent model class
 - updated Model::newInstance() method to also copy a model casts
 - added TinyBuilder::withCasts() method to apply query-time casts
   to the model instance
 - added withCasts() model and relation proxies
 - added functional tests for withCasts() (for all proxies 😎)
 - set the u_casts for the boolean columns/attributes on RoleUser,
   Tagged, and User models
2022-09-20 20:20:56 +02:00
silverqx
66d5635c8e fixtypo
[skip ci]
2022-09-01 16:45:34 +02:00
silverqx
a4cec6d0d7 note about Custom Pivot models and u_xyz members 2022-09-01 15:25:39 +02:00
silverqx
6d6ad32e7d added support for u_table on Custom Pivot models
User can define the u_table data attribute on a Custom Pivot model,
it has higher priority than the guessed table name
in the HasRelationships::pivotTableName().

Also fixed BasePivot::getTable(), it didn't return correct results.
2022-09-01 12:53:32 +02:00
silverqx
46808b1005 added unix timestamps support
The Model::u_dateFormat now also accepts 'U' for unix timestamps.
The database column type should be qint64 or int.

 - added extensive unit tests also for null values, for getAttribute()
   and also for setAttribute()
 - updated migrations, added a new added_on unix timestamp column
   to the Role model
 - updated database seeders
 - updated docs
2022-08-30 10:26:40 +02:00
silverqx
61bd9e87f1 added Soft Deleting support 🤯🙌
In addition to actually removing records from your database, TinyORM
can also "soft delete" models. When models are soft deleted, they are
not actually removed from your database. Instead, a deleted_at attribute
is set on the model indicating the date and time at which the model was
"deleted".

 - added docs
 - added all proxies
 - added complete functional tests and a few unit tests
 - carefully verified all possible scenarios because it changes
   practically all queries 🤯

Others:

 - also added from() to all proxies
 - bugfix in seeders, bad ID in the PostgreSQL primary key (sequence)
2022-08-26 18:21:45 +02:00
silverqx
72f864d69f fixtypo 2022-08-19 13:40:22 +02:00
silverqx
e8006ec47a added u_fillable to User model 2022-08-19 06:51:39 +02:00
silverqx
a166922d1f fixed TinyBuilder::pluck() 🤯
Apply u_date QDateTime transformations during the pluck algorithm.

 - also added unit tests for pluck-ing with QDateTime/u_dates
2022-07-29 18:36:55 +02:00
silverqx
9f28895ec6 removed unnecessary include 2022-07-08 11:07:17 +02:00
silverqx
6f70f930c2 fixtypo
[skip ci]
2022-06-27 02:15:23 +02:00
silverqx
a30dfcd364 removed deprecated tag
[skip ci]
2022-06-27 02:01:05 +02:00
silverqx
b6f9b7899a add indent in #ifdef 2022-04-13 19:18:11 +02:00
silverqx
bd889521db sync, exposed playground connection names 2022-03-06 12:23:25 +01:00
silverqx
37c5f6e0d5 sync torrent model, connection names constants 2022-02-23 11:15:23 +01:00
silverqx
d5ad16fb06 added SIZE constant 2022-02-20 15:21:49 +01:00
silverqx
d9e857d5ed added using consts. NAME 2022-02-12 13:16:53 +01:00
silverqx
b734a09b53 fixed includes and typos 2022-01-15 11:08:01 +01:00
silverqx
5f49aa54d0 fixed Orm:DB in torrent.hpp
After DB alias to Orm::DB was removed.
2022-01-13 17:08:33 +01:00
silverqx
b5b01a5184 sync, models 2022-01-07 18:56:02 +01:00
silverqx
f2212bd930 moved all ORM types to tinytypes.hpp 2022-01-03 19:18:53 +01:00
silverqx
fe333e43d6 bugfix Model::u_dates 2021-11-16 19:31:18 +01:00
silverqx
911a83266a changed macro guards for models 2021-11-14 11:28:39 +01:00
silverqx
e0b66e7490 models enhancements 2021-11-14 11:21:44 +01:00
silverqx
794cacdd56 mingw clang missing pivot includes 2021-11-13 11:37:25 +01:00
silverqx
272d27043c database connections in threads support 🔥🚀
Database connection can run in a thread, if you create a connection in
a thread, then you have to use it only from that thread, moving it to
another thread is disallowed.
All restrictions for QSqlDatabase are true also for TinyORM.
The most important work was in DatabaseManager because more threads can
access it at once.
DatabaseManager has to be created only once in the whole application,
so it means when it will be created in a non-main thread, then it has
to be used from this non-main thread the whole time, I didn't try it
to move to another thread, the conclusion is that DatabaseManager can be
created and used from a worker thread.

 - refactored db configuration
 - added DatabaseConnectionsMap to outsource ConnectionsType and make it
   thread_local
 - added resetDefaultConnection()
 - m_queryLogId is atomic
 - made all class static data members thread_local

Thread unrelated:
 - added two new addConnections() overloads
 - added Thread utils class
   - added NameThreadForDebugging()
 - enhanced tst_DatabaseManager
2021-11-12 09:54:28 +01:00
silverqx
7f3c81de7e added thread_local for all static variables 2021-10-30 19:21:02 +02:00
silverqx
35cb2164d1 models namespace and global using/s
- all models added to Models namespace
   - it solved all google-global-names-in-headers
 - solved code model unable parsing
2021-10-29 10:35:54 +02:00
silverqx
930acaefb3 passed clang-tidy portability-*, readability-*
Enhanced .clang-tidy files.

 - -readability-function-cognitive-complexity for all tests
2021-10-28 14:55:28 +02:00
silverqx
c5d1b36e34 passed clang-tidy misc-* 2021-10-27 17:20:17 +02:00
silverqx
5330ee6fac passed clang-tidy llvm-* (cpp files) 2021-10-26 19:26:07 +02:00
silverqx
48b2477f4c passed * clang-tidy (hpp files)
Diagnostics discovered by qtc6-beta1.
2021-10-26 12:22:56 +02:00