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()); std::string path=strprintf("%s%s", theConfig.DATA_DIR.c_str(), name.c_str());
mDatabase=new SqliteDatabase(path.c_str()); mDatabase=new SqliteDatabase(path.c_str());
mDatabase->connect(); mDatabase->connect();
for(int i=0; i<initCount; i++)
mDatabase->executeSQL(initStrings[i], true);
} }
DatabaseCon::~DatabaseCon() 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() void Application::run()
{ {
assert(mTxnDB==NULL); assert(mTxnDB==NULL);
mTxnDB=new DatabaseCon("transaction.db"); mTxnDB=new DatabaseCon("transaction.db", TxnDBInit, TxnDBCount);
mTxnDB->getDB()->executeSQL(TxnDBInit.c_str()); mLedgerDB=new DatabaseCon("ledger.db", LedgerDBInit, LedgerDBCount);
mWalletDB=new DatabaseCon("wallet.db", WalletDBInit, WalletDBCount);
mLedgerDB=new DatabaseCon("ledger.db"); mHashNodeDB=new DatabaseCon("hashnode.db", HashNodeDBInit, HashNodeDBCount);
mLedgerDB->getDB()->executeSQL(LedgerDBInit.c_str()); mNetNodeDB=new DatabaseCon("netnode.db", NetNodeDBInit, NetNodeDBCount);
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());
if(theConfig.PEER_PORT) if(theConfig.PEER_PORT)
{ {

View File

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

View File

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

View File

@@ -63,7 +63,7 @@ CKey::pointer PubKeyCache::store(const uint160& id, CKey::pointer key)
sql+=encodedPK; sql+=encodedPK;
sql.append(");"); sql.append(");");
ScopedLock dbl(theApp->getTxnDB()->getDBLock()); ScopedLock dbl(theApp->getTxnDB()->getDBLock());
theApp->getTxnDB()->getDB()->executeSQL(sql.c_str()); theApp->getTxnDB()->getDB()->executeSQL(sql.c_str(), true);
return key; return key;
} }

View File

@@ -29,13 +29,20 @@ void SqliteDatabase::disconnect()
} }
// returns true if the query went ok // 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); sqlite3_finalize(mCurrentStmt);
int rc=sqlite3_prepare_v2(mConnection,sql,-1,&mCurrentStmt,NULL); int rc=sqlite3_prepare_v2(mConnection,sql,-1,&mCurrentStmt,NULL);
if( rc!=SQLITE_OK ) if( rc!=SQLITE_OK )
{
if(!fail_ok)
{ {
cout << "SQL Perror:" << rc << endl; cout << "SQL Perror:" << rc << endl;
#ifdef DEBUG
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return(false); return(false);
} }
rc=sqlite3_step(mCurrentStmt); rc=sqlite3_step(mCurrentStmt);
@@ -48,7 +55,14 @@ bool SqliteDatabase::executeSQL(const char* sql)
}else }else
{ {
mMoreRows=false; mMoreRows=false;
if(!fail_ok)
{
cout << "SQL Serror:" << rc << endl; cout << "SQL Serror:" << rc << endl;
#ifdef DEBUG
cout << "Statement: " << sql << endl;
cout << "Error: " << sqlite3_errmsg(mConnection) << endl;
#endif
}
return(false); return(false);
} }
@@ -148,18 +162,13 @@ 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]; char buf[3];
retStr.append("X'");
for(int n=0; n<size; n++) for(int n=0; n<size; n++)
{ {
sprintf(buf, "%x", start[n]); sprintf(buf, "%02X", start[n]);
if(buf[1]==0)
{
retStr.append("0");
retStr.append(buf); retStr.append(buf);
}else retStr.append(buf);
} }
retStr.push_back('\''); retStr.push_back('\'');

View File

@@ -15,7 +15,7 @@ public:
void disconnect(); void disconnect();
// returns true if the query went ok // 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 // tells you how many rows were changed by an update or insert
int getNumRowsAffected(); int getNumRowsAffected();

View File

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