⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrecord.cxx

📁 vovida的软交换
💻 CXX
字号:
#include "TestRecord.hxx"
#include "Record.hxx"
#include "DBResultset.hxx"
#include "DBConnection.hxx"
#include "PGSQLConnection.hxx"
#include "RecordTypes.h"
#include "DBTypes.h"
#include "VException.hxx"
#include "cpLog.h"

#include <ctime>


TestRecord::TestRecord() {
   setRecordType(TEST_RECORD);
}

TestRecord::~TestRecord() {}

void TestRecord::displayRecord() { 
    cout << "TestRecord.cxx: Generic Data\n";
    Record::displayRecord("TestRecord.cxx");
    cout << "TestRecord.cxx: Specific Data\n";
    cout << "TestRecord.cxx: Data: " << data << "\n";
}

bool TestRecord::populateRecord(KeyType key, DBConnection* dbconn) {
 
    DBResultset* results;

    try { 
      results = dbconn->DBSQLSelect( (string) " " +  // (string allows '+' op)
        "SELECT fullname FROM TESTUSERS "         +
	" WHERE username ='" + key + "'"          +  
	";"
      );
    }
    catch ( VException &e ) { 
      cpLog(LOG_ERR, "DBSQLSelect failed due to %s",e.getDescription().c_str());
      return false;
    }

    ResultTabletype recs = results->GetRecords();
    delete results;  // Delete the results object in case this fails

    if (recs.begin() == recs.end()) return false; // No records

    ResultRecordtype oneRec = *(recs.begin());
    if (oneRec.begin() == oneRec.end()) return false; // No data in record

    // Populate the record;
    setKey(key);
    setData(*(oneRec.begin()));
    
    return true;
}
 
bool TestRecord::amendRecord(DBConnection* dbconn) {

    bool result;

    try { 
      result = dbconn->DBSQLUpdate( (string) " " +  // (string allows '+' op)
        "UPDATE TESTUSERS "                      + 
        " SET fullname='" + data + "' "          +
        " WHERE username ='" + key + "'"         +
        ";"
      );
    }
    catch ( VException &e ) { 
      cpLog(LOG_ERR, "DBSQLUpdate failed due to %s",e.getDescription().c_str());
      return false;
    }

    return result;
}

bool TestRecord::deleteRecord(DBConnection* dbconn) {
    bool result;

    try { 
      result = dbconn->DBSQLDelete( 
        "DELETE FROM TESTUSERS WHERE username ='" + key + "';"
      );
    }
    catch ( VException &e ) { 
      cpLog(LOG_ERR, "DBSQLDelete failed due to %s", e.getDescription().c_str());
      return false;
    }

    return result;
}

bool TestRecord::insertRecord(DBConnection* dbconn) {
    bool result;

    try { 
      result = dbconn->DBSQLInsert((string) " "      +  // (string allows '+' op)
        "INSERT INTO TESTUSERS (username, fullname)" + 
        " VALUES ('" + key + "', '" + data + "')"    + 
	";"
      );
    }
    catch ( VException &e ) { 
      cpLog(LOG_ERR, "DBSQLInsert failed due to %s", e.getDescription().c_str());
      return false;
    }

    return result;
}

KeyList TestRecord::getAllKeys(DBConnection* dbconn) { 
  KeyList keys; 
  DBResultset* results;

  try { 
    results = dbconn->DBSQLSelect(
      "SELECT username FROM TESTUSERS;"
    );
  }
  catch ( VException &e ) { 
    cpLog(LOG_ERR, "DBSQLSelect (for getAllKeys) failed due to %s",e.getDescription().c_str());
    return keys;
  }

  ResultTabletype recs = results->GetRecords();
  delete results;  // Delete the results object in case this fails

  for (ResultTabletype::iterator i = recs.begin();i < recs.end();i++) { 
    // i should point to a single string in a list, check this
    if ((*i).begin() < (*i).end()) { 
      // Add the first string to the key list 
      keys.push_back(*((*i).begin())); 
    }
  }

  return keys;
}

Record* TestRecord::clone() { 
  
  TestRecord* rec = new TestRecord();
  Record::copyData(rec);
  rec->setData(data);
  
  return rec;
}

string TestRecord::whereCondition() { 
  return " WHERE username ='" + key + "'";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -