" *****************************************************************************************************
*
*  binddef.txt
*
*   This examples shows how to use bindColumnsToDefault
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement|

aTextDisplay := TextWindow  windowLabeled: 'ODBC Interface - Using bindColumns'
                           frame: (0 @ 0 corner: 1000 @ 1000).
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
    ].

" *** allocate a new statement *** "
statement1:= connection1 newStatement.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


sqlStatement:= Prompter prompt: 'Please Enter a select SQL statement:' default: ''.


"*** execute the statement directly  ***"
statement1 executeDirect: sqlStatement.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


"*** bind the columns - could be done after prepare or Execute  ***"
aTextDisplay nextPutAll: 'NumberOfColumns: '
                        ; nextPutAll: (statement1 bindColumnsToDefault asString); cr.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].

"*** fetch the rows and display the values ***"   

statement1 fetch.
   [statement1 hasSuccess]
        whileTrue: [
            1 to: statement1 numberOfColumns do:
                [ :icol |
                        aTextDisplay nextPutAll: (statement1 colValue: icol) asString; tab
                ].
             aTextDisplay cr.
             statement1 fetch.  
      ].
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


"*** free the statement *** "
statement1 drop.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; 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 ***'. 
