#include #include "../../shared/DatabaseWithRetry.h" #include "../../shared/MiscSupport.h" #include "ReadFromDatabase.h" void readFromDatabase(std::vector< std::string > const &exchanges, StockInfoList &results) { // We overwrite this. We don't update it. results.clear(); std::set< std::string > exchangeSet; // Remove blanks and whitespace. // This could easily happen if the input was read from a file. // Do this before we check for the empty list or the code which generates the SQL will fail. for (std::vector< std::string >::const_iterator it = exchanges.begin(); it != exchanges.end(); it++) { const std::string possible = trim(*it); if (!possible.empty()) exchangeSet.insert(possible); } // Set the default if necessary if (exchangeSet.empty()) { exchangeSet.insert("NYSE"); exchangeSet.insert("AMEX"); exchangeSet.insert("CAV"); exchangeSet.insert("OTC"); exchangeSet.insert("PINK"); exchangeSet.insert("CAT"); exchangeSet.insert("NASD"); } std::string sql = "SELECT d_symbol AS symbol, MAX(advol*last_price) AS dollarVolume, MAX(UNIX_TIMESTAMP(date)) AS closeDate FROM alerts_daily WHERE list_exch IN ("; bool first = true; for (std::set< std::string >::const_iterator it = exchangeSet.begin(); it != exchangeSet.end(); it++) { if (first) first = false; else sql += ", "; sql += '"'; sql += mysqlEscapeString(*it); sql += '"'; } assert (!first); sql += ") GROUP BY d_symbol"; DatabaseWithRetry database(true, "get symbols"); for (MysqlResultRef result = database.tryQueryUntilSuccess(sql); result->rowIsValid(); result->nextRow()) { StockInfo info; info.symbol = result->getStringField("symbol"); info.dollarVolume = result->getDoubleField("dollarVolume", 0); info.closeDate = result->getIntegerField("closeDate", 0); results.push_back(info); } }