" *****************************************************************************************************
*
*  ioparm3.txt
*
*   This examples shows how to set and get arrays of inout parameter
*
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement numParms parmValue
    parmSqlType pirow|

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

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

" this procedures takes an integer as input and returns the next integer"
sqlStatement:= 'call nextInt(?)'.


"*** prepare   ***"
statement1 prepare: sqlStatement.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].

"*** change the Scroll Options ***"
statement1 paramOptions: 20.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


"bind the parameter"
statement1 bindInOutParameter: 1 fSqlType: Integer fSqlType.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].

parmValue :=  Array new: 20.

" do it for the first 20 integers "
0 to: 19 do: [ :i | parmValue at: i+1 put: i ].

 "set the parameter value"
 statement1 setParameter: 1 value: parmValue.

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

pirow := statement1 pirow.

aTextDisplay nextPutAll: 'rows processed successfully: ', pirow asString ; cr.

1 to: pirow do: [ :i |
    aTextDisplay nextPutAll: 'Returned value: '
        ; nextPutAll: ( statement1 parmValue: 1 at: i) asString; cr.
].

"*** free the statement *** "
statement1 freeParamBuffer.
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 ***'.
