]> git.sesse.net Git - pkanalytics/blob - formations.cpp
Support filtering passes by thrower and receiver.
[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() == int(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         int formation_id = sqlite3_last_insert_rowid(db);
74
75         beginResetModel();  // Simplest for our use, though not ideal.
76         load_data();
77         endResetModel();
78
79         return formation_id;
80 }
81
82 void FormationsModel::load_data()
83 {
84         formations.clear();
85
86         // Read the formations.
87         sqlite3_stmt *stmt;
88         int ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation WHERE offense=? ORDER BY name", -1, &stmt, 0);
89         if (ret != SQLITE_OK) {
90                 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
91                 abort();
92         }
93         sqlite3_bind_int(stmt, 1, offense);
94         for ( ;; ) {
95                 ret = sqlite3_step(stmt);
96                 if (ret == SQLITE_ROW) {
97                         Formation f;
98                         f.formation_id = sqlite3_column_int(stmt, 0);
99                         f.name = (const char *)sqlite3_column_text(stmt, 1);
100                         formations.push_back(f);
101                 } else if (ret == SQLITE_DONE) {
102                         break;
103                 } else {
104                         fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
105                         abort();
106                 }
107         }
108         ret = sqlite3_finalize(stmt);
109         if (ret != SQLITE_OK) {
110                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
111                 abort();
112         }
113 }