1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // pgSet.h - PostgreSQL ResultSet class
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 #ifndef PGSET_H
13 #define PGSET_H
14 
15 // wxWindows headers
16 #include <wx/wx.h>
17 #include <wx/datetime.h>
18 
19 // PostgreSQL headers
20 #include <libpq-fe.h>
21 
22 #include "utils/misc.h"
23 
24 typedef enum
25 {
26 	PGTYPCLASS_NUMERIC = 1,
27 	PGTYPCLASS_BOOL,
28 	PGTYPCLASS_STRING,
29 	PGTYPCLASS_DATE,
30 	PGTYPCLASS_OTHER
31 } pgTypClass;
32 
33 class pgConn;
34 
35 // Class declarations
36 class pgSet
37 {
38 public:
39 	pgSet();
40 	pgSet(PGresult *newRes, pgConn *newConn, wxMBConv &cnv, bool needColQt);
41 	~pgSet();
NumRows()42 	long NumRows() const
43 	{
44 		return nRows;
45 	}
NumCols()46 	long NumCols() const
47 	{
48 		return nCols;
49 	}
50 
MoveNext()51 	void MoveNext()
52 	{
53 		if (pos <= nRows) pos++;
54 	}
MovePrevious()55 	void MovePrevious()
56 	{
57 		if (pos > 0) pos--;
58 	}
MoveFirst()59 	void MoveFirst()
60 	{
61 		if (nRows) pos = 1;
62 		else pos = 0;
63 	}
MoveLast()64 	void MoveLast()
65 	{
66 		pos = nRows;
67 	}
Locate(long l)68 	void Locate(long l)
69 	{
70 		pos = l;
71 	}
CurrentPos()72 	long CurrentPos() const
73 	{
74 		return pos;
75 	}
Bof()76 	bool Bof() const
77 	{
78 		return (!nRows || pos < 1);
79 	}
Eof()80 	bool Eof() const
81 	{
82 		return (!nRows || pos > nRows);
83 	}
84 	wxString ColName(const int col) const;
85 	OID ColTypeOid(const int col) const;
86 	long ColTypeMod(const int col) const;
87 	wxString ColType(const int col) const;
88 	wxString ColFullType(const int col) const;
89 	pgTypClass ColTypClass(const int col) const;
90 
GetInsertedOid()91 	OID GetInsertedOid() const
92 	{
93 		return PQoidValue(res);
94 	}
95 	long GetInsertedCount() const;
ColSize(const int col)96 	int ColSize(const int col) const
97 	{
98 		return PQfsize(res, col);
99 	}
IsNull(const int col)100 	bool IsNull(const int col) const
101 	{
102 		return (PQgetisnull(res, pos - 1, col) != 0);
103 	}
104 	int ColScale(const int col) const;
105 	int ColNumber(const wxString &colName) const;
106 	bool HasColumn(const wxString &colname) const;
107 
108 
109 	wxString GetVal(const int col) const;
110 	wxString GetVal(const wxString &col) const;
111 	long GetLong(const int col) const;
112 	long GetLong(const wxString &col) const;
113 	bool GetBool(const int col) const;
114 	bool GetBool(const wxString &col) const;
115 	double GetDouble(const int col) const;
116 	double GetDouble(const wxString &col) const;
117 	wxDateTime GetDateTime(const int col) const;
118 	wxDateTime GetDateTime(const wxString &col) const;
119 	wxDateTime GetDate(const int col) const;
120 	wxDateTime GetDate(const wxString &col) const;
121 	wxULongLong GetLongLong(const int col) const;
122 	wxULongLong GetLongLong(const wxString &col) const;
123 	OID GetOid(const int col) const;
124 	OID GetOid(const wxString &col) const;
125 
126 	char *GetCharPtr(const int col) const;
127 	char *GetCharPtr(const wxString &col) const;
128 
GetConversion()129 	wxMBConv &GetConversion() const
130 	{
131 		return conv;
132 	}
133 
GetCommandStatus()134 	wxString GetCommandStatus() const
135 	{
136 		if (res)
137 		{
138 			return wxString(PQcmdStatus(res), conv);
139 		}
140 		return wxEmptyString;
141 	}
142 
143 protected:
144 	pgConn *conn;
145 	PGresult *res;
146 	long pos, nRows, nCols;
147 	wxString ExecuteScalar(const wxString &sql) const;
148 	wxMBConv &conv;
149 	bool needColQuoting;
150 	mutable wxArrayString colTypes, colFullTypes;
151 	wxArrayInt colClasses;
152 };
153 
154 
155 
156 class pgSetIterator
157 {
158 public:
159 	pgSetIterator(pgSet *s);
160 	pgSetIterator(pgConn *conn, const wxString &sql);
161 	~pgSetIterator();
162 
163 	bool RowsLeft();
164 	bool MovePrev();
IsValid()165 	bool IsValid()
166 	{
167 		return set != 0;
168 	}
Set()169 	pgSet *Set()
170 	{
171 		return set;
172 	}
173 
GetVal(const int col)174 	wxString GetVal(const int col) const
175 	{
176 		return set->GetVal(col);
177 	}
GetVal(const wxString & col)178 	wxString GetVal(const wxString &col) const
179 	{
180 		return set->GetVal(col);
181 	}
GetLong(const int col)182 	long GetLong(const int col) const
183 	{
184 		return set->GetLong(col);
185 	}
GetLong(const wxString & col)186 	long GetLong(const wxString &col) const
187 	{
188 		return set->GetLong(col);
189 	}
GetBool(const int col)190 	bool GetBool(const int col) const
191 	{
192 		return set->GetBool(col);
193 	}
GetBool(const wxString & col)194 	bool GetBool(const wxString &col) const
195 	{
196 		return set->GetBool(col);
197 	}
GetDouble(const int col)198 	double GetDouble(const int col) const
199 	{
200 		return set->GetDouble(col);
201 	}
GetDouble(const wxString & col)202 	double GetDouble(const wxString &col) const
203 	{
204 		return set->GetDouble(col);
205 	}
GetDateTime(const int col)206 	wxDateTime GetDateTime(const int col) const
207 	{
208 		return set->GetDateTime(col);
209 	}
GetDateTime(const wxString & col)210 	wxDateTime GetDateTime(const wxString &col) const
211 	{
212 		return set->GetDateTime(col);
213 	}
GetDate(const int col)214 	wxDateTime GetDate(const int col) const
215 	{
216 		return set->GetDate(col);
217 	}
GetDate(const wxString & col)218 	wxDateTime GetDate(const wxString &col) const
219 	{
220 		return set->GetDate(col);
221 	}
GetLongLong(const int col)222 	wxULongLong GetLongLong(const int col) const
223 	{
224 		return set->GetLongLong(col);
225 	}
GetLongLong(const wxString & col)226 	wxULongLong GetLongLong(const wxString &col) const
227 	{
228 		return set->GetLongLong(col);
229 	}
GetOid(const int col)230 	OID GetOid(const int col) const
231 	{
232 		return set->GetOid(col);
233 	}
GetOid(const wxString & col)234 	OID GetOid(const wxString &col) const
235 	{
236 		return set->GetOid(col);
237 	}
238 
239 protected:
240 	pgSet *set;
241 	bool first;
242 };
243 
244 #endif
245