Modified:
trunk/Driver/Setup/Library/MYODBCSetup.h
trunk/Driver/Setup/Library/MYODBCSetupDataSourceDialog.h
trunk/SDK/C/include/text.h
trunk/Tools/Shell/MYODBCShell.cpp
trunk/Tools/Shell/MYODBCShell.h
Log:
Tools/Shells - 64-bit fixes, use SQLLEN
other header fixes to keep qt stuff 'together'
Modified: trunk/Driver/Setup/Library/MYODBCSetup.h
===================================================================
--- trunk/Driver/Setup/Library/MYODBCSetup.h 2007-05-03 15:45:43 UTC (rev 904)
+++ trunk/Driver/Setup/Library/MYODBCSetup.h 2007-05-11 05:14:49 UTC (rev 905)
@@ -29,10 +29,6 @@
#ifndef MYODBCSETUP_H
#define MYODBCSETUP_H
-#include <MYODBCC.h>
-#include <MYODBCIns.h>
-#include <myodbc_dbug.h>
-
#include <QApplication>
#include <QColor>
#include <QDialog>
@@ -47,6 +43,10 @@
#include <QProcess>
#include <QToolTip>
+#include <MYODBCC.h>
+#include <MYODBCIns.h>
+#include <myodbc_dbug.h>
+
#ifdef __cplusplus
extern "C" {
#endif
Modified: trunk/Driver/Setup/Library/MYODBCSetupDataSourceDialog.h
===================================================================
--- trunk/Driver/Setup/Library/MYODBCSetupDataSourceDialog.h 2007-05-03 15:45:43 UTC (rev
904)
+++ trunk/Driver/Setup/Library/MYODBCSetupDataSourceDialog.h 2007-05-11 05:14:49 UTC (rev
905)
@@ -29,8 +29,8 @@
#ifndef MYODBCSETUPDATASOURCEDIALOG_H
#define MYODBCSETUPDATASOURCEDIALOG_H
+#include "MYODBCSetupDataSourceTab1.h"
#include "MYODBCSetup.h"
-#include "MYODBCSetupDataSourceTab1.h"
class MYODBCSetupDataSourceDialog : public QDialog
{
Modified: trunk/SDK/C/include/text.h
===================================================================
--- trunk/SDK/C/include/text.h 2007-05-03 15:45:43 UTC (rev 904)
+++ trunk/SDK/C/include/text.h 2007-05-11 05:14:49 UTC (rev 905)
@@ -34,7 +34,7 @@
#define _TEXT_H
#ifdef _WIN32
-# define NOMINMAX
+# define NOMINMAX
# include <windows.h>
# include <tchar.h>
# if _MSC_VER < 1400
Modified: trunk/Tools/Shell/MYODBCShell.cpp
===================================================================
--- trunk/Tools/Shell/MYODBCShell.cpp 2007-05-03 15:45:43 UTC (rev 904)
+++ trunk/Tools/Shell/MYODBCShell.cpp 2007-05-11 05:14:49 UTC (rev 905)
@@ -1,1014 +1,1014 @@
-/*!
- \file MYODBCShell.cpp
- \author Peter Harvey <pharvey@stripped>
- Copyright (C) MySQL AB 2004-2007, Released under GPL.
- \version Connector/ODBC v5
- \date 2007
- \brief Command-line ODBC tool.
-
- This program is a command-line tool for working with ODBC - it
- allows statements to be submitted to a data source and displays
- results in a variety of formats.
-
- \license GPL
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- There are special exceptions to the terms and conditions of the GPL as it
- is applied to this software. View the full text of the exception in file
- EXCEPTIONS in the directory of this software distribution.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-#include "MYODBCShell.h"
-
-MYODBCShell::MYODBCShell()
-{
- bDriverConnect = false;
- bVerbose = true;
- bLabels = true;
- nResultFormat = RESULT_FORMAT_NORMAL;
- cDelimiter = '|';
- nMaxDataWidth = 0;
- hEnv = NULL;
- hDbc = NULL;
- hStm = NULL;
- pstreamStdIn = new QTextStream(stdin);
- pstreamStdOut = new QTextStream(stdout);
- pstreamStdErr = new QTextStream(stderr);
-}
-
-MYODBCShell::~MYODBCShell()
-{
- delete pstreamStdIn;
- delete pstreamStdOut;
- delete pstreamStdErr;
-}
-
-void MYODBCShell::setConnect(const QString &stringConnect)
-{
- this->stringConnect = stringConnect;
-}
-
-void MYODBCShell::setUID(const QString &stringUID)
-{
- this->stringUID = stringUID;
-}
-
-void MYODBCShell::setPWD(const QString &stringPWD)
-{
- this->stringPWD = stringPWD;
-}
-
-void MYODBCShell::setDriverConnect(bool bDriverConnect)
-{
- this->bDriverConnect = bDriverConnect;
-}
-
-void MYODBCShell::setVerbose(bool bVerbose)
-{
- this->bVerbose = bVerbose;
-}
-
-void MYODBCShell::setLabels(bool bLabels)
-{
- this->bLabels = bLabels;
-}
-
-void MYODBCShell::setResultFormat(RESULT_FORMAT nResultFormat)
-{
- this->nResultFormat = nResultFormat;
-}
-
-void MYODBCShell::setDelimiter(QChar cDelimiter)
-{
- this->cDelimiter = cDelimiter;
-}
-
-void MYODBCShell::setMaxDataWidth(int nMaxDataWidth)
-{
- this->nMaxDataWidth = nMaxDataWidth;
-}
-
-QString MYODBCShell::getConnect()
-{
- return stringConnect;
-}
-
-QString MYODBCShell::getUID()
-{
- return stringUID;
-}
-
-QString MYODBCShell::getPWD()
-{
- return stringPWD;
-}
-
-bool MYODBCShell::getDriverConnect()
-{
- return bDriverConnect;
-}
-
-bool MYODBCShell::getVerbose()
-{
- return bVerbose;
-}
-
-bool MYODBCShell::getLabels()
-{
- return bLabels;
-}
-
-MYODBCShell::RESULT_FORMAT MYODBCShell::getResultFormat()
-{
- return nResultFormat;
-}
-
-QChar MYODBCShell::getDelimiter()
-{
- return cDelimiter;
-}
-
-int MYODBCShell::getMaxDataWidth()
-{
- return nMaxDataWidth;
-}
-
-bool MYODBCShell::doConnect()
-{
- SQLRETURN nReturn;
-
- if (hEnv)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hEnv)))
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_ENV.").arg(__FILE__).arg(__LINE__) << endl;
- hEnv = NULL;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, 0)))
- {
- if (bVerbose) doErrors(hEnv, NULL, NULL);
- *pstreamStdErr << QString("[%1][%2][WARNING] Could not SQLSetEnvAttr to set
version to SQL_OV_ODBC3.").arg(__FILE__).arg(__LINE__) << endl;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc)))
- {
- if (bVerbose) doErrors(hEnv, NULL, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_DBC.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
- hEnv = NULL;
- hDbc = NULL;
- return false;
- }
-
- if (bDriverConnect)
- {
- SQLWCHAR sOut[4096];
- SQLSMALLINT nLen;
- nReturn = SQLDriverConnect(hDbc, 0, (SQLWCHAR*)stringConnect.utf16(),
stringConnect.length(), sOut, 4096, &nLen, SQL_DRIVER_NOPROMPT);
- }
- else
- nReturn = SQLConnect(hDbc, (SQLWCHAR*)stringConnect.utf16(),
stringConnect.length(), (SQLWCHAR*)stringUID.utf16(), stringUID.length(),
(SQLWCHAR*)stringPWD.utf16(), stringPWD.length());
-
- if (!SQL_SUCCEEDED(nReturn))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
connect.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
- SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
- hEnv = NULL;
- hDbc = NULL;
- return false;
- }
-
- return true;
-}
-
-bool MYODBCShell::doCommand(const QString &stringCommand)
-{
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (stringCommand.startsWith("help"))
- return doHelp(stringCommand);
- else if (stringCommand.startsWith("SQLTables"))
- return doTables(stringCommand);
- else if (stringCommand.startsWith("SQLColumns"))
- return doColumns(stringCommand);
- else if (stringCommand.startsWith("SQLStatistics"))
- return doStatistics(stringCommand);
- else
- return doSubmit(stringCommand);
-
- return true;
-}
-
-
-bool MYODBCShell::doDisconnect()
-{
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLDisconnect(hDbc)))
- doErrors(hEnv, hDbc, NULL);
- if (!SQL_SUCCEEDED(SQLFreeConnect(hDbc)))
- doErrors(hEnv, hDbc, NULL);
- if (!SQL_SUCCEEDED(SQLFreeEnv(hEnv)))
- doErrors(hEnv, NULL, NULL);
-
- hDbc = NULL;
- hEnv = NULL;
-
- return true;
-}
-
-SQLINTEGER MYODBCShell::getOptimalDisplayWidth(SQLINTEGER nColumn)
-{
- SQLINTEGER nDataWidth = 10;
- SQLINTEGER nOptimalDisplayWidth = 10;
- SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
- SQLSMALLINT nLabelLengthBytes = 0;
-
- SQLColAttribute(hStm, nColumn, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL,
&nDataWidth);
- SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
&nLabelLengthBytes, NULL);
-
- nOptimalDisplayWidth = std::max((SQLINTEGER)(nLabelLengthBytes / sizeof(SQLWCHAR)),
nDataWidth);
-
- if (nMaxDataWidth > 0)
- nOptimalDisplayWidth = std::min(nOptimalDisplayWidth, nMaxDataWidth);
-
- if (nOptimalDisplayWidth > MAX_DATA_WIDTH)
- nOptimalDisplayWidth = MAX_DATA_WIDTH;
-
- return nOptimalDisplayWidth;
-}
-
-bool MYODBCShell::doSubmit(const QString &stringCommand)
-{
- SQLINTEGER nReturn;
-
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- // get results (if any)
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
- hStm = NULL;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLPrepare(hStm, (SQLWCHAR*)stringCommand.utf16(),
stringCommand.length())))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLPrepare.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
-
- nReturn = SQLExecute(hStm);
- if (nReturn == SQL_NO_DATA)
- {
- *pstreamStdErr << QString("[%1][%2][INFO] SQLExecute returned
SQL_NO_DATA.").arg(__FILE__).arg(__LINE__) << endl;
- }
- else if (nReturn == SQL_SUCCESS_WITH_INFO)
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][INFO] SQLExecute returned
SQL_SUCCESS_WITH_INFO.").arg(__FILE__).arg(__LINE__) << endl;
- }
- else if (nReturn != SQL_SUCCESS)
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLExecute.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return 0;
- }
-
- // process results (if any)
- do
- {
- doResults();
- }
- while (SQL_SUCCEEDED(SQLMoreResults(hStm)));
-
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
-
- return true;
-}
-
-bool MYODBCShell::doHelp(const QString &stringCommand)
-{
- QString stringTable;
-
- // init
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
- hStm = NULL;
- return false;
- }
-
- QStringList stringlist = stringCommand.split(' ');
- if (stringlist.count() > 1)
- {
- QString stringTable = stringlist[1].trimmed();
- if (!SQL_SUCCEEDED(SQLColumns(hStm, NULL, 0, NULL, 0,
(SQLWCHAR*)stringTable.utf16(), stringTable.length(), NULL, 0)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLColumns.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
- }
- else
- {
- if (!SQL_SUCCEEDED(SQLTables(hStm, NULL, 0, NULL, 0, NULL, 0, NULL, 0)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLTables.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
- }
-
- // process results (if any)
- doResults();
-
- // fini
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
-
- return true;
-}
-
-bool MYODBCShell::doTables(const QString &stringCommand)
-{
- SQLWCHAR * psCatalog = NULL;
- SQLWCHAR * psSchema = NULL;
- SQLWCHAR * psTable = NULL;
- SQLWCHAR * psTableTypes = NULL;
- SQLSMALLINT nCatalog = 0;
- SQLSMALLINT nSchema = 0;
- SQLSMALLINT nTable = 0;
- SQLSMALLINT nTableTypes = 0;
-
- // init
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
- hStm = NULL;
- return false;
- }
-
- QStringList stringlist = stringCommand.split('|');
-
- if (stringlist.count() != 6)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 6
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
- return false;
- }
-
- QString stringCatalog = stringlist.at(1);
- QString stringSchema = stringlist.at(2);
- QString stringTable = stringlist.at(3);
- QString stringTableTypes = stringlist.at(4);
-
- if (stringCatalog != "NULL" && !stringCatalog.isNull())
- {
- psCatalog = (SQLWCHAR*)stringCatalog.utf16();
- nCatalog = stringCatalog.length();
- }
- if (stringSchema != "NULL" && !stringSchema.isNull())
- {
- psSchema = (SQLWCHAR*)stringSchema.utf16();
- nSchema = stringSchema.length();
- }
- if (stringTable != "NULL" && !stringTable.isNull())
- {
- psTable = (SQLWCHAR*)stringTable.utf16();
- nTable = stringTable.length();
- }
- if (stringTableTypes != "NULL" && !stringTableTypes.isNull())
- {
- psTableTypes = (SQLWCHAR*)stringTableTypes.utf16();
- nTableTypes = stringTableTypes.length();
- }
-
- if (!SQL_SUCCEEDED(SQLTables(hStm, psCatalog, nCatalog, psSchema, nSchema, psTable,
nTable, psTableTypes, nTableTypes)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLTables.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
-
- // process results (if any)
- doResults();
-
- // fini
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
-
- return true;
-}
-
-bool MYODBCShell::doColumns(const QString &stringCommand)
-{
- SQLWCHAR * psCatalog = NULL;
- SQLWCHAR * psSchema = NULL;
- SQLWCHAR * psTable = NULL;
- SQLWCHAR * psColumn = NULL;
- SQLSMALLINT nCatalog = 0;
- SQLSMALLINT nSchema = 0;
- SQLSMALLINT nTable = 0;
- SQLSMALLINT nColumn = 0;
-
- // init
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
- hStm = NULL;
- return false;
- }
-
- QStringList stringlist = stringCommand.split('|');
-
- if (stringlist.count() != 6)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 6
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
- return false;
- }
-
- QString stringCatalog = stringlist.at(1);
- QString stringSchema = stringlist.at(2);
- QString stringTable = stringlist.at(3);
- QString stringColumn = stringlist.at(4);
-
- if (stringCatalog != "NULL" && !stringCatalog.isNull())
- {
- psCatalog = (SQLWCHAR*)stringCatalog.utf16();
- nCatalog = stringCatalog.length();
- }
- if (stringSchema != "NULL" && !stringSchema.isNull())
- {
- psSchema = (SQLWCHAR*)stringSchema.utf16();
- nSchema = stringSchema.length();
- }
- if (stringTable != "NULL" && !stringTable.isNull())
- {
- psTable = (SQLWCHAR*)stringTable.utf16();
- nTable = stringTable.length();
- }
- if (stringColumn != "NULL" && !stringColumn.isNull())
- {
- psColumn = (SQLWCHAR*)stringColumn.utf16();
- nColumn = stringColumn.length();
- }
-
- if (!SQL_SUCCEEDED(SQLColumns(hStm, psCatalog, nCatalog, psSchema, nSchema, psTable,
nTable, psColumn, nColumn)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLColumn.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
-
- // process results (if any)
- doResults();
-
- // fini
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
-
- return true;
-}
-
-/*!
- \brief Process a SQLStatictics command.
-
- The command should start with "SQLStatistics " and be followed by a filter
specification.
-
- Example;
-
- SQLStatistics |||categories|
-*/
-bool MYODBCShell::doStatistics(const QString &stringCommand)
-{
- SQLWCHAR * psCatalog = NULL;
- SQLWCHAR * psSchema = NULL;
- SQLWCHAR * psTable = NULL;
- SQLSMALLINT nCatalog = 0;
- SQLSMALLINT nSchema = 0;
- SQLSMALLINT nTable = 0;
-
- // init
- if (!hDbc)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, NULL);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
- hStm = NULL;
- return false;
- }
-
- QStringList stringlist = stringCommand.split('|');
-
- if (stringlist.count() != 5)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 5
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
- return false;
- }
-
- QString stringCatalog = stringlist.at(1);
- QString stringSchema = stringlist.at(2);
- QString stringTable = stringlist.at(3);
- QString stringColumn = stringlist.at(4);
-
- if (stringCatalog != "NULL" && !stringCatalog.isNull())
- {
- psCatalog = (SQLWCHAR*)stringCatalog.utf16();
- nCatalog = stringCatalog.length();
- }
- if (stringSchema != "NULL" && !stringSchema.isNull())
- {
- psSchema = (SQLWCHAR*)stringSchema.utf16();
- nSchema = stringSchema.length();
- }
- if (stringTable != "NULL" && !stringTable.isNull())
- {
- psTable = (SQLWCHAR*)stringTable.utf16();
- nTable = stringTable.length();
- }
-
- if (!SQL_SUCCEEDED(SQLStatistics(hStm, psCatalog, nCatalog, psSchema, nSchema,
psTable, nTable, SQL_INDEX_ALL, 0)))
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLStatistics.").arg(__FILE__).arg(__LINE__) << endl;
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
- return false;
- }
-
- // process results (if any)
- doResults();
-
- // fini
- SQLFreeHandle(SQL_HANDLE_STMT, hStm);
- hStm = NULL;
-
- return true;
-}
-
-bool MYODBCShell::doResults()
-{
- SQLSMALLINT nCols;
- SQLINTEGER nRows = 0;
-
- if (!hStm)
- {
- *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- stringSepLine = "";
-
- if (SQLNumResultCols(hStm, &nCols) != SQL_SUCCESS)
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLNumResultCols.").arg(__FILE__).arg(__LINE__) << endl;
- return false;
- }
-
- if (nCols > 0)
- {
- // header
- switch (nResultFormat)
- {
- case RESULT_FORMAT_NORMAL:
- doHeaderNormal();
- break;
- case RESULT_FORMAT_HTML:
- doHeaderHTMLTable();
- break;
- case RESULT_FORMAT_DELIMITED:
- doHeaderDelimited();
- break;
- }
-
- // body
- switch (nResultFormat)
- {
- case RESULT_FORMAT_NORMAL:
- nRows = doBodyNormal();
- break;
- case RESULT_FORMAT_HTML:
- doBodyHTMLTable();
- break;
- case RESULT_FORMAT_DELIMITED:
- doBodyDelimited();
- break;
- }
- }
-
- // footer
- switch (nResultFormat)
- {
- case RESULT_FORMAT_NORMAL:
- doFooterNormal(nRows);
- break;
- case RESULT_FORMAT_HTML:
- doFooterHTMLTable();
- break;
- case RESULT_FORMAT_DELIMITED:
- break;
- }
-
- return true;
-}
-
-bool MYODBCShell::doHeaderNormal()
-{
- SQLINTEGER nColumn = 0;
- SQLSMALLINT nColumns = 0;
- SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
- QString stringFormatBuffer;
- QString stringNameLine;
- SQLINTEGER nChar;
-
- if (!doCalcOptimalDisplayWidths())
- return false;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- for (nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- sColumnName[0] = '\0';
-
- if (!SQL_SUCCEEDED(SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName,
sizeof(sColumnName), NULL, NULL)) && bVerbose)
- doErrors(NULL, NULL, hStm);
-
- stringSepLine += "+";
- for (nChar=0; nChar <= vectorOptimalDisplayWidths[nColumn]; nChar++)
stringSepLine += "-";
-
- stringNameLine += stringFormatBuffer.sprintf("| %-*.*ls",
vectorOptimalDisplayWidths[nColumn], vectorOptimalDisplayWidths[nColumn], sColumnName);
- }
- stringSepLine += "+";
- stringNameLine += "|";
-
- *pstreamStdOut << stringSepLine << endl;
- *pstreamStdOut << stringNameLine << endl;
- *pstreamStdOut << stringSepLine << endl;
-
- return true;
-}
-
-SQLINTEGER MYODBCShell::doBodyNormal()
-{
- SQLINTEGER nColumn = 0;
- SQLSMALLINT nColumns = 0;
- SQLLEN nIndicator = 0;
- SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
- SQLRETURN nReturn = 0;
- SQLINTEGER nRows = 0;
- QString stringFormatBuffer;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- nReturn = SQLFetch(hStm);
- while (SQL_SUCCEEDED(nReturn))
- {
- for (nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
- if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
- {
- *pstreamStdOut << stringFormatBuffer.sprintf("| %-*.*ls",
vectorOptimalDisplayWidths[nColumn], vectorOptimalDisplayWidths[nColumn], sColumnValue);
- }
- else if (nReturn == SQL_ERROR)
- {
- break;
- }
- else
- {
- *pstreamStdOut << stringFormatBuffer.sprintf("| %-*s",
vectorOptimalDisplayWidths[nColumn], "");
- }
-
- } /* for columns */
-
- nRows++;
- *pstreamStdOut << "|" << endl;
- nReturn = SQLFetch(hStm);
-
- } /* while rows */
-
- if (nReturn == SQL_ERROR)
- {
- if (bVerbose) doErrors(NULL, NULL, hStm);
- }
-
- return nRows;
-}
-
-bool MYODBCShell::doFooterNormal(SQLINTEGER nRows)
-{
- SQLLEN nRowsAffected = -1;
-
- *pstreamStdOut << stringSepLine << endl;
-
- if (SQLRowCount(hStm, &nRowsAffected) != SQL_SUCCESS)
- {
- if (bVerbose) doErrors(hEnv, hDbc, hStm);
- }
-
- *pstreamStdOut << nRowsAffected << " rows affected" << endl;
- *pstreamStdOut << nRows << " rows fetched" << endl;
-
- return true;
-}
-
-bool MYODBCShell::doHeaderHTMLTable()
-{
- SQLSMALLINT nColumns = 0;
- SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
-
- *pstreamStdOut << "<table BORDER>" << endl;
- *pstreamStdOut << "<tr BGCOLOR=#000099>" << endl;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
NULL, NULL);
- *pstreamStdOut << "<td>" << endl;
- *pstreamStdOut << "<font face=Arial,Helvetica><font
color=#FFFFFF>" << endl;
- *pstreamStdOut << QString::fromStdWString(sColumnName) << endl;
- *pstreamStdOut << "</font></font>" << endl;
- *pstreamStdOut << "</td>" << endl;
- }
- *pstreamStdOut << "</tr>" << endl;
-
- return true;
-}
-
-bool MYODBCShell::doBodyHTMLTable()
-{
- SQLSMALLINT nColumns = 0;
- SQLRETURN nReturn = 0;
- bool bReturn = true;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- while (SQL_SUCCEEDED(SQLFetch(hStm)))
- {
- *pstreamStdOut << "<tr>" << endl;
-
- for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- SQLLEN nIndicator = 0;
- SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
-
- *pstreamStdOut << "<td>" << endl;
- *pstreamStdOut << "<font face=Arial,Helvetica>" << endl;
-
- nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
- if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
- {
- *pstreamStdOut << QString::fromStdWString(sColumnValue) <<
endl;
- }
- else if (nReturn == SQL_ERROR)
- {
- *pstreamStdOut << endl;
- if (bVerbose) doErrors(NULL, NULL, hStm);
- bReturn = false;
- break;
- }
- else
- *pstreamStdOut << endl;
-
- *pstreamStdOut << "</font>" << endl;
- *pstreamStdOut << "</td>" << endl;
- }
- if (!bReturn)
- break;
- *pstreamStdOut << "</tr>" << endl;
- }
-
- return bReturn;
-}
-
-bool MYODBCShell::doFooterHTMLTable()
-{
- *pstreamStdOut << "</table>" << endl;
- return true;
-}
-
-bool MYODBCShell::doHeaderDelimited()
-{
- SQLSMALLINT nColumns = 0;
- SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
NULL, NULL);
- *pstreamStdOut << QString::fromStdWString(sColumnName);
- if (nColumn < nColumns)
- *pstreamStdOut << cDelimiter;
- }
- *pstreamStdOut << endl;
-
- return true;
-}
-
-bool MYODBCShell::doBodyDelimited()
-{
- SQLSMALLINT nColumns = 0;
- SQLRETURN nReturn = 0;
- bool bReturn = true;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- nColumns = -1;
-
- while (SQL_SUCCEEDED(SQLFetch(hStm)))
- {
- for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- SQLLEN nIndicator = 0;
- SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
-
- nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
- if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
- {
- *pstreamStdOut << QString::fromStdWString(sColumnValue);
- if (nColumn < nColumns)
- *pstreamStdOut << cDelimiter;
- }
- else if (nReturn == SQL_ERROR)
- {
- *pstreamStdOut << endl;
- if (bVerbose) doErrors(NULL, NULL, hStm);
- bReturn = false;
- break;
- }
- else
- {
- if (nColumn < nColumns)
- *pstreamStdOut << cDelimiter;
- }
- }
- if (!bReturn)
- break;
- *pstreamStdOut << endl;
- }
-
- return bReturn;
-}
-
-bool MYODBCShell::doErrors(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStm)
-{
- SQLSMALLINT nRecord = 1;
- SQLWCHAR sState[6];
- SQLINTEGER nNativeError;
- SQLWCHAR sMessageText[SQL_MAX_MESSAGE_LENGTH];
- SQLSMALLINT nTextLength;
-
- if (hStm)
- {
- while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_STMT,
- hStm,
- nRecord,
- sState,
- &nNativeError,
- sMessageText,
- SQL_MAX_MESSAGE_LENGTH,
- &nTextLength)))
- {
- sState[5] = '\0';
- *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
- nRecord++;
- }
- }
-
- if (hDbc)
- {
- nRecord = 1;
- while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_DBC,
- hDbc,
- nRecord,
- sState,
- &nNativeError,
- sMessageText,
- SQL_MAX_MESSAGE_LENGTH,
- &nTextLength)))
- {
- sState[5] = '\0';
- *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
- nRecord++;
- }
- }
-
- if (hEnv)
- {
- nRecord = 1;
- while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_ENV,
- hEnv,
- nRecord,
- sState,
- &nNativeError,
- sMessageText,
- SQL_MAX_MESSAGE_LENGTH,
- &nTextLength)))
- {
- sState[5] = '\0';
- *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
- nRecord++;
- }
- }
-
- return 1;
-}
-
-bool MYODBCShell::doCalcOptimalDisplayWidths()
-{
- SQLSMALLINT nColumns;
-
- if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
- return true;
-
- vectorOptimalDisplayWidths.resize(nColumns + 1);
-
- for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
- {
- vectorOptimalDisplayWidths[nColumn] = getOptimalDisplayWidth(nColumn);
- }
-
- return true;
-}
-
+/*!
+ \file MYODBCShell.cpp
+ \author Peter Harvey <pharvey@stripped>
+ Copyright (C) MySQL AB 2004-2007, Released under GPL.
+ \version Connector/ODBC v5
+ \date 2007
+ \brief Command-line ODBC tool.
+
+ This program is a command-line tool for working with ODBC - it
+ allows statements to be submitted to a data source and displays
+ results in a variety of formats.
+
+ \license GPL
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ There are special exceptions to the terms and conditions of the GPL as it
+ is applied to this software. View the full text of the exception in file
+ EXCEPTIONS in the directory of this software distribution.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#include "MYODBCShell.h"
+
+MYODBCShell::MYODBCShell()
+{
+ bDriverConnect = false;
+ bVerbose = true;
+ bLabels = true;
+ nResultFormat = RESULT_FORMAT_NORMAL;
+ cDelimiter = '|';
+ nMaxDataWidth = 0;
+ hEnv = NULL;
+ hDbc = NULL;
+ hStm = NULL;
+ pstreamStdIn = new QTextStream(stdin);
+ pstreamStdOut = new QTextStream(stdout);
+ pstreamStdErr = new QTextStream(stderr);
+}
+
+MYODBCShell::~MYODBCShell()
+{
+ delete pstreamStdIn;
+ delete pstreamStdOut;
+ delete pstreamStdErr;
+}
+
+void MYODBCShell::setConnect(const QString &stringConnect)
+{
+ this->stringConnect = stringConnect;
+}
+
+void MYODBCShell::setUID(const QString &stringUID)
+{
+ this->stringUID = stringUID;
+}
+
+void MYODBCShell::setPWD(const QString &stringPWD)
+{
+ this->stringPWD = stringPWD;
+}
+
+void MYODBCShell::setDriverConnect(bool bDriverConnect)
+{
+ this->bDriverConnect = bDriverConnect;
+}
+
+void MYODBCShell::setVerbose(bool bVerbose)
+{
+ this->bVerbose = bVerbose;
+}
+
+void MYODBCShell::setLabels(bool bLabels)
+{
+ this->bLabels = bLabels;
+}
+
+void MYODBCShell::setResultFormat(RESULT_FORMAT nResultFormat)
+{
+ this->nResultFormat = nResultFormat;
+}
+
+void MYODBCShell::setDelimiter(QChar cDelimiter)
+{
+ this->cDelimiter = cDelimiter;
+}
+
+void MYODBCShell::setMaxDataWidth(int nMaxDataWidth)
+{
+ this->nMaxDataWidth = nMaxDataWidth;
+}
+
+QString MYODBCShell::getConnect()
+{
+ return stringConnect;
+}
+
+QString MYODBCShell::getUID()
+{
+ return stringUID;
+}
+
+QString MYODBCShell::getPWD()
+{
+ return stringPWD;
+}
+
+bool MYODBCShell::getDriverConnect()
+{
+ return bDriverConnect;
+}
+
+bool MYODBCShell::getVerbose()
+{
+ return bVerbose;
+}
+
+bool MYODBCShell::getLabels()
+{
+ return bLabels;
+}
+
+MYODBCShell::RESULT_FORMAT MYODBCShell::getResultFormat()
+{
+ return nResultFormat;
+}
+
+QChar MYODBCShell::getDelimiter()
+{
+ return cDelimiter;
+}
+
+int MYODBCShell::getMaxDataWidth()
+{
+ return nMaxDataWidth;
+}
+
+bool MYODBCShell::doConnect()
+{
+ SQLRETURN nReturn;
+
+ if (hEnv)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hEnv)))
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_ENV.").arg(__FILE__).arg(__LINE__) << endl;
+ hEnv = NULL;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,
(SQLPOINTER)SQL_OV_ODBC3, 0)))
+ {
+ if (bVerbose) doErrors(hEnv, NULL, NULL);
+ *pstreamStdErr << QString("[%1][%2][WARNING] Could not SQLSetEnvAttr to set
version to SQL_OV_ODBC3.").arg(__FILE__).arg(__LINE__) << endl;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc)))
+ {
+ if (bVerbose) doErrors(hEnv, NULL, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_DBC.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
+ hEnv = NULL;
+ hDbc = NULL;
+ return false;
+ }
+
+ if (bDriverConnect)
+ {
+ SQLWCHAR sOut[4096];
+ SQLSMALLINT nLen;
+ nReturn = SQLDriverConnect(hDbc, 0, (SQLWCHAR*)stringConnect.utf16(),
stringConnect.length(), sOut, 4096, &nLen, SQL_DRIVER_NOPROMPT);
+ }
+ else
+ nReturn = SQLConnect(hDbc, (SQLWCHAR*)stringConnect.utf16(),
stringConnect.length(), (SQLWCHAR*)stringUID.utf16(), stringUID.length(),
(SQLWCHAR*)stringPWD.utf16(), stringPWD.length());
+
+ if (!SQL_SUCCEEDED(nReturn))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
connect.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
+ SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
+ hEnv = NULL;
+ hDbc = NULL;
+ return false;
+ }
+
+ return true;
+}
+
+bool MYODBCShell::doCommand(const QString &stringCommand)
+{
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (stringCommand.startsWith("help"))
+ return doHelp(stringCommand);
+ else if (stringCommand.startsWith("SQLTables"))
+ return doTables(stringCommand);
+ else if (stringCommand.startsWith("SQLColumns"))
+ return doColumns(stringCommand);
+ else if (stringCommand.startsWith("SQLStatistics"))
+ return doStatistics(stringCommand);
+ else
+ return doSubmit(stringCommand);
+
+ return true;
+}
+
+
+bool MYODBCShell::doDisconnect()
+{
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLDisconnect(hDbc)))
+ doErrors(hEnv, hDbc, NULL);
+ if (!SQL_SUCCEEDED(SQLFreeConnect(hDbc)))
+ doErrors(hEnv, hDbc, NULL);
+ if (!SQL_SUCCEEDED(SQLFreeEnv(hEnv)))
+ doErrors(hEnv, NULL, NULL);
+
+ hDbc = NULL;
+ hEnv = NULL;
+
+ return true;
+}
+
+SQLLEN MYODBCShell::getOptimalDisplayWidth( SQLINTEGER nColumn )
+{
+ SQLLEN nDataWidth = 10;
+ SQLLEN nOptimalDisplayWidth = 10;
+ SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
+ SQLSMALLINT nLabelLengthBytes = 0;
+
+ SQLColAttribute(hStm, nColumn, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL,
&nDataWidth);
+ SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
&nLabelLengthBytes, NULL);
+
+ nOptimalDisplayWidth = std::max( (SQLLEN)(nLabelLengthBytes / sizeof(SQLWCHAR)),
nDataWidth );
+
+ if (nMaxDataWidth > 0)
+ nOptimalDisplayWidth = std::min(nOptimalDisplayWidth, nMaxDataWidth);
+
+ if (nOptimalDisplayWidth > MAX_DATA_WIDTH)
+ nOptimalDisplayWidth = MAX_DATA_WIDTH;
+
+ return nOptimalDisplayWidth;
+}
+
+bool MYODBCShell::doSubmit(const QString &stringCommand)
+{
+ SQLINTEGER nReturn;
+
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ // get results (if any)
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
+ hStm = NULL;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLPrepare(hStm, (SQLWCHAR*)stringCommand.utf16(),
stringCommand.length())))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLPrepare.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+
+ nReturn = SQLExecute(hStm);
+ if (nReturn == SQL_NO_DATA)
+ {
+ *pstreamStdErr << QString("[%1][%2][INFO] SQLExecute returned
SQL_NO_DATA.").arg(__FILE__).arg(__LINE__) << endl;
+ }
+ else if (nReturn == SQL_SUCCESS_WITH_INFO)
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][INFO] SQLExecute returned
SQL_SUCCESS_WITH_INFO.").arg(__FILE__).arg(__LINE__) << endl;
+ }
+ else if (nReturn != SQL_SUCCESS)
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLExecute.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return 0;
+ }
+
+ // process results (if any)
+ do
+ {
+ doResults();
+ }
+ while (SQL_SUCCEEDED(SQLMoreResults(hStm)));
+
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+
+ return true;
+}
+
+bool MYODBCShell::doHelp(const QString &stringCommand)
+{
+ QString stringTable;
+
+ // init
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
+ hStm = NULL;
+ return false;
+ }
+
+ QStringList stringlist = stringCommand.split(' ');
+ if (stringlist.count() > 1)
+ {
+ QString stringTable = stringlist[1].trimmed();
+ if (!SQL_SUCCEEDED(SQLColumns(hStm, NULL, 0, NULL, 0,
(SQLWCHAR*)stringTable.utf16(), stringTable.length(), NULL, 0)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLColumns.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+ }
+ else
+ {
+ if (!SQL_SUCCEEDED(SQLTables(hStm, NULL, 0, NULL, 0, NULL, 0, NULL, 0)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLTables.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+ }
+
+ // process results (if any)
+ doResults();
+
+ // fini
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+
+ return true;
+}
+
+bool MYODBCShell::doTables(const QString &stringCommand)
+{
+ SQLWCHAR * psCatalog = NULL;
+ SQLWCHAR * psSchema = NULL;
+ SQLWCHAR * psTable = NULL;
+ SQLWCHAR * psTableTypes = NULL;
+ SQLSMALLINT nCatalog = 0;
+ SQLSMALLINT nSchema = 0;
+ SQLSMALLINT nTable = 0;
+ SQLSMALLINT nTableTypes = 0;
+
+ // init
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
+ hStm = NULL;
+ return false;
+ }
+
+ QStringList stringlist = stringCommand.split('|');
+
+ if (stringlist.count() != 6)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 6
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
+ return false;
+ }
+
+ QString stringCatalog = stringlist.at(1);
+ QString stringSchema = stringlist.at(2);
+ QString stringTable = stringlist.at(3);
+ QString stringTableTypes = stringlist.at(4);
+
+ if (stringCatalog != "NULL" && !stringCatalog.isNull())
+ {
+ psCatalog = (SQLWCHAR*)stringCatalog.utf16();
+ nCatalog = stringCatalog.length();
+ }
+ if (stringSchema != "NULL" && !stringSchema.isNull())
+ {
+ psSchema = (SQLWCHAR*)stringSchema.utf16();
+ nSchema = stringSchema.length();
+ }
+ if (stringTable != "NULL" && !stringTable.isNull())
+ {
+ psTable = (SQLWCHAR*)stringTable.utf16();
+ nTable = stringTable.length();
+ }
+ if (stringTableTypes != "NULL" && !stringTableTypes.isNull())
+ {
+ psTableTypes = (SQLWCHAR*)stringTableTypes.utf16();
+ nTableTypes = stringTableTypes.length();
+ }
+
+ if (!SQL_SUCCEEDED(SQLTables(hStm, psCatalog, nCatalog, psSchema, nSchema, psTable,
nTable, psTableTypes, nTableTypes)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLTables.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+
+ // process results (if any)
+ doResults();
+
+ // fini
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+
+ return true;
+}
+
+bool MYODBCShell::doColumns(const QString &stringCommand)
+{
+ SQLWCHAR * psCatalog = NULL;
+ SQLWCHAR * psSchema = NULL;
+ SQLWCHAR * psTable = NULL;
+ SQLWCHAR * psColumn = NULL;
+ SQLSMALLINT nCatalog = 0;
+ SQLSMALLINT nSchema = 0;
+ SQLSMALLINT nTable = 0;
+ SQLSMALLINT nColumn = 0;
+
+ // init
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
+ hStm = NULL;
+ return false;
+ }
+
+ QStringList stringlist = stringCommand.split('|');
+
+ if (stringlist.count() != 6)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 6
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
+ return false;
+ }
+
+ QString stringCatalog = stringlist.at(1);
+ QString stringSchema = stringlist.at(2);
+ QString stringTable = stringlist.at(3);
+ QString stringColumn = stringlist.at(4);
+
+ if (stringCatalog != "NULL" && !stringCatalog.isNull())
+ {
+ psCatalog = (SQLWCHAR*)stringCatalog.utf16();
+ nCatalog = stringCatalog.length();
+ }
+ if (stringSchema != "NULL" && !stringSchema.isNull())
+ {
+ psSchema = (SQLWCHAR*)stringSchema.utf16();
+ nSchema = stringSchema.length();
+ }
+ if (stringTable != "NULL" && !stringTable.isNull())
+ {
+ psTable = (SQLWCHAR*)stringTable.utf16();
+ nTable = stringTable.length();
+ }
+ if (stringColumn != "NULL" && !stringColumn.isNull())
+ {
+ psColumn = (SQLWCHAR*)stringColumn.utf16();
+ nColumn = stringColumn.length();
+ }
+
+ if (!SQL_SUCCEEDED(SQLColumns(hStm, psCatalog, nCatalog, psSchema, nSchema, psTable,
nTable, psColumn, nColumn)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLColumn.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+
+ // process results (if any)
+ doResults();
+
+ // fini
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+
+ return true;
+}
+
+/*!
+ \brief Process a SQLStatictics command.
+
+ The command should start with "SQLStatistics " and be followed by a filter
specification.
+
+ Example;
+
+ SQLStatistics |||categories|
+*/
+bool MYODBCShell::doStatistics(const QString &stringCommand)
+{
+ SQLWCHAR * psCatalog = NULL;
+ SQLWCHAR * psSchema = NULL;
+ SQLWCHAR * psTable = NULL;
+ SQLSMALLINT nCatalog = 0;
+ SQLSMALLINT nSchema = 0;
+ SQLSMALLINT nTable = 0;
+
+ // init
+ if (!hDbc)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStm)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, NULL);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not allocate
SQL_HANDLE_STMT.").arg(__FILE__).arg(__LINE__) << endl;
+ hStm = NULL;
+ return false;
+ }
+
+ QStringList stringlist = stringCommand.split('|');
+
+ if (stringlist.count() != 5)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] %3 filter elements provide but 5
expected.").arg(__FILE__).arg(__LINE__).arg(stringlist.count()) << endl;
+ return false;
+ }
+
+ QString stringCatalog = stringlist.at(1);
+ QString stringSchema = stringlist.at(2);
+ QString stringTable = stringlist.at(3);
+ QString stringColumn = stringlist.at(4);
+
+ if (stringCatalog != "NULL" && !stringCatalog.isNull())
+ {
+ psCatalog = (SQLWCHAR*)stringCatalog.utf16();
+ nCatalog = stringCatalog.length();
+ }
+ if (stringSchema != "NULL" && !stringSchema.isNull())
+ {
+ psSchema = (SQLWCHAR*)stringSchema.utf16();
+ nSchema = stringSchema.length();
+ }
+ if (stringTable != "NULL" && !stringTable.isNull())
+ {
+ psTable = (SQLWCHAR*)stringTable.utf16();
+ nTable = stringTable.length();
+ }
+
+ if (!SQL_SUCCEEDED(SQLStatistics(hStm, psCatalog, nCatalog, psSchema, nSchema,
psTable, nTable, SQL_INDEX_ALL, 0)))
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLStatistics.").arg(__FILE__).arg(__LINE__) << endl;
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+ return false;
+ }
+
+ // process results (if any)
+ doResults();
+
+ // fini
+ SQLFreeHandle(SQL_HANDLE_STMT, hStm);
+ hStm = NULL;
+
+ return true;
+}
+
+bool MYODBCShell::doResults()
+{
+ SQLSMALLINT nCols;
+ SQLINTEGER nRows = 0;
+
+ if (!hStm)
+ {
+ *pstreamStdErr << QString("[%1][%2][ERROR] Function sequence
error.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ stringSepLine = "";
+
+ if (SQLNumResultCols(hStm, &nCols) != SQL_SUCCESS)
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ *pstreamStdErr << QString("[%1][%2][ERROR] Could not
SQLNumResultCols.").arg(__FILE__).arg(__LINE__) << endl;
+ return false;
+ }
+
+ if (nCols > 0)
+ {
+ // header
+ switch (nResultFormat)
+ {
+ case RESULT_FORMAT_NORMAL:
+ doHeaderNormal();
+ break;
+ case RESULT_FORMAT_HTML:
+ doHeaderHTMLTable();
+ break;
+ case RESULT_FORMAT_DELIMITED:
+ doHeaderDelimited();
+ break;
+ }
+
+ // body
+ switch (nResultFormat)
+ {
+ case RESULT_FORMAT_NORMAL:
+ nRows = doBodyNormal();
+ break;
+ case RESULT_FORMAT_HTML:
+ doBodyHTMLTable();
+ break;
+ case RESULT_FORMAT_DELIMITED:
+ doBodyDelimited();
+ break;
+ }
+ }
+
+ // footer
+ switch (nResultFormat)
+ {
+ case RESULT_FORMAT_NORMAL:
+ doFooterNormal(nRows);
+ break;
+ case RESULT_FORMAT_HTML:
+ doFooterHTMLTable();
+ break;
+ case RESULT_FORMAT_DELIMITED:
+ break;
+ }
+
+ return true;
+}
+
+bool MYODBCShell::doHeaderNormal()
+{
+ SQLINTEGER nColumn = 0;
+ SQLSMALLINT nColumns = 0;
+ SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
+ QString stringFormatBuffer;
+ QString stringNameLine;
+ SQLINTEGER nChar;
+
+ if (!doCalcOptimalDisplayWidths())
+ return false;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ for (nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ sColumnName[0] = '\0';
+
+ if (!SQL_SUCCEEDED(SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName,
sizeof(sColumnName), NULL, NULL)) && bVerbose)
+ doErrors(NULL, NULL, hStm);
+
+ stringSepLine += "+";
+ for (nChar=0; nChar <= vectorOptimalDisplayWidths[nColumn]; nChar++)
stringSepLine += "-";
+
+ stringNameLine += stringFormatBuffer.sprintf("| %-*.*ls",
vectorOptimalDisplayWidths[nColumn], vectorOptimalDisplayWidths[nColumn], sColumnName);
+ }
+ stringSepLine += "+";
+ stringNameLine += "|";
+
+ *pstreamStdOut << stringSepLine << endl;
+ *pstreamStdOut << stringNameLine << endl;
+ *pstreamStdOut << stringSepLine << endl;
+
+ return true;
+}
+
+SQLINTEGER MYODBCShell::doBodyNormal()
+{
+ SQLINTEGER nColumn = 0;
+ SQLSMALLINT nColumns = 0;
+ SQLLEN nIndicator = 0;
+ SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
+ SQLRETURN nReturn = 0;
+ SQLINTEGER nRows = 0;
+ QString stringFormatBuffer;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ nReturn = SQLFetch(hStm);
+ while (SQL_SUCCEEDED(nReturn))
+ {
+ for (nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
+ if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
+ {
+ *pstreamStdOut << stringFormatBuffer.sprintf("| %-*.*ls",
vectorOptimalDisplayWidths[nColumn], vectorOptimalDisplayWidths[nColumn], sColumnValue);
+ }
+ else if (nReturn == SQL_ERROR)
+ {
+ break;
+ }
+ else
+ {
+ *pstreamStdOut << stringFormatBuffer.sprintf("| %-*s",
vectorOptimalDisplayWidths[nColumn], "");
+ }
+
+ } /* for columns */
+
+ nRows++;
+ *pstreamStdOut << "|" << endl;
+ nReturn = SQLFetch(hStm);
+
+ } /* while rows */
+
+ if (nReturn == SQL_ERROR)
+ {
+ if (bVerbose) doErrors(NULL, NULL, hStm);
+ }
+
+ return nRows;
+}
+
+bool MYODBCShell::doFooterNormal(SQLINTEGER nRows)
+{
+ SQLLEN nRowsAffected = -1;
+
+ *pstreamStdOut << stringSepLine << endl;
+
+ if (SQLRowCount(hStm, &nRowsAffected) != SQL_SUCCESS)
+ {
+ if (bVerbose) doErrors(hEnv, hDbc, hStm);
+ }
+
+ *pstreamStdOut << nRowsAffected << " rows affected" << endl;
+ *pstreamStdOut << nRows << " rows fetched" << endl;
+
+ return true;
+}
+
+bool MYODBCShell::doHeaderHTMLTable()
+{
+ SQLSMALLINT nColumns = 0;
+ SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
+
+ *pstreamStdOut << "<table BORDER>" << endl;
+ *pstreamStdOut << "<tr BGCOLOR=#000099>" << endl;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
NULL, NULL);
+ *pstreamStdOut << "<td>" << endl;
+ *pstreamStdOut << "<font face=Arial,Helvetica><font
color=#FFFFFF>" << endl;
+ *pstreamStdOut << QString::fromStdWString(sColumnName) << endl;
+ *pstreamStdOut << "</font></font>" << endl;
+ *pstreamStdOut << "</td>" << endl;
+ }
+ *pstreamStdOut << "</tr>" << endl;
+
+ return true;
+}
+
+bool MYODBCShell::doBodyHTMLTable()
+{
+ SQLSMALLINT nColumns = 0;
+ SQLRETURN nReturn = 0;
+ bool bReturn = true;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ while (SQL_SUCCEEDED(SQLFetch(hStm)))
+ {
+ *pstreamStdOut << "<tr>" << endl;
+
+ for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ SQLLEN nIndicator = 0;
+ SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
+
+ *pstreamStdOut << "<td>" << endl;
+ *pstreamStdOut << "<font face=Arial,Helvetica>" << endl;
+
+ nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
+ if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
+ {
+ *pstreamStdOut << QString::fromStdWString(sColumnValue) <<
endl;
+ }
+ else if (nReturn == SQL_ERROR)
+ {
+ *pstreamStdOut << endl;
+ if (bVerbose) doErrors(NULL, NULL, hStm);
+ bReturn = false;
+ break;
+ }
+ else
+ *pstreamStdOut << endl;
+
+ *pstreamStdOut << "</font>" << endl;
+ *pstreamStdOut << "</td>" << endl;
+ }
+ if (!bReturn)
+ break;
+ *pstreamStdOut << "</tr>" << endl;
+ }
+
+ return bReturn;
+}
+
+bool MYODBCShell::doFooterHTMLTable()
+{
+ *pstreamStdOut << "</table>" << endl;
+ return true;
+}
+
+bool MYODBCShell::doHeaderDelimited()
+{
+ SQLSMALLINT nColumns = 0;
+ SQLWCHAR sColumnName[MAX_DATA_WIDTH] = L"";
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ SQLColAttribute(hStm, nColumn, SQL_DESC_LABEL, sColumnName, sizeof(sColumnName),
NULL, NULL);
+ *pstreamStdOut << QString::fromStdWString(sColumnName);
+ if (nColumn < nColumns)
+ *pstreamStdOut << cDelimiter;
+ }
+ *pstreamStdOut << endl;
+
+ return true;
+}
+
+bool MYODBCShell::doBodyDelimited()
+{
+ SQLSMALLINT nColumns = 0;
+ SQLRETURN nReturn = 0;
+ bool bReturn = true;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ nColumns = -1;
+
+ while (SQL_SUCCEEDED(SQLFetch(hStm)))
+ {
+ for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ SQLLEN nIndicator = 0;
+ SQLWCHAR sColumnValue[MAX_DATA_WIDTH] = L"";
+
+ nReturn = SQLGetData(hStm, nColumn, SQL_C_WCHAR, (SQLPOINTER)sColumnValue,
sizeof(sColumnValue), &nIndicator);
+ if (SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA)
+ {
+ *pstreamStdOut << QString::fromStdWString(sColumnValue);
+ if (nColumn < nColumns)
+ *pstreamStdOut << cDelimiter;
+ }
+ else if (nReturn == SQL_ERROR)
+ {
+ *pstreamStdOut << endl;
+ if (bVerbose) doErrors(NULL, NULL, hStm);
+ bReturn = false;
+ break;
+ }
+ else
+ {
+ if (nColumn < nColumns)
+ *pstreamStdOut << cDelimiter;
+ }
+ }
+ if (!bReturn)
+ break;
+ *pstreamStdOut << endl;
+ }
+
+ return bReturn;
+}
+
+bool MYODBCShell::doErrors(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStm)
+{
+ SQLSMALLINT nRecord = 1;
+ SQLWCHAR sState[6];
+ SQLINTEGER nNativeError;
+ SQLWCHAR sMessageText[SQL_MAX_MESSAGE_LENGTH];
+ SQLSMALLINT nTextLength;
+
+ if (hStm)
+ {
+ while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_STMT,
+ hStm,
+ nRecord,
+ sState,
+ &nNativeError,
+ sMessageText,
+ SQL_MAX_MESSAGE_LENGTH,
+ &nTextLength)))
+ {
+ sState[5] = '\0';
+ *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
+ nRecord++;
+ }
+ }
+
+ if (hDbc)
+ {
+ nRecord = 1;
+ while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_DBC,
+ hDbc,
+ nRecord,
+ sState,
+ &nNativeError,
+ sMessageText,
+ SQL_MAX_MESSAGE_LENGTH,
+ &nTextLength)))
+ {
+ sState[5] = '\0';
+ *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
+ nRecord++;
+ }
+ }
+
+ if (hEnv)
+ {
+ nRecord = 1;
+ while (SQL_SUCCEEDED(SQLGetDiagRec(SQL_HANDLE_ENV,
+ hEnv,
+ nRecord,
+ sState,
+ &nNativeError,
+ sMessageText,
+ SQL_MAX_MESSAGE_LENGTH,
+ &nTextLength)))
+ {
+ sState[5] = '\0';
+ *pstreamStdErr << "[" << QString::fromStdWString(sState) <<
"]" << QString::fromStdWString(sMessageText) << endl;
+ nRecord++;
+ }
+ }
+
+ return 1;
+}
+
+bool MYODBCShell::doCalcOptimalDisplayWidths()
+{
+ SQLSMALLINT nColumns;
+
+ if (!SQL_SUCCEEDED(SQLNumResultCols(hStm, &nColumns)))
+ return true;
+
+ vectorOptimalDisplayWidths.resize(nColumns + 1);
+
+ for (SQLINTEGER nColumn = 1; nColumn <= nColumns; nColumn++)
+ {
+ vectorOptimalDisplayWidths[nColumn] = getOptimalDisplayWidth(nColumn);
+ }
+
+ return true;
+}
+
Modified: trunk/Tools/Shell/MYODBCShell.h
===================================================================
--- trunk/Tools/Shell/MYODBCShell.h 2007-05-03 15:45:43 UTC (rev 904)
+++ trunk/Tools/Shell/MYODBCShell.h 2007-05-11 05:14:49 UTC (rev 905)
@@ -1,136 +1,136 @@
-/*!
- \file MYODBCShell.h
- \author Peter Harvey <pharvey@stripped>
- Copyright (C) MySQL AB 2004-2007, Released under GPL.
- \version Connector/ODBC v5
- \date 2007
- \brief Command-line ODBC tool.
-
- This program is a command-line tool for working with ODBC - it
- allows statements to be submitted to a data source and displays
- results in a variety of formats.
-
- \license GPL
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- There are special exceptions to the terms and conditions of the GPL as it
- is applied to this software. View the full text of the exception in file
- EXCEPTIONS in the directory of this software distribution.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-#ifndef MYODBC_SHELL_H
-#define MYODBC_SHELL_H
-
-#include <QObject>
-#include <QCoreApplication>
-#include <QString>
-#include <QTextStream>
-#include <QVector>
-#include <QStringList>
-
-#include <MYODBCC.h>
-
-#define MAX_DATA_WIDTH 300
-
-/*! \todo break this out into multiple classes */
-class MYODBCShell : public QObject
-{
-public:
- MYODBCShell();
- ~MYODBCShell();
-
- typedef enum tRESULT_FORMAT
- {
- RESULT_FORMAT_NORMAL,
- RESULT_FORMAT_HTML,
- RESULT_FORMAT_DELIMITED
-
- } RESULT_FORMAT;
-
- QTextStream * pstreamStdIn;
- QTextStream * pstreamStdOut;
- QTextStream * pstreamStdErr;
-
- void setConnect(const QString &stringConnect);
- void setUID(const QString &stringUID);
- void setPWD(const QString &stringPWD);
- void setDriverConnect(bool bDriverConnect = true);
- void setVerbose(bool bVerbose = true);
- void setLabels(bool bLabels = true);
- void setResultFormat(RESULT_FORMAT nResultFormat = RESULT_FORMAT_NORMAL);
- void setDelimiter(QChar cDelimiter = '|');
- void setMaxDataWidth(int nMaxDataWidth = 0);
-
- QString getConnect();
- QString getUID();
- QString getPWD();
- bool getDriverConnect();
- bool getVerbose();
- bool getLabels();
- RESULT_FORMAT getResultFormat();
- QChar getDelimiter();
- int getMaxDataWidth();
-
- bool doConnect();
- bool doCommand(const QString &stringCommand);
- bool doDisconnect();
-
-protected:
- QString stringConnect;
- QString stringUID;
- QString stringPWD;
- bool bDriverConnect;
- bool bVerbose;
- bool bLabels;
- RESULT_FORMAT nResultFormat;
- QChar cDelimiter;
- SQLINTEGER nMaxDataWidth;
- SQLHENV hEnv;
- SQLHDBC hDbc;
- SQLHSTMT hStm;
-
- QVector<SQLINTEGER> vectorOptimalDisplayWidths;
- QString stringSepLine;
-
- SQLINTEGER getOptimalDisplayWidth(SQLINTEGER nColumn);
-
- bool doSubmit(const QString &stringCommand);
- bool doHelp(const QString &stringCommand);
- bool doTables(const QString &stringCommand);
- bool doColumns(const QString &stringCommand);
- bool doStatistics(const QString &stringCommand);
-
- bool doResults();
-
- bool doHeaderNormal();
- SQLINTEGER doBodyNormal();
- bool doFooterNormal(SQLINTEGER nRows);
-
- bool doHeaderHTMLTable();
- bool doBodyHTMLTable();
- bool doFooterHTMLTable();
-
- bool doHeaderDelimited();
- bool doBodyDelimited();
-
- bool doErrors(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStm);
-
- bool doCalcOptimalDisplayWidths();
-};
-
-#endif
-
-
-
+/*!
+ \file MYODBCShell.h
+ \author Peter Harvey <pharvey@stripped>
+ Copyright (C) MySQL AB 2004-2007, Released under GPL.
+ \version Connector/ODBC v5
+ \date 2007
+ \brief Command-line ODBC tool.
+
+ This program is a command-line tool for working with ODBC - it
+ allows statements to be submitted to a data source and displays
+ results in a variety of formats.
+
+ \license GPL
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ There are special exceptions to the terms and conditions of the GPL as it
+ is applied to this software. View the full text of the exception in file
+ EXCEPTIONS in the directory of this software distribution.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+#ifndef MYODBC_SHELL_H
+#define MYODBC_SHELL_H
+
+#include <QObject>
+#include <QCoreApplication>
+#include <QString>
+#include <QTextStream>
+#include <QVector>
+#include <QStringList>
+
+#include <MYODBCC.h>
+
+#define MAX_DATA_WIDTH 300
+
+/*! \todo break this out into multiple classes */
+class MYODBCShell : public QObject
+{
+public:
+ MYODBCShell();
+ ~MYODBCShell();
+
+ typedef enum tRESULT_FORMAT
+ {
+ RESULT_FORMAT_NORMAL,
+ RESULT_FORMAT_HTML,
+ RESULT_FORMAT_DELIMITED
+
+ } RESULT_FORMAT;
+
+ QTextStream * pstreamStdIn;
+ QTextStream * pstreamStdOut;
+ QTextStream * pstreamStdErr;
+
+ void setConnect(const QString &stringConnect);
+ void setUID(const QString &stringUID);
+ void setPWD(const QString &stringPWD);
+ void setDriverConnect(bool bDriverConnect = true);
+ void setVerbose(bool bVerbose = true);
+ void setLabels(bool bLabels = true);
+ void setResultFormat(RESULT_FORMAT nResultFormat = RESULT_FORMAT_NORMAL);
+ void setDelimiter(QChar cDelimiter = '|');
+ void setMaxDataWidth(int nMaxDataWidth = 0);
+
+ QString getConnect();
+ QString getUID();
+ QString getPWD();
+ bool getDriverConnect();
+ bool getVerbose();
+ bool getLabels();
+ RESULT_FORMAT getResultFormat();
+ QChar getDelimiter();
+ int getMaxDataWidth();
+
+ bool doConnect();
+ bool doCommand(const QString &stringCommand);
+ bool doDisconnect();
+
+protected:
+ QString stringConnect;
+ QString stringUID;
+ QString stringPWD;
+ bool bDriverConnect;
+ bool bVerbose;
+ bool bLabels;
+ RESULT_FORMAT nResultFormat;
+ QChar cDelimiter;
+ SQLLEN nMaxDataWidth;
+ SQLHENV hEnv;
+ SQLHDBC hDbc;
+ SQLHSTMT hStm;
+
+ QVector<SQLLEN> vectorOptimalDisplayWidths;
+ QString stringSepLine;
+
+ SQLLEN getOptimalDisplayWidth( SQLINTEGER nColumn );
+
+ bool doSubmit(const QString &stringCommand);
+ bool doHelp(const QString &stringCommand);
+ bool doTables(const QString &stringCommand);
+ bool doColumns(const QString &stringCommand);
+ bool doStatistics(const QString &stringCommand);
+
+ bool doResults();
+
+ bool doHeaderNormal();
+ SQLINTEGER doBodyNormal();
+ bool doFooterNormal(SQLINTEGER nRows);
+
+ bool doHeaderHTMLTable();
+ bool doBodyHTMLTable();
+ bool doFooterHTMLTable();
+
+ bool doHeaderDelimited();
+ bool doBodyDelimited();
+
+ bool doErrors(SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStm);
+
+ bool doCalcOptimalDisplayWidths();
+};
+
+#endif
+
+
+
| Thread |
|---|
| • Connector/ODBC 5 commit: r905 - in trunk: Driver/Setup/Library SDK/C/include Tools/Shell | jbalint | 11 May |