#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>

#include "wsqllib.h"


char errMsg[1000];

MYSQLCONN conn[SQL_COMMANDS];


/*******************************************************************/
/* miscellaneous support routines (error handling etc) */
/*******************************************************************/

void ShowMsg(char *s)
{
       HWND focus;

       focus = GetFocus();
       MessageBox(NULL, s, "SQL Error",
                       MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
       if (focus)
               SetFocus(focus);
}


void OutputError(int sqlNum) {
       sprintf(errMsg,"Error %u (%s)",
               mysql_errno(&conn[sqlNum].mysql),
               mysql_error(&conn[sqlNum].mysql));
       ShowMsg(errMsg);
}


/*******************************************************************/
/* sql connect */
/*******************************************************************/

int SQLConnect(int sqlNum, char *host, char *user, char *passwd) {
       /* check not already connected? */
       if (conn[sqlNum].connected) {
               ShowMsg("sql connect; already connected.");
               return 0;
       }
       if (! (mysql_connect(&conn[sqlNum].mysql, host, user, passwd))) {
               OutputError(sqlNum);
               return 0;
       } else {
               conn[sqlNum].connected = 1;
               return 1;
       }
}


int SQLDisconnect(int sqlNum) {
       /* check that we are connected? */
       if (! conn[sqlNum].connected) {
               ShowMsg("sql disconnect; not connected.");
               return 0;
       }
       mysql_close(&conn[sqlNum].mysql);
       conn[sqlNum].connected = 0;
       return 1;
}


/*******************************************************************/
/* sql general commands */
/*******************************************************************/

int SQLSelectDB(int sqlNum, char *database) {
       /* check that we are connected to a mysql server */
       if (!conn[sqlNum].connected) {
               ShowMsg("sql query statement; you are not connected to a mysql server yet (sql connect).");
               return 0;
       }

       if (mysql_select_db(&conn[sqlNum].mysql, database)) {
               OutputError(sqlNum);
               return 0;
       } else {
               return 1;
       }
}


/*******************************************************************/
/* sql query command */
/*******************************************************************/

int SQLQuery(int sqlNum, char *query) {
       if (! conn[sqlNum].connected) {
               ShowMsg("sql query statement; you are not connected to a mysql server yet");
               return 0;
       }

       /* check there is no other active query? */
       if (conn[sqlNum].qFlag) {
               ShowMsg("Another query cannot be made until the current query has been finished with \"sql endquery\".");
               return 0;
       }

       /* execute the sql query statement */
       if (mysql_query(&conn[sqlNum].mysql, query)) {
               OutputError(sqlNum);
               return 0;
       }

       conn[sqlNum].res = mysql_use_result(&conn[sqlNum].mysql);
       conn[sqlNum].nFields = mysql_field_count(&conn[sqlNum].mysql);

       /* if no results were found and none expected then all ok */
       /* otherwise we assume the query worked ok, now get the results */
       if (conn[sqlNum].res == NULL) {
               if (conn[sqlNum].nFields) {
                       OutputError(sqlNum);
                       return 0;
               }
//              ShowMsg("sql startquery; query executed ok but returned no results.");
               return 2;
       }

       /* return all ok now, see fetchrow & endquery for more details */
       conn[sqlNum].qFlag = 1;
       return 1;
}


MYSQL_FIELD* SQLFetchField(int sqlNum)
{
       /* check we are connected? */
       if (! conn[sqlNum].connected) {
               ShowMsg("Not connected to a server.");
               return NULL;
       }
       /* check we had a query started? */
       if (! conn[sqlNum].qFlag) {
               ShowMsg("No query has been started.");
               return NULL;
       }
       return mysql_fetch_field(conn[sqlNum].res);
}


MYSQL_ROW SQLFetchRow(int sqlNum)
{
       /* check we are connected? */
       if (! conn[sqlNum].connected) {
               ShowMsg("Not connected to a server.");
               return NULL;
       }
       /* check we had a query started? */
       if (! conn[sqlNum].qFlag) {
               ShowMsg("No query has been started.");
               return NULL;
       }
       return mysql_fetch_row(conn[sqlNum].res);
}


int SQLEndQuery(int sqlNum) {
       /* check we are connected? */
       if (! conn[sqlNum].connected) {
               ShowMsg("Not connected to a server.");
               return 0;
       }

       /* check we had a query started? */
       if (! conn[sqlNum].qFlag) {
               ShowMsg("No query has been started.");
               return 0;
       }

       mysql_free_result(conn[sqlNum].res);
       conn[sqlNum].qFlag = 0;
       return 1;
}