Commit 26bf803c authored by Olivier Bertrand's avatar Olivier Bertrand

- Update the MYSQL table handling to use only client API functions.

  It is no more necessary to be liked to libmysql.lib nor mysqlclient.lib.

modified:
  storage/connect/libdoc.cpp
  storage/connect/myconn.cpp
  storage/connect/myconn.h
  storage/connect/tabmysql.cpp
  storage/connect/tabmysql.h
parent 818d0a56
/************** MyConn C++ Program Source Code File (.CPP) **************/ /************** MyConn C++ Program Source Code File (.CPP) **************/
/* PROGRAM NAME: MYCONN */ /* PROGRAM NAME: MYCONN */
/* ------------- */ /* ------------- */
/* Version 1.6 */ /* Version 1.7 */
/* */ /* */
/* COPYRIGHT: */ /* COPYRIGHT: */
/* ---------- */ /* ---------- */
...@@ -317,12 +317,12 @@ int MYSQLC::Open(PGLOBAL g, const char *host, const char *db, ...@@ -317,12 +317,12 @@ int MYSQLC::Open(PGLOBAL g, const char *host, const char *db,
/***********************************************************************/ /***********************************************************************/
bool MYSQLC::Connected(void) bool MYSQLC::Connected(void)
{ {
int rc; //int rc;
if (!m_DB) if (!m_DB)
return FALSE; return FALSE;
else if ((rc = mysql_ping(m_DB)) == CR_SERVER_GONE_ERROR) //else if ((rc = mysql_ping(m_DB)) == CR_SERVER_GONE_ERROR)
return FALSE; // return FALSE;
else else
return TRUE; return TRUE;
...@@ -364,9 +364,11 @@ int MYSQLC::KillQuery(ulong id) ...@@ -364,9 +364,11 @@ int MYSQLC::KillQuery(ulong id)
char kill[20]; char kill[20];
sprintf(kill, "KILL QUERY %u", (unsigned int) id); sprintf(kill, "KILL QUERY %u", (unsigned int) id);
return (m_DB) ? mysql_query(m_DB, kill) : 1; //return (m_DB) ? mysql_query(m_DB, kill) : 1;
return (m_DB) ? mysql_real_query(m_DB, kill, strlen(kill)) : 1;
} // end of KillQuery } // end of KillQuery
#if defined (MYSQL_PREPARED_STATEMENTS)
/***********************************************************************/ /***********************************************************************/
/* Prepare the SQL statement used to insert into a MySQL table. */ /* Prepare the SQL statement used to insert into a MySQL table. */
/***********************************************************************/ /***********************************************************************/
...@@ -405,58 +407,6 @@ int MYSQLC::PrepareSQL(PGLOBAL g, const char *stmt) ...@@ -405,58 +407,6 @@ int MYSQLC::PrepareSQL(PGLOBAL g, const char *stmt)
#endif // !ALPHA #endif // !ALPHA
} // end of PrepareSQL } // end of PrepareSQL
/***********************************************************************/
/* Exec the Select SQL command and get back the result size in rows. */
/***********************************************************************/
int MYSQLC::ExecSQL(PGLOBAL g, const char *query, int *w)
{
int rc = RC_OK;
if (!m_DB) {
strcpy(g->Message, "MySQL not connected");
return RC_FX;
} // endif m_DB
if (w)
*w = 0;
if (m_Rows >= 0)
return RC_OK; // Already done
//if (mysql_query(m_DB, query) != 0) {
if (mysql_real_query(m_DB, query, strlen(query))) {
char *msg = (char*)PlugSubAlloc(g, NULL, 512 + strlen(query));
sprintf(msg, "(%d) %s [%s]", mysql_errno(m_DB),
mysql_error(m_DB), query);
strncpy(g->Message, msg, sizeof(g->Message) - 1);
g->Message[sizeof(g->Message) - 1] = 0;
rc = RC_FX;
} else if (mysql_field_count(m_DB) > 0) {
if (!(m_Res = mysql_store_result(m_DB))) {
char *msg = (char*)PlugSubAlloc(g, NULL, 512 + strlen(query));
sprintf(msg, "mysql_store_result failed: %s", mysql_error(m_DB));
strncpy(g->Message, msg, sizeof(g->Message) - 1);
g->Message[sizeof(g->Message) - 1] = 0;
rc = RC_FX;
} else {
m_Fields = mysql_num_fields(m_Res);
m_Rows = (int)mysql_num_rows(m_Res);
} // endif m_Res
} else {
m_Rows = (int)mysql_affected_rows(m_DB);
sprintf(g->Message, "Affected rows: %d\n", m_Rows);
rc = RC_NF;
} // endif field count
if (w)
*w = mysql_warning_count(m_DB);
return rc;
} // end of ExecSQL
/***********************************************************************/ /***********************************************************************/
/* Bind the parameter buffers. */ /* Bind the parameter buffers. */
/***********************************************************************/ /***********************************************************************/
...@@ -481,7 +431,6 @@ int MYSQLC::BindParams(PGLOBAL g, MYSQL_BIND *bind) ...@@ -481,7 +431,6 @@ int MYSQLC::BindParams(PGLOBAL g, MYSQL_BIND *bind)
} // endif bind } // endif bind
return RC_OK; return RC_OK;
} // end of BindParams
/***********************************************************************/ /***********************************************************************/
/* Execute a prepared statement. */ /* Execute a prepared statement. */
...@@ -515,6 +464,77 @@ int MYSQLC::ExecStmt(PGLOBAL g) ...@@ -515,6 +464,77 @@ int MYSQLC::ExecStmt(PGLOBAL g)
return RC_OK; return RC_OK;
} // end of ExecStmt } // end of ExecStmt
#endif // MYSQL_PREPARED_STATEMENTS
/***********************************************************************/
/* Exec the Select SQL command and get back the result size in rows. */
/***********************************************************************/
int MYSQLC::ExecSQL(PGLOBAL g, const char *query, int *w)
{
int rc = RC_OK;
if (!m_DB) {
strcpy(g->Message, "MySQL not connected");
return RC_FX;
} // endif m_DB
if (w)
*w = 0;
if (m_Rows >= 0)
return RC_OK; // Already done
//if (mysql_query(m_DB, query) != 0) {
if (mysql_real_query(m_DB, query, strlen(query))) {
char *msg = (char*)PlugSubAlloc(g, NULL, 512 + strlen(query));
sprintf(msg, "(%d) %s [%s]", mysql_errno(m_DB),
mysql_error(m_DB), query);
strncpy(g->Message, msg, sizeof(g->Message) - 1);
g->Message[sizeof(g->Message) - 1] = 0;
rc = RC_FX;
//} else if (mysql_field_count(m_DB) > 0) {
} else if (m_DB->field_count > 0) {
if (!(m_Res = mysql_store_result(m_DB))) {
char *msg = (char*)PlugSubAlloc(g, NULL, 512 + strlen(query));
sprintf(msg, "mysql_store_result failed: %s", mysql_error(m_DB));
strncpy(g->Message, msg, sizeof(g->Message) - 1);
g->Message[sizeof(g->Message) - 1] = 0;
rc = RC_FX;
} else {
m_Fields = mysql_num_fields(m_Res);
m_Rows = (int)mysql_num_rows(m_Res);
} // endif m_Res
} else {
// m_Rows = (int)mysql_affected_rows(m_DB);
m_Rows = (int)m_DB->affected_rows;
sprintf(g->Message, "Affected rows: %d\n", m_Rows);
rc = RC_NF;
} // endif field count
if (w)
//*w = mysql_warning_count(m_DB);
*w = m_DB->warning_count;
return rc;
} // end of ExecSQL
/***********************************************************************/
/* Move to a specific row and column */
/***********************************************************************/
void MYSQLC::DataSeek(my_ulonglong row)
{
MYSQL_ROWS *tmp=0;
//DBUG_PRINT("info",("mysql_data_seek(%ld)",(long) row));
if (m_Res->data)
for (tmp = m_Res->data->data; row-- && tmp; tmp = tmp->next) ;
m_Res->current_row = 0;
m_Res->data_cursor = tmp;
} // end of DataSeek
/***********************************************************************/ /***********************************************************************/
/* Fetch one result line from the query result set. */ /* Fetch one result line from the query result set. */
...@@ -534,7 +554,8 @@ int MYSQLC::Fetch(PGLOBAL g, int pos) ...@@ -534,7 +554,8 @@ int MYSQLC::Fetch(PGLOBAL g, int pos)
N++; N++;
if (pos >= 0) if (pos >= 0)
mysql_data_seek(m_Res, (my_ulonglong)pos); // mysql_data_seek(m_Res, (my_ulonglong)pos);
DataSeek((my_ulonglong)pos);
m_Row = mysql_fetch_row(m_Res); m_Row = mysql_fetch_row(m_Res);
return (m_Row) ? RC_OK : RC_EF; return (m_Row) ? RC_OK : RC_EF;
...@@ -547,7 +568,7 @@ char *MYSQLC::GetCharField(int i) ...@@ -547,7 +568,7 @@ char *MYSQLC::GetCharField(int i)
{ {
if (m_Res && m_Row) { if (m_Res && m_Row) {
#if defined(_DEBUG) #if defined(_DEBUG)
MYSQL_FIELD *fld = mysql_fetch_field_direct(m_Res, i); // MYSQL_FIELD *fld = mysql_fetch_field_direct(m_Res, i);
#endif // _DEBUG #endif // _DEBUG
MYSQL_ROW row = m_Row + i; MYSQL_ROW row = m_Row + i;
...@@ -563,16 +584,25 @@ char *MYSQLC::GetCharField(int i) ...@@ -563,16 +584,25 @@ char *MYSQLC::GetCharField(int i)
int MYSQLC::GetFieldLength(int i) int MYSQLC::GetFieldLength(int i)
{ {
if (m_Res) { if (m_Res) {
MYSQL_FIELD *fld = mysql_fetch_field_direct(m_Res, i); // MYSQL_FIELD *fld = mysql_fetch_field_direct(m_Res, i);
// return fld->max_length;
return fld->max_length; return (m_Res)->fields[i].max_length;
} else } else
return 0; return 0;
} // end of GetFieldLength } // end of GetFieldLength
/***********************************************************************/ /***********************************************************************/
/* Make a PlugDB result structure from the MySQL result. */ /* Return next field of the query results. */
/***********************************************************************/
MYSQL_FIELD *MYSQLC::GetNextField(void)
{
return (m_Res->current_field >= m_Res->field_count) ? NULL
: &m_Res->fields[m_Res->current_field++];
} // end of GetNextField
/***********************************************************************/
/* Make a CONNECT result structure from the MySQL result. */
/***********************************************************************/ /***********************************************************************/
PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb) PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
{ {
...@@ -604,8 +634,10 @@ PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb) ...@@ -604,8 +634,10 @@ PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
qrp->Nblin = 0; qrp->Nblin = 0;
qrp->Cursor = 0; qrp->Cursor = 0;
for (fld = mysql_fetch_field(m_Res); fld;
fld = mysql_fetch_field(m_Res)) { //for (fld = mysql_fetch_field(m_Res); fld;
// fld = mysql_fetch_field(m_Res)) {
for (fld = GetNextField(); fld; fld = GetNextField()) {
*pcrp = (PCOLRES)PlugSubAlloc(g, NULL, sizeof(COLRES)); *pcrp = (PCOLRES)PlugSubAlloc(g, NULL, sizeof(COLRES));
crp = *pcrp; crp = *pcrp;
pcrp = &crp->Next; pcrp = &crp->Next;
...@@ -701,7 +733,7 @@ void MYSQLC::FreeResult(void) ...@@ -701,7 +733,7 @@ void MYSQLC::FreeResult(void)
void MYSQLC::Rewind(void) void MYSQLC::Rewind(void)
{ {
if (m_Res) if (m_Res)
mysql_data_seek(m_Res, 0); DataSeek(0);
} // end of Rewind } // end of Rewind
...@@ -715,6 +747,7 @@ void MYSQLC::Close(void) ...@@ -715,6 +747,7 @@ void MYSQLC::Close(void)
m_DB = NULL; m_DB = NULL;
} // end of Close } // end of Close
#if 0 // not used yet
/***********************************************************************/ /***********************************************************************/
/* Discard additional results from a stored procedure. */ /* Discard additional results from a stored procedure. */
/***********************************************************************/ /***********************************************************************/
...@@ -722,9 +755,10 @@ void MYSQLC::DiscardResults(void) ...@@ -722,9 +755,10 @@ void MYSQLC::DiscardResults(void)
{ {
MYSQL_RES *res; MYSQL_RES *res;
while(!mysql_next_result(m_DB)) { while (!mysql_next_result(m_DB)) {
res = mysql_store_result(m_DB); res = mysql_store_result(m_DB);
mysql_free_result(res); mysql_free_result(res);
} // endwhile next result } // endwhile next result
} // end of DiscardResults } // end of DiscardResults
#endif // 0
/***********************************************************************/ /***********************************************************************/
/* MYCONN.H Olivier Bertrand 2007-2012 */ /* MYCONN.H Olivier Bertrand 2007-2013 */
/* */ /* */
/* This is the declaration file for the MySQL connection class and */ /* This is the declaration file for the MySQL connection class and */
/* a few utility functions used to communicate with MySQL. */ /* a few utility functions used to communicate with MySQL. */
...@@ -64,7 +64,7 @@ class DllItem MYSQLC { ...@@ -64,7 +64,7 @@ class DllItem MYSQLC {
int pt= 0); int pt= 0);
//ulong GetThreadID(void); //ulong GetThreadID(void);
//ulong ServerVersion(void); //ulong ServerVersion(void);
const char *ServerInfo(void); //const char *ServerInfo(void);
int KillQuery(ulong id); int KillQuery(ulong id);
int ExecSQL(PGLOBAL g, const char *query, int *w = NULL); int ExecSQL(PGLOBAL g, const char *query, int *w = NULL);
int PrepareSQL(PGLOBAL g, const char *query); int PrepareSQL(PGLOBAL g, const char *query);
...@@ -77,9 +77,12 @@ class DllItem MYSQLC { ...@@ -77,9 +77,12 @@ class DllItem MYSQLC {
void Rewind(void); void Rewind(void);
void FreeResult(void); void FreeResult(void);
void Close(void); void Close(void);
void DiscardResults(void); //void DiscardResults(void);
protected: protected:
MYSQL_FIELD *GetNextField(void);
void DataSeek(my_ulonglong row);
// Members // Members
MYSQL *m_DB; // The return from MySQL connection MYSQL *m_DB; // The return from MySQL connection
MYSQL_STMT *m_Stmt; // Prepared statement handle MYSQL_STMT *m_Stmt; // Prepared statement handle
......
...@@ -281,8 +281,14 @@ bool TDBMYSQL::MakeInsert(PGLOBAL g) ...@@ -281,8 +281,14 @@ bool TDBMYSQL::MakeInsert(PGLOBAL g)
*colist = '\0'; *colist = '\0';
if (Prep) { if (Prep) {
#if defined(MYSQL_PREPARED_STATEMENTS)
valist = (char*)PlugSubAlloc(g, NULL, 2 * Nparm); valist = (char*)PlugSubAlloc(g, NULL, 2 * Nparm);
*valist = '\0'; *valist = '\0';
#else // !MYSQL_PREPARED_STATEMENTS
strcpy(g->Message, "Prepared statements not used (not supported)");
PushWarning(g, this);
Prep = FALSE;
#endif // !MYSQL_PREPARED_STATEMENTS
} // endif Prep } // endif Prep
for (colp = Columns; colp; colp = colp->GetNext()) { for (colp = Columns; colp; colp = colp->GetNext()) {
...@@ -472,7 +478,7 @@ int TDBMYSQL::GetMaxSize(PGLOBAL g) ...@@ -472,7 +478,7 @@ int TDBMYSQL::GetMaxSize(PGLOBAL g)
Query = NULL; // Must be remade when columns are known Query = NULL; // Must be remade when columns are known
#endif // 0 #endif // 0
MaxSize = 0; MaxSize = 10; // To make MySQL happy
} // endif MaxSize } // endif MaxSize
return MaxSize; return MaxSize;
...@@ -500,6 +506,7 @@ int TDBMYSQL::GetProgMax(PGLOBAL g) ...@@ -500,6 +506,7 @@ int TDBMYSQL::GetProgMax(PGLOBAL g)
/***********************************************************************/ /***********************************************************************/
int TDBMYSQL::BindColumns(PGLOBAL g) int TDBMYSQL::BindColumns(PGLOBAL g)
{ {
#if defined(MYSQL_PREPARED_STATEMENTS)
if (Prep) { if (Prep) {
Bind = (MYSQL_BIND*)PlugSubAlloc(g, NULL, Nparm * sizeof(MYSQL_BIND)); Bind = (MYSQL_BIND*)PlugSubAlloc(g, NULL, Nparm * sizeof(MYSQL_BIND));
...@@ -507,15 +514,15 @@ int TDBMYSQL::BindColumns(PGLOBAL g) ...@@ -507,15 +514,15 @@ int TDBMYSQL::BindColumns(PGLOBAL g)
colp->InitBind(g); colp->InitBind(g);
return Myc.BindParams(g, Bind); return Myc.BindParams(g, Bind);
} else { } // endif prep
#endif // MYSQL_PREPARED_STATEMENTS
for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next) for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next)
if (colp->Buf_Type == TYPE_DATE) if (colp->Buf_Type == TYPE_DATE)
// Format must match DATETIME MySQL type // Format must match DATETIME MySQL type
((DTVAL*)colp->GetValue())->SetFormat(g, "YYYY-MM-DD hh:mm:ss", 19); ((DTVAL*)colp->GetValue())->SetFormat(g, "YYYY-MM-DD hh:mm:ss", 19);
return RC_OK; return RC_OK;
} // endif Prep
} // end of BindColumns } // end of BindColumns
/***********************************************************************/ /***********************************************************************/
...@@ -553,6 +560,7 @@ bool TDBMYSQL::OpenDB(PGLOBAL g) ...@@ -553,6 +560,7 @@ bool TDBMYSQL::OpenDB(PGLOBAL g)
} else if (Mode == MODE_INSERT) { } else if (Mode == MODE_INSERT) {
if (!MakeInsert(g)) { if (!MakeInsert(g)) {
#if defined(MYSQL_PREPARED_STATEMENTS)
int n = (Prep) ? Myc.PrepareSQL(g, Query) : Nparm; int n = (Prep) ? Myc.PrepareSQL(g, Query) : Nparm;
if (Nparm != n) { if (Nparm != n) {
...@@ -560,6 +568,7 @@ bool TDBMYSQL::OpenDB(PGLOBAL g) ...@@ -560,6 +568,7 @@ bool TDBMYSQL::OpenDB(PGLOBAL g)
strcpy(g->Message, MSG(BAD_PARM_COUNT)); strcpy(g->Message, MSG(BAD_PARM_COUNT));
} else } else
#endif // MYSQL_PREPARED_STATEMENTS
m_Rc = BindColumns(g); m_Rc = BindColumns(g);
} // endif MakeInsert } // endif MakeInsert
...@@ -630,8 +639,10 @@ int TDBMYSQL::ReadDB(PGLOBAL g) ...@@ -630,8 +639,10 @@ int TDBMYSQL::ReadDB(PGLOBAL g)
/***********************************************************************/ /***********************************************************************/
int TDBMYSQL::WriteDB(PGLOBAL g) int TDBMYSQL::WriteDB(PGLOBAL g)
{ {
#if defined(MYSQL_PREPARED_STATEMENTS)
if (Prep) if (Prep)
return Myc.ExecStmt(g); return Myc.ExecStmt(g);
#endif // MYSQL_PREPARED_STATEMENTS
// Statement was not prepared, we must construct and execute // Statement was not prepared, we must construct and execute
// an insert query for each line to insert // an insert query for each line to insert
...@@ -853,6 +864,7 @@ void MYSQLCOL::WriteColumn(PGLOBAL g) ...@@ -853,6 +864,7 @@ void MYSQLCOL::WriteColumn(PGLOBAL g)
if (Value != To_Val) if (Value != To_Val)
Value->SetValue_pval(To_Val, FALSE); // Convert the inserted value Value->SetValue_pval(To_Val, FALSE); // Convert the inserted value
#if defined(MYSQL_PREPARED_STATEMENTS)
if (((PTDBMY)To_Tdb)->Prep) { if (((PTDBMY)To_Tdb)->Prep) {
if (Buf_Type == TYPE_DATE) { if (Buf_Type == TYPE_DATE) {
Value->ShowValue((char *)Bind->buffer, (int)*Bind->length); Value->ShowValue((char *)Bind->buffer, (int)*Bind->length);
...@@ -861,6 +873,7 @@ void MYSQLCOL::WriteColumn(PGLOBAL g) ...@@ -861,6 +873,7 @@ void MYSQLCOL::WriteColumn(PGLOBAL g)
Slen = strlen(Value->GetCharValue()); Slen = strlen(Value->GetCharValue());
} // endif Prep } // endif Prep
#endif // MYSQL_PREPARED_STATEMENTS
} // end of WriteColumn } // end of WriteColumn
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment