~kai-mast/ydatabase/trunk

« back to all changes in this revision

Viewing changes to CSQLResultPostgre.cpp

  • Committer: kai.mast at freakybytes
  • Date: 2012-03-17 15:34:04 UTC
  • Revision ID: kai.mast@freakybytes.org-20120317153404-153ykjxyal6hah2o
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "CSQLResultPostgre.h"
 
2
 
 
3
#include <cstring>
 
4
#include <iostream>
 
5
#include <sstream>
 
6
 
 
7
#include "IDatabaseManager.h"
 
8
 
 
9
namespace yogi
 
10
{
 
11
    CSQLResultPostgre::CSQLResultPostgre(PGresult *res_, IDatabaseManager *databaseMgr_, const std::string query_)
 
12
    : ISQLResult(databaseMgr_, query_), res(res_)
 
13
    {
 
14
    }
 
15
 
 
16
    CSQLResultPostgre::~CSQLResultPostgre()
 
17
    {
 
18
        if(res)
 
19
            PQclear(res);
 
20
    }
 
21
 
 
22
    s32 CSQLResultPostgre::getRowCount() const
 
23
    {
 
24
        if(!res || !this->isValidTuple())
 
25
            return -1;
 
26
 
 
27
        return PQntuples(res);
 
28
    }
 
29
 
 
30
    s32 CSQLResultPostgre::getColumnCount() const
 
31
    {
 
32
        if(!res || !this->isValidTuple())
 
33
            return -1;
 
34
 
 
35
        return PQnfields(res);
 
36
    }
 
37
 
 
38
    s32 CSQLResultPostgre::getColumnFromName(const std::string name) const
 
39
    {
 
40
        s32 val = -1;
 
41
 
 
42
        if(res && this->isValidTuple())
 
43
            val = PQfnumber(res, name.c_str());
 
44
 
 
45
        if(val < 0)
 
46
            databaseMgr->logWarning(std::string("Couldn't get column-number: ") + name);
 
47
 
 
48
        return val;
 
49
    }
 
50
 
 
51
    ESQLResultType CSQLResultPostgre::getType() const
 
52
    {
 
53
        ExecStatusType type = PQresultStatus(res);
 
54
 
 
55
        if(type == PGRES_EMPTY_QUERY)
 
56
            return ESQLR_EmptyQuery;
 
57
        else if(type == PGRES_COMMAND_OK)
 
58
            return ESQLR_CommandOk;
 
59
        else if(type == PGRES_TUPLES_OK)
 
60
            return ESQLR_TuplesOk;
 
61
        else if(type == PGRES_BAD_RESPONSE || type == PGRES_NONFATAL_ERROR)
 
62
            return ESQLR_Error;
 
63
        else if(type == PGRES_FATAL_ERROR)
 
64
            return ESQLR_ErrorFatal;
 
65
        else
 
66
            return ESQLR_Unknown;
 
67
    }
 
68
 
 
69
    s32 CSQLResultPostgre::getFieldLength(const s32 row, const s32 column) const
 
70
    {
 
71
        if(!this->isValidTuple())
 
72
            return 0;
 
73
 
 
74
        if(!this->isInRange(row, column))
 
75
            return false;
 
76
        else
 
77
            return PQgetlength(res, row, column);
 
78
    }
 
79
 
 
80
    std::string CSQLResultPostgre::getString(const s32 row, const s32 column) const
 
81
    {
 
82
        if(!this->isValidTuple())
 
83
            return "";
 
84
 
 
85
        if(!this->isInRange(row, column))
 
86
            return "";
 
87
 
 
88
        return std::string(PQgetvalue(res, row, column));
 
89
    }
 
90
 
 
91
    std::string CSQLResultPostgre::getError()
 
92
    {
 
93
        if(!res)
 
94
            return "Null Result";
 
95
 
 
96
        return PQresultErrorMessage(res);
 
97
    }
 
98
 
 
99
   bool CSQLResultPostgre::isInRange(const s32 row, const s32 column) const
 
100
    {
 
101
        if((row >= this->getRowCount()) || (column >= this->getColumnCount()))
 
102
        {
 
103
            std::stringstream msg;
 
104
            msg << "Database Request out of range (Query: \"" << this->getQuery() << " \")";
 
105
 
 
106
            databaseMgr->logWarning(msg.str());
 
107
            return false;
 
108
        }
 
109
        else
 
110
            return true;
 
111
    }
 
112
}