" *****************************************************************************************************
*
*   Odbcqry.txt
*
*   This examples shows how to use the class OdbcQuery
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString sqlStatement answerSet query1|

aTextDisplay := TextWindow  windowLabeled: 'ODBC Interface - Using the class OdbcQuery'
                           frame: (0 @ 0 corner: 500 @ 500).
aTextDisplay cr.



" *** Open the connection ****"
connection1 := OdbcConnection open.
connection1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: connection1 getMessage; cr
    ].



" *** Driver  Connect ***  "
connectString :=  connection1 driverConnect: '' window: aTextDisplay
        driverCompletion: (OdbcDriverCompletions at: 'DRIVERPROMPT').

connection1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: connection1 getMessage; cr
    ].

sqlStatement:= Prompter prompt: 'Please Enter a sqlStatement:' default: ''.


"*** open the query***"
query1 := connection1 openQuery: sqlStatement.

answerSet := query1 executeSql.
query1  hasError
    ifTrue:
    [    aTextDisplay nextPutAll: query1 getMessage; cr
    ]
    ifFalse:
    [
        " *** display the column names ***"
        1 to: query1 numberOfColumns do:
            [ :c |
                aTextDisplay nextPutAll: (query1 colName: c) ; tab ].
                aTextDisplay cr.

                " *** display the table values "
             answerSet do:
            [ :r |
                1 to: query1 numberOfColumns do:
                 [ :c |
                        aTextDisplay nextPutAll: (r at: c) asString; tab ].
                 aTextDisplay cr.
            ].
    ].

" *** disconnect *** "
connection1 disconnect.
connection1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: connection1 getMessage; cr
    ].


" *** free the connection *** "

connection1 free.
connection1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: connection1 getMessage; cr
    ].


aTextDisplay cr ; nextPutAll: '*** finished executing ***'.
