File: resultset.hpp

package info (click to toggle)
mapnik 4.2.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,656 kB
  • sloc: cpp: 163,870; python: 1,332; sh: 690; xml: 161; makefile: 123; perl: 28; lisp: 13
file content (121 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2025 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#ifndef POSTGIS_RESULTSET_HPP
#define POSTGIS_RESULTSET_HPP

extern "C" {
#include "libpq-fe.h"
}

class IResultSet
{
  public:
    // virtual IResultSet& operator=(const IResultSet& rhs) = 0;
    virtual ~IResultSet() {}
    virtual void close() = 0;
    virtual int getNumFields() const = 0;
    virtual bool next() = 0;
    virtual char const* getFieldName(int index) const = 0;
    virtual int getFieldLength(int index) const = 0;
    virtual int getFieldLength(char const* name) const = 0;
    virtual int getTypeOID(int index) const = 0;
    virtual int getTypeOID(char const* name) const = 0;
    virtual bool isNull(int index) const = 0;
    virtual char const* getValue(int index) const = 0;
    virtual char const* getValue(char const* name) const = 0;
};

class ResultSet : public IResultSet,
                  private mapnik::util::noncopyable
{
  public:
    ResultSet(PGresult* res)
        : res_(res),
          pos_(-1)
    {
        numTuples_ = PQntuples(res_);
    }

    virtual void close()
    {
        PQclear(res_);
        res_ = 0;
    }

    virtual ~ResultSet() { PQclear(res_); }

    virtual int getNumFields() const { return PQnfields(res_); }

    int pos() const { return pos_; }

    int size() const { return numTuples_; }

    virtual bool next() { return (++pos_ < numTuples_); }

    virtual char const* getFieldName(int index) const { return PQfname(res_, index); }

    virtual int getFieldLength(int index) const { return PQgetlength(res_, pos_, index); }

    virtual int getFieldLength(char const* name) const
    {
        int col = PQfnumber(res_, name);
        if (col >= 0)
        {
            return PQgetlength(res_, pos_, col);
        }
        return 0;
    }

    virtual int getTypeOID(int index) const { return PQftype(res_, index); }

    virtual int getTypeOID(char const* name) const
    {
        int col = PQfnumber(res_, name);
        if (col >= 0)
        {
            return PQftype(res_, col);
        }
        return 0;
    }

    virtual bool isNull(int index) const { return static_cast<bool>(PQgetisnull(res_, pos_, index)); }

    virtual char const* getValue(int index) const { return PQgetvalue(res_, pos_, index); }

    virtual char const* getValue(char const* name) const
    {
        int col = PQfnumber(res_, name);
        if (col >= 0)
        {
            return getValue(col);
        }
        return 0;
    }

  private:
    PGresult* res_;
    int pos_;
    int numTuples_;
};

#endif // POSTGIS_RESULTSET_HPP