source: admin/Sources/Includes/Database.cpp @ 7caf5a7c

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 7caf5a7c was 3ec149c, checked in by alonso <alonso@…>, 15 years ago

git-svn-id: https://opengnsys.es/svn/trunk@1314 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 5.4 KB
Line 
1// ********************************************************************************************************
2// Nombre del fichero: Database.cpp
3// Autor: José Manuel Alonso (E.T.S.I.I.) Universidad de Sevilla
4// Fecha Creación: Marzo-2010
5// Fecha Última modificación: Marzo-2010
6// Descripción:
7//              Fichero de implementación de la clase Database para funciones de manipulación
8//              de bases de datos sobre un Servidor Mysql
9// ********************************************************************************************************
10#include "Database.h"
11// __________________________________________________________________________
12void ErrorHandler(Herror hr, char* ErrStr)
13{
14        sprintf(ErrStr,"Error:\n");
15        sprintf(ErrStr,"%sCode = %d\n",ErrStr ,hr.nError);
16        sprintf(ErrStr,"%sDescription = %s",ErrStr, (char*) hr.dError);
17}
18// __________________________________________________________________________
19Database::Database()
20{
21        m_Cnn=NULL;
22        sprintf(m_ErrStr,"NULL POINTER");
23}
24// __________________________________________________________________________
25void Database::GetErrorErrStr(char* ErrStr)
26{
27        sprintf(ErrStr,"%s",m_ErrStr);
28}
29// __________________________________________________________________________
30void Table::GetErrorErrStr(char* ErrStr)
31{
32        sprintf(ErrStr,"%s",m_ErrStr);
33}
34// __________________________________________________________________________
35bool Database::Open(char* UserName, char* Pwd,char* server,char*Bd)
36{
37        Herror hr;
38        m_Cnn=mysql_init(NULL);
39        if(m_Cnn==NULL){
40                hr.nError=0;
41                strcpy(hr.dError,"Error en la Creación del objeto MYSQL");
42                ErrorHandler(hr,m_ErrStr);
43                return(false); // Fallo de inicializaci�
44        }
45       
46        if(!mysql_real_connect(m_Cnn, server,UserName,Pwd,Bd, MYSQL_PORT,NULL,0)){
47                mysql_error(m_Cnn);
48                hr.nError=mysql_errno(m_Cnn);
49                strcpy(hr.dError,mysql_error(m_Cnn));
50                ErrorHandler(hr,m_ErrStr);
51                return(false); // Fallo de conexi�
52        }
53        hr.nError=0;
54        strcpy(hr.dError,"Success");
55        ErrorHandler(hr,m_ErrStr);
56        return (true);
57}
58// __________________________________________________________________________
59bool Database::Close()
60{
61                mysql_close(m_Cnn);
62                return(true);
63}
64// __________________________________________________________________________
65bool Database::Execute(char* CmdStr)
66{
67        Herror hr;
68        if (mysql_query(m_Cnn,CmdStr)){ // Ejecuta la consulta
69                mysql_error(m_Cnn);
70                hr.nError=mysql_errno(m_Cnn);
71                strcpy(hr.dError,mysql_error(m_Cnn));
72                ErrorHandler(hr,m_ErrStr);
73                mysql_close(m_Cnn);
74                return(false); // Fallo de conexión
75        }
76        hr.nError=0;
77        strcpy(hr.dError,"Success");
78        ErrorHandler(hr,m_ErrStr);
79        return (true);
80}
81// __________________________________________________________________________
82bool Database::Execute(char* CmdStr, Table& Tbl)
83{
84        Herror hr;
85        if (mysql_query(m_Cnn,CmdStr)) { // Ejecuta la consulta
86                mysql_error(m_Cnn);
87                hr.nError=mysql_errno(m_Cnn);
88                strcpy(hr.dError,mysql_error(m_Cnn));
89                ErrorHandler(hr,m_ErrStr);
90                mysql_close(m_Cnn);
91                return(false); // Fallo de conexi�
92        }
93
94        hr.nError=0;
95        strcpy(hr.dError,"Success");
96        ErrorHandler(hr,m_ErrStr);
97
98        Tbl.m_Rec = mysql_store_result(m_Cnn) ; // Toma el recordset
99        if(Tbl.m_Rec){
100                Tbl.row=mysql_fetch_row(Tbl.m_Rec);
101                Tbl.fields = mysql_fetch_fields(Tbl.m_Rec);
102                Tbl.num_fields = mysql_num_fields(Tbl.m_Rec);
103                Tbl.numreg=mysql_num_rows(Tbl.m_Rec);
104                Tbl.eof=Tbl.numreg==0; // Consulta vacia
105        }
106        return (true);
107}
108// __________________________________________________________________________
109Table::Table()
110{
111        m_Rec=NULL;
112}
113// __________________________________________________________________________
114bool Table::ISEOF()
115{
116        return(eof);
117}
118// __________________________________________________________________________
119bool Table::Get(const char* FieldName, char *FieldValue)
120{
121        char * aux;
122        aux=tomadato(FieldName);
123        if(aux)
124                strcpy(FieldValue,aux);
125        else
126                strcpy(FieldValue,"");
127        return(true);
128}
129// __________________________________________________________________________
130bool Table::Get(const char* FieldName,int &FieldValue)
131{
132        char *aux;
133        aux=tomadato(FieldName);
134        if(aux)
135                FieldValue=atoi(aux);
136        else
137                FieldValue=0;
138        return(true);
139}
140// __________________________________________________________________________
141bool Table::Get(const char* FieldName,char &FieldValue)
142{
143        char *aux;
144        aux=tomadato(FieldName);
145        FieldValue=aux[0];
146        return(true);
147}
148// __________________________________________________________________________
149char* Table::tomadato(const char* FieldName)
150{
151        Herror hr;
152        unsigned int i;
153
154        for(i = 0; i < num_fields; i++){
155                if(strcmp((char*)fields[i].name,FieldName)==0){
156                        sprintf(m_ErrStr,"Success");
157                        return((char*)row[i]);
158                }
159        }
160        hr.nError=-1;
161        strcpy(hr.dError,"El nombre del campo no existe");
162        ErrorHandler(hr,m_ErrStr);
163        return(NULL); // No existe el nombre del campo en la tabla
164}
165// __________________________________________________________________________
166
167bool Table::MoveNext()
168{
169        eof=false;
170        row=mysql_fetch_row(m_Rec);
171        if(row==NULL){
172                if(!mysql_eof(m_Rec))
173                        return(false); // Fallo de lectura
174                else
175                        eof=true; // Fin de fichero
176        }
177        return (true);
178}
179// __________________________________________________________________________
180bool Table::MoveFirst()
181{
182        my_ulonglong auxnumreg;
183       
184        auxnumreg=0;
185        mysql_data_seek(m_Rec,auxnumreg);
186        return (MoveNext());
187}
188// __________________________________________________________________________
189bool Table::MoveLast()
190{
191        my_ulonglong auxnumreg;
192        auxnumreg=numreg;
193        auxnumreg--;
194        if(auxnumreg<0) auxnumreg=0; // Principio de fichero
195        mysql_data_seek(m_Rec,auxnumreg);
196        return (MoveNext());
197        return (true);
198}
Note: See TracBrowser for help on using the repository browser.