" *****************************************************************************************************
*
*  colattri.txt
*
*   This examples shows how to use colAttributes
*
*
******************************************************************************************************"

|connection1  aTextDisplay connectString statement1 sqlStatement colAttributes|

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


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

"*** these are already defined in ODBCConstants ***"
colAttributes := Dictionary new.
colAttributes
at:  'SQLCOLUMNCOUNT'    put:        0
;at: 'SQLCOLUMNNAME'      put:       1
;at: 'SQLCOLUMNTYPE'       put:      2
;at: 'SQLCOLUMNLENGTH'  put:         3
;at: 'SQLCOLUMNPRECISION'   put:     4
;at: 'SQLCOLUMNSCALE'          put:  5
;at: 'SQLCOLUMNDISPLAYSIZE'  put:   6
;at: 'SQLCOLUMNNULLABLE'      put:   7
;at: 'SQLCOLUMNUNSIGNED'      put:   8
;at: 'SQLCOLUMNMONEY'          put:  9
;at: 'SQLCOLUMNUPDATABLE'    put:  10
;at: 'SQLCOLUMNAUTOINCREMENT' put:  11
;at: 'SQLCOLUMNCASESENSITIVE' put: 12
;at: 'SQLCOLUMNSEARCHABLE'     put: 13
;at: 'SQLCOLUMNTYPENAME'      put: 14
.

"*** display the colAttributes ***"
1 to: statement1 numberOfColumns do:
    [ :icol |
        aTextDisplay nextPutAll: 'Column: '; nextPutAll: icol asString; cr.
        colAttributes associationsDo:
                [ :a |
                    aTextDisplay nextPutAll: (a key) ; tab
                             ;nextPutAll:  (statement1 colAttributes: icol fDescType: ( a value)) asString
                             ; cr.
                ]
      ].
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 ***'.
