EHM DAL 0.2.3
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_AbstractTableItem, 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
29 // File i/o
30 bool readStream(QDataStream &in) override;
31
32 // Insert data
33 bool insertRows(qint32 row, qint32 count, const QModelIndex &parent = QModelIndex()) override;
34
35 // Table model type
36 inline QString modelName() const override { return QStringLiteral("Game standard table"); }
37 inline constexpr AbstractBaseModel::AbstractTableModelType modelType() const override
38 {
39 return AbstractBaseModel::AbstractTableModelType::GameTable;
40 }
41
42 // Unknown/junk data
43 inline virtual qsizetype junkDataPrefixSize() const { return junk_data_prefix_.size(); }
44 //inline qsizetype junkDataSuffixSize() const { return junk_data_suffix_.size(); }
45
46private:
47 // Unknown/junk data
48 QByteArray junk_data_prefix_; // Start of file junk
49 //QByteArray junk_data_suffix_; // End of file junk
50};
51
52/* =================== */
53/* Game Table */
54/* =================== */
55
56template<class T_AbstractTableItem, ColumnData T_ColumnData>
57GameTable<T_AbstractTableItem, T_ColumnData>::GameTable(const QString &table_name,
58 const TableIndex table_id,
59 const TableType table_type)
60 : AbstractTable(table_name, table_id, table_type, new T_ColumnData())
61{}
62
63/* ================= */
64/* File i/o */
65/* ================= */
66
67template<class T_AbstractTableItem, ColumnData T_ColumnData>
69{
70 // Junk data (for saved game tables)
71 const auto junk_data_prefix_size{junkDataPrefixSize()};
72 if (junk_data_prefix_size > 0) {
73 junk_data_prefix_.resize(junk_data_prefix_size);
74 in.readRawData(junk_data_prefix_.data(), junk_data_prefix_size);
75 }
76
77 // Table header
78 auto error_count{0};
79 const auto record_count{table_attributes_.readTableHeader(in)};
80
82
83 qDebug().noquote() << QStringLiteral("Reading table:") << table_attributes_.name()
84 << QString("-> %L1 record(s)").arg(record_count);
85
86 // Table items/records
87 for (quint32 i = 0; i < record_count; ++i) {
88 auto item{std::make_unique<T_AbstractTableItem>()};
89 item->read(in, database_version);
90 item->fixErrors();
91 data_.emplace_back(std::move(item));
92 }
93
94 if (error_count)
95 qWarning().noquote() << QStringLiteral("%L1 error(s) encountered when reading %2")
96 .arg(error_count)
97 .arg(table_attributes_.name());
98
99 return error_count == 0;
100}
101
102/* ==================== */
103/* Insert Data */
104/* ==================== */
105
106template<class T_AbstractTableItem, ColumnData T_ColumnData>
108 qint32 count,
109 const QModelIndex &parent)
110{
111 if (count <= 0 || row < 0 || row > static_cast<qint32>(this->data_.size()))
112 return false;
113
114 beginInsertRows(parent, row, row + count - 1);
115
116 for (int i = 0; i < count; ++i) {
117 auto new_item{std::make_shared<T_AbstractTableItem>()};
118 new_item->alloc(static_cast<qint32>(this->data_.size()));
119 this->data_.insert(this->data_.begin() + row, std::move(new_item));
120 }
121
122 endInsertRows();
123 return true;
124}
125
126} // namespace ehm_dal::tables
The DatabaseVersion class represents the database version number.
Definition: database_version.h:12
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:68
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