EHM DAL 0.2.5
Data abstraction layer for Eastside Hockey Manager
Loading...
Searching...
No Matches
game_table.h
1#pragma once
2
3// Application headers
4#include "include/column_data/column_data.h"
5#include "include/database/database_version.h"
6#include "include/tables/abstract_table.h"
7
8// Qt headers
9#include <QDataStream>
10
11namespace ehm_dal::tables {
12
13// --- EHM database table template sub-class --- //
17template<class T_ColumnData>
18concept ColumnData = std::derived_from<T_ColumnData, ehm_dal::column_data::ColumnData>
19 || std::is_same_v<ehm_dal::column_data::ColumnData, T_ColumnData>;
20
21template<class T_AbstractIdTableItem, ColumnData T_ColumnData = ehm_dal::column_data::ColumnData>
23{
24public:
25 GameTable(const QString &table_name,
26 const ehm_dal::tables::TableIndex table_id,
27 const TableType table_type,
28 const qint16 minimum_supported_database_version = TableAttributes::NO_DATABASE_VERSION);
29
30 // File i/o
31 bool readStream(QDataStream &in) override;
32
33 // Insert data
34 bool insertRows(qint32 row, qint32 count, const QModelIndex &parent = QModelIndex()) override;
35
36 // Table model type
37 inline QString modelName() const override { return QStringLiteral("Game standard table"); }
38 inline constexpr AbstractBaseModel::AbstractTableModelType modelType() const override
39 {
40 return AbstractBaseModel::AbstractTableModelType::GameTable;
41 }
42
43 // Unknown/junk data
44 inline virtual qsizetype junkDataPrefixSize() const { return junk_data_prefix_.size(); }
45 //inline qsizetype junkDataSuffixSize() const { return junk_data_suffix_.size(); }
46
47private:
48 // Unknown/junk data
49 QByteArray junk_data_prefix_; // Start of file junk
50 //QByteArray junk_data_suffix_; // End of file junk
51};
52
53/* =================== */
54/* Game Table */
55/* =================== */
56
57template<class T_AbstractIdTableItem, ColumnData T_ColumnData>
58GameTable<T_AbstractIdTableItem, T_ColumnData>::GameTable(
59 const QString &table_name,
60 const TableIndex table_id,
61 const TableType table_type,
62 const qint16 minimum_supported_database_version)
63 : AbstractTable(table_name,
64 table_id,
65 table_type,
66 new T_ColumnData(),
67 minimum_supported_database_version)
68{}
69
70/* ================= */
71/* File i/o */
72/* ================= */
73
74template<class T_AbstractIdTableItem, ColumnData T_ColumnData>
76{
78
79 if (table_attributes_.minimumSupportedDatabaseVersion() > database_version.version()) {
80 qDebug().noquote() << QStringLiteral(
81 "Skipped table: %L1 (Min DB version %2 | Actual DB version %3)")
82 .arg(table_attributes_.name())
83 .arg(table_attributes_.minimumSupportedDatabaseVersion())
84 .arg(database_version.version());
85 return true; // Abort if the database version does not support the table
86 }
87
88 // Junk data (for saved game tables)
89 const auto junk_data_prefix_size{junkDataPrefixSize()};
90 if (junk_data_prefix_size > 0) {
91 junk_data_prefix_.resize(junk_data_prefix_size);
92 in.readRawData(junk_data_prefix_.data(), junk_data_prefix_size);
93 }
94
95 // Table header
96 auto error_count{0};
97 const auto record_count{table_attributes_.readTableHeader(in)};
98
99 qDebug().noquote() << QStringLiteral("Reading table: %L1 -> %L2 record(s)")
100 .arg(table_attributes_.name())
101 .arg(record_count);
102
103 // Table items/records
104 for (quint32 i = 0; i < record_count; ++i) {
105 auto item{std::make_unique<T_AbstractIdTableItem>()};
106 item->read(in, database_version);
107 item->fixErrors();
108 data_.emplace_back(std::move(item));
109 }
110
111 if (error_count)
112 qWarning().noquote() << QStringLiteral("%L1 error(s) encountered when reading %2")
113 .arg(error_count)
114 .arg(table_attributes_.name());
115
116 return error_count == 0;
117}
118
119/* ==================== */
120/* Insert Data */
121/* ==================== */
122
123template<class T_AbstractIdTableItem, ColumnData T_ColumnData>
125 qint32 count,
126 const QModelIndex &parent)
127{
128 if (count <= 0 || row < 0 || row > static_cast<qint32>(this->data_.size()))
129 return false;
130
131 beginInsertRows(parent, row, row + count - 1);
132
133 for (int i = 0; i < count; ++i) {
134 auto new_item{std::make_shared<T_AbstractIdTableItem>()};
135 new_item->alloc(static_cast<qint32>(this->data_.size()));
136 this->data_.insert(this->data_.begin() + row, std::move(new_item));
137 }
138
139 endInsertRows();
140 return true;
141}
142
143} // namespace ehm_dal::tables
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
qint16 version() const
Returns the database version number.
Definition: database_version.h:27
The Table class represents a database table and provides access to rows of data.
Definition: abstract_table.h:29
Definition: game_table.h:23
bool readStream(QDataStream &in) override
Reads the table data from a QDataStream.
Definition: game_table.h:75
The GameTable class is a template sub-class of Table and is intended for use as a database table.
Definition: game_table.h:18
namespace ehm_dal::tables
Definition: column.h:4
TableType
The TableType enum denotes different types of database table.
Definition: table_type.h:12
TableIndex
The TableIndex enum represents the id number of each table within the database.
Definition: table_index.h:14