예제 #1
0
int main( int argc, char *argv[] )
{
    int     nArg, count;
    int     bNewStyle                   = 0;
    char    *szDSN;
    char    *szUID;
    char    *szPWD;
    char    *szSQL;
    char    *line_buffer;
    int     buffer_size = 9000;
    int     line_buffer_size = 9000;
    int     bufpos,linen;
    char    prompt[24];

    szDSN = NULL;
    szUID = NULL;
    szPWD = NULL;

    if ( argc < 2 )
    {
        fputs( szSyntax, stderr );
        exit( 1 );
    }

#ifdef HAVE_SETLOCALE
    /*
     * Default locale
     */
    setlocale( LC_ALL, "" );
#endif

    /****************************
     * PARSE ARGS
     ***************************/
    for ( nArg = 1, count = 1 ; nArg < argc; nArg++ )
    {
        if ( argv[nArg][0] == '-' )
        {
            /* Options */
            switch ( argv[nArg][1] )
            {
                case 'd':
                    cDelimiter = argv[nArg][2];
                    break;
                case 'm':
                    nUserWidth = atoi( &(argv[nArg][2]) );
                    break;
                case 's':
                    buffer_size = atoi( &(argv[nArg][2]) );
                    line_buffer_size = buffer_size;
                    break;
                case 'w':
                    bHTMLTable = 1;
                    break;
                case 'b':
                    bBatch = 1;
                    break;
                case 'c':
                    bColumnNames = 1;
                    break;
                case '3':
                    version3 = 1;
                case 'v':
                    bVerbose = 1;
                    break;
                case 'q':
                    bQuote = 1;
                    break;
                case 'n':
                    bNewStyle = 1;
                    break;
                case '-':
                    printf( "unixODBC " VERSION "\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 ))
                    {
                        fprintf( stderr, "isql: can't set locale to '%s'\n", argv[nArg]+2 );
                        exit ( -1 );
                    }
                    break;
#endif

                default:
                    fputs( szSyntax, stderr );
                    exit( 1 );
            }
            continue;
        }
        else if ( count == 1 )
            szDSN = argv[nArg];
        else if ( count == 2 )
            szUID = argv[nArg];
        else if ( count == 3 )
            szPWD = argv[nArg];
        count++;
    }

    szSQL = calloc( 1, buffer_size + 1 );
    line_buffer = calloc( 1, buffer_size + 1 );

    /****************************
     * CONNECT
     ***************************/

    if ( !OpenDatabase( &hEnv, &hDbc, szDSN, szUID, szPWD ) )
        exit( 1 );

    /****************************
     * EXECUTE
     ***************************/
    if ( !bBatch )
    {
        printf( "+---------------------------------------+\n" );
        printf( "| Connected!                            |\n" );
        printf( "|                                       |\n" );
        if ( bNewStyle )
        {
            printf( "| sql-statement(s)[;]                   |\n" );
            printf( "| go                                    |\n" );
            printf( "| \\noac                                 |\n" );
            printf( "| \\ac                                   |\n" );
            printf( "| \\commit                               |\n" );
            printf( "| \\rollback                             |\n" );
            printf( "| \\tables                               |\n" );
            printf( "| \\columns <table-name>                 |\n" );
            printf( "| \\quit                                 |\n" );
        }
        else
        {
            printf( "| sql-statement                         |\n" );
            printf( "| help [tablename]                      |\n" );
            printf( "| quit                                  |\n" );
        }
        printf( "|                                       |\n" );
        printf( "+---------------------------------------+\n" );
    }

    linen = 0;
    bufpos = 0;

    do
    {
        char *line = NULL;
        int malloced = 0;
        int len = 0;
        int dont_copy, exec_now;

        szSQL[ bufpos ] = '\0';

        if ( bNewStyle )
        {
            if ( ac_off )
            {
                sprintf( prompt, "*%d SQL> ", ++linen );
            }
            else
            {
                sprintf( prompt, "%d SQL> ", ++linen );
            }
        }
        else
        {
            sprintf( prompt, "SQL> " );
        }

        if ( !bBatch )
        {
#ifdef HAVE_READLINE
            line=readline( prompt );
            if ( !line )        /* EOF - ctrl D */
            {
                malloced = 1;
                line = strdup( "quit" );
            }
            else
            {
                malloced = 0;
            }

            add_history(line);
#else
            fputs( prompt, stdout );

            line = fgets( line_buffer, line_buffer_size, stdin );
            if ( !line )        /* EOF - ctrl D */
            {
                malloced = 1;
                line = strdup( "quit" );
            }
            else
            {
				if ( line[ 0 ] == '\n' ) 
				{
					malloced = 1;
					line = strdup( "quit" );
				}
				else 
				{
					malloced = 0;
				}
            }
#endif
        }
        else
        {
            line = fgets( line_buffer, line_buffer_size, stdin );
            if ( !line )        /* EOF - ctrl D */
            {
                malloced = 1;
                line = strdup( "quit" );
            }
            else
            {
				if ( line[ 0 ] == '\n' ) 
				{
					malloced = 1;
					line = strdup( "quit" );
				}
				else 
				{
					malloced = 0;
				}
            }
        }

        /* remove any ctrl chars, and find the length */

        len = 0;
        while ( line[ len ] )
        {
            if ( line[ len ] == '\n' )
            {
                line[ len ] = ' ';
            }
            if ( line[ len ] == '\r' )
            {
                line[ len ] = ' ';
            }
            len ++;
        }

        /*
         * is it a comment? 
         */

        if ( bNewStyle )
        {
            if ( len >= 2 && line[ 0 ] == '-' && line[ 1 ] == '-' )
            {
                /* 
                 * it can't have been malloc'd
                 */
                continue;
            }
        }

        dont_copy = 0;
        exec_now = 0;

        if ( bNewStyle )
        {
            if ( len > 0 && line[ len - 1 ] == ';' )
            {
                line[ len - 1 ] = '\0';
                exec_now = 1;
                len --;
            }
            else if ( len == 3 && memcmp( line, "go", 2 ) == 0 )
            {
                exec_now = 1;
                dont_copy = 1;
            }
            else if ( len > 1 && line[ 0 ] == '\\' )
            {
                bufpos = 0;
                linen = 1;
                exec_now = 1;
            }
        }
        else
        {
            exec_now = 1;
        }

        if ( !bNewStyle )
        {
            if ( len >= 4 && memcmp( line, "quit", 4 ) == 0 )
            {
                if ( malloced )
                {
                    free(line);
                }
                break;
            }
        }

        /*
         * stop on a blank line
         */

        if ( !bNewStyle )
        {
            if ( line[ 0 ] == '\0' )
            {
                break;
            }
        }

        if ( !dont_copy )
        {
            /*
             * is there space
             */

            if ( len > 0 && bufpos + len + 2 > buffer_size )
            {
                szSQL = realloc( szSQL, bufpos + len + 2 );
                buffer_size = bufpos + len + 2;
            }

            /*
             * insert space between the lines
             * the above length check will make sure there is room for 
             * the extra space
             */
            if ( linen > 1 )
            {
                szSQL[ bufpos ] = ' ';
                bufpos ++;
            }

            memcpy( szSQL + bufpos, line, len );
            bufpos += len;
            szSQL[ bufpos ] = '\0';
        }

        if ( exec_now )
        {
            if ( bNewStyle )
            {
                if ( bufpos >= 1 && szSQL[ 0 ] == '\\' )
                {
                    if ( ExecuteSlash( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable ) == 0 )
                    {
                        break;
                    }
                }
                else
                {
                    ExecuteSQL( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
                }
            }
            else
            {
                if ( bufpos >= 4 && memcmp( szSQL, "help", 4 ) == 0 )
                {
                    ExecuteHelp( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
                }
                else
                {
                    ExecuteSQL( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
                }
            }
            linen = 0;
            bufpos = 0;
        }

    } while ( 1 );

    /****************************
     * DISCONNECT
     ***************************/
    CloseDatabase( hEnv, hDbc );

    exit( 0 );
}
예제 #2
0
int main( int argc, char *argv[] )
{
	int 	nArg, count;
	int 	bHTMLTable					= 0;
	int		bBatch						= 0;
	int 	cDelimiter					= 0;
    int     bColumnNames                = 0;
	int rc = 0;
	char	*szDSN;
	char	*szUID;
	char	*szPWD;
	char	*szSQL;
	char    *pEscapeChar;
    int     buffer_size = 9000;

	szDSN = NULL;
	szUID = NULL;
	szPWD = NULL;

	if ( argc < 2 )
	{
		fprintf( stderr, szSyntax );
		exit( 1 );
	}

#ifdef HAVE_SETLOCALE
    /*
     * Default locale
     */
    setlocale( LC_ALL, "" );
#endif

	/****************************
	 * PARSE ARGS
	 ***************************/
	for ( nArg = 1, count = 1 ; nArg < argc; nArg++ )
	{
		if ( argv[nArg][0] == '-' )
		{
			/* Options */
			switch ( argv[nArg][1] )
			{
			case 'd':
				cDelimiter = argv[nArg][2];
				break;
            case 'm':
				nUserWidth = atoi( &(argv[nArg][2]) );
				break;
            case 's':
				buffer_size = atoi( &(argv[nArg][2]) );
				break;
			case 'w':
				bHTMLTable = 1;
				break;
			case 'b':
				bBatch = 1;
				break;
			case 'c':
				bColumnNames = 1;
				break;
			case 'v':
				bVerbose = 1;
				break;
			case '-':
				printf( "unixODBC " VERSION "\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 ))
                {
                    fprintf( stderr, "isql: can't set locale to '%s'\n", argv[nArg]+2 );
                    exit ( -1 );
                }
                break;
#endif

			default:
				fprintf( stderr, szSyntax );
				exit( 1 );
			}
			continue;
		}
		else if ( count == 1 )
			szDSN = argv[nArg];
		else if ( count == 2 )
			szUID = argv[nArg];
		else if ( count == 3 )
			szPWD = argv[nArg];
		count++;
	}

    szSQL = calloc( 1, buffer_size + 1 );

	/****************************
	 * CONNECT
	 ***************************/
	
	if ( !OpenDatabase( &hEnv, &hDbc, szDSN, szUID, szPWD ) )
		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" );
	}
	do
	{
		if ( !bBatch )
#ifndef HAVE_READLINE
			printf( "SQL> " );
#else
		{
			char *line;
            int malloced;

			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;

		    line = fgets( szSQL, buffer_size, stdin );
            if ( !line )        /* EOF - ctrl D */
            {
                malloced = 1;
                line = 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 ( strncmp( szSQL, "quit", 4 ) == 0 )
				szSQL[1] = '\0';
			else if ( strncmp( szSQL, "help", 4 ) == 0 )
				ExecuteHelp( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
			else if (memcmp(szSQL, "--", 2) != 0) 
				ExecuteSQL( hDbc, szSQL, cDelimiter, bColumnNames, bHTMLTable );
		}

	} while ( szSQL[1] != '\0' );