00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "MySQL.h"
00012
00013
00014 MUTEX MySQL::MySQLMutex;
00015 int MySQL::MySQLInstance = 0;
00016
00017 MySQL::MySQL(char *pHost, char *pUser, char *pPass, char *pBase, int iPort)
00018 {
00019 if(!MySQLInstance)
00020 MutexInit(&MySQLMutex);
00021
00022 MySQLInstance++;
00023
00024 strncpy(Host, pHost, MYSQL_HOST);
00025 strncpy(User, pUser, MYSQL_USER);
00026 strncpy(Pass, pPass, MYSQL_PASS);
00027 strncpy(Name, pBase, MYSQL_BASE);
00028 Port = iPort;
00029
00030 Connected = 0;
00031 }
00032
00033 MySQL::~MySQL()
00034 {
00035 MySQLInstance--;
00036
00037 if(!MySQLInstance)
00038 MutexStop(&MySQLMutex);
00039
00040 mysql_close(&Base);
00041
00042 Datas.clear();
00043 }
00044
00045 int MySQL::Open()
00046 {
00047 BEGIN
00048
00049 if(!Connected)
00050 {
00051 mysql_init(&Base);
00052
00053 if(!mysql_real_connect(&Base, Host, User, Pass, Name, Port, NULL, 0))
00054 {
00055 MySQLError();
00056 CERROR = ERROR_C_CONNECT;
00057 }
00058
00059 Connected = 1;
00060 }
00061 ENDTRY(ERROR_C_ALREADY_START)
00062
00063 ENDBEGIN
00064 }
00065
00066 int MySQL::Close()
00067 {
00068 BEGIN
00069
00070 if(Connected)
00071 {
00072 mysql_close(&Base);
00073
00074 Connected = 0;
00075 }
00076 ENDTRY(ERROR_C_ALREADY_STOP)
00077
00078 ENDBEGIN
00079 }
00080
00081 int MySQL::Query(char *pQuery, ...)
00082 {
00083 char Temp[MYSQL_BUFFER];
00084 va_list args;
00085 BEGIN
00086
00087 if(Connected)
00088 {
00089 va_start(args, pQuery);
00090 vsprintf(Temp, pQuery, args);
00091 va_end(args);
00092
00093 if(mysql_query(&Base, Temp))
00094 {
00095 MySQLError();
00096 CERROR = ERROR_C_SEND;
00097 }
00098 else
00099 {
00100 CERROR = SaveResult();
00101 }
00102 }
00103 ENDTRY(ERROR_C_ALREADY_STOP)
00104
00105 ENDBEGIN
00106 }
00107
00108 int MySQL::QueryBin(char *pQuery, int iSize)
00109 {
00110 BEGIN
00111
00112 if(Connected)
00113 {
00114 if(mysql_real_query(&Base, pQuery, iSize))
00115 {
00116 MySQLError();
00117 CERROR = ERROR_C_SEND;
00118 }
00119 else
00120 {
00121 CERROR = SaveResult();
00122 }
00123 }
00124 ENDTRY(ERROR_C_ALREADY_STOP)
00125
00126 ENDBEGIN
00127 }
00128
00129 int MySQL::SaveResult()
00130 {
00131 MYSQL_RES *Res;
00132 MYSQL_ROW Row;
00133 BEGIN
00134
00135 Res = mysql_store_result(&Base);
00136 if(Res)
00137 {
00138 Datas.clear();
00139
00140 while(Row = mysql_fetch_row(Res))
00141 Datas.push_back(Row);
00142
00143 if(mysql_errno(&Base))
00144 {
00145 CERROR = ERROR_C_RECEIVE;
00146 }
00147 }
00148 else
00149 {
00150 if(mysql_field_count(&Base))
00151 {
00152 CERROR = ERROR_C_UNKNOW;
00153 }
00154 }
00155
00156 if(CERROR)
00157 {
00158 MySQLError();
00159 Datas.clear();
00160 }
00161
00162 mysql_free_result(Res);
00163
00164 ENDBEGIN
00165 }