Finish SQL read/write outer ledger functions.

This commit is contained in:
JoelKatz
2011-12-13 19:30:35 -08:00
parent 78fedc05b4
commit b829f2ca16
2 changed files with 46 additions and 0 deletions

View File

@@ -322,12 +322,56 @@ void Ledger::saveAcceptedLedger(Ledger::pointer ledger)
theApp->getDB()->executeSQL(sql.c_str()); theApp->getDB()->executeSQL(sql.c_str());
} }
Ledger::pointer Ledger::getSQL(const std::string& sql)
{
uint256 ledgerHash, prevHash, accountHash, transHash;
uint64 feeHeld, closingTime;
uint32 ledgerSeq;
std::string hash;
if(1)
{
ScopedLock sl(theApp->getDBLock());
Database *db=theApp->getDB();
if(!db->executeSQL(sql.c_str()) || !db->getNextRow())
return Ledger::pointer();
db->getStr("LedgerHash", hash);
ledgerHash.SetHex(hash);
db->getStr("PrevHash", hash);
prevHash.SetHex(hash);
db->getStr("AccountSetHash", hash);
accountHash.SetHex(hash);
db->getStr("TransSetHash", hash);
transHash.SetHex(hash);
feeHeld=db->getBigInt("FeeHeld");
closingTime=db->getBigInt("ClosingTime");
ledgerSeq=db->getBigInt("LedgerSeq");
}
Ledger::pointer ret(new Ledger(prevHash, transHash, accountHash, feeHeld, closingTime, ledgerSeq));
if(ret->getHash()!=ledgerHash)
{
assert(false);
return Ledger::pointer();
}
return ret;
}
Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex) Ledger::pointer Ledger::loadByIndex(uint32 ledgerIndex)
{ {
std::string sql="SELECT * from Ledgers WHERE LedgerSeq='";
sql.append(boost::lexical_cast<std::string>(ledgerIndex));
sql.append("';");
return getSQL(sql);
} }
Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash) Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash)
{ {
std::string sql="SELECT * from Ledgers WHERE LedgerHash='";
sql.append(ledgerHash.GetHex());
sql.append("';");
return getSQL(sql);
} }
#if 0 #if 0

View File

@@ -58,6 +58,8 @@ protected:
bool updateAccountState(AccountState::pointer); bool updateAccountState(AccountState::pointer);
bool addTransaction(Transaction::pointer); bool addTransaction(Transaction::pointer);
bool delTransaction(const uint256& id); bool delTransaction(const uint256& id);
static Ledger::pointer getSQL(const std::string& sqlStatement);
public: public:
Ledger(const uint160& masterID, uint64 startAmount); // used for the starting bootstrap ledger Ledger(const uint160& masterID, uint64 startAmount); // used for the starting bootstrap ledger