DBDataTable *database::query(bool distinct, const std::string &table, const std::vector<std::string> &columns, const std::string &where, const std::vector<std::string> &whereArgs, const std::string &groupBy, const std::string &having, const std::string &orderBy, const std::string &limit) { std::string sql("SELECT "); if (distinct) { sql.append(" DISTINCT "); } if (columns.size() > 0) { bool isFirst = true; for (auto it = columns.begin(); it != columns.end(); it++) { if (it->length() > 0) { if (isFirst) { isFirst = false; } else { sql.append(", "); } sql.append(*it); } } sql.append(" "); } else { sql.append(" * "); } sql.append(" FROM "); sql.append(table.data()); if (where.length() > 0) { sql.append(" WHERE "); sql.append(where.data()); } if (groupBy.length() > 0) { sql.append(" GROUP BY "); sql.append(groupBy.data()); } if (having.length() > 0) { sql.append(" HAVING "); sql.append(having.data()); } if (orderBy.length() > 0) { sql.append(" ORDER BY "); sql.append(orderBy.data()); } if (limit.length() > 0) { sql.append(" LIMIT "); sql.append(limit.data()); } return rawQuery(sql, whereArgs); }
bool recordDatabase::updateRecord(Field field, int value, bool onlyIfGreater){ if (isLoggedIn()){ DatabaseEntry *prevValue[MAX_FIELDS]; bool recordGotten = getRecord({ &field }, 1, prevValue); if (recordGotten && (!onlyIfGreater || prevValue[0]->intValue < value)){ CString q = _T(""); q.Format(_T("UPDATE accounts SET %s = %d WHERE %s = %d"), fieldNames[field], value, dbFieldNames[ID], id); rawQuery(q,NULL); } } return false; }
bool recordDatabase::removeUser(){ if (isLoggedIn()){ try{ CString q = _T(""); q.Format(_T("DELETE FROM accounts WHERE %s=%d"), dbFieldNames[ID],id); rawQuery(q,NULL); logOut(); return true; } catch (int e){ return false; } } return false; }
bool recordDatabase::addUser(CString name, CString pass){ try{ CString q = _T(""); q.Format(_T("INSERT INTO accounts (%s,%s) VALUES ('%s','%s');"), dbFieldNames[USERNAME], dbFieldNames[PASSWORD],name, pass); rawQuery(q,NULL); logIn(name, pass); if (isLoggedIn()){ return true; } } catch (int e){ id = -1; return false; } }
bool recordDatabase::getRecord(Field fields[], int numFields, DatabaseEntry *result[MAX_FIELDS]){ if (isLoggedIn()){ CString q = _T(""); CString fieldsStr = _T(""); for (int i = 0; i < numFields; i++){ fieldsStr += fieldNames[fields[i]]; } q.Format(_T("SELECT %s FROM accounts WHERE %s = %d"), fieldsStr, dbFieldNames[ID], id); DatabaseEntry *results[MAX_RESULT_SIZE][MAX_FIELDS]; clearResults(results); rawQuery(q,results); result = results[0]; return true; } return false; }
bool recordDatabase::logIn(CString name, CString pass){ try{ CString q = _T(""); q.Format(_T("SELECT %d FROM accounts WHERE username == '%s' AND password == '%s';"),dbFieldNames[ID],name,pass); DatabaseEntry *results[MAX_RESULT_SIZE][MAX_FIELDS]; //clearResults(results); rawQuery(q,results); if (results[0][0]->gotten){ id = results[0][0]->intValue; } return isLoggedIn(); } catch (int e){ id = -1; return false; } }