Bonjour

Pour un de mes projets en VC++ j'utilise une classe cBD de ma fabrication pour gerer le SQL envoyé à MySql (cf la classe ci-dessous)

Or chaque appel (en particulier des methodes query_one et query_N) de la memoire (dans le gestionnaire de taches) est consommée ... Ca augmente tout le temps jusqu'a atteindre plusieurs giga. je dois redémarrer l'appli (qui est un serveur en service) régulierement

Je suis convaincu d'avoir une coquille lors de mes appels à mysql dans cette classe mais je ne trouve pas.

Est ce qu'un pro pourrait jeter un oeil a mes deux methodes de ma classe et me dire ou est le pb ?


codé en VC++ 6.0 convertir en VS2010
MySql : 5.0.67

Dans l'attente de votre aide..
JFL



-------------------------------------------------------------------
cBD.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
 
//pragma warning(disable: 4005 4244)
#include "cBd.h"
 
cBd::cBd()
{
	this->server	= "localhost";
	this->user		= "root";
	this->mdp		= "";
	this->base		= "terranota";
}
 
cBd::cBd(char* server,char* user,char* mdp,char* base)
{
	this->server = server;
	this->user   = user;
	this->mdp    = mdp;
	this->base   = base;
}
 
cBd::~cBd()
{
}
 
 
void cBd::connect(void)
{
	string strMessageErreur;
MYSQL* conn;
 
	this->connexion = mysql_init(NULL); 
	conn = mysql_real_connect(this->connexion,this->server,this->user,this->mdp,this->base,0,NULL,0);
	if ( conn == NULL) 
	{ 
		strMessageErreur.append("Probleme pour se connecter à la base de données : ");
		strMessageErreur.append(this->base);
		strMessageErreur.append(" (Server:");
		strMessageErreur.append(this->server);
		strMessageErreur.append("    user:");
		strMessageErreur.append(this->user);
		strMessageErreur.append(") ");
//		strMessageErreur.append(mysql_errno(this->connexion));
		strMessageErreur.append(":");
		strMessageErreur.append(mysql_error(this->connexion));
		strMessageErreur.append("\n");
 
 
		*myLog<<'e'<<strMessageErreur<<".\n";
        ::SetEvent(hTerminateEvent);
		throw BdException(strMessageErreur);
	} 
	else 
	{ 
		*myLog<<'s'<<"Connecté à la base '"<<this->base<<"' sur '"<<this->server<<"' avec le user '"<<this->user<<"'.\n";
		this->serverInfos = mysql_get_server_info(this->connexion); 
 
		if (this->serverInfos != NULL) 
		{ 
			*myLog<<'s'<<"Information sur le serveur:"<<this->serverInfos<<".\n";
		} 
		else 
		{ 
			strMessageErreur.append("Impossible de recuperer les informations du serveur.");
			*myLog<<'e'<<strMessageErreur<<"\n";
			throw BdException(strMessageErreur);
		}
	}
}
 
