" *****************************************************************************************************
*
*  putdata.txt
*
*   This examples shows how to use putdata
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement numParms parmValue
    parmSqlType appValue|

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

"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
    ].

sqlStatement:= 'insert into testinteger values(?)' .

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


 statement1 setParamAtExec: 1 fSqlType: SmallInteger fSqlType .
statement1 hasError
    ifTrue:
        [    aTextDisplay nextPutAll: statement1 getMessage; cr
        ].


"*** execute the statement  ***"
1 to: 20 do: [ :i |
    statement1 execute.
    statement1 hasError
        ifFalse:
        [    aTextDisplay nextPutAll: statement1 getMessage; cr
     ].

    " We should get a return of SQLNEEDDATA "
    statement1 hasSqlNeedData
        ifTrue: [
            statement1 paramData.
             [statement1 hasSqlNeedData]
                whileTrue:[
                    parmValue := 1000+i.
                statement1 putData: parmValue.         
                    statement1 hasError
    ifTrue:
     [    aTextDisplay nextPutAll: statement1 getMessage; cr
        ].
                statement1 paramData.
                ].
    ].

     statement1 hasError
    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 ***'.
