7 #include <TCriteriaConverter>
32 virtual int findBy(
int column,
const QVariant &value) = 0;
33 virtual int findIn(
int column,
const QVariantList &values) = 0;
36 virtual int findCountBy(
int column,
const QVariant &value) = 0;
79 int findIn(
int column,
const QVariantList &values);
117 it = std::min(
it + 1,
m->rowCount());
123 it = std::min(
it + 1,
m->rowCount());
147 virtual void clear();
149 virtual int rowCount(
const QModelIndex &parent)
const;
153 QList<QPair<QString, Tf::SortOrder>> sortColumns;
157 QStringList joinClauses;
158 QStringList joinWhereClauses;
171 setTable(T().tableName());
190 TCriteriaConverter<T> conv(cri, database(), QStringLiteral(
"t0"));
198 int oldLimit = queryLimit;
202 queryLimit = oldLimit;
225 int idx = T().primaryKeyIndex();
227 tSystemDebug(
"Primary key not found, table name: %s", qUtf8Printable(T().tableName()));
244 TCriteriaConverter<T> conv(cri, database(), QStringLiteral(
"t0"));
253 while (canFetchMore()) {
291 return QSqlTableModel::rowCount();
300 return QSqlTableModel::rowCount(parent);
333 rec.setRecord(record(i), QSqlError());
367 QString columnName = TCriteriaConverter<T>::getPropertyName(column, QSqlTableModel::database().driver());
368 if (!columnName.isEmpty()) {
369 sortColumns << qMakePair(columnName, order);
380 if (!column.isEmpty()) {
383 sortColumns << qMakePair(column, order);
385 tWarn(
"Unable to set sort order : '%s' column not found in '%s' table",
386 qUtf8Printable(column), qUtf8Printable(obj.tableName()));
442 queryFilter = filter;
453 bool joinFlag = !joinClauses.isEmpty();
458 query += QLatin1String(
"SELECT ");
460 for (
int i = 0; i < rec.count(); ++i) {
461 if (rec.isGenerated(i)) {
463 query += QLatin1String(
"t0.");
466 query += QLatin1Char(
',');
471 if (Q_UNLIKELY(!valid)) {
476 query += QLatin1String(
" FROM ");
478 query += QLatin1String(
" t0");
481 for (
auto &
join : (
const QStringList &)joinClauses) {
486 QString filter = queryFilter;
487 if (!joinWhereClauses.isEmpty()) {
488 for (
auto &wh : (
const QStringList &)joinWhereClauses) {
489 if (!filter.isEmpty()) {
490 filter += QLatin1String(
" AND ");
496 if (Q_LIKELY(!filter.isEmpty())) {
497 query.append(QLatin1String(
" WHERE ")).append(filter);
501 if (!orderby.isEmpty()) {
502 query.append(orderby);
505 if (queryLimit > 0) {
506 query.append(QLatin1String(
" LIMIT ")).append(QString::number(queryLimit));
509 if (queryOffset > 0) {
510 query.append(QLatin1String(
" OFFSET ")).append(QString::number(queryOffset));
524 TCriteriaConverter<T> conv(cri, database(),
"t0");
532 query += QLatin1String(
"SELECT COUNT(*) FROM (");
534 query += QLatin1String(
") t");
538 bool res = q.
exec(query);
541 cnt = q.
value(0).toInt();
564 static const QByteArray
UpdatedAt(
"updated_at");
565 static const QByteArray
ModifiedAt(
"modified_at");
569 upd.append(QLatin1String(
"UPDATE ")).append(tableName()).append(QLatin1String(
" SET "));
571 QSqlDatabase db = database();
572 TCriteriaConverter<T> conv(cri, db);
573 QString where = conv.toString();
575 if (values.isEmpty()) {
581 for (
int i = obj.metaObject()->propertyOffset(); i < obj.metaObject()->propertyCount(); ++i) {
582 const char *propName = obj.metaObject()->property(i).name();
583 QByteArray prop = QByteArray(propName).toLower();
586 upd += QLatin1Char(
'=');
587 #if QT_VERSION < 0x060000
590 static const QMetaType metaType(QMetaType::QDateTime);
593 upd += QLatin1Char(
',');
598 auto it = values.begin();
600 upd += conv.propertyName(it.key(), db.driver());
601 upd += QLatin1Char(
'=');
604 if (++it == values.end()) {
607 upd += QLatin1Char(
',');
610 if (!where.isEmpty()) {
611 upd.append(QLatin1String(
" WHERE ")).append(where);
615 bool res = sqlQuery.
exec(upd);
626 QMap<int, QVariant> map;
627 map.insert(column,
value);
638 QSqlDatabase db = database();
639 QString del = db.driver()->sqlStatement(QSqlDriver::DeleteStatement,
640 T().tableName(), QSqlRecord(),
false);
641 TCriteriaConverter<T> conv(cri, db);
642 QString where = conv.toString();
648 if (!where.isEmpty()) {
649 del.append(QLatin1String(
" WHERE ")).append(where);
653 bool res = sqlQuery.
exec(del);
664 if (column < 0 ||
join.joinColumn() < 0) {
670 switch (
join.joinMode()) {
672 clause = QLatin1String(
" INNER JOIN ");
676 clause = QLatin1String(
" LEFT OUTER JOIN ");
680 clause = QLatin1String(
" RIGHT OUTER JOIN ");
687 int joinCount = joinClauses.count();
688 QString alias = QLatin1Char(
't') + QString::number(joinCount + 1);
689 QSqlDatabase db = database();
691 clause += C().tableName();
692 clause += QLatin1Char(
' ');
694 clause += QLatin1String(
" ON ");
695 clause += TCriteriaConverter<T>::getPropertyName(column, db.driver(),
"t0");
696 clause += QLatin1Char(
'=');
697 clause += TCriteriaConverter<C>::getPropertyName(
join.joinColumn(), db.driver(), alias);
698 joinClauses << clause;
700 if (!
join.criteria().isEmpty()) {
701 TCriteriaConverter<C> conv(
join.criteria(), db, alias);
702 joinWhereClauses << conv.toString();
720 setTable(tableName());
729 QSqlTableModel::clear();
736 joinWhereClauses.clear();
750 if (!sortColumns.isEmpty()) {
752 str += QLatin1String(
" ORDER BY ");
753 for (
auto &p : sortColumns) {
754 str += QLatin1String(
"t0.");
756 str += (p.second ==
Tf::AscendingOrder) ? QLatin1String(
" ASC,") : QLatin1String(
" DESC,");
The TAbstractSqlORMapper class is the abstract base class of functionality to object-relational mappi...
Definition: tsqlormapper.h:20
virtual int findCount(const TCriteria &cri=TCriteria())=0
virtual void setOffset(int offset)=0
virtual int findCountBy(int column, const QVariant &value)=0
virtual int updateAll(const TCriteria &cri, const QMap< int, QVariant > &values)=0
virtual int updateAll(const TCriteria &cri, int column, const QVariant &value)=0
virtual int find(const TCriteria &cri=TCriteria())=0
virtual void setLimit(int limit)=0
virtual ~TAbstractSqlORMapper()
Definition: tsqlormapper.h:23
TAbstractSqlORMapper(const QSqlDatabase &db)
Definition: tsqlormapper.h:22
virtual void setSortOrder(const QString &column, Tf::SortOrder order=Tf::AscendingOrder)=0
virtual int removeAll(const TCriteria &cri=TCriteria())=0
virtual void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)=0
virtual int findIn(int column, const QVariantList &values)=0
virtual int findBy(int column, const QVariant &value)=0
The TCriteria class represents a WHERE clause without SQL for the sake of database abstraction.
Definition: tcriteria.h:6
bool isEmpty() const
Returns true if the criteria has no data; otherwise returns false.
Definition: tcriteria.cpp:345
The TSqlJoin class represents JOIN clause for combination to a record of other table.
Definition: tsqljoin.h:14
Const iterator.
Definition: tsqlormapper.h:98
bool operator!=(const ConstIterator &o) const
Definition: tsqlormapper.h:114
ConstIterator & operator--()
Definition: tsqlormapper.h:126
ConstIterator operator--(int)
Definition: tsqlormapper.h:131
bool operator==(const ConstIterator &o) const
Definition: tsqlormapper.h:113
ConstIterator operator++(int)
Definition: tsqlormapper.h:120
const T operator*() const
Definition: tsqlormapper.h:112
ConstIterator(const ConstIterator &o)
Definition: tsqlormapper.h:104
ConstIterator & operator=(const ConstIterator &o)
Definition: tsqlormapper.h:106
ConstIterator & operator++()
Definition: tsqlormapper.h:115
const TSqlORMapper< T > * m
Definition: tsqlormapper.h:100
int it
Definition: tsqlormapper.h:101
ConstIterator()
Definition: tsqlormapper.h:103
The TSqlORMapper class is a template class that provides concise functionality to object-relational m...
Definition: tsqlormapper.h:53
int findIn(int column, const QVariantList &values)
Retrieves with the criteria that the column is within the list of values values returns the number of...
Definition: tsqlormapper.h:280
virtual void clear()
Clears and releases any acquired resource.
Definition: tsqlormapper.h:727
int find(const TCriteria &cri=TCriteria())
Retrieves with the criteria cri from the table and returns the number of the ORM objects.
Definition: tsqlormapper.h:241
TSqlORMapper()
Constructor.
Definition: tsqlormapper.h:168
virtual QString selectStatement() const
Returns a SELECT statement generated from the specified parameters.
Definition: tsqlormapper.h:450
T value(int i) const
Returns the ORM object in the results retrieved by find() function.
Definition: tsqlormapper.h:329
T findFirst(const TCriteria &cri=TCriteria())
Returns the first ORM object retrieved with the criteria cri from the table.
Definition: tsqlormapper.h:187
void setJoin(int column, const TSqlJoin< C > &join)
Sets a JOIN clause for column to join.
Definition: tsqlormapper.h:662
void setSortOrder(int column, Tf::SortOrder order=Tf::AscendingOrder)
Sets the sort order for column to order.
Definition: tsqlormapper.h:364
T last() const
Returns the last ORM object in the results retrieved by find() function.
Definition: tsqlormapper.h:318
int findBy(int column, const QVariant &value)
Retrieves with the criteria for the column as the value in the table and returns the number of the OR...
Definition: tsqlormapper.h:268
int findCount(const TCriteria &cri=TCriteria())
Returns the number of records retrieved with the criteria cri from the table.
Definition: tsqlormapper.h:521
T findFirstBy(int column, const QVariant &value)
Returns the first ORM object retrieved with the criteria for the column as the value in the table.
Definition: tsqlormapper.h:213
int rowCount() const
Returns the number of rows of the current query.
Definition: tsqlormapper.h:289
void setOffset(int offset)
Sets the offset to offset, which is the number of rows to skip for execution of SELECT statement.
Definition: tsqlormapper.h:355
TSqlORMapper< T > & join(int column, const TSqlJoin< C > &join)
Definition: tsqlormapper.h:708
int findCountBy(int column, const QVariant &value)
Returns the number of records retrieved with the criteria for the column as the value from the table.
Definition: tsqlormapper.h:551
void setLimit(int limit)
Sets the limit to limit, which is the limited number of rows for execution of SELECT statement.
Definition: tsqlormapper.h:345
virtual ~TSqlORMapper()
Destructor.
Definition: tsqlormapper.h:178
int updateAll(const TCriteria &cri, int column, const QVariant &value)
Updates the value of the specified column in all rows that satisfy the criteria cri and returns the n...
Definition: tsqlormapper.h:624
ConstIterator end() const
Definition: tsqlormapper.h:93
void reset()
Reset the internal state of the mapper object.
Definition: tsqlormapper.h:718
int removeAll(const TCriteria &cri=TCriteria())
Removes all rows based on the criteria cri from the table and returns the number of the rows affected...
Definition: tsqlormapper.h:636
T first() const
Returns the first ORM object in the results retrieved by find() function.
Definition: tsqlormapper.h:308
void setFilter(const QString &filter)
Sets the current filter to filter.
Definition: tsqlormapper.h:440
virtual QString orderByClause() const
Definition: tsqlormapper.h:146
QString orderBy() const
Returns a SQL WHERE clause generated from a criteria.
Definition: tsqlormapper.h:746
T findByPrimaryKey(const QVariant &pk)
Returns the ORM object retrieved with the primary key pk from the table.
Definition: tsqlormapper.h:223
ConstIterator begin() const
Definition: tsqlormapper.h:92
TSqlORMapper< T > & offset(int offset)
Sets the offset to offset, which is the number of rows to skip for execution of SELECT statement.
Definition: tsqlormapper.h:407
TSqlORMapper< T > & limit(int limit)
Sets the limit to limit, which is the limited number of rows for execution of SELECT statement.
Definition: tsqlormapper.h:396
The TSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: tsqlquery.h:6
QVariant value(int index) const
Returns the value of field index in the current record.
Definition: tsqlquery.h:74
bool exec(const QString &query)
Executes the SQL in query.
Definition: tsqlquery.cpp:220
int numRowsAffected() const
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined.
Definition: tsqlquery.h:56
static QString formatValue(const QVariant &val, const QMetaType &type, int databaseId=0)
Returns a string representation of the value val for the database databaseId.
Definition: tsqlquery.cpp:128
static QString escapeIdentifier(const QString &identifier, QSqlDriver::IdentifierType type=QSqlDriver::FieldName, int databaseId=0)
Returns the identifier escaped according to the rules of the database databaseId.
Definition: tsqlquery.cpp:102
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: tsqlquery.h:68
@ In
Definition: tfnamespace.h:292
@ InnerJoin
Definition: tfnamespace.h:301
@ RightJoin
Definition: tfnamespace.h:303
@ LeftJoin
Definition: tfnamespace.h:302
The Tf namespace contains miscellaneous identifiers used throughout the library of TreeFrog Framework...
Definition: tdebug.h:10
SortOrder
Definition: tfnamespace.h:133
@ AscendingOrder
Definition: tfnamespace.h:134
bool strcmp(const QByteArray &str1, const QByteArray &str2)
Definition: tglobal.h:294
T_CORE_EXPORT void writeQueryLog(const QString &query, bool success, const QSqlError &error, int duration)
Definition: tsystemglobal.cpp:202
T_CORE_EXPORT QSqlDatabase & currentSqlDatabase(int id) noexcept
Definition: tglobal.cpp:177
@ CaseInsensitive
Definition: tfnamespace.h:17
@ DateTime
Definition: tfnamespace.h:119
QSqlError lastError()
Returns the VariantMap object of the error status of the last operation.
Definition: tcachesqlitestore.cpp:26
#define T_DISABLE_COPY(Class)
Definition: tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition: tdeclexport.h:41
#define tWarn
Definition: tglobal.h:235
const QByteArray ModifiedAt("modifiedAt")
const QByteArray UpdatedAt("updatedAt")
void tSystemError(const char *msg,...)
Definition: tsystemglobal.cpp:134
void tSystemDebug(const char *msg,...)
Definition: tsystemglobal.cpp:162