Fix some database issues.

This commit is contained in:
JoelKatz
2012-01-01 07:45:13 -08:00
parent d4a79d7c7f
commit a24e7a4c27
7 changed files with 75 additions and 70 deletions

View File

@@ -25,11 +25,13 @@ What needs to happen:
*/
DatabaseCon::DatabaseCon(const std::string& name)
DatabaseCon::DatabaseCon(const std::string& name, const char *initStrings[], int initCount)
{
std::string path=strprintf("%s%s", theConfig.DATA_DIR.c_str(), name.c_str());
mDatabase=new SqliteDatabase(path.c_str());
mDatabase->connect();
for(int i=0; i<initCount; i++)
mDatabase->executeSQL(initStrings[i], true);
}
DatabaseCon::~DatabaseCon()
@@ -46,26 +48,18 @@ Application::Application() :
}
extern std::string TxnDBInit, LedgerDBInit, WalletDBInit, HashNodeDBInit, NetNodeDBInit;
extern const char *TxnDBInit[], *LedgerDBInit[], *WalletDBInit[], *HashNodeDBInit[], *NetNodeDBInit[];
extern int TxnDBCount, LedgerDBCount, WalletDBCount, HashNodeDBCount, NetNodeDBCount;
void Application::run()
{
assert(mTxnDB==NULL);
mTxnDB=new DatabaseCon("transaction.db");
mTxnDB->getDB()->executeSQL(TxnDBInit.c_str());
mLedgerDB=new DatabaseCon("ledger.db");
mLedgerDB->getDB()->executeSQL(LedgerDBInit.c_str());
mWalletDB=new DatabaseCon("wallet.db");
mWalletDB->getDB()->executeSQL(WalletDBInit.c_str());
mHashNodeDB=new DatabaseCon("hashnode.db");
mHashNodeDB->getDB()->executeSQL(HashNodeDBInit.c_str());
mNetNodeDB=new DatabaseCon("netnode.db");
mNetNodeDB->getDB()->executeSQL(NetNodeDBInit.c_str());
mTxnDB=new DatabaseCon("transaction.db", TxnDBInit, TxnDBCount);
mLedgerDB=new DatabaseCon("ledger.db", LedgerDBInit, LedgerDBCount);
mWalletDB=new DatabaseCon("wallet.db", WalletDBInit, WalletDBCount);
mHashNodeDB=new DatabaseCon("hashnode.db", HashNodeDBInit, HashNodeDBCount);
mNetNodeDB=new DatabaseCon("netnode.db", NetNodeDBInit, NetNodeDBCount);
if(theConfig.PEER_PORT)
{

View File

@@ -25,7 +25,7 @@ protected:
boost::recursive_mutex mLock;
public:
DatabaseCon(const std::string& name);
DatabaseCon(const std::string& name, const char *initString[], int countInit);
~DatabaseCon();
Database* getDB() { return mDatabase; }
ScopedLock getDBLock() { return ScopedLock(mLock); }

View File

@@ -1,8 +1,8 @@
#include <string>
// Transaction database holds transactions and public keys
std::string TxnDBInit(" \
CREATE TABLE Transactions ( \
const char *TxnDBInit[] = {
"CREATE TABLE Transactions ( \
TransID CHARACTER(64) PRIMARY KEY, \
FromAcct CHARACTER(40), \
FromSeq BIGINT UNSIGNED, \
@@ -15,17 +15,17 @@ std::string TxnDBInit(" \
CommitSeq BIGINT UNSIGNED, \
Status CHARACTER(1), \
Signature BLOB \
); \
\
CREATE TABLE PubKeys ( \
);",
"CREATE TABLE PubKeys ( \
ID CHARACTER(40) PRIMARY KEY, \
PubKey BLOB \
); \
");
);" };
int TxnDBCount=sizeof(TxnDBInit)/sizeof(const char *);
// Ledger database holds ledgers and ledger confirmations
std::string LedgerDBInit(" \
CREATE TABLE Ledgers ( \
const char *LedgerDBInit[] = {
"CREATE TABLE Ledgers ( \
LedgerHash CHARACTER(64) PRIMARY KEY, \
LedgerSeq BIGINT UNSIGNED, \
PrevHash CHARACTER(64), \
@@ -33,31 +33,29 @@ std::string LedgerDBInit(" \
ClosingTime BIGINT UNSINGED, \
AccountSetHash CHARACTER(64), \
TransSetHash CHARACTER(64) \
); \
CREATE INDEX SeqLedger ON Ledgers(LedgerSeq); \
\
CREATE TABLE LedgerConfirmations ( \
);",
"CREATE INDEX SeqLedger ON Ledgers(LedgerSeq);",
"CREATE TABLE LedgerConfirmations ( \
LedgerSeq BIGINT UNSIGNED, \
LedgerHash CHARACTER(64), \
Hanko CHARACTER(40), \
Signature BLOB \
); \
CREATE INDEX LedgerConfByHash ON \
LedgerConfirmations(LedgerHash); \
");
);",
"CREATE INDEX LedgerConfByHash ON \
LedgerConfirmations(LedgerHash)l" };
int LedgerDBCount=sizeof(LedgerDBInit)/sizeof(const char *);
// Wallet database holds local accounts and trusted nodes
std::string WalletDBInit(" \
CREATE TABLE LocalAcctFamilies ( \
const char *WalletDBInit[] = {
"CREATE TABLE LocalAcctFamilies ( \
FamilyName CHARACTER(40) PRIMARY KEY, \
RootPubKey CHARACTER(66), \
Seq BIGINT UNSIGNED, \
Name TEXT, \
Comment TEXT \
); \
\
CREATE TABLE LocalAccounts ( \
);",
"CREATE TABLE LocalAccounts ( \
ID CHARACTER(40) PRIMARY KEY, \
DKID CHARACTER(40), \
DKSeq BIGINT UNSIGNED, \
@@ -65,33 +63,37 @@ std::string WalletDBInit(" \
Balance BIGINT UNSIGNED, \
LedgerSeq BIGINT UNSIGNED, \
Comment TEXT \
); \
\
CREATE TABLE TrustedNodes ( ` \
);",
"CREATE TABLE TrustedNodes ( ` \
Hanko CHARACTER(40) PRIMARY KEY, \
TrustLevel SMALLINT, \
Comment TEXT \
); \
");
);" };
int WalletDBCount=sizeof(WalletDBInit)/sizeof(const char *);
// Hash node database holds nodes indexed by hash
std::string HashNodeDBInit(" \
CREATE TABLE CommittedObjects ( \
const char *HashNodeDBInit[] = {
"CREATE TABLE CommittedObjects \
Hash CHARACTER(64) PRIMARY KEY, \
ObjType CHAR(1) NOT NULL, \
LedgerIndex BIGINT UNSIGNED, \
Object BLOB \
); \
CREATE INDEX ObjectLocate ON \
CommittedObjects(LedgerIndex, ObjType); \
");
);",
"CREATE INDEX ObjectLocate ON \
CommittedObjects(LedgerIndex, ObjType);" };
int HashNodeDBCount=sizeof(HashNodeDBInit)/sizeof(const char *);
// Net node database holds nodes seen on the network
std::string NetNodeDBInit(" \
CREATE TABLE KnownNodes ( \
const char *NetNodeDBInit[] = {
"CREATE TABLE KnownNodes ( \
Hanko CHARACTER(40) PRIMARY KEY, \
LastSeen TEXT, \
HaveContactInfo CHARACTER(1), \
ContactObject BLOB \
); \
");
);" };
int NetNodeDBCount=sizeof(NetNodeDBInit)/sizeof(const char *);

