File: MySqlRecordset.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 (182 lines) | stat: -rw-r--r-- 3,424 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
/*
	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 version 2.1 as published by the Free Software Foundation.

    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 strcasecmp stricmp
#endif

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

#ifdef _WIN32
#include "mysql-3.23/mysql.h"
#else
#include <mysql.h>
#endif

#include <string>

#include "../cvs_string.h"
#include "MySqlRecordset.h"

CMySqlField::CMySqlField()
{
	field=NULL;
	data=NULL;
}

CMySqlField::~CMySqlField()
{
}

CMySqlField::operator int()
{
	return(atoi(*(char**)data));
}

CMySqlField::operator long()
{
	return (long)operator unsigned long();
}

CMySqlField::operator unsigned()
{
	return (unsigned)operator unsigned long();
}

CMySqlField::operator unsigned long()
{
	return strtoul(*(char**)data,NULL,10);
}

#if defined(_WIN32) || defined(_WIN64)
CMySqlField::operator __int64()
{
	__int64 s;
	if(!sscanf(*(char**)data,"%I64u",&s))
		return 0;
	return s;
}
#else
CMySqlField::operator long long()
{
	long long s;
	if(!sscanf(*(char**)data,"%Lu",&s))
		return 0;
	return s;
}
#endif

CMySqlField::operator const char *()
{
	return *(const char **)data;
}

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

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

CMySqlRecordset::CMySqlRecordset()
{
	m_result = NULL;
	m_bEof=false;
}

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

bool CMySqlRecordset::Init()
{
	m_bEof=false;

	m_fields = mysql_fetch_fields(m_result);
	if (!m_fields)
		return false;
	m_num_fields = mysql_num_fields(m_result);

	m_sqlfields.resize(m_num_fields);
	for(int n=0; n<m_num_fields; n++)
	{
		m_sqlfields[n].field = m_fields+n;
		m_sqlfields[n].data = NULL;
	}

	Next();
	return true;
};

bool CMySqlRecordset::Close()
{
	if(m_result)
		mysql_free_result(m_result);
	m_result=NULL;
	return true;
}

bool CMySqlRecordset::Closed() const
{
	if(!m_result)
		return true;
	return false;
}

bool CMySqlRecordset::Eof() const
{
	return m_bEof;
}

bool CMySqlRecordset::Next()
{
	MYSQL_ROW row = mysql_fetch_row(m_result);
	if(row==NULL)
		m_bEof=true;
	for(int n=0; n<m_num_fields; n++)
		m_sqlfields[n].data = row+n;
	return !m_bEof;
}

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

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

CSqlField* CMySqlRecordset::operator[](const char *item) const
{
	for(size_t n=0; n<(size_t)m_num_fields; n++)
		if(!strcasecmp(m_fields[n].name,item))
			return (CSqlField*)&m_sqlfields[n];
	return NULL;
}