int cBd::query_one(CString CstrSql,MYSQL_ROW *row,my_ulonglong *id)
{
	MYSQL_RES*	res=NULL;
 
	string strSql = (LPCSTR) CstrSql;
	string strTmpSql = "";
	string typeRequete = strSql.substr(0,6);
 
	if(this->connexion == NULL)	this->connect();
	CSingleLock singlelock(&mutexBD);
	singlelock.Lock();
 
	if(typeRequete == "INSERT")
	{
		// ajout dans la liste des champs
		int intPosVALUES = strSql.find("VALUES");
		strTmpSql = strSql.substr(0,intPosVALUES - 2);
		strTmpSql.append(", user_creation , date_creation ");
		strTmpSql.append(strSql.substr(intPosVALUES - 2));
		strSql = strTmpSql;
 
		// ajout dans les valeurs
		int intPosFin = strSql.find_last_of(")");
		strTmpSql = strSql.substr(0,intPosFin);
		strTmpSql.append(", 'terraNota', now() ");
		strTmpSql.append(strSql.substr(intPosFin));
		strSql = strTmpSql;
	}
	else
	{
		if(typeRequete == "UPDATE")
		{
			int intPosSET = strSql.find("SET");
			strTmpSql = strSql.substr(0,intPosSET + 3);
			strTmpSql.append(" user_modification = 'terraNota', date_modification = now(), ");
			strTmpSql.append(strSql.substr(intPosSET + 3));
			strSql = strTmpSql;
		}
	}
	*myLogSql<<'i'<<"QUERY_ONE : "<<strSql.c_str()<<"\n";
 
	try
	{
		int result = mysql_query(this->connexion, (char*)strSql.c_str());
		if(result == TELPASS_SQL_OK)
		{
			res = mysql_store_result(this->connexion);
			int intNbRows = mysql_affected_rows(this->connexion);
			*myLogSql<<'i'<<"QUERY_ONE : SUCCESS : AffectedRow : "<<intNbRows<<"\n";
			if(intNbRows == -1)
			{
				// erreur dans la requete
				char* tmp = "";
				int intErreurNum = mysql_errno(this->connexion);
				string strErreurMessage = mysql_error(this->connexion);
				*myLogSql<<'e'<<"QUERY_ONE : ERREUR :"<<intErreurNum<<":"<<strErreurMessage;
				singlelock.Unlock();
				mysql_free_result(res);
				return TELPASS_SQL_NOK;
			}
			if(intNbRows == 0)
			{
				// aucune ligne affectee
				(*row) = NULL;
				singlelock.Unlock();
				mysql_free_result(res);
				return TELPASS_SQL_AUCUN;
			}
			else
			{
				// N lignes affectées
				if(typeRequete == "SELECT")
				{
					MYSQL_ROW tmp = mysql_fetch_row(res);
					(*row) = tmp;
				}
				else
				{
					(*row) = NULL;
					if(typeRequete == "INSERT")
					{
						 my_ulonglong tmp = mysql_insert_id(this->connexion);
						(*id) = tmp;
					}
				}
				singlelock.Unlock();
//				mysql_free_result(res);
				return TELPASS_SQL_OK;
			}
		}
		else
		{
			char* tmp = "";
			int intErreurNum = mysql_errno(this->connexion);
			string strErreurMessage = mysql_error(this->connexion);
			*myLogSql<<'i'<<"QUERY_ONE : ERREUR :"<<intErreurNum<<":"<<strErreurMessage;
			(*row) = NULL;
			singlelock.Unlock();
			mysql_free_result(res);
			cErreur* erreur = new cErreur();
			switch(intErreurNum)
			{
				case 2013 :     *myLogSql<<'e'<<"QUERY_ONE : "<<strErreurMessage<<" : "<<strSql<<"\n";
								erreur->mailErreur(TELPASS_ERRORMAIL_SQL,"",CstrSql,0);
								break;
				case 2006 :     *myLog<<'e'<<"QUERY_ONE : "<<strErreurMessage<<"\n";
								*myLogSql<<'e'<<"QUERY_ONE : "<<strErreurMessage<<"\n";
								::SetEvent(hTerminateEvent); 
//								delete(erreur);
								throw BdException(strErreurMessage);
								break;
			}
//			delete(erreur);
			return TELPASS_SQL_NOK;
		}
	}
	catch(exception& e)
	{
		singlelock.Unlock();
 
		throw e;
	}
	singlelock.Unlock();
}
 
