" *****************************************************************************************************
*
*  bindint2.txt
*
*   This examples shows how to use setparameter with an Integer or (smallint) data type
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement numParms parmValue
    parmSqlType|

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

" set autocommit on"
connection1 setOption: 102 value: 1.
connection1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: connection1 getMessage; cr
    ].



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

" testinteger has 1 column
   create table testsmallint (c1 smallint) "

sqlStatement:= 'insert into testsmallint (c1) values(?)'.


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

" bind the parameter - here we use the default type of class Integer "
statement1 bindInputParameter: 1 fSqlType: Integer fSqlType  .
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].

" set the parameter values and execute the statement"
1 to: 100 do: [ :i | 
    statement1 setParameter: 1 value: i. 

    "*** execute the statement  ***"
    statement1 execute.
    statement1 hasSuccess
        ifFalse:
        [    aTextDisplay nextPutAll: statement1 getMessage; cr
        ].
].


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

statement1 drop.
statement1 hasError
    ifTrue:
    [    aTextDisplay nextPutAll: statement1 getMessage; cr
    ].


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



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


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