From a24e7a4c27d87c9376e8b20e7396f0d53026d127 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 1 Jan 2012 07:45:13 -0800 Subject: [PATCH] Fix some database issues. --- Application.cpp | 26 +++++-------- Application.h | 2 +- DBInit.cpp | 76 +++++++++++++++++++------------------ PubKeyCache.cpp | 4 +- database/SqliteDatabase.cpp | 33 ++++++++++------ database/SqliteDatabase.h | 2 +- database/database.h | 2 +- 7 files changed, 75 insertions(+), 70 deletions(-) diff --git a/Application.cpp b/Application.cpp index 11c359b4b7..51e6716ab2 100644 --- a/Application.cpp +++ b/Application.cpp @@ -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; iexecuteSQL(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) { diff --git a/Application.h b/Application.h index 8c92406d52..5cbe449210 100644 --- a/Application.h +++ b/Application.h @@ -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); } diff --git a/DBInit.cpp b/DBInit.cpp index 947cb571d8..5f0a0335d3 100644 --- a/DBInit.cpp +++ b/DBInit.cpp @@ -1,8 +1,8 @@ #include // 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 *); diff --git a/PubKeyCache.cpp b/PubKeyCache.cpp index 686eb97a9c..ea3cede8ec 100644 --- a/PubKeyCache.cpp +++ b/PubKeyCache.cpp @@ -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; } diff --git a/database/SqliteDatabase.cpp b/database/SqliteDatabase.cpp index ca92cfcbcb..9639dd0be2 100644 --- a/database/SqliteDatabase.cpp +++ b/database/SqliteDatabase.cpp @@ -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