LedgerKey LedgerEntryKey(LedgerEntry const& e) { auto& d = e.data; LedgerKey k; switch (d.type()) { case ACCOUNT: k.type(ACCOUNT); k.account().accountID = d.account().accountID; break; case TRUSTLINE: k.type(TRUSTLINE); k.trustLine().accountID = d.trustLine().accountID; k.trustLine().asset = d.trustLine().asset; break; case OFFER: k.type(OFFER); k.offer().sellerID = d.offer().sellerID; k.offer().offerID = d.offer().offerID; break; case DATA: k.type(DATA); k.data().accountID = d.data().accountID; k.data().dataName = d.data().dataName; break; } return k; }
std::shared_ptr<LedgerEntry const> LedgerStateRoot::Impl::loadData(LedgerKey const& key) const { std::string actIDStrKey = KeyUtils::toStrKey(key.data().accountID); std::string const& dataName = key.data().dataName; std::string dataValue; soci::indicator dataValueIndicator; LedgerEntry le; le.data.type(DATA); DataEntry& de = le.data.data(); std::string sql = "SELECT datavalue, lastmodified " "FROM accountdata " "WHERE accountid= :id AND dataname= :dataname"; auto prep = mDatabase.getPreparedStatement(sql); auto& st = prep.statement(); st.exchange(soci::into(dataValue, dataValueIndicator)); st.exchange(soci::into(le.lastModifiedLedgerSeq)); st.exchange(soci::use(actIDStrKey)); st.exchange(soci::use(dataName)); st.define_and_bind(); st.execute(true); if (!st.got_data()) { return nullptr; } de.accountID = key.data().accountID; de.dataName = dataName; if (dataValueIndicator != soci::i_ok) { throw std::runtime_error("bad database state"); } decoder::decode_b64(dataValue, de.dataValue); return std::make_shared<LedgerEntry const>(std::move(le)); }
void LedgerStateRoot::Impl::deleteData(LedgerKey const& key) { auto const& data = key.data(); std::string actIDStrKey = KeyUtils::toStrKey(data.accountID); std::string const& dataName = data.dataName; auto prep = mDatabase.getPreparedStatement( "DELETE FROM accountdata WHERE accountid=:id AND dataname=:s"); auto& st = prep.statement(); st.exchange(soci::use(actIDStrKey)); st.exchange(soci::use(dataName)); st.define_and_bind(); { auto timer = mDatabase.getDeleteTimer("data"); st.execute(true); } if (st.get_affected_rows() != 1) { throw std::runtime_error("Could not update data in SQL"); } }