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
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
The MySQL >=8.0.19 supports aliases in the VALUES and SET clauses
of INSERT INTO ... ON DUPLICATE KEY UPDATE statement for the row to be
inserted and its columns. It also generates warning from >=8.0.20 if
an old style used.
This enhancement detects the MySQL version and on the base of it uses
alias during the upsert call.
Also a user can override the version through the MySQL database
configuration. It helps to save/avoid one database query
(select version()) during the upsert method call or during connecting
to the MySQL database (version is needed if strict mode is enabled).
- added unit and functional tests
- updated number of unit tests to 1402
- updated upsert docs
- added ConfigUtils to avoid duplicates
Others:
- added the version database configuration everywhere
- docs added few lines about MySQL version configuration option
- docs updated database configurations, added a new missing options
- added fixPostgresSequences() to our tom seeder
- added PostgreSQL connection to tom_testdata
- updated msvc workflow
- tom, throw if empty connection only if exactly one connection
provided
- also updated create_and_seed_database.php script to exclude postgres
connection using --skip-postgres-migrate command-line parameter