#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wsqllib.h>
#include <winlib/winlib.h>

#include "sqlshell.h"
#include "sqlcmd.h"


void GetLine(VARS *v)
{
       int line;
       unsigned int dwStart, dwEnd;

       SendMessage(v->hwndSQLCmdWnd, EM_GETSEL, (WPARAM)&dwStart,
                       (LPARAM)&dwEnd);

       line = (int)SendMessage(v->hwndSQLCmdWnd, EM_LINEFROMCHAR,
                       (WPARAM)dwStart, (LPARAM)0);

       memset(v->buf, 0, 1024);
       *((int*)v->buf) = 1024;
       SendMessage(v->hwndSQLCmdWnd, EM_GETLINE, (WPARAM)line,
                       (LPARAM)(LPCSTR)v->buf);
}


BOOL AddText(char **result, char *s, int *bytes)
{
       int len;

       len = strlen(s);
       if (! (*result = realloc(*result, (*bytes) + len))) {
               MessageBox(NULL, "Realloc failed", "SQL Shell",
                               MB_ICONERROR | MB_OK | MB_SETFOREGROUND);
               return FALSE;
       }
       memcpy((*result) + (*bytes), s, len);
       *bytes += len;
       return TRUE;
}


void ExecuteCmd(void *arg)
{
       MYSQL_ROW row;
       MYSQL_FIELD *field[100], *fld;
       int i, j, r, nf, fw;
       char fmt[30], *result;
       int bytes;
       VARS *v = (VARS*)arg;

       v->sqlCmdActive = TRUE;

       ShowStatus(v->hwndStatus, 0, "Processing...");

       GetLine(v);

       r = SQLQuery(0, v->buf);
       if ((! r) || (r == 2))
               goto end;

       fw = nf = 0;
       while (fld = SQLFetchField(0)) {
               if (strlen(fld->name) > fw)
                       fw = strlen(fld->name);
               field[nf++] = fld;
       }

       sprintf(fmt, "%%-%ds: %%s\r\n", fw + 2);

       result = (char*)malloc(1);
       bytes = 0;

       i = 0;
       while ((row = SQLFetchRow(0))) {
               if (i) {
                       if (! AddText(&result, "\r\n", &bytes))
                               goto endQuery;
               }
               sprintf(v->buf, "[%d] ------------------------------------------------------------\r\n\r\n",
                               (i++) + 1);
               if (! AddText(&result, v->buf, &bytes))
                       goto endQuery;
               for (j = 0; j < nf; j++) {
                       sprintf(v->buf, fmt, field[j]->name, row[j] ? row[j] : "");
                       if (! AddText(&result, v->buf, &bytes))
                               goto endQuery;
               }
       }

       if (! (result = realloc(result, bytes + 1)))
               goto endQuery;
       *(result + bytes) = '\0';

       if (! bytes) {
               ShowStatus(v->hwndStatus, 0, "No rows found");
               goto endQuery;
       }

       SetWindowText(v->hwndSQLResWnd, result);

       if (i == 1)
               sprintf(v->buf, "%d row found", i);
       else
               sprintf(v->buf, "%d rows found", i);
       ShowStatus(v->hwndStatus, 0, v->buf);

endQuery:
       SQLEndQuery(0);
       free(result);
       goto end2;
end:
       ShowStatus(v->hwndStatus, 0, "");
end2:
       v->sqlCmdActive = FALSE;
}