📄 pgsqlresultset.cxx
字号:
#include <iostream>#include "PGSQLResultset.hxx"#include "VDbException.hxx"using namespace std;// SQLResetset constructor, initialize attributesPGSQLResultset::PGSQLResultset(PGresult* result, const string& searchfilter):mySearchFilter(searchfilter){ myPgSQLresultP = result; myRecordCount = PQntuples(myPgSQLresultP); myFieldList = GetFields();};// SQLResetset destructor, free attributes PGSQLResultset::~PGSQLResultset(void){ // Free memory used by PQresult pointer PQclear(myPgSQLresultP);};// GetRecords[offset] - returns the record at offsetconst ResultRecordtype&PGSQLResultset::operator[](unsigned short offset) const{ string fieldbuff; unsigned short i; static ResultRecordtype returnResult; // Check offsets are within range of results if (offset > myRecordCount) { throw VDbException("Record Offset out of range", __FILE__, __LINE__, 0); } // Clear contents of returnResult returnResult.clear(); // If Records exist, iterate through fields filling returnResult if (myRecordCount) { for (i = 0; i < myFieldList.size(); i++) { fieldbuff = PQgetvalue(myPgSQLresultP, offset, i); returnResult.push_back(fieldbuff); } } return returnResult;}// GetRecord(offset) - returns the record at offsetconst ResultRecordtype&PGSQLResultset::GetRecord(unsigned short offset) const{ string fieldbuff; unsigned short i; static ResultRecordtype returnResult; // Check offsets are within range of results if (offset > myRecordCount) { throw VDbException("Record Offset out of range", __FILE__, __LINE__, 0); } // Clear contents of returnResult returnResult.clear(); // If Records exist, iterate through fields filling returnResult if (myRecordCount) { for (i = 0; i < myFieldList.size(); i++) { fieldbuff = PQgetvalue(myPgSQLresultP, offset, i); returnResult.push_back(fieldbuff); } } return returnResult;};// GetRecords(offset1, offset2) - returns all records between offset1// and offset2const ResultTabletype&PGSQLResultset::GetRecords(unsigned short offset1, unsigned short offset2) const{ string fieldbuff; ResultRecordtype recordbuff; static ResultTabletype returnResult; unsigned short recordnumber; unsigned short i; // Check offsets are within range of results if ((offset1 > offset2) || (offset1 > myRecordCount) || (offset2 > myRecordCount)) { throw VDbException("Record Offsets out of range", __FILE__, __LINE__, 0); } // Clear contents of last returnResult returnResult.clear(); for (recordnumber = offset1; recordnumber <= offset2; recordnumber++) { for (i = 0; i < myFieldList.size(); i++) { fieldbuff = PQgetvalue(myPgSQLresultP, recordnumber, i); recordbuff.push_back(fieldbuff); } // Insert record buffer into result table returnResult.push_back(recordbuff); // Clear recordbuff for next record recordbuff.clear(); } return returnResult;}; // GetRecords - returns all records from a searchconst ResultTabletype&PGSQLResultset::GetRecords() const{ ResultRecordtype recordbuff; static ResultTabletype returnResult; string fieldbuff; unsigned short recordnumber; unsigned short i; // Clear contents of returnResult returnResult.clear(); for (recordnumber = 0; recordnumber < myRecordCount; recordnumber++) { for (i = 0; i < myFieldList.size(); i++) { fieldbuff = PQgetvalue(myPgSQLresultP, recordnumber, i); cout << "\nResult field: " << fieldbuff <<"\n"; recordbuff.push_back(fieldbuff); } // Insert record buffer into result table returnResult.push_back(recordbuff); // Clear recordbuff for next record recordbuff.clear(); } return returnResult;};// GetRecordCount- Return Record countconst unsigned intPGSQLResultset::GetRecordCount(void) const{ return myRecordCount;};// GetFields - Return fields on a resultsetconst FieldListtype&PGSQLResultset::GetFields(void) const{ static FieldListtype returnResult; unsigned short i; unsigned short nfields = PQnfields(myPgSQLresultP); // Clear contents of returnResult returnResult.clear(); // If Records exist, fill myFieldList if (myRecordCount) { for (i = 0; i < nfields; i++) { returnResult.push_back(PQfname(myPgSQLresultP, i)); } } return returnResult;}// GetmySearchFilter- Return searchfilter that was used to create this// resultset. This is normally used when a resultset is passed back // to DBUpdate() or DBSelect()const string&PGSQLResultset::GetSearchFilter(void) const{ return mySearchFilter;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -