1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Copyright (c) 1997,2002 by Jim Lynch.
// Copyright (c) 2010-2012,2026 by Philipp Schafft.
// This software comes with NO WARRANTY WHATSOEVER.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 dated June, 1991, or, at your
// option, any LATER version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this software; see the file COPYING. If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
#include <iostream>
#include "sqlite-stuff.h"
static bool WriteInsertOnly(sqlite3 *db, const std::string &theKey, const std::string &theValue) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, "INSERT INTO animals (key, value) VALUES (?, ?)", -1, &stmt, NULL) != SQLITE_OK)
return false;
if (sqlite3_bind_text(stmt, 1, theKey.c_str(), -1, SQLITE_STATIC) != SQLITE_OK) {
sqlite3_finalize(stmt);
return false;
}
if (sqlite3_bind_text(stmt, 2, theValue.c_str(), -1, SQLITE_STATIC) != SQLITE_OK) {
sqlite3_finalize(stmt);
return false;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return true;
}
bool Write(sqlite3 *db, const std::string &theKey, const std::string &theValue) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, "DELETE FROM animals WHERE key = ?", -1, &stmt, NULL) != SQLITE_OK)
return false;
if (sqlite3_bind_text(stmt, 1, theKey.c_str(), -1, SQLITE_STATIC) != SQLITE_OK) {
sqlite3_finalize(stmt);
return false;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
return false;
}
sqlite3_finalize(stmt);
return WriteInsertOnly(db, theKey, theValue);
}
bool Read(sqlite3 *db, const std::string &theKey, std::string &theValue) {
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, "SELECT value FROM animals WHERE key = ?", -1, &stmt, NULL) != SQLITE_OK)
return false;
if (sqlite3_bind_text(stmt, 1, theKey.c_str(), -1, SQLITE_STATIC) != SQLITE_OK) {
sqlite3_finalize(stmt);
return false;
}
if (sqlite3_step(stmt) != SQLITE_ROW) {
sqlite3_finalize(stmt);
return false;
}
theValue = (const char*)sqlite3_column_text(stmt, 0);
sqlite3_finalize(stmt);
return true;
}
sqlite3 * OpenFile(std::string dbfile) {
sqlite3_stmt *stmt;
sqlite3 *ret;
if (!(dbfile.ends_with(".sqlite") || dbfile.ends_with(".sqlite3"))) {
dbfile += ".sqlite3";
}
if (sqlite3_open(dbfile.c_str(), &ret) != SQLITE_OK) {
std::cerr << "Error: Opening " << dbfile << " failed" << std::endl;
exit(1);
}
if (sqlite3_prepare_v2(ret, "CREATE TABLE IF NOT EXISTS animals (key VARCHAR(255) NOT NULL PRIMARY KEY, value VARCHAR(255) NOT NULL)", -1, &stmt, NULL) != SQLITE_OK) {
sqlite3_finalize(stmt);
std::cerr << "Error: Writing " << dbfile << std::endl;
exit(1);
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
sqlite3_finalize(stmt);
std::cerr << "Error: Writing " << dbfile << std::endl;
exit(1);
}
sqlite3_finalize(stmt);
WriteInsertOnly(ret, "last", "0"); // insert default value.
return ret;
}
void CloseFile(sqlite3 *db) {
sqlite3_close(db);
}
void SetLast(sqlite3 *db, const std::string &newKey) {
Write(db, "last", newKey);
}
std::string GetLast(sqlite3 *db) {
std::string result;
Read(db, "last", result);
return result;
}
void SetDbType(sqlite3 *db, const std::string &newType, bool plural) {
Write(db, plural ? "dbtype_plural" : "dbtype_singular", newType);
}
std::string GetDbType(sqlite3 *db, bool plural) {
std::string result;
bool rdOK = Read(db, plural ? "dbtype_plural" : "dbtype_singular", result);
// Test if we got something or use defaults:
if ( !rdOK || result.length() == 0 ) {
if (plural) {
result = GetDbType(db, false);
result += "s"; // add plural 's'
} else {
return "animal";
}
}
return result;
}
bool IsAnimal(sqlite3 *db, const std::string &theKey) {
std::string theValue;
Read(db, theKey, theValue);
return theValue[0] == 'a';
}
|