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 171 172 173 174 175
|
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
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; either version 2
of the License, 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "configstore.h"
#include "global.h"
#include "sql/nsqlquery.h"
#include <QVariant>
extern Global global;
//**********************
// Generic constructor.
//**********************
ConfigStore::ConfigStore(DatabaseConnection *conn)
{
db = conn;
db->lockForRead();
// Check if the database exists. If not, create it.
NSqlQuery sql(db);
sql.exec("Select * from sqlite_master where type='table' and name='ConfigStore';");
if (!sql.next()) {
db->unlock();
sql.finish();
this->createTable();
return;
}
sql.finish();
db->unlock();
}
//********************************************
// Create the table & set the initial values
//********************************************
void ConfigStore::createTable() {
QLOG_TRACE() << "Entering ConfigStore::createTable()";
QLOG_DEBUG() << "Creating table ConfigStore";
NSqlQuery sql(db);
db->lockForWrite();
// build the SQL command & cretae the table
QString command("Create table if not exists ConfigStore (" +
QString("key integer primary key not null unique,") +
QString("value blob)"));
if (!sql.exec(command)) {
QLOG_ERROR() << "Creation of ConfigStore table failed: " << sql.lastError();
sql.finish();
db->unlock();
} else {
// insert default values into the new table
this->initTable();
sql.finish();
db->unlock();
}
}
//*******************************************
// Insert default values into the new table
//*******************************************
void ConfigStore::initTable() {
QLOG_TRACE() << "Entering ConfigStore::initTable()";
QLOG_DEBUG() << "Initializing table ConfigStore";
db->lockForWrite();
NSqlQuery sql(db);
sql.prepare("Insert into ConfigStore (key, value) values (:key, :value);");
sql.bindValue(":key", CONFIG_STORE_LID);
sql.bindValue(":value", 0);
if (!sql.exec()) {
QLOG_ERROR() << "Insertion of initial counter failed: " << sql.lastError();
}
sql.finish();
db->unlock();
}
//*******************************************************************
// Every time we add a new object, we call this to get its unique
// local ID. This number never changes
//*******************************************************************
qint32 ConfigStore::incrementLidCounter() {
db->lockForWrite();
NSqlQuery sql(db);
// Prepare the SQL statement & fetch the row
sql.prepare("Select value from ConfigStore where key=:key");
sql.bindValue(":key", CONFIG_STORE_LID);
if (!sql.exec()) {
QLOG_ERROR() << "Fetch of ConfigStore LID counter statement failed: " << sql.lastError();
}
if (!sql.next()) {
QLOG_ERROR() << "Fetch from ConfigStore failure: LID NOT FOUND!!!";
} else {
// Now that we have the next lid, increment the number & save it
qint32 sequence = QVariant(sql.value(0)).toInt();
sequence++;
db->lockForWrite();
sql.prepare("Update ConfigStore set value=:lid where key=:key;");
sql.bindValue(":lid",sequence);
sql.bindValue(":key", CONFIG_STORE_LID);
if (!sql.exec()) {
QLOG_ERROR() << "Error updating sequence number: " << sql.lastError();
}
sql.finish();
db->unlock();
// Return the next lid to the caller
return sequence;
}
sql.finish();
db->unlock();
return -1;
}
//*******************************************************************
// Save a setting to the DB
//*******************************************************************
void ConfigStore::saveSetting(int key, QByteArray value) {
NSqlQuery sql(db);
// Prepare the SQL statement & fetch the row
db->lockForWrite();
sql.prepare("Delete from ConfigStore where key=:key");
sql.bindValue(":key", key);
sql.exec();
sql.prepare("insert into ConfigStore (key, value) values (:key, :value)");
sql.bindValue(":key", key);
sql.bindValue(":value", value);
sql.exec();
sql.finish();
db->unlock();
}
//*******************************************************************
// Return a value from the DB
//*******************************************************************
bool ConfigStore::getSetting(QByteArray &value, int key) {
db->lockForRead();
NSqlQuery sql(db);
sql.prepare("select value from ConfigStore where key=:key");
sql.bindValue(":key", key);
sql.exec();
while (sql.next()) {
value = sql.value(0).toByteArray();
sql.finish();
db->unlock();
return true;
}
sql.finish();
db->unlock();
return false;
}
|