File: PostgresRecordset.cpp

package info (click to toggle)
cvsnt 2.5.03.2382-3.3%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 32,288 kB
  • ctags: 24,567
  • sloc: ansic: 136,648; cpp: 102,037; sh: 42,846; asm: 3,495; makefile: 1,763; ada: 1,681; perl: 1,352; pascal: 1,089; cs: 1,008; yacc: 805; python: 453; xml: 263; sql: 210; lisp: 7
file content (186 lines) | stat: -rw-r--r-- 3,965 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
	CVSNT Generic API
    Copyright (C) 2004 Tony Hoyle and March-Hare Software Ltd

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#endif

#include <config.h>
#include "../lib/api_system.h"

#ifdef HAVE_LIBPQ_FE_H
#include <libpq-fe.h>
#else
// else what??
#endif

#include <string>

#include "../cvs_string.h"
#include "../ServerIO.h"
#include "PostgresRecordset.h"

CPostgresField::CPostgresField()
{
}

CPostgresField::~CPostgresField()
{
}

CPostgresField::operator int()
{
	int r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%d",&r);
	return r;
}

CPostgresField::operator long()
{
	long r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%ld",&r);
	return r;
}

CPostgresField::operator unsigned()
{
	unsigned r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%u",&r);
	return r;
}

CPostgresField::operator unsigned long()
{
	unsigned long r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%lu",&r);
	return r;
}

#if defined(_WIN32) || defined(_WIN64)
CPostgresField::operator __int64()
{
	__int64 r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%I64d",&r);
	return r;
}
#else
CPostgresField::operator long long()
{
	long long r = 0;
	sscanf(PQgetvalue(rs->m_pStmt,rs->m_current_row,field),"%Ld",&r);
	return r;
}
#endif

CPostgresField::operator const char *()
{
	return PQgetvalue(rs->m_pStmt,rs->m_current_row,field);
}

CPostgresField::operator const wchar_t *()
{
	wdata=cvs::wide(operator const char *());
	return wdata.c_str();
}

/**********************************************************************/

CPostgresRecordset::CPostgresRecordset()
{
	m_pStmt = NULL;
}

CPostgresRecordset::~CPostgresRecordset()
{
	Close();
}

bool CPostgresRecordset::Init(PGconn *pDb, PGresult *pStmt)
{
	m_pStmt = pStmt;

	m_num_fields = PQnfields(m_pStmt);
	m_sqlfields.resize(m_num_fields);
	for(int n=0; n<m_num_fields; n++)
	{
		m_sqlfields[n].field = n;
		m_sqlfields[n].rs = this;
		m_sqlfields[n].name = PQfname(m_pStmt,n);
		m_sqlfields[n].type = PQftype(m_pStmt,n);
	}

	m_num_rows = PQntuples(m_pStmt);
	m_current_row = 0;

	return true;
}

bool CPostgresRecordset::Close()
{
	if(m_pStmt)
		PQclear(m_pStmt);
	m_pStmt = NULL;
	return true;
}

bool CPostgresRecordset::Closed() const
{
	if(m_pStmt)
		return false;
	return true;
}

bool CPostgresRecordset::Eof() const
{
	return m_current_row>=m_num_rows;
}

bool CPostgresRecordset::Next()
{
	if(m_current_row >= m_num_rows)
		return false;
	return (++m_current_row >= m_num_rows);
}

CSqlField* CPostgresRecordset::operator[](size_t item) const
{
	if(item>=(size_t)m_num_fields)
		return NULL;
	return (CSqlField*)&m_sqlfields[item];
}

CSqlField* CPostgresRecordset::operator[](int item) const
{
	if(item<0 || item>=m_num_fields)
		return NULL;
	return (CSqlField*)&m_sqlfields[item];
}

CSqlField* CPostgresRecordset::operator[](const char *item) const
{
	for(size_t n=0; n<(size_t)m_num_fields; n++)
		if(!strcasecmp(m_sqlfields[n].name.c_str(),item))
			return (CSqlField*)&m_sqlfields[n];
	CServerIo::error("Database error - field '%s' not found in recordset.",item);
	return NULL;
}