Skip to content

Commit

Permalink
fix transaction record incorrect issue
Browse files Browse the repository at this point in the history
  • Loading branch information
heropan committed May 7, 2020
1 parent 3d9fb25 commit 3f1c032
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
7 changes: 7 additions & 0 deletions SDK/Database/Sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ namespace Elastos {
EXCLUSIVE
} SqliteTransactionType;

/*
** The maximum value of a ?nnn wildcard that the parser will accept.
*/
#ifndef SQLITE_MAX_VARIABLE_NUMBER
# define SQLITE_MAX_VARIABLE_NUMBER 999
#endif

class Sqlite {
public:
Sqlite(const boost::filesystem::path &path);
Expand Down
51 changes: 31 additions & 20 deletions SDK/Database/TransactionNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,42 @@ namespace Elastos {
if (uniqueHash.empty())
return txns;

sql = "SELECT " + _txHash + "," + _buff + "," + _blockHeight + "," + _timestamp + "," + _iso +
" FROM " + _tableName + " WHERE " + _txHash + " IN (";
for (const std::string &hash : uniqueHash)
sql += "?,";
sql.back() = ')';
sql += ";";
std::set<std::string>::iterator it = uniqueHash.cbegin();
size_t cnt, maxCnt = uniqueHash.size(), markCnt;
std::string mark;

sqlite3_stmt *stmt = NULL;
if (!_sqlite->Prepare(sql, &stmt, nullptr)) {
Log::error("prepare sql: {}", sql);
return txns;
}
for (cnt = 0; cnt < maxCnt; ) {
sql = "SELECT " + _txHash + "," + _buff + "," + _blockHeight + "," + _timestamp + "," + _iso +
" FROM " + _tableName + " WHERE " + _txHash + " IN (";

markCnt = (maxCnt - cnt) < SQLITE_MAX_VARIABLE_NUMBER ? (maxCnt - cnt) : SQLITE_MAX_VARIABLE_NUMBER;

int index = 1;
for (const std::string &h: uniqueHash) {
if (!_sqlite->BindText(stmt, index++, h, nullptr)) {
Log::error("bind args");
for (size_t i = 0; i < markCnt; ++i)
sql += "?,";
sql.back() = ')';
sql += ";";

sqlite3_stmt *stmt = NULL;
if (!_sqlite->Prepare(sql, &stmt, nullptr)) {
Log::error("prepare sql: {}", sql);
return txns;
}
}

GetSelectedTxns(txns, chainID, stmt);
for (size_t i = 0; i < markCnt; ++i, ++it) {
if (!_sqlite->BindText(stmt, (int)(i + 1), *it, nullptr)) {
Log::error("bind args");
break;
}
}

if (!_sqlite->Finalize(stmt)) {
Log::error("Tx get all finalize");
return {};
GetSelectedTxns(txns, chainID, stmt);

if (!_sqlite->Finalize(stmt)) {
Log::error("Tx get all finalize");
return {};
}

cnt += markCnt;
}

return txns;
Expand Down

0 comments on commit 3f1c032

Please sign in to comment.