int cBd::query_N(CString CstrSql,mysql_rows *rows,my_ulonglong *id)
{
	MYSQL_RES*	res		=	NULL;
 
	string strSql = (LPCSTR) CstrSql;
	string strTmpSql = "";
 
	CSingleLock singlelock(&mutexBD);
	singlelock.Lock();
 
	if(this->connexion == NULL)	this->connect();
 
	*myLogSql<<'i'<<"QUERY_N : "<<strSql.c_str()<<"\n";
	try
	{
		int result = mysql_query(this->connexion, (char*)strSql.c_str());
		if(result == 0)
		{
			res = mysql_store_result(this->connexion);
			int intNbRows = mysql_affected_rows(this->connexion);
			*myLogSql<<'i'<<"QUERY_N : SUCCESS : AffectedRow : "<<intNbRows<<"\n";
			if(intNbRows == -1)
			{
				// erreur dans la requete
				char* tmp = "";
				int intErreurNum = mysql_errno(this->connexion);
				string strErreurMessage = mysql_error(this->connexion);
				*myLogSql<<'e'<<"QUERY_N : ERREUR :"<<intErreurNum<<":"<<strErreurMessage;
				singlelock.Unlock();
				return TELPASS_SQL_NOK;
			}
			if(intNbRows == 0)
			{
				// aucune ligne affectee
				(*rows).RemoveAll();
				singlelock.Unlock();
				return TELPASS_SQL_AUCUN;
			}
			else
			{
				MYSQL_ROW row;
				while((row = mysql_fetch_row(res)) != NULL)
				{
					(*rows).Add(row);
				}
				singlelock.Unlock();
				return TELPASS_SQL_OK;
			}
		}
		else
		{
			char* tmp = "";
			int intErreurNum = mysql_errno(this->connexion);
			string strErreurMessage = mysql_error(this->connexion);
			*myLogSql<<'i'<<"QUERY_N : ERREUR :"<<intErreurNum<<":"<<strErreurMessage<<":"<<strSql;
			(*rows).RemoveAll();
			singlelock.Unlock();
			return TELPASS_SQL_NOK;
		}
	}
	catch(exception& e)
	{
		singlelock.Unlock();
		throw e;
	}
}
int cBd::query(CString CstrSql,int *NbRowsAffected)
{
	MYSQL_RES*	res		=	NULL;
 
	string strSql = (LPCSTR) CstrSql;
	string strTmpSql = "";
 
	CSingleLock singlelock(&mutexBD);
	singlelock.Lock();
 
	if(this->connexion == NULL)	this->connect();
 
	*myLogSql<<'i'<<"QUERY : "<<strSql.c_str()<<"\n";
	try
	{
		int result = mysql_query(this->connexion, (char*)strSql.c_str());
		if(result == 0)
		{
			res = mysql_store_result(this->connexion);
			(*NbRowsAffected) = mysql_affected_rows(this->connexion);
			*myLogSql<<'i'<<"QUERY : SUCCESS : AffectedRow : "<<(*NbRowsAffected)<<"\n";
 
			singlelock.Unlock();
			if((*NbRowsAffected) == -1)
			{
				// erreur dans la requete
				char* tmp = "";
				int intErreurNum = mysql_errno(this->connexion);
				string strErreurMessage = mysql_error(this->connexion);
				*myLogSql<<'e'<<"QUERY : ERREUR :"<<intErreurNum<<":"<<strErreurMessage;
				return TELPASS_SQL_NOK;
			}
			if((*NbRowsAffected) == 0)
			{
				// aucune ligne affectee
				return TELPASS_SQL_AUCUN;
			}
			else
			{
				return TELPASS_SQL_OK;
			}
		}
		else
		{
			char* tmp = "";
			int intErreurNum = mysql_errno(this->connexion);
			string strErreurMessage = mysql_error(this->connexion);
			*myLogSql<<'i'<<"QUERY : ERREUR :"<<intErreurNum<<":"<<strErreurMessage;
			singlelock.Unlock();
			return TELPASS_SQL_NOK;
		}
	}
	catch(exception& e)
	{
		singlelock.Unlock();
		throw e;
	}
}
 
void cBd::test(void)
{
	if(this->connexion == NULL)	this->connect();
	int retour = mysql_query(this->connexion,"select * from t_parametres");
	fprintf(stdout,(char*) mysql_affected_rows(this->connexion));
}
 
CString cBd::setForSql(time_t field,time_format format)
{
	CString strSql = "";
	if(field == 0)
		strSql = "NULL";
	else
		strSql.Format("'%s'", MyTime::getOneTime(format,field).c_str());
	return strSql;
}
 
CString cBd::setForSql(string field)
{
	CString strSql = "";
	if(field.c_str() == NULL)
		strSql ="NULL";
	else
	{
		stringReplace(field,"'","''");
		strSql.Format("'%s'",field.c_str());
	}
	return strSql;
}
 
CString cBd::setForSql(CString field)
{
	CString strSql = "";
	if(field == "")
		strSql ="NULL";
	else
	{
		field.Replace("'","''");
		strSql = "'"+field+"'";
	}
	return strSql;
}
 
CString cBd::setForSql(int field)
{
	CString strSql = "";
	if(field == NULL_INT)
		strSql ="NULL";
	else
		strSql.Format("%d",field);
	return strSql;
}
CString cBd::setForSql(long field)
{
	CString strSql = "";
	if(field == NULL_LONG)
		strSql ="NULL";
	else
		strSql.Format("%l",field);
	return strSql;
}
time_t cBd::getFromSql_datetime(char* field)
{
	if(field==NULL)
		return NULL;
	if(strcmp(field,"")==0)
		return NULL;
	if(strcmp(field,"0000-00-00 00:00:00")==0)
		return NULL;
	else
		return	MyTime::setOneTime(mysql_datetime,field);
}
string cBd::getFromSql_string(char* field)
{
	return field;
}
CString cBd::getFromSql_CString(char* field)
{
	return (LPCSTR)field;
}
int cBd::getFromSql_int(char* field)
{
	return atoi(field); 
}
int cBd::getFromSql_int(CString field)
{
	return atoi(field); 
}
bool cBd::getFromSql_bool(char* field)
{
	return (atoi(field) == 1); 
}
bool cBd::getFromSql_bool(CString field)
{
	return (atoi(field) == 1);  
}


-------------------------------------------------------------------
cBD.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef TELPASS_CBD_H
#define TELPASS_CBD_H
#define TELPASS_SQL_OK	0
#define TELPASS_SQL_NOK	1
#define TELPASS_SQL_AUCUN	2
 
#define NULL_INT INT_MAX 
#define NULL_LONG LONG_MAX 
 
#include <afxmt.h>
#include "Utils/Logger.h"
#include <winsock2.h>
#include <mysql.h>
#include <mysqld_error.h>
#include <errmsg.h>
#include "Exceptions/BdException.h"
#include "Utils/fonctions.h"
#include "cErreur.h"
#include <string>
using namespace std;
 
extern Logger* myLog;
extern Logger* myLogSql;
extern CMutex mutexBD;
extern HANDLE hTerminateEvent;
 
 
/** Format de la date
*/
typedef enum sql_format_ {
	SQL_DATETIME,
	SQL_TIME,
	SQL_DATE,
	SQL_VARCHAR,
	SQL_INT,
} sql_format;
 
typedef CArray<MYSQL_ROW, MYSQL_ROW&> mysql_rows;
 
class cBd
{
	public:
				cBd::cBd();
				cBd::cBd(char* server,char* user,char* mdp,char* base);
				cBd::~cBd();
		void	cBd::connect(void);
		int		cBd::query_one(CString strSql,MYSQL_ROW *row,my_ulonglong *id);
		int		cBd::query_N(CString strSql,mysql_rows *rows,my_ulonglong *id);
		int		cBd::query(CString CstrSql,int *NbRowsAffected);
		void	cBd::test(void);
		CString cBd::setForSql(time_t field,time_format format);
		CString cBd::setForSql(string field);
		CString cBd::setForSql(CString field);
		CString cBd::setForSql(int field);
		CString cBd::setForSql(long field);
		time_t	cBd::getFromSql_datetime(char* field);
		string	cBd::getFromSql_string(char* field);
		CString	cBd::getFromSql_CString(char* field);
		int		cBd::getFromSql_int(char* field);
		int		cBd::getFromSql_int(CString field);
		bool	cBd::getFromSql_bool(char* field);
		bool	cBd::getFromSql_bool(CString field);
	private:
		const char* server;
		const char* base;
		const char* user;
		const char* mdp;
 
		MYSQL*	connexion;
		const char*	serverInfos;
 
		CMutex mutexBD;
};
#endif