" *****************************************************************************************************
*
*  extdef.txt
*
*   This examples shows how to use extendedFetch using bindColumnsToDefault
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement numRows numCols value|

aTextDisplay := TextWindow  windowLabeled: 'ODBC Interface - Using Extended Fetch'
                           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 that supports extended fetches*** "
statement1:= connection1 newStatement.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].

"*** change the Scroll Options ***"
statement1 setScrollOptions: 1
                                                crowKeySet:  0
                                                crowRowSet:3.
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  ***"
numCols :=  statement1 bindColumns.
aTextDisplay nextPutAll: 'NumberOfColumns: '
                        ; nextPutAll: numCols asString; cr.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


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

aTextDisplay nextPutAll:  'Number of rows fetched in Extended Fetch: '.
numRows:= statement1 extendedFetch: (OdbcfFetchTypes at: 'FETCHNEXT') irow: 1.
aTextDisplay nextPutAll: (numRows asString)
                    ; cr.
 [statement1 hasSuccess or: [statement1 hasSuccessWithInfo]   ]
    whileTrue: [
           1 to: numRows   do:
        [ :irow |
                aTextDisplay nextPutAll: 'Row Status: '
                                    ; nextPutAll: (statement1 rowStatus: irow) asString
                                    ; tab.
                1 to: numCols do:
                [ :icol |
                    value := statement1 colValue: icol at: irow.
                    aTextDisplay nextPutAll: 'Class: ', (value class) asString
                                        ; nextPutAll: value asString
                                        ; tab.
                ].
             aTextDisplay cr
        ].
    aTextDisplay nextPutAll:  'Number of rows fetched in Extended Fetch: '.
    numRows:= statement1 extendedFetch: (OdbcfFetchTypes at: 'FETCHNEXT') irow: 1.
    aTextDisplay nextPutAll: (numRows asString)
        ;cr

   ].

( statement1 hasError or: [ statement1 hasSuccessWithInfo ])
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


"   *** free the statement *** "
statement1 free: (OdbcConstants at: 'SQLDROP').
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 ***'.
