" *****************************************************************************************************
*
*   Odbctbl.txt
*
*   This examples shows how to use the class OdbcTable
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString tableQualifier tableOwner tableName table1 
    answerSet |

aTextDisplay := TextWindow  windowLabeled: 'ODBC Interface - Using the class OdbcTable'
                           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
    ].

tableQualifier:= Prompter prompt: 'Please Enter a table qualifier:' default: ''.
tableOwner:= Prompter prompt: 'Please Enter a table owner:' default: ''.
tableName := Prompter prompt: 'Please Enter a table name:' default: ''.


"*** open the table ***"
table1 := connection1 openTable: tableName
                    qualifier: tableQualifier
                    owner: tableOwner.

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

                " *** display the table values "
             answerSet do:
            [ :r |
                1 to: table1 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 ***'.
