Remove Configuration module and libsqlite dependency
Change-Id: I823aea91367d586507bbf352f1b6f25bdd635baa
This commit is contained in:
parent
de1685f6d7
commit
caf2abc58f
3
AUTHORS
3
AUTHORS
|
@ -24,9 +24,6 @@ David A. Burgess, dburgess@kestrelsp.com:
|
|||
CommonLibs/Assert.h
|
||||
CommonLibs/BitVector.cpp
|
||||
CommonLibs/BitVectorTest.cpp
|
||||
CommonLibs/Configuration.cpp
|
||||
CommonLibs/Configuration.h
|
||||
CommonLibs/ConfigurationTest.cpp
|
||||
CommonLibs/Interthread.h
|
||||
CommonLibs/InterthreadTest.cpp
|
||||
CommonLibs/LinkedLists.cpp
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,422 +0,0 @@
|
|||
/*
|
||||
* Copyright 2009, 2010 Free Software Foundation, Inc.
|
||||
* Copyright 2010 Kestrel Signal Processing, Inc.
|
||||
* Copyright 2011, 2012 Range Networks, Inc.
|
||||
*
|
||||
* This software is distributed under the terms of the GNU Affero Public License.
|
||||
* See the COPYING file in the main directory for details.
|
||||
*
|
||||
* This use of this software may be subject to additional restrictions.
|
||||
* See the LEGAL file in the main directory for details.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CONFIGURATION_H
|
||||
#define CONFIGURATION_H
|
||||
|
||||
|
||||
#include "sqlite3util.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <Threads.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/** A class for configuration file errors. */
|
||||
class ConfigurationTableError {};
|
||||
extern char gCmdName[]; // Gotta be global, gotta be char*, gotta love it.
|
||||
|
||||
/** An exception thrown when a given config key isn't found. */
|
||||
class ConfigurationTableKeyNotFound : public ConfigurationTableError {
|
||||
|
||||
private:
|
||||
|
||||
std::string mKey;
|
||||
|
||||
public:
|
||||
|
||||
ConfigurationTableKeyNotFound(const std::string& wKey)
|
||||
:mKey(wKey)
|
||||
{ }
|
||||
|
||||
const std::string& key() const { return mKey; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
class ConfigurationRecord {
|
||||
|
||||
private:
|
||||
|
||||
std::string mValue;
|
||||
long mNumber;
|
||||
bool mDefined;
|
||||
|
||||
public:
|
||||
|
||||
ConfigurationRecord(bool wDefined=true):
|
||||
mDefined(wDefined)
|
||||
{ }
|
||||
|
||||
ConfigurationRecord(const std::string& wValue):
|
||||
mValue(wValue),
|
||||
mNumber(strtol(wValue.c_str(),NULL,0)),
|
||||
mDefined(true)
|
||||
{ }
|
||||
|
||||
ConfigurationRecord(const char* wValue):
|
||||
mValue(std::string(wValue)),
|
||||
mNumber(strtol(wValue,NULL,0)),
|
||||
mDefined(true)
|
||||
{ }
|
||||
|
||||
|
||||
const std::string& value() const { return mValue; }
|
||||
long number() const { return mNumber; }
|
||||
bool defined() const { return mDefined; }
|
||||
|
||||
float floatNumber() const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/** A string class that uses a hash function for comparison. */
|
||||
class HashString : public std::string {
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
uint64_t mHash;
|
||||
|
||||
void computeHash();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
HashString(const char* src)
|
||||
:std::string(src)
|
||||
{
|
||||
computeHash();
|
||||
}
|
||||
|
||||
HashString(const std::string& src)
|
||||
:std::string(src)
|
||||
{
|
||||
computeHash();
|
||||
}
|
||||
|
||||
HashString()
|
||||
{
|
||||
mHash=0;
|
||||
}
|
||||
|
||||
HashString& operator=(std::string& src)
|
||||
{
|
||||
std::string::operator=(src);
|
||||
computeHash();
|
||||
return *this;
|
||||
}
|
||||
|
||||
HashString& operator=(const char* src)
|
||||
{
|
||||
std::string::operator=(src);
|
||||
computeHash();
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const HashString& other)
|
||||
{
|
||||
return mHash==other.mHash;
|
||||
}
|
||||
|
||||
bool operator<(const HashString& other)
|
||||
{
|
||||
return mHash<other.mHash;
|
||||
}
|
||||
|
||||
bool operator>(const HashString& other)
|
||||
{
|
||||
return mHash<other.mHash;
|
||||
}
|
||||
|
||||
uint64_t hash() const { return mHash; }
|
||||
|
||||
};
|
||||
|
||||
|
||||
typedef std::map<std::string, ConfigurationRecord> ConfigurationRecordMap;
|
||||
typedef std::map<HashString, ConfigurationRecord> ConfigurationMap;
|
||||
class ConfigurationKey;
|
||||
typedef std::map<std::string, ConfigurationKey> ConfigurationKeyMap;
|
||||
|
||||
/**
|
||||
A class for maintaining a configuration key-value table,
|
||||
based on sqlite3 and a local map-based cache.
|
||||
Thread-safe, too.
|
||||
*/
|
||||
class ConfigurationTable {
|
||||
|
||||
private:
|
||||
|
||||
sqlite3* mDB; ///< database connection
|
||||
ConfigurationMap mCache; ///< cache of recently access configuration values
|
||||
mutable Mutex mLock; ///< control for multithreaded access to the cache
|
||||
std::vector<std::string> (*mCrossCheck)(const std::string&); ///< cross check callback pointer
|
||||
|
||||
public:
|
||||
|
||||
ConfigurationKeyMap mSchema;///< definition of configuration default values and validation logic
|
||||
|
||||
ConfigurationTable(const char* filename = ":memory:", const char *wCmdName = 0, ConfigurationKeyMap wSchema = ConfigurationKeyMap());
|
||||
|
||||
/** Generate an up-to-date example sql file for new installs. */
|
||||
std::string getDefaultSQL(const std::string& program, const std::string& version);
|
||||
|
||||
/** Generate an up-to-date TeX snippet. */
|
||||
std::string getTeX(const std::string& program, const std::string& version);
|
||||
|
||||
/** Return true if the key is used in the table. */
|
||||
bool defines(const std::string& key);
|
||||
|
||||
/** Return true if the application's schema knows about this key. */
|
||||
bool keyDefinedInSchema(const std::string& name);
|
||||
|
||||
/** Return true if the provided value validates correctly against the defined schema. */
|
||||
bool isValidValue(const std::string& name, const std::string& val);
|
||||
|
||||
/** Return true if the provided value validates correctly against the defined schema. */
|
||||
bool isValidValue(const std::string& name, const int val) { std::stringstream ss; ss << val; return isValidValue(name, ss.str()); }
|
||||
|
||||
/** Return a map of all similar keys in the defined schema. */
|
||||
ConfigurationKeyMap getSimilarKeys(const std::string& snippet);
|
||||
|
||||
/** Return true if this key is identified as static. */
|
||||
bool isStatic(const std::string& key);
|
||||
|
||||
/**
|
||||
Get a string parameter from the table.
|
||||
Throw ConfigurationTableKeyNotFound if not found.
|
||||
*/
|
||||
std::string getStr(const std::string& key);
|
||||
|
||||
|
||||
/**
|
||||
Get a boolean from the table.
|
||||
Return false if NULL or 0, true otherwise.
|
||||
*/
|
||||
bool getBool(const std::string& key);
|
||||
|
||||
/**
|
||||
Get a numeric parameter from the table.
|
||||
Throw ConfigurationTableKeyNotFound if not found.
|
||||
*/
|
||||
long getNum(const std::string& key);
|
||||
|
||||
/**
|
||||
Get a vector of strings from the table.
|
||||
*/
|
||||
std::vector<std::string> getVectorOfStrings(const std::string& key);
|
||||
|
||||
/**
|
||||
Get a float from the table.
|
||||
Throw ConfigurationTableKeyNotFound if not found.
|
||||
*/
|
||||
float getFloat(const std::string& key);
|
||||
|
||||
/**
|
||||
Get a numeric vector from the table.
|
||||
*/
|
||||
std::vector<unsigned> getVector(const std::string& key);
|
||||
|
||||
/** Get length of a vector */
|
||||
unsigned getVectorLength(const std::string &key)
|
||||
{ return getVector(key).size(); }
|
||||
|
||||
/** Set or change a value in the table. */
|
||||
bool set(const std::string& key, const std::string& value);
|
||||
|
||||
/** Set or change a value in the table. */
|
||||
bool set(const std::string& key, long value);
|
||||
|
||||
/** Create an entry in the table, no value though. */
|
||||
bool set(const std::string& key);
|
||||
|
||||
/**
|
||||
Remove an entry from the table.
|
||||
Will not alter required values.
|
||||
@param key The key of the item to be removed.
|
||||
@return true if anything was actually removed.
|
||||
*/
|
||||
bool remove(const std::string& key);
|
||||
|
||||
/** Search the table, dumping to a stream. */
|
||||
void find(const std::string& pattern, std::ostream&) const;
|
||||
|
||||
/** Return all key/value pairs stored in the ConfigurationTable */
|
||||
ConfigurationRecordMap getAllPairs() const;
|
||||
|
||||
/** Define the callback to purge the cache whenever the database changes. */
|
||||
void setUpdateHook(void(*)(void *,int ,char const *,char const *,sqlite3_int64));
|
||||
|
||||
/** Define the callback for cross checking. */
|
||||
void setCrossCheckHook(std::vector<std::string> (*wCrossCheck)(const std::string&));
|
||||
|
||||
/** Execute the application specific value cross checking logic. */
|
||||
std::vector<std::string> crossCheck(const std::string& key);
|
||||
|
||||
/** purege cache if it exceeds a certain age */
|
||||
void checkCacheAge();
|
||||
|
||||
/** Delete all records from the cache. */
|
||||
void purge();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
Attempt to lookup a record, cache if needed.
|
||||
Throw ConfigurationTableKeyNotFound if not found.
|
||||
Caller should hold mLock because the returned reference points into the cache.
|
||||
*/
|
||||
const ConfigurationRecord& lookup(const std::string& key);
|
||||
|
||||
};
|
||||
|
||||
|
||||
typedef std::map<HashString, std::string> HashStringMap;
|
||||
|
||||
class SimpleKeyValue {
|
||||
|
||||
protected:
|
||||
|
||||
HashStringMap mMap;
|
||||
|
||||
public:
|
||||
|
||||
/** Take a C string "A=B" and set map["A"]="B". */
|
||||
void addItem(const char*);
|
||||
|
||||
/** Take a C string "A=B C=D E=F ..." and add all of the pairs to the map. */
|
||||
void addItems(const char*s);
|
||||
|
||||
/** Return a reference to the string at map["key"]. */
|
||||
const char* get(const char*) const;
|
||||
};
|
||||
|
||||
|
||||
class ConfigurationKey {
|
||||
|
||||
public:
|
||||
|
||||
enum VisibilityLevel
|
||||
{
|
||||
CUSTOMER,
|
||||
CUSTOMERSITE,
|
||||
CUSTOMERTUNE,
|
||||
CUSTOMERWARN,
|
||||
DEVELOPER,
|
||||
FACTORY
|
||||
};
|
||||
|
||||
enum Type
|
||||
{
|
||||
BOOLEAN,
|
||||
CHOICE_OPT,
|
||||
CHOICE,
|
||||
CIDR_OPT,
|
||||
CIDR,
|
||||
FILEPATH_OPT,
|
||||
FILEPATH,
|
||||
IPADDRESS_OPT,
|
||||
IPADDRESS,
|
||||
IPANDPORT,
|
||||
MIPADDRESS_OPT,
|
||||
MIPADDRESS,
|
||||
PORT_OPT,
|
||||
PORT,
|
||||
REGEX_OPT,
|
||||
REGEX,
|
||||
STRING_OPT,
|
||||
STRING,
|
||||
VALRANGE
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
std::string mName;
|
||||
std::string mDefaultValue;
|
||||
std::string mUnits;
|
||||
VisibilityLevel mVisibility;
|
||||
Type mType;
|
||||
std::string mValidValues;
|
||||
bool mIsStatic;
|
||||
std::string mDescription;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
ConfigurationKey(const std::string& wName, const std::string& wDefaultValue, const std::string& wUnits, const VisibilityLevel wVisibility, const Type wType, const std::string& wValidValues, bool wIsStatic, const std::string& wDescription):
|
||||
mName(wName),
|
||||
mDefaultValue(wDefaultValue),
|
||||
mUnits(wUnits),
|
||||
mVisibility(wVisibility),
|
||||
mType(wType),
|
||||
mValidValues(wValidValues),
|
||||
mIsStatic(wIsStatic),
|
||||
mDescription(wDescription)
|
||||
{ }
|
||||
|
||||
ConfigurationKey()
|
||||
{ }
|
||||
|
||||
const std::string& getName() const { return mName; }
|
||||
const std::string& getDefaultValue() const { return mDefaultValue; }
|
||||
void updateDefaultValue(const std::string& newValue) { mDefaultValue = newValue; }
|
||||
void updateDefaultValue(const int newValue) { std::stringstream ss; ss << newValue; updateDefaultValue(ss.str()); }
|
||||
const std::string& getUnits() const { return mUnits; }
|
||||
const VisibilityLevel& getVisibility() const { return mVisibility; }
|
||||
const Type& getType() const { return mType; }
|
||||
const std::string& getValidValues() const { return mValidValues; }
|
||||
bool isStatic() const { return mIsStatic; }
|
||||
const std::string& getDescription() const { return mDescription; }
|
||||
|
||||
static bool isValidIP(const std::string& ip);
|
||||
static void getMinMaxStepping(const ConfigurationKey &key, std::string &min, std::string &max, std::string &stepping);
|
||||
template<class T> static bool isInValRange(const ConfigurationKey &key, const std::string& val, const bool isInteger);
|
||||
static const std::string visibilityLevelToString(const VisibilityLevel& visibility);
|
||||
static const std::string typeToString(const ConfigurationKey::Type& type);
|
||||
static void printKey(const ConfigurationKey &key, const std::string& currentValue, std::ostream& os);
|
||||
static void printDescription(const ConfigurationKey &key, std::ostream& os);
|
||||
static const std::string getARFCNsString();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// vim: ts=4 sw=4
|
|
@ -1,149 +0,0 @@
|
|||
/*
|
||||
* Copyright 2009, 2010 Free Software Foundation, Inc.
|
||||
* Copyright 2010 Kestrel Signal Processing, Inc.
|
||||
*
|
||||
*
|
||||
* This software is distributed under the terms of the GNU Affero Public License.
|
||||
* See the COPYING file in the main directory for details.
|
||||
*
|
||||
* This use of this software may be subject to additional restrictions.
|
||||
* See the LEGAL file in the main directory for details.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "Configuration.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ConfigurationKeyMap getConfigurationKeys();
|
||||
ConfigurationTable gConfig("exampleconfig.db","test", getConfigurationKeys());
|
||||
|
||||
void purgeConfig(void*,int,char const*, char const*, sqlite3_int64)
|
||||
{
|
||||
//cout << "update hook" << endl;
|
||||
gConfig.purge();
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
gConfig.setUpdateHook(purgeConfig);
|
||||
|
||||
const char *keys[5] = {"key1", "key2", "key3", "key4", "key5"};
|
||||
|
||||
for (int i=0; i<5; i++) {
|
||||
gConfig.set(keys[i],i);
|
||||
}
|
||||
|
||||
for (int i=0; i<5; i++) {
|
||||
cout << "table[" << keys[i] << "]=" << gConfig.getStr(keys[i]) << endl;
|
||||
cout << "table[" << keys[i] << "]=" << gConfig.getNum(keys[i]) << endl;
|
||||
}
|
||||
|
||||
for (int i=0; i<5; i++) {
|
||||
cout << "defined table[" << keys[i] << "]=" << gConfig.defines(keys[i]) << endl;
|
||||
}
|
||||
|
||||
gConfig.set("key5","100 200 300 400 ");
|
||||
std::vector<unsigned> vect = gConfig.getVector("key5");
|
||||
cout << "vect length " << vect.size() << ": ";
|
||||
for (unsigned i=0; i<vect.size(); i++) cout << " " << vect[i];
|
||||
cout << endl;
|
||||
std::vector<string> svect = gConfig.getVectorOfStrings("key5");
|
||||
cout << "vect length " << svect.size() << ": ";
|
||||
for (unsigned i=0; i<svect.size(); i++) cout << " " << svect[i] << ":";
|
||||
cout << endl;
|
||||
|
||||
cout << "bool " << gConfig.getBool("booltest") << endl;
|
||||
gConfig.set("booltest",1);
|
||||
cout << "bool " << gConfig.getBool("booltest") << endl;
|
||||
gConfig.set("booltest",0);
|
||||
cout << "bool " << gConfig.getBool("booltest") << endl;
|
||||
|
||||
gConfig.getStr("newstring");
|
||||
gConfig.getNum("numnumber");
|
||||
|
||||
|
||||
SimpleKeyValue pairs;
|
||||
pairs.addItems(" a=1 b=34 dd=143 ");
|
||||
cout<< pairs.get("a") << endl;
|
||||
cout<< pairs.get("b") << endl;
|
||||
cout<< pairs.get("dd") << endl;
|
||||
|
||||
gConfig.set("fkey","123.456");
|
||||
float fval = gConfig.getFloat("fkey");
|
||||
cout << "fkey " << fval << endl;
|
||||
|
||||
cout << "search fkey:" << endl;
|
||||
gConfig.find("fkey",cout);
|
||||
cout << "search fkey:" << endl;
|
||||
gConfig.find("fkey",cout);
|
||||
gConfig.remove("fkey");
|
||||
cout << "search fkey:" << endl;
|
||||
gConfig.find("fkey",cout);
|
||||
|
||||
try {
|
||||
gConfig.getNum("supposedtoabort");
|
||||
} catch (ConfigurationTableKeyNotFound) {
|
||||
cout << "ConfigurationTableKeyNotFound exception successfully caught." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurationKeyMap getConfigurationKeys()
|
||||
{
|
||||
ConfigurationKeyMap map;
|
||||
ConfigurationKey *tmp;
|
||||
|
||||
tmp = new ConfigurationKey("booltest","0",
|
||||
"",
|
||||
ConfigurationKey::DEVELOPER,
|
||||
ConfigurationKey::BOOLEAN,
|
||||
"",
|
||||
false,
|
||||
""
|
||||
);
|
||||
map[tmp->getName()] = *tmp;
|
||||
free(tmp);
|
||||
|
||||
tmp = new ConfigurationKey("numnumber","42",
|
||||
"",
|
||||
ConfigurationKey::DEVELOPER,
|
||||
ConfigurationKey::VALRANGE,
|
||||
"0-100",
|
||||
false,
|
||||
""
|
||||
);
|
||||
map[tmp->getName()] = *tmp;
|
||||
free(tmp);
|
||||
|
||||
tmp = new ConfigurationKey("newstring","new string value",
|
||||
"",
|
||||
ConfigurationKey::DEVELOPER,
|
||||
ConfigurationKey::STRING,
|
||||
"",
|
||||
false,
|
||||
""
|
||||
);
|
||||
map[tmp->getName()] = *tmp;
|
||||
free(tmp);
|
||||
|
||||
return map;
|
||||
}
|
|
@ -36,9 +36,7 @@ libcommon_la_SOURCES = \
|
|||
Sockets.cpp \
|
||||
Threads.cpp \
|
||||
Timeval.cpp \
|
||||
Logger.cpp \
|
||||
Configuration.cpp \
|
||||
sqlite3util.cpp
|
||||
Logger.cpp
|
||||
|
||||
noinst_PROGRAMS = \
|
||||
BitVectorTest \
|
||||
|
@ -47,7 +45,6 @@ noinst_PROGRAMS = \
|
|||
SocketsTest \
|
||||
TimevalTest \
|
||||
VectorTest \
|
||||
ConfigurationTest \
|
||||
LogTest
|
||||
|
||||
# ReportingTest
|
||||
|
@ -61,12 +58,10 @@ noinst_HEADERS = \
|
|||
Threads.h \
|
||||
Timeval.h \
|
||||
Vector.h \
|
||||
Configuration.h \
|
||||
Logger.h \
|
||||
sqlite3util.h
|
||||
Logger.h
|
||||
|
||||
BitVectorTest_SOURCES = BitVectorTest.cpp
|
||||
BitVectorTest_LDADD = libcommon.la $(SQLITE3_LIBS)
|
||||
BitVectorTest_LDADD = libcommon.la
|
||||
|
||||
PRBSTest_SOURCES = PRBSTest.cpp
|
||||
|
||||
|
@ -82,15 +77,12 @@ TimevalTest_SOURCES = TimevalTest.cpp
|
|||
TimevalTest_LDADD = libcommon.la
|
||||
|
||||
VectorTest_SOURCES = VectorTest.cpp
|
||||
VectorTest_LDADD = libcommon.la $(SQLITE3_LIBS)
|
||||
|
||||
ConfigurationTest_SOURCES = ConfigurationTest.cpp
|
||||
ConfigurationTest_LDADD = libcommon.la $(SQLITE3_LIBS)
|
||||
VectorTest_LDADD = libcommon.la
|
||||
|
||||
# ReportingTest_SOURCES = ReportingTest.cpp
|
||||
# ReportingTest_LDADD = libcommon.la $(SQLITE_LA)
|
||||
# ReportingTest_LDADD = libcommon.la
|
||||
|
||||
LogTest_SOURCES = LogTest.cpp
|
||||
LogTest_LDADD = libcommon.la $(SQLITE3_LIBS)
|
||||
LogTest_LDADD = libcommon.la
|
||||
|
||||
MOSTLYCLEANFILES += testSource testDestination
|
||||
|
|
|
@ -1,154 +0,0 @@
|
|||
/*
|
||||
* Copyright 2010 Kestrel Signal Processing, Inc.
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include "sqlite3util.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
// Wrappers to sqlite operations.
|
||||
// These will eventually get moved to commonlibs.
|
||||
|
||||
int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query)
|
||||
{
|
||||
int src = SQLITE_BUSY;
|
||||
while (src==SQLITE_BUSY) {
|
||||
src = sqlite3_prepare_v2(DB,query,strlen(query),stmt,NULL);
|
||||
if (src==SQLITE_BUSY) {
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
if (src) {
|
||||
fprintf(stderr,"sqlite3_prepare_v2 failed for \"%s\": %s\n",query,sqlite3_errmsg(DB));
|
||||
sqlite3_finalize(*stmt);
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt)
|
||||
{
|
||||
int src = SQLITE_BUSY;
|
||||
while (src==SQLITE_BUSY) {
|
||||
src = sqlite3_step(stmt);
|
||||
if (src==SQLITE_BUSY) {
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
if ((src!=SQLITE_DONE) && (src!=SQLITE_ROW)) {
|
||||
fprintf(stderr,"sqlite3_run_query failed: %s: %s\n", sqlite3_sql(stmt), sqlite3_errmsg(DB));
|
||||
}
|
||||
return src;
|
||||
}
|
||||
|
||||
|
||||
bool sqlite3_exists(sqlite3* DB, const char *tableName,
|
||||
const char* keyName, const char* keyData)
|
||||
{
|
||||
size_t stringSize = 100 + strlen(tableName) + strlen(keyName) + strlen(keyData);
|
||||
char query[stringSize];
|
||||
sprintf(query,"SELECT * FROM %s WHERE %s == \"%s\"",tableName,keyName,keyData);
|
||||
// Prepare the statement.
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
|
||||
// Read the result.
|
||||
int src = sqlite3_run_query(DB,stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
// Anything there?
|
||||
return (src == SQLITE_ROW);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char *tableName,
|
||||
const char* keyName, const char* keyData,
|
||||
const char* valueName, unsigned &valueData)
|
||||
{
|
||||
size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData);
|
||||
char query[stringSize];
|
||||
sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData);
|
||||
// Prepare the statement.
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
|
||||
// Read the result.
|
||||
int src = sqlite3_run_query(DB,stmt);
|
||||
bool retVal = false;
|
||||
if (src == SQLITE_ROW) {
|
||||
valueData = (unsigned)sqlite3_column_int64(stmt,0);
|
||||
retVal = true;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
// This function returns an allocated string that must be free'd by the caller.
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char* tableName,
|
||||
const char* keyName, const char* keyData,
|
||||
const char* valueName, char* &valueData)
|
||||
{
|
||||
valueData=NULL;
|
||||
size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData);
|
||||
char query[stringSize];
|
||||
sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData);
|
||||
// Prepare the statement.
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
|
||||
// Read the result.
|
||||
int src = sqlite3_run_query(DB,stmt);
|
||||
bool retVal = false;
|
||||
if (src == SQLITE_ROW) {
|
||||
const char* ptr = (const char*)sqlite3_column_text(stmt,0);
|
||||
if (ptr) valueData = strdup(ptr);
|
||||
retVal = true;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
// This function returns an allocated string that must be free'd by tha caller.
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char* tableName,
|
||||
const char* keyName, unsigned keyData,
|
||||
const char* valueName, char* &valueData)
|
||||
{
|
||||
valueData=NULL;
|
||||
size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + 20;
|
||||
char query[stringSize];
|
||||
sprintf(query,"SELECT %s FROM %s WHERE %s == %u",valueName,tableName,keyName,keyData);
|
||||
// Prepare the statement.
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
|
||||
// Read the result.
|
||||
int src = sqlite3_run_query(DB,stmt);
|
||||
bool retVal = false;
|
||||
if (src == SQLITE_ROW) {
|
||||
const char* ptr = (const char*)sqlite3_column_text(stmt,0);
|
||||
if (ptr) valueData = strdup(ptr);
|
||||
retVal = true;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool sqlite3_command(sqlite3* DB, const char* query)
|
||||
{
|
||||
// Prepare the statement.
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
|
||||
// Run the query.
|
||||
int src = sqlite3_run_query(DB,stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
return src==SQLITE_DONE;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
#ifndef SQLITE3UTIL_H
|
||||
#define SQLITE3UTIL_H
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query);
|
||||
|
||||
int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt);
|
||||
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char *tableName,
|
||||
const char* keyName, const char* keyData,
|
||||
const char* valueName, unsigned &valueData);
|
||||
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char* tableName,
|
||||
const char* keyName, const char* keyData,
|
||||
const char* valueName, char* &valueData);
|
||||
|
||||
// This function returns an allocated string that must be free'd by the caller.
|
||||
bool sqlite3_single_lookup(sqlite3* DB, const char* tableName,
|
||||
const char* keyName, unsigned keyData,
|
||||
const char* valueName, char* &valueData);
|
||||
|
||||
bool sqlite3_exists(sqlite3* DB, const char* tableName,
|
||||
const char* keyName, const char* keyData);
|
||||
|
||||
/** Run a query, ignoring the result; return true on success. */
|
||||
bool sqlite3_command(sqlite3* DB, const char* query);
|
||||
|
||||
#endif
|
|
@ -21,7 +21,7 @@
|
|||
include $(top_srcdir)/Makefile.common
|
||||
|
||||
ACLOCAL_AMFLAGS = -I config
|
||||
AM_CPPFLAGS = $(STD_DEFINES_AND_INCLUDES) $(USB_INCLUDES) $(WITH_INCLUDES) $(SQLITE3_CFLAGS)
|
||||
AM_CPPFLAGS = $(STD_DEFINES_AND_INCLUDES) $(USB_INCLUDES) $(WITH_INCLUDES)
|
||||
AM_CXXFLAGS = -Wall -pthread -ldl
|
||||
#AM_CXXFLAGS = -Wall -O2 -NDEBUG -pthread -ldl
|
||||
#AM_CFLAGS = -Wall -O2 -NDEBUG -pthread -ldl
|
||||
|
|
|
@ -97,7 +97,7 @@ osmo_trx_LDADD = \
|
|||
libtransceiver.la \
|
||||
$(ARCH_LA) \
|
||||
$(GSM_LA) \
|
||||
$(COMMON_LA) $(SQLITE3_LIBS)
|
||||
$(COMMON_LA)
|
||||
|
||||
if USRP1
|
||||
libtransceiver_la_SOURCES += USRPDevice.cpp
|
||||
|
|
|
@ -158,10 +158,7 @@ AM_CONDITIONAL(USRP1, [test "x$with_usrp1" = "xyes"])
|
|||
AM_CONDITIONAL(ARCH_ARM, [test "x$with_neon" = "xyes" || test "x$with_neon_vfpv4" = "xyes"])
|
||||
AM_CONDITIONAL(ARCH_ARM_A15, [test "x$with_neon_vfpv4" = "xyes"])
|
||||
|
||||
AC_CHECK_LIB(sqlite3, sqlite3_open, , AC_MSG_ERROR(sqlite3 is not available))
|
||||
|
||||
PKG_CHECK_MODULES(LIBUSB, libusb-1.0)
|
||||
PKG_CHECK_MODULES(SQLITE3, sqlite3)
|
||||
|
||||
AC_CHECK_HEADER([boost/config.hpp],[],
|
||||
[AC_MSG_ERROR([boost/config.hpp not found, install e.g. libboost-dev])])
|
||||
|
|
|
@ -5,7 +5,6 @@ Maintainer: Ivan Klyuchnikov <ivan.kluchnikov@fairwaves.ru>
|
|||
Build-Depends: debhelper (>= 9),
|
||||
autotools-dev,
|
||||
autoconf-archive,
|
||||
libsqlite3-dev,
|
||||
pkg-config,
|
||||
dh-autoreconf,
|
||||
libuhd-dev,
|
||||
|
|
|
@ -52,20 +52,6 @@ Copyright: 2005-2009 United States Government as represented by
|
|||
the U.S. Army Research Laboratory.
|
||||
License: BSD-3-clause
|
||||
|
||||
Files: CommonLibs/sqlite3util.cpp
|
||||
Copyright: 2010 Kestrel Signal Processing Inc.
|
||||
License: none
|
||||
No license described for file.
|
||||
Comment: In the previous version of the file in the git repository
|
||||
at upstream it is written:
|
||||
Written by David A. Burgess, Kestrel Signal Processing, Inc., 2010
|
||||
The author disclaims copyright to this source code.
|
||||
In the git log, this is written:
|
||||
I do not claim any copyright over this change, as it's very basic.
|
||||
Looking forward to see it merged into mainline.
|
||||
See revision e766abbf82f02473038a83fd2f78befd08544cab at
|
||||
https://github.com/osmocom/osmo-trx
|
||||
|
||||
Files: debian/*
|
||||
Copyright: 2015 Ruben Undheim <ruben.undheim@gmail.com>
|
||||
License: GPL-3+
|
||||
|
|
Loading…
Reference in New Issue