Modified:
MYODBCShell/MYODBCShell.cpp
MYODBCShell/MYODBCShell.h
MYODBCShell/main.cpp
Log:
Modified: MYODBCShell/MYODBCShell.cpp
===================================================================
--- MYODBCShell/MYODBCShell.cpp 2006-04-18 17:43:35 UTC (rev 84)
+++ MYODBCShell/MYODBCShell.cpp 2006-04-19 04:29:44 UTC (rev 85)
@@ -1,4 +1,63 @@
+/*!
+ \file MYODBCShell.cpp
+ \author Peter Harvey <pharvey@stripped>
+ Copyright (C) MySQL AB 2004-2006, Released under GPL.
+ \version Connector/ODBC v5
+ \date 2006
+ \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 = false;
+ bLabels = true;
+ nResultFormat = RESULT_FORMAT_NORMAL;
+ cDelimiter = '|';
+ nMaxDataWidth = 0;
+ hEnv = 0;
+ hDbc = 0;
+ pstreamStdIn = new QTextStream( stdin );
+ pstreamStdOut = new QTextStream( stdout );
+ pstreamStdErr = new QTextStream( stderr );
+}
+
+MYODBCShell::~MYODBCShell()
+{
+ delete pstreamStdIn;
+ delete pstreamStdOut;
+ delete pstreamStdErr;
+}
+
+
++++++++++++++
+
int getElement( char *pszData, char cSeperator, char cTerminator, int nElement, char
*pszElement, int nMaxElement )
{
int nCurElement = 0;
Modified: MYODBCShell/MYODBCShell.h
===================================================================
--- MYODBCShell/MYODBCShell.h 2006-04-18 17:43:35 UTC (rev 84)
+++ MYODBCShell/MYODBCShell.h 2006-04-19 04:29:44 UTC (rev 85)
@@ -1,5 +1,5 @@
/*!
- \file MYODBCShell.cpp
+ \file MYODBCShell.h
\author Peter Harvey <pharvey@stripped>
Copyright (C) MySQL AB 2004-2006, Released under GPL.
\version Connector/ODBC v5
@@ -48,29 +48,71 @@
MYODBCShell();
~MYODBCShell();
- QTextStream *pstreamStdOut;
- QTextStream *pstreamStdErr;
+ 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 &stringConnect );
+ void setPWD( const QString &stringConnect );
+ 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( stringCommand );
+ bool doDisconnect();
+
protected:
- int OpenDatabase( SQLHENV *phEnv, SQLHDBC *phDbc, char *szDSN, char *szUID, char
*szPWD );
- int ExecuteSQL( SQLHDBC hDbc, char *szSQL, char cDelimiter, int bColumnNames, int
bHTMLTable );
- int ExecuteHelp( SQLHDBC hDbc, char *szSQL, char cDelimiter, int bColumnNames, int
bHTMLTable );
- int CloseDatabase( SQLHENV hEnv, SQLHDBC hDbc );
+ QString stringConnect;
+ QString stringUID;
+ QString stringPWD;
+ bool bDriverConnect;
+ bool bVerbose;
+ bool bLabels;
+ RESULT_FORMAT nResultFormat;
+ QChar cDelimiter;
+ int nMaxDataWidth;
+ SQLHENV hEnv;
+ SQLHDBC hDbc;
+ SQLHSTMT hStm;
- int doTables( SQLHDBC hDbc, char *szSQL, char cDelimiter, int bColumnNames, int
bHTMLTable );
- int doColumns( SQLHDBC hDbc, char *szSQL, char cDelimiter, int bColumnNames, int
bHTMLTable );
+ bool doSubmit( stringCommand );
+ bool doHelp( stringCommand );
+ bool doTables( stringCommand );
+ bool doColumns( stringCommand );
- void WriteHeaderHTMLTable( SQLHSTMT hStmt );
- void WriteHeaderNormal( SQLHSTMT hStmt, SQLCHAR *szSepLine );
- void WriteHeaderDelimited( SQLHSTMT hStmt, char cDelimiter );
- void WriteBodyHTMLTable( SQLHSTMT hStmt );
- SQLINTEGER WriteBodyNormal( SQLHSTMT hStmt );
- void WriteBodyDelimited( SQLHSTMT hStmt, char cDelimiter );
- void WriteFooterHTMLTable( SQLHSTMT hStmt );
- void WriteFooterNormal( SQLHSTMT hStmt, SQLCHAR *szSepLine, SQLINTEGER nRows );
- bool doShowErrors();
-// int DumpODBCLog( SQLHENV hEnv, SQLHDBC hDbc, SQLHSTMT hStmt );
+ void doHeaderHTMLTable();
+ void doHeaderNormal( const QString &stringSepLine );
+ void doHeaderDelimited();
+ void doBodyHTMLTable();
+ SQLINTEGER doBodyNormal();
+ void doBodyDelimited();
+ void doFooterHTMLTable();
+ void doFooterNormal( const QString &stringSepLine, SQLINTEGER nRows );
+
+ bool doErrors();
};
#endif
Modified: MYODBCShell/main.cpp
===================================================================
--- MYODBCShell/main.cpp 2006-04-18 17:43:35 UTC (rev 84)
+++ MYODBCShell/main.cpp 2006-04-19 04:29:44 UTC (rev 85)
@@ -33,7 +33,8 @@
#include "MYODBCShell.h"
-MYODBCShell *pShell = NULL;
+MYODBCShell * pShell = NULL;
+bool bBatch = false;
int doShowSyntax( QTextStream *pstream )
{
@@ -49,17 +50,12 @@
(*pstream) << "|" << endl;
(*pstream) << "| -r Use SQLDriverConnect to connect" << endl;
(*pstream) << "| and use STR as connection string" <<
endl;
- (*pstream) << "| -b batch.(no prompting etc)" << endl;
+ (*pstream) << "| -b batch (no prompting)" << endl;
(*pstream) << "| -dx delimit columns with x" << endl;
- (*pstream) << "| -x0xXX delimit columns with XX, where " << endl;
- (*pstream) << "| x is in hex, ie 0x09 is tab " << endl;
- (*pstream) << "| -w wrap results in an HTML table " << endl;
- (*pstream) << "| -c column names on first row. " << endl;
- (*pstream) << "| (only used when -d) " << endl;
+ (*pstream) << "| -w wrap results in an HTML table" << endl;
+ (*pstream) << "| -c labels off" << endl;
(*pstream) << "| -mn limit column display width to n" << endl;
- (*pstream) << "| -v verbose. " << endl;
- (*pstream) << "| -lx set locale to x " << endl;
- (*pstream) << "| --version version " << endl;
+ (*pstream) << "| -v verbose off" << endl;
(*pstream) << "|" << endl;
(*pstream) << "| Examples" << endl;
(*pstream) << "|" << endl;
@@ -73,207 +69,111 @@
return 1;
}
-
-+++++++++++++++++
-
-int bDriverConnect = 0;
-int bVerbose = 0;
-int nUserWidth = 0;
-SQLHENV hEnv = 0;
-SQLHDBC hDbc = 0;
-
int main( int argc, char *argv[] )
{
- int nArg, count;
- int bHTMLTable = 0;
- int bBatch = 0;
- int cDelimiter = 0;
- int bColumnNames = 0;
- TCHAR szDSN[MAX_DATA_WIDTH+1] = L"";
- TCHAR szUID[MAX_DATA_WIDTH+1] = L"";
- TCHAR szPWD[MAX_DATA_WIDTH+1] = L"";
- TCHAR * szSQL;
- TCHAR * pEscapeChar;
- int buffer_size = 9000;
+ QCoreApplication app( argc, argv );
+ MYODBCShell shell;
+ QStringList stringlistArguments = app.arguments();
- if ( argc < 2 )
- {
- _ftprintf( stderr, szSyntax );
- exit( 1 );
- }
+ pShell = &shell;
-#ifdef HAVE_SETLOCALE
- /*
- * Default locale
- */
- setlocale( LC_ALL, "" );
-#endif
+ if ( stringlistArguments.count() < 2 )
+ return doShowSyntax( shell.pstreamStdOut );
- /****************************
- * PARSE ARGS
- ***************************/
- for ( nArg = 1, count = 1 ; nArg < argc; nArg++ )
+ // extract args from command-line
+ QStringList::const_iterator i;
+
+ for ( i = stringlistArguments.constBegin(), ++i; i != stringlistArguments.constEnd();
++i )
{
- if ( argv[nArg][0] == '-' )
+ if ( i->at( 0 ) == '-' )
{
- /* Options */
- switch ( argv[nArg][1] )
+ switch ( i->at( 1 ).toAscii() )
{
- case 'd':
- cDelimiter = argv[nArg][2];
- break;
- case 'm':
- nUserWidth = atoi( &(argv[nArg][2]) );
- break;
case 'r':
- bDriverConnect = 1;
+ shell.setDriverConnect();
break;
- case 's':
- buffer_size = atoi( &(argv[nArg][2]) );
+ case 'b':
+ bBatch = true;
break;
+ case 'd':
+ shell.setDelimiter( i->at( 2 ) );
+ shell.setResultFormat( MYODBCShell::RESULT_FORMAT_DELIMITER );
+ break;
case 'w':
- bHTMLTable = 1;
+ shell.setResultFormat( MYODBCShell::RESULT_FORMAT_HTML );
break;
- case 'b':
- bBatch = 1;
- break;
case 'c':
- bColumnNames = 1;
+ shell.setLabels( false );
break;
+ case 'm':
+ shell.setMaxDataWidth( i->mid( 2 ).toInt() );
+ break;
case 'v':
- bVerbose = 1;
+ shell.setVerbose( false );
break;
- case '-':
- printf( "MYODBCShell\n" );
- exit(0);
-#ifdef HAVE_STRTOL
- case 'x':
- cDelimiter = strtol( argv[nArg]+2, NULL, 0 );
- break;
-#endif
-#ifdef HAVE_SETLOCALE
- case 'l':
- if ( !setlocale( LC_ALL, argv[nArg]+2 ))
- {
- _ftprintf( stderr, "MYODBCShell: can't set locale to '%s'\n",
argv[nArg]+2 );
- exit ( -1 );
- }
- break;
-#endif
-
default:
- _ftprintf( stderr, szSyntax );
- exit( 1 );
+ (*installer.pstreamStdOut) << "[ERROR] Invalid switch "
<< i->at( 1 ) << endl;
+ return 1;
}
- continue;
}
- else if ( count == 1 )
- strncpy( szDSN, argv[nArg], MAX_DATA_WIDTH );
- else if ( count == 2 )
- strncpy( szUID, argv[nArg], MAX_DATA_WIDTH );
- else if ( count == 3 )
- strncpy( szPWD, argv[nArg], MAX_DATA_WIDTH );
- count++;
+ else
+ {
+ if ( shell.getConnect().isNull() )
+ shell.setConnect( *i );
+ else if ( shell.getUID().isNull() )
+ shell.setUID( *i );
+ else if ( shell.getPWD().isNull() )
+ shell.setPWD( *i );
+ else
+ (*installer.pstreamStdOut) << "[ERROR] Invalid/extra arg " <<
*i << endl;
+ }
}
- szSQL = calloc( 1, buffer_size + 1 );
-
- /****************************
- * CONNECT
- ***************************/
-
- if ( !OpenDatabase( &hEnv, &hDbc, szDSN, szUID, szPWD ) )
+ // start processing stdin
+ if ( !shell.doConnect() )
exit( 1 );
- /****************************
- * EXECUTE
- ***************************/
if ( !bBatch )
{
- printf( "+---------------------------------------+\n" );
- printf( "| Connected! |\n" );
- printf( "| |\n" );
- printf( "| sql-statement |\n" );
- printf( "| help [tablename] |\n" );
- printf( "| quit |\n" );
- printf( "| |\n" );
- printf( "+---------------------------------------+\n" );
+ shell.pstreamStdOut << "+----" << endl;
+ shell.pstreamStdOut << "| Connected!" << endl;
+ shell.pstreamStdOut << "|" << endl;
+ shell.pstreamStdOut << "| sql-statement" << endl;
+ shell.pstreamStdOut << "| help [table_name]" << endl;
+ shell.pstreamStdOut << "| SQLTables catalog|schema|table|table_type"
<< endl;
+ shell.pstreamStdOut << "| SQLColumns catalog|schema|table|column" <<
endl;
+ shell.pstreamStdOut << "| quit" << endl;
+ shell.pstreamStdOut << "|" << endl;
+ shell.pstreamStdOut << "| NOTE: Blank line causes buffered text" <<
endl;
+ shell.pstreamStdOut << "| to be processed." << endl;
+ shell.pstreamStdOut << "|" << endl;
+ shell.pstreamStdOut << "+---" << endl;
}
+
+ QString stringLine;
+ QString stringCommand;
+
do
{
if ( !bBatch )
-#ifndef HAVE_READLINE
- printf( "SQL> " );
-#else
- {
- char *line;
- int malloced;
+ shell.pstreamStdOut << "MYODBCShell> ";
- line=readline("SQL> ");
- if ( !line ) /* EOF - ctrl D */
- {
- malloced = 1;
- line = strdup( "quit" );
- }
- else
- {
- malloced = 0;
- }
- strncpy(szSQL, line, buffer_size );
- add_history(line);
- if ( malloced )
- {
- free(line);
- }
- }
- else
-#endif
- {
- char *line;
- int malloced;
+ stringLine = shell.pstreamStdIn->readLine();
- line = fgets( szSQL, buffer_size, stdin );
- if ( !line ) /* EOF - ctrl D */
- {
- malloced = 1;
- line = (char*)strdup( "quit" );
- }
- else
- {
- malloced = 0;
- }
- strncpy(szSQL, line, buffer_size );
- if ( malloced )
- {
- free(line);
- }
- }
-
- /* strip away escape chars */
- while ( (pEscapeChar=(char*)strchr(szSQL, '\n')) != NULL ||
(pEscapeChar=(char*)strchr(szSQL, '\r')) != NULL )
- *pEscapeChar = ' ';
-
- if ( szSQL[1] != '\0' )
+ if ( stringLine.isEmpty() )
{
- /* intercept some keywords or simply send to data source */
- if ( strncmp( szSQL, "quit", 4 ) == 0 )
- szSQL[1] = '\0';
- else if ( strncmp( szSQL, "help", 4 ) == 0 )
- ExecuteHelp( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
- else if ( strncmp( szSQL, "SQLTables", 9 ) == 0 )
- doTables( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
- else if ( strncmp( szSQL, "SQLColumns", 10 ) == 0 )
- doColumns( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
- else if (memcmp(szSQL, "--", 2) != 0)
- ExecuteSQL( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
+ shell.doCommand( stringCommand );
+ stringCommand = QString::null;
}
+ else if ( stringLine == "quit" )
+ break;
+ }
+ else
+ stringCommand += ( " " + stringLine );
- } while ( szSQL[1] != '\0' );
+ } while ( 1 );
- /****************************
- * DISCONNECT
- ***************************/
- CloseDatabase( hEnv, hDbc );
+ shell.doDisconnect();
exit( 0 );
}
| Thread |
|---|
| • Connector/ODBC 5 commit: r85 - MYODBCShell | pharvey | 19 Apr |