void OfferFrame::storeChange(LedgerDelta& delta, Database& db) const { auto timer = db.getUpdateTimer("offer"); soci::statement st = (db.getSession().prepare << "UPDATE offers SET amount=:a, pricen=:n, " "priced=:D, price=:p WHERE offerid=:s", use(mOffer.amount), use(mOffer.price.n), use(mOffer.price.d), use(computePrice()), use(mOffer.offerID)); st.execute(true); if (st.get_affected_rows() != 1) { throw std::runtime_error("could not update SQL"); } delta.modEntry(*this); }
void OfferFrame::storeUpdateHelper(LedgerDelta& delta, Database& db, bool insert) { touch(delta); if (!isValid()) { throw std::runtime_error("Invalid asset"); } std::string actIDStrKey = PubKeyUtils::toStrKey(mOffer.sellerID); unsigned int sellingType = mOffer.selling.type(); unsigned int buyingType = mOffer.buying.type(); std::string sellingIssuerStrKey, buyingIssuerStrKey; std::string sellingAssetCode, buyingAssetCode; soci::indicator selling_ind = soci::i_null, buying_ind = soci::i_null; if (sellingType == ASSET_TYPE_CREDIT_ALPHANUM4) { sellingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.selling.alphaNum4().issuer); assetCodeToStr(mOffer.selling.alphaNum4().assetCode, sellingAssetCode); selling_ind = soci::i_ok; } else if (sellingType == ASSET_TYPE_CREDIT_ALPHANUM12) { sellingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.selling.alphaNum12().issuer); assetCodeToStr(mOffer.selling.alphaNum12().assetCode, sellingAssetCode); selling_ind = soci::i_ok; } if (buyingType == ASSET_TYPE_CREDIT_ALPHANUM4) { buyingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.buying.alphaNum4().issuer); assetCodeToStr(mOffer.buying.alphaNum4().assetCode, buyingAssetCode); buying_ind = soci::i_ok; } else if (buyingType == ASSET_TYPE_CREDIT_ALPHANUM12) { buyingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.buying.alphaNum12().issuer); assetCodeToStr(mOffer.buying.alphaNum12().assetCode, buyingAssetCode); buying_ind = soci::i_ok; } string sql; if (insert) { sql = "INSERT INTO offers (sellerid,offerid," "sellingassettype,sellingassetcode,sellingissuer," "buyingassettype,buyingassetcode,buyingissuer," "amount,pricen,priced,price,flags,lastmodified) VALUES " "(:sid,:oid,:sat,:sac,:si,:bat,:bac,:bi,:a,:pn,:pd,:p,:f,:l)"; } else { sql = "UPDATE offers SET sellingassettype=:sat " ",sellingassetcode=:sac,sellingissuer=:si," "buyingassettype=:bat,buyingassetcode=:bac,buyingissuer=:bi," "amount=:a,pricen=:pn,priced=:pd,price=:p,flags=:f," "lastmodified=:l WHERE offerid=:oid"; } auto prep = db.getPreparedStatement(sql); auto& st = prep.statement(); if (insert) { st.exchange(use(actIDStrKey, "sid")); } st.exchange(use(mOffer.offerID, "oid")); st.exchange(use(sellingType, "sat")); st.exchange(use(sellingAssetCode, selling_ind, "sac")); st.exchange(use(sellingIssuerStrKey, selling_ind, "si")); st.exchange(use(buyingType, "bat")); st.exchange(use(buyingAssetCode, buying_ind, "bac")); st.exchange(use(buyingIssuerStrKey, buying_ind, "bi")); st.exchange(use(mOffer.amount, "a")); st.exchange(use(mOffer.price.n, "pn")); st.exchange(use(mOffer.price.d, "pd")); auto price = computePrice(); st.exchange(use(price, "p")); st.exchange(use(mOffer.flags, "f")); st.exchange(use(getLastModified(), "l")); st.define_and_bind(); auto timer = insert ? db.getInsertTimer("offer") : db.getUpdateTimer("offer"); st.execute(true); if (st.get_affected_rows() != 1) { throw std::runtime_error("could not update SQL"); } if (insert) { delta.addEntry(*this); } else { delta.modEntry(*this); } }
void OfferFrame::storeAdd(LedgerDelta& delta, Database& db) const { std::string actIDStrKey = PubKeyUtils::toStrKey(mOffer.sellerID); soci::statement st(db.getSession().prepare << "select 1"); auto timer = db.getInsertTimer("offer"); unsigned int sellingType = mOffer.selling.type(); unsigned int buyingType = mOffer.buying.type(); std::string sellingIssuerStrKey, buyingIssuerStrKey; std::string sellingAssetCode, buyingAssetCode; if(sellingType == ASSET_TYPE_CREDIT_ALPHANUM4) { sellingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.selling.alphaNum4().issuer); assetCodeToStr(mOffer.selling.alphaNum4().assetCode, sellingAssetCode); } else if(sellingType == ASSET_TYPE_CREDIT_ALPHANUM12) { sellingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.selling.alphaNum12().issuer); assetCodeToStr(mOffer.selling.alphaNum12().assetCode, sellingAssetCode); } if(buyingType == ASSET_TYPE_CREDIT_ALPHANUM4) { buyingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.buying.alphaNum4().issuer); assetCodeToStr(mOffer.buying.alphaNum4().assetCode, buyingAssetCode); } else if(buyingType == ASSET_TYPE_CREDIT_ALPHANUM12) { buyingIssuerStrKey = PubKeyUtils::toStrKey(mOffer.buying.alphaNum12().issuer); assetCodeToStr(mOffer.buying.alphaNum12().assetCode, buyingAssetCode); } st = (db.getSession().prepare << "INSERT INTO offers (sellerid,offerid," "sellingassettype,sellingassetcode,sellingissuer," "buyingassettype,buyingassetcode,buyingissuer," "amount,pricen,priced,price,flags) VALUES " "(:v1,:v2,:v3,:v4,:v5,:v6,:v7,:v8,:v9,:v10,:v11,:v12,:v13)", use(actIDStrKey), use(mOffer.offerID), use(sellingType), use(sellingAssetCode), use(sellingIssuerStrKey), use(buyingType), use(buyingAssetCode), use(buyingIssuerStrKey), use(mOffer.amount), use(mOffer.price.n), use(mOffer.price.d), use(computePrice()), use(mOffer.flags)); st.execute(true); if (st.get_affected_rows() != 1) { throw std::runtime_error("could not update SQL"); } delta.addEntry(*this); }