]> git.sesse.net Git - pkanalytics/blob - formations.cpp
55e6de8c95f7d0429c8ff79bd19e6c109c1bb914
[pkanalytics] / formations.cpp
1 #include <string>
2 #include <vector>
3 #include <sqlite3.h>
4 #include "formations.h"
5
6 using namespace std;
7
8 FormationsModel::FormationsModel(sqlite3 *db, bool offense) : db(db), offense(offense)
9 {
10         load_data();
11 }
12
13 QVariant FormationsModel::headerData(int section, Qt::Orientation orientation, int role) const
14 {
15         if (role != Qt::DisplayRole) {
16                 return QVariant();
17         }
18         if (orientation == Qt::Horizontal) {
19                 if (section == 0) {
20                         return "Name";
21                 } else {
22                         return QVariant();
23                 }
24         } else {
25                 return "";
26         }
27 }
28
29 QVariant FormationsModel::data(const QModelIndex &index, int role) const
30 {
31         if (role == Qt::TextAlignmentRole) {
32                 return (Qt::AlignLeft | Qt::AlignVCenter).toInt();
33         }
34         if (role != Qt::DisplayRole) {
35                 return QVariant();
36         }
37         if (index.column() == 0) {
38                 if (index.row() == 0) {
39                         return QString::fromUtf8("(None/unknown)");
40                 } else if (index.row() == formations.size() + 1) {
41                         return QString::fromUtf8("Add new…");
42                 } else {
43                         return QString::fromUtf8(formations[index.row() - 1].name);
44                 }
45         }
46         return QVariant();
47 }
48
49 unsigned FormationsModel::insert_new(const std::string &name)
50 {
51         // Insert the new row into the database.
52         sqlite3_stmt *stmt;
53         int ret = sqlite3_prepare_v2(db, "INSERT INTO formation (name, offense) VALUES (?, ?)", -1, &stmt, 0);
54         if (ret != SQLITE_OK) {
55                 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
56                 abort();
57         }
58
59         sqlite3_bind_text(stmt, 1, name.data(), name.size(), SQLITE_STATIC);
60         sqlite3_bind_int(stmt, 2, offense);
61
62         ret = sqlite3_step(stmt);
63         if (ret == SQLITE_ROW) {
64                 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
65                 abort();
66         }
67
68         ret = sqlite3_finalize(stmt);
69         if (ret != SQLITE_OK) {
70                 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
71                 abort();
72         }
73
74         beginResetModel();  // Simplest for our use, though not ideal.
75
76         int formation_id = sqlite3_last_insert_rowid(db);
77         formations.push_back(Formation{ formation_id, name });
78
79         endResetModel();
80
81         return formation_id;
82 }
83
84 void FormationsModel::load_data()
85 {
86         formations.clear();
87
88         // Read the formations.
89         sqlite3_stmt *stmt;
90         int ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation WHERE offense=? ORDER BY name", -1, &stmt, 0);
91         if (ret != SQLITE_OK) {
92                 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
93                 abort();
94         }
95         sqlite3_bind_int(stmt, 1, offense);
96         for ( ;; ) {
97                 ret = sqlite3_step(stmt);
98                 if (ret == SQLITE_ROW) {
99                         Formation f;
100                         f.formation_id = sqlite3_column_int(stmt, 0);
101                         f.name = (const char *)sqlite3_column_text(stmt, 1);
102                         formations.push_back(f);
103                 } else if (ret == SQLITE_DONE) {
104                         break;
105                 } else {
106                         fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
107                         abort();
108                 }
109         }
110         ret = sqlite3_finalize(stmt);
111         if (ret != SQLITE_OK) {
112                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
113                 abort();
114         }
115 }