INSERT OR IGNORE on sqlite

This commit is contained in:
def 2020-09-04 16:12:12 +02:00
parent 0d3489e0ef
commit c3f9dc7c9f
6 changed files with 22 additions and 6 deletions

View file

@ -67,6 +67,8 @@ public:
virtual void GetString(int Col, char *pBuffer, int BufferSize) const = 0;
// returns number of bytes read into the buffer
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const = 0;
// syntax to insert a row into table or ignore if it already exists
virtual const char *GetInsertIgnore() const = 0;
// SQL statements, that can't be abstracted, has side effects to the result
virtual void AddPoints(const char *pPlayer, int Points) = 0;

View file

@ -312,6 +312,11 @@ int CMysqlConnection::GetBlob(int Col, unsigned char *pBuffer, int BufferSize) c
#endif
}
const char *CMysqlConnection::GetInsertIgnore() const
{
return "INSERT IGNORE";
}
void CMysqlConnection::AddPoints(const char *pPlayer, int Points)
{
char aBuf[512];

View file

@ -54,6 +54,7 @@ public:
virtual int GetInt(int Col) const;
virtual void GetString(int Col, char *pBuffer, int BufferSize) const;
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const;
virtual const char *GetInsertIgnore() const;
virtual void AddPoints(const char *pPlayer, int Points);

View file

@ -205,6 +205,11 @@ int CSqliteConnection::GetBlob(int Col, unsigned char *pBuffer, int BufferSize)
return Size;
}
const char *CSqliteConnection::GetInsertIgnore() const
{
return "INSERT OR IGNORE";
}
bool CSqliteConnection::Execute(const char *pQuery)
{
char *pErrorMsg;

View file

@ -42,6 +42,7 @@ public:
virtual void GetString(int Col, char *pBuffer, int BufferSize) const;
// passing a negative buffer size is undefined behavior
virtual int GetBlob(int Col, unsigned char *pBuffer, int BufferSize) const;
virtual const char *GetInsertIgnore() const;
virtual void AddPoints(const char *pPlayer, int Points);

View file

@ -508,7 +508,7 @@ bool CScore::SaveScoreThread(IDbConnection *pSqlServer, const ISqlData *pGameDat
// save score. Can't fail, because no UNIQUE/PRIMARY KEY constrain is defined.
str_format(aBuf, sizeof(aBuf),
"INSERT IGNORE INTO %s_race("
"%s INTO %s_race("
"Map, Name, Timestamp, Time, Server, "
"cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10, cp11, cp12, cp13, "
"cp14, cp15, cp16, cp17, cp18, cp19, cp20, cp21, cp22, cp23, cp24, cp25, "
@ -518,7 +518,8 @@ bool CScore::SaveScoreThread(IDbConnection *pSqlServer, const ISqlData *pGameDat
"%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, "
"%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, "
"?, false);",
pSqlServer->GetPrefix(), pSqlServer->InsertTimestampAsUtc(), pData->m_Time,
pSqlServer->GetInsertIgnore(), pSqlServer->GetPrefix(),
pSqlServer->InsertTimestampAsUtc(), pData->m_Time,
pData->m_aCpCurrent[0], pData->m_aCpCurrent[1], pData->m_aCpCurrent[2],
pData->m_aCpCurrent[3], pData->m_aCpCurrent[4], pData->m_aCpCurrent[5],
pData->m_aCpCurrent[6], pData->m_aCpCurrent[7], pData->m_aCpCurrent[8],
@ -631,9 +632,10 @@ bool CScore::SaveTeamScoreThread(IDbConnection *pSqlServer, const ISqlData *pGam
{
// if no entry found... create a new one
str_format(aBuf, sizeof(aBuf),
"INSERT IGNORE INTO %s_teamrace(Map, Name, Timestamp, Time, ID, GameID, DDNet7) "
"%s INTO %s_teamrace(Map, Name, Timestamp, Time, ID, GameID, DDNet7) "
"VALUES (?, ?, %s, %.2f, ?, ?, false);",
pSqlServer->GetPrefix(), pSqlServer->InsertTimestampAsUtc(), pData->m_Time);
pSqlServer->GetInsertIgnore(), pSqlServer->GetPrefix(),
pSqlServer->InsertTimestampAsUtc(), pData->m_Time);
pSqlServer->PrepareStatement(aBuf);
pSqlServer->BindString(1, pData->m_Map);
pSqlServer->BindString(2, pData->m_aNames[i]);
@ -1321,9 +1323,9 @@ bool CScore::SaveTeamThread(IDbConnection *pSqlServer, const ISqlData *pGameData
if(UseCode)
{
str_format(aBuf, sizeof(aBuf),
"INSERT IGNORE INTO %s_saves(Savegame, Map, Code, Timestamp, Server, SaveID, DDNet7) "
"%s INTO %s_saves(Savegame, Map, Code, Timestamp, Server, SaveID, DDNet7) "
"VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?, false)",
pSqlServer->GetPrefix());
pSqlServer->GetInsertIgnore(), pSqlServer->GetPrefix());
pSqlServer->PrepareStatement(aBuf);
pSqlServer->BindString(1, pSaveState);
pSqlServer->BindString(2, pData->m_Map);