View File

@@ -53,7 +53,7 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key)
if(!pit.second) // there was an existing key
return pit.first->second;
}
std::string sql="INSERT INTO PubKeys (ID, PubKey) VALUES ('";
std::string sql="INSERT INTO PubKeys (ID,PubKey) VALUES ('";
sql+=id.GetHex();
sql+="',";
@@ -63,7 +63,7 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key)
sql+=encodedPK;
sql.append(");");
ScopedLock dbl(theApp->getTxnDB()->getDBLock());
theApp->getTxnDB()->getDB()->executeSQL(sql.c_str());
theApp->getTxnDB()->getDB()->executeSQL(sql.c_str(), true);
return key;
}

View File

@@ -29,13 +29,20 @@ void SqliteDatabase::disconnect()
}
// returns true if the query went ok
bool SqliteDatabase::executeSQL(const char* sql)
bool SqliteDatabase::executeSQL(const char* sql, bool fail_ok)
{
sqlite3_finalize(mCurrentStmt);
int rc=sqlite3_prepare_v2(mConnection,sql,-1,&mCurrentStmt,NULL);
if( rc!=SQLITE_OK )
{
cout << "SQL Perror:" << rc << endl;
if(!fail_ok)
{
cout << "SQL Perror:" << rc << endl;
#ifdef DEBUG
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return(false);
}
rc=sqlite3_step(mCurrentStmt);
@@ -48,7 +55,14 @@ bool SqliteDatabase::executeSQL(const char* sql)
}else
{
mMoreRows=false;
cout << "SQL Serror:" << rc << endl;
if(!fail_ok)
{
cout << "SQL Serror:" << rc << endl;
#ifdef DEBUG
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return(false);
}
@@ -146,20 +160,15 @@ uint64 SqliteDatabase::getBigInt(int colIndex)
BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character. For example:
X'53514C697465'
*/
void SqliteDatabase::escape(const unsigned char* start,int size,std::string& retStr)
void SqliteDatabase::escape(const unsigned char* start, int size, std::string& retStr)
{
retStr.clear();
retStr="X'";
char buf[3];
retStr.append("X'");
for(int n=0; n<size; n++)
{
sprintf(buf, "%x", start[n]);
if(buf[1]==0)
{
retStr.append("0");
retStr.append(buf);
}else retStr.append(buf);
sprintf(buf, "%02X", start[n]);
retStr.append(buf);
}
retStr.push_back('\'');

View File

@@ -15,7 +15,7 @@ public:
void disconnect();
// returns true if the query went ok
bool executeSQL(const char* sql);
bool executeSQL(const char* sql, bool fail_okay);
// tells you how many rows were changed by an update or insert
int getNumRowsAffected();

View File

@@ -32,7 +32,7 @@ public:
virtual void escape(const unsigned char* start,int size,std::string& retStr)=0;
// returns true if the query went ok
virtual bool executeSQL(const char* sql)=0;
virtual bool executeSQL(const char* sql, bool fail_okay=false)=0;
// tells you how many rows were changed by an update or insert
virtual int getNumRowsAffected()=0;