summaryrefslogtreecommitdiffstats
path: root/opengnsys-admin/ogAdmSources/includes/Database.cpp
blob: 752d313cbeb22e9b6cf6491416678b0109724abc (plain)
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
// ******************************************************************************************************
// Aplicación HIDRA
// Copyright 2004 Jos�Manuel Alonso. Todos los derechos reservados.
// Fichero: Database.cpp
//	Descripción:
//		Fichero de implementaci� de la clase Database para funciones de manipulaci�
//		de bases de datos sobre un Servidor Mysql
// ******************************************************************************************************
#include "Database.h"
// __________________________________________________________________________
void ErrorHandler(Herror hr, char* ErrStr)
{
	sprintf(ErrStr,"Error:\n");
	sprintf(ErrStr,"%sCode = %d\n",ErrStr ,hr.nError);
	sprintf(ErrStr,"%sDescription = %s",ErrStr, (char*) hr.dError);
}
// __________________________________________________________________________
Database::Database()
{
	m_Cnn=NULL;
	sprintf(m_ErrStr,"NULL POINTER");
}
// __________________________________________________________________________
void Database::GetErrorErrStr(char* ErrStr)
{
	sprintf(ErrStr,"%s",m_ErrStr);
}
// __________________________________________________________________________
void Table::GetErrorErrStr(char* ErrStr)
{
	sprintf(ErrStr,"%s",m_ErrStr);
}
// __________________________________________________________________________
bool Database::Open(char* UserName, char* Pwd,char* server,char*Bd)
{
	Herror hr;
	m_Cnn=mysql_init(NULL);
	if(m_Cnn==NULL){
		hr.nError=0;
		strcpy(hr.dError,"Error en la Creación del objeto MYSQL");
		ErrorHandler(hr,m_ErrStr);
		return(false); // Fallo de inicializaci�
	}
	
	if(!mysql_real_connect(m_Cnn, server,UserName,Pwd,Bd, MYSQL_PORT,NULL,0)){
		mysql_error(m_Cnn);
		hr.nError=mysql_errno(m_Cnn);
		strcpy(hr.dError,mysql_error(m_Cnn));
		ErrorHandler(hr,m_ErrStr);
		return(false); // Fallo de conexi�
	}
	hr.nError=0;
	strcpy(hr.dError,"Success");
	ErrorHandler(hr,m_ErrStr);
	return (true);
}
// __________________________________________________________________________
bool Database::Close()
{
		mysql_close(m_Cnn);
		return(true);
}
// __________________________________________________________________________
bool Database::Execute(char* CmdStr)
{
	Herror hr;
	if (mysql_query(m_Cnn,CmdStr)){ // Ejecuta la consulta
		mysql_error(m_Cnn);
		hr.nError=mysql_errno(m_Cnn);
		strcpy(hr.dError,mysql_error(m_Cnn));
		ErrorHandler(hr,m_ErrStr);
		mysql_close(m_Cnn);
		return(false); // Fallo de conexión
	}
	hr.nError=0;
	strcpy(hr.dError,"Success");
	ErrorHandler(hr,m_ErrStr);
	return (true);
}
// __________________________________________________________________________
bool Database::Execute(char* CmdStr, Table& Tbl)
{
	Herror hr;
	if (mysql_query(m_Cnn,CmdStr)) { // Ejecuta la consulta
		mysql_error(m_Cnn);
		hr.nError=mysql_errno(m_Cnn);
		strcpy(hr.dError,mysql_error(m_Cnn));
		ErrorHandler(hr,m_ErrStr);
		mysql_close(m_Cnn);
		return(false); // Fallo de conexi�
	}

	hr.nError=0;
	strcpy(hr.dError,"Success");
	ErrorHandler(hr,m_ErrStr);

	Tbl.m_Rec = mysql_store_result(m_Cnn) ; // Toma el recordset
	if(Tbl.m_Rec){
		Tbl.row=mysql_fetch_row(Tbl.m_Rec);
		Tbl.fields = mysql_fetch_fields(Tbl.m_Rec);
		Tbl.num_fields = mysql_num_fields(Tbl.m_Rec);
		Tbl.numreg=mysql_num_rows(Tbl.m_Rec);
		Tbl.eof=Tbl.numreg==0; // Consulta vacia
	}
	return (true);
}
// __________________________________________________________________________
Table::Table()
{
	m_Rec=NULL;
}
// __________________________________________________________________________
bool Table::ISEOF()
{
	return(eof);
}
// __________________________________________________________________________
bool Table::Get(const char* FieldName, char *FieldValue)
{
	char * aux;
	aux=tomadato(FieldName);
	if(aux)
		strcpy(FieldValue,aux);
	else
		strcpy(FieldValue,"");
	return(true);
}
// __________________________________________________________________________
bool Table::Get(const char* FieldName,int &FieldValue)
{
	char *aux;
	aux=tomadato(FieldName);
	if(aux)
		FieldValue=atoi(aux);
	else
		FieldValue=0;
	return(true);
}
// __________________________________________________________________________
bool Table::Get(const char* FieldName,char &FieldValue)
{
	char *aux;
	aux=tomadato(FieldName);
	FieldValue=aux[0];
	return(true);
}
// __________________________________________________________________________
char* Table::tomadato(const char* FieldName)
{
	Herror hr;
	unsigned int i;

	for(i = 0; i < num_fields; i++){
		if(strcmp((char*)fields[i].name,FieldName)==0){
			sprintf(m_ErrStr,"Success");
			return((char*)row[i]);
		}
	}
	hr.nError=-1;
	strcpy(hr.dError,"El nombre del campo no existe");
	ErrorHandler(hr,m_ErrStr);
	return(NULL); // No existe el nombre del campo en la tabla
}
// __________________________________________________________________________

bool Table::MoveNext()
{
	eof=false;
	row=mysql_fetch_row(m_Rec);
	if(row==NULL){
		if(!mysql_eof(m_Rec)) 
			return(false); // Fallo de lectura
		else
			eof=true; // Fin de fichero
	}
	return (true);
}
// __________________________________________________________________________
bool Table::MoveFirst()
{
	my_ulonglong auxnumreg;
	
	auxnumreg=0;
	mysql_data_seek(m_Rec,auxnumreg);
	return (MoveNext());
}
// __________________________________________________________________________
bool Table::MoveLast()
{
	my_ulonglong auxnumreg;
	auxnumreg=numreg;
	auxnumreg--;
	if(auxnumreg<0) auxnumreg=0; // Principio de fichero
	mysql_data_seek(m_Rec,auxnumreg);
	return (MoveNext());
	return (true);
}