]> git.sesse.net Git - pkanalytics/blobdiff - formations.cpp
Make columns sortable (no reverse sort).
[pkanalytics] / formations.cpp
index fa2337badaa538daf0977cb3bba95f814ea251d9..dc07a1f666b5d5790690f4e8ed152ff30e9d9d9b 100644 (file)
@@ -37,7 +37,7 @@ QVariant FormationsModel::data(const QModelIndex &index, int role) const
        if (index.column() == 0) {
                if (index.row() == 0) {
                        return QString::fromUtf8("(None/unknown)");
-               } else if (index.row() == formations.size() + 1) {
+               } else if (index.row() == int(formations.size() + 1)) {
                        return QString::fromUtf8("Add new…");
                } else {
                        return QString::fromUtf8(formations[index.row() - 1].name);
@@ -46,6 +46,39 @@ QVariant FormationsModel::data(const QModelIndex &index, int role) const
        return QVariant();
 }
 
+unsigned FormationsModel::insert_new(const std::string &name)
+{
+       // Insert the new row into the database.
+       sqlite3_stmt *stmt;
+       int ret = sqlite3_prepare_v2(db, "INSERT INTO formation (name, offense) VALUES (?, ?)", -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       sqlite3_bind_text(stmt, 1, name.data(), name.size(), SQLITE_STATIC);
+       sqlite3_bind_int(stmt, 2, offense);
+
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW) {
+               fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+       int formation_id = sqlite3_last_insert_rowid(db);
+
+       beginResetModel();  // Simplest for our use, though not ideal.
+       load_data();
+       endResetModel();
+
+       return formation_id;
+}
+
 void FormationsModel::load_data()
 {
        formations.clear();