#include "tqsql.h"

TQConnect::TQConnect()
{ henv = hdbc = hstmt = NULL;
  buf[0] = '\0';
  buflen = 0;
  rc = 0;
  statement = NULL;
  srcStr = "";
  SQLAllocEnv(&henv);
  rc = GetDataSources();
};

TQConnect::~TQConnect()
{   SQLFreeEnv(henv); };

int TQConnect::cConnections = 0;

/* Connect to the server described by szDSN eg
   "DSN=tqweb;UID=admin;PWD=mypassword"
*/

SQLRETURN TQConnect::connect(const char * szDSN)
{
  rc = SQLAllocConnect(henv, &hdbc);
  if (rc == SQL_SUCCESS)
   {  rc = SQLDriverConnect( hdbc, 0, (SQLCHAR *) szDSN,
                             SQL_NTS,
                             (SQLCHAR *) buf,
                             sizeof(buf),
                             &buflen,
                             SQL_DRIVER_COMPLETE
                           );
      cConnections++;
      return rc;
   }
  else
   return rc;
};

SQLRETURN TQConnect::disconnect(void)
{ SQLRETURN s;

  s = SQLDisconnect(hdbc);
  if (s == SQL_SUCCESS)
   { s = SQLFreeConnect(hdbc);
     if (s == SQL_SUCCESS)
      { cConnections--;
        statement = NULL;
      }
   }
  return s;
};

SQLINTEGER TQConnect::getBuflen(void)
{   return buflen; };

SQLINTEGER TQConnect::connectCount(void)
{  return cConnections; };

/* Execute the SQL statement *s and return the result table in t */
SQLRETURN TQConnect::execSQL(const char * s, Table & t)
{ statement = new char[strlen(s) + 1];
  RESULTATTR attribs;

  tab1 = new Table;
  rc = SQLAllocStmt(hdbc, &hstmt);
  if (rc == SQL_SUCCESS)
   { strcpy(statement, s);
     rc = SQLExecDirect(hstmt, (SQLCHAR *) statement, SQL_NTS);
     if (rc != SQL_SUCCESS)
      return rc;
     rc = SQLNumResultCols(hstmt, &attribs.cColumns);
     if (rc != SQL_SUCCESS)
      return rc;
     rc = SQLRowCount(hstmt, &attribs.cRows);
     if (rc != SQL_SUCCESS)
      return rc;
     tab1->setAttribs(attribs.cRows, attribs.cColumns);
     tab1->buildSchema(hstmt, attribs.cColumns);
     rc = tab1->bindColumns(hstmt);
     tab1->transferData(hstmt);
   }
  delete statement;
  t = *tab1;
  delete tab1;
  return rc;
};

SQLRETURN TQConnect::GetDataSources(void)
{       SQLRETURN rc;
        SQLCHAR dsn[100], dsnDesc[256];
        SQLSMALLINT dsnRet, dsnDescRet;

        rc = SQLDataSources(henv,               //void *,
                        SQL_FETCH_FIRST,        //short unsigned int
                        dsn,                    //SQLCHAR *,
                        100,                    //short int,
                        &dsnRet,                //SQLSMALLINT *,
                        dsnDesc,                //SQLCHAR *,
                        255,                    //short int,
                        &dsnDescRet             //SQLSMALLINT *
                   );
        if (rc != SQL_NO_DATA_FOUND)
        { srcStr.append((char *)dsn);
          srcStr.append(":");
          srcStr.append((char *)dsnDesc);
          srcStr.append("\n");
        }
        else
                return rc;
        while (rc != SQL_NO_DATA_FOUND)
        { rc = SQLDataSources(henv,SQL_FETCH_NEXT,dsn,100,&dsnRet,dsnDesc,256,&dsnDescRet);
          srcStr.append((char *) dsn);
          srcStr.append(":");
          srcStr.append((char *)dsnDesc);
          srcStr.append("\n");
        }
        return rc;
}

string TQConnect::showDataSources(void)
{
        return srcStr;
}