Initial commit
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
#include "sessions_model.h"
|
||||
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
|
||||
SessionsModel::SessionsModel(QObject *parent, const QSqlDatabase &database)
|
||||
: BaseSqlTableModel(parent, database) {}
|
||||
|
||||
void SessionsModel::initialize() {
|
||||
setTable("sessions");
|
||||
|
||||
// Set up the relationship with the movies and halls tables
|
||||
setRelation(fieldIndex("movie_id"),
|
||||
QSqlRelation("movies", "movie_id", "title"));
|
||||
setRelation(fieldIndex("hall_id"),
|
||||
QSqlRelation("halls", "hall_id", "hall_name"));
|
||||
|
||||
setEditStrategy(QSqlTableModel::OnFieldChange);
|
||||
|
||||
bool success = select();
|
||||
if (!success) {
|
||||
qWarning() << "Error:" << lastError().text();
|
||||
}
|
||||
}
|
||||
|
||||
bool SessionsModel::buildHallIdFilterCondition(const QString &inFilter,
|
||||
QString &outFilter) const {
|
||||
QSqlQuery query;
|
||||
query.prepare("SELECT hall_id FROM halls WHERE hall_name LIKE ?");
|
||||
query.addBindValue("%" + inFilter + "%");
|
||||
query.exec();
|
||||
|
||||
QStringList hallIds;
|
||||
while (query.next()) {
|
||||
hallIds << query.value(0).toString();
|
||||
}
|
||||
|
||||
if (hallIds.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outFilter = QString("sessions.hall_id IN (%1)").arg(hallIds.join(", "));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SessionsModel::buildMovieIdFilterCondition(const QString &inFilter,
|
||||
QString &outFilter) const {
|
||||
QSqlQuery query;
|
||||
query.prepare("SELECT movie_id FROM movies WHERE title LIKE ?");
|
||||
query.addBindValue("%" + inFilter + "%");
|
||||
query.exec();
|
||||
|
||||
QStringList movieIds;
|
||||
while (query.next()) {
|
||||
movieIds << query.value(0).toString();
|
||||
}
|
||||
|
||||
if (movieIds.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outFilter = QString("sessions.movie_id IN (%1)").arg(movieIds.join(", "));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QVariant SessionsModel::headerData(int section, Qt::Orientation orientation,
|
||||
int role) const {
|
||||
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
|
||||
switch (section) {
|
||||
case SessionsColumns::Id:
|
||||
return tr("ID");
|
||||
case SessionsColumns::MovieId:
|
||||
return tr("Movie Title");
|
||||
case SessionsColumns::HallId:
|
||||
return tr("Hall Name");
|
||||
case SessionsColumns::BeginAt:
|
||||
return tr("Start Time");
|
||||
case SessionsColumns::TicketPrice:
|
||||
return tr("Ticket Price");
|
||||
default:
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
return QSqlRelationalTableModel::headerData(section, orientation, role);
|
||||
}
|
||||
|
||||
QString SessionsModel::createMultiColumnFilter(const QString &filter) const {
|
||||
QStringList conditions;
|
||||
|
||||
QString buffer; // SessionsColumns::HallId And SessionsColumns::MovieId
|
||||
if (buildHallIdFilterCondition(filter, buffer)) {
|
||||
conditions.append(buffer);
|
||||
}
|
||||
if (buildMovieIdFilterCondition(filter, buffer)) {
|
||||
conditions.append(buffer);
|
||||
}
|
||||
|
||||
// WARNING: Does not filter these models transmitted through the data method,
|
||||
// such as QDatetime
|
||||
|
||||
using Columns = SessionsColumns;
|
||||
QStringList columns = {columnName(Columns::Id),
|
||||
columnName(Columns::TicketPrice)};
|
||||
|
||||
#ifndef NO_DATE_TIME_SEARCH
|
||||
columns.append(columnName(Columns::BeginAt));
|
||||
#endif
|
||||
|
||||
for (const QString &column : columns) {
|
||||
conditions.append(QString("%1 LIKE '%%2%'").arg(column).arg(filter));
|
||||
}
|
||||
|
||||
return conditions.join(" OR ");
|
||||
}
|
||||
Reference in New Issue
Block a user