1818
1919#include " resultset.h"
2020#include " exception.h"
21+ #include < QDebug>
2122
2223ResultSet::ResultSet ()
2324{
2425 sql_result = nullptr ;
2526 empty_result = false ;
26- is_res_copied = false ;
2727 current_tuple = -1 ;
2828}
2929
30- ResultSet::ResultSet (PGresult *sql_result)
30+ ResultSet::ResultSet (PGresult *sql_result) : ResultSet()
3131{
32- QString str_aux;
33- int res_state;
34-
35- if (!sql_result)
36- throw Exception (ErrorCode::AsgNotAllocatedSQLResult, __PRETTY_FUNCTION__, __FILE__, __LINE__);
37-
38- this ->sql_result =sql_result;
39- res_state=PQresultStatus (this ->sql_result );
40-
41- // Handling the status of the result
42- switch (res_state)
43- {
44- // Generating an error in case the server returns an incomprehensible response
45- case PGRES_BAD_RESPONSE:
46- throw Exception (ErrorCode::IncomprehensibleDBMSResponse, __PRETTY_FUNCTION__, __FILE__, __LINE__);
47-
48- // Generating an error in case the server returns a fatal error
49- case PGRES_FATAL_ERROR:
50- str_aux=Exception::getErrorMessage (ErrorCode::DBMSFatalError)
51- .arg (PQresultErrorMessage (sql_result));
52- throw Exception (str_aux,ErrorCode::DBMSFatalError, __PRETTY_FUNCTION__, __FILE__, __LINE__);
53-
54- // In case of sucess states the result will be created
55- default :
56- /* For any other result set status different from PGRES_TUPLES_OK
57- * we flag the result set as empty since they either return no tuples
58- * or aren't support at the moment by this class */
59- empty_result = res_state != PGRES_TUPLES_OK;
60- current_tuple = -1 ;
61- is_res_copied = false ;
62- break ;
63- }
32+ initResultSet (sql_result);
6433}
6534
6635ResultSet::~ResultSet ()
6736{
6837 clearResultSet ();
6938}
7039
40+ void ResultSet::initResultSet (PGresult *sql_res)
41+ {
42+ if (!sql_res)
43+ throw Exception (ErrorCode::AsgNotAllocatedSQLResult, __PRETTY_FUNCTION__, __FILE__, __LINE__);
44+
45+ int res_state = -1 ;
46+
47+ clearResultSet ();
48+ this ->sql_result = sql_res;
49+ res_state = PQresultStatus (sql_res);
50+
51+ // Handling the status of the result
52+ switch (res_state)
53+ {
54+ // Generating an error in case the server returns an incomprehensible response
55+ case PGRES_BAD_RESPONSE:
56+ throw Exception (ErrorCode::IncomprehensibleDBMSResponse, __PRETTY_FUNCTION__, __FILE__, __LINE__);
57+
58+ // Generating an error in case the server returns a fatal error
59+ case PGRES_FATAL_ERROR:
60+ throw Exception (Exception::getErrorMessage (ErrorCode::DBMSFatalError)
61+ .arg (PQresultErrorMessage (sql_res)),
62+ ErrorCode::DBMSFatalError, __PRETTY_FUNCTION__, __FILE__, __LINE__);
63+
64+ // In case of sucess states the result will be created
65+ default :
66+ /* For any other result set status different from PGRES_TUPLES_OK
67+ * we flag the result set as empty since they either return no tuples
68+ * or aren't support at the moment by this class */
69+ empty_result = res_state != PGRES_TUPLES_OK;
70+ current_tuple = -1 ;
71+ break ;
72+ }
73+ }
74+
7175void ResultSet::clearResultSet ()
7276{
73- /* Destroy the resultset of the object if it was not copied
74- to another class instance (see 'operator =') */
75- if (sql_result && !is_res_copied)
76- PQclear (sql_result);
77+ if (sql_result)
78+ PQclear (sql_result);
7779
7880 // Reset the other attributes
79- sql_result=nullptr ;
80- empty_result=false ;
81- is_res_copied=false ;
82- current_tuple=-1 ;
81+ sql_result = nullptr ;
82+ empty_result = false ;
83+ current_tuple = -1 ;
8384}
8485
8586QString ResultSet::getColumnName (int column_idx)
@@ -89,7 +90,10 @@ QString ResultSet::getColumnName(int column_idx)
8990 throw Exception (ErrorCode::RefTupleColumnInvalidIndex, __PRETTY_FUNCTION__, __FILE__, __LINE__);
9091
9192 // Returns the column name on the specified index
92- return QString (PQfname (sql_result, column_idx));
93+ // QString col_name { PQfname(sql_result, column_idx) };
94+ // return col_name;
95+
96+ return { PQfname (sql_result, column_idx) };
9397}
9498
9599QStringList ResultSet::getColumnNames ()
@@ -332,20 +336,3 @@ bool ResultSet::isValid()
332336 return (sql_result != nullptr );
333337}
334338
335- void ResultSet::operator = (ResultSet &res)
336- {
337- /* Mark the result parameter as copied, avoiding
338- the sql_result attribute to be deallocated */
339- res.is_res_copied =true ;
340-
341- /* If the resultset 'this' is allocated,
342- it will be deallocated to avoid memory leaks */
343- clearResultSet ();
344-
345- // Copy the parameter restulset attributes to 'this' resultset
346- this ->current_tuple =res.current_tuple ;
347- this ->empty_result =res.empty_result ;
348- this ->sql_result =PQcopyResult (res.sql_result , PG_COPYRES_TUPLES | PG_COPYRES_ATTRS | PG_COPYRES_EVENTS);
349- this ->is_res_copied =false ;
350- }
351-
0 commit comments