NB. Text/CSV ODBC examples
NB.
NB. See odbc.txt for general comments on these scripts
NB.
NB. requires ODBC setup of:
NB.   data source name:  jtext
NB.   data source:       system\examples\data
NB.
NB. The text/csv driver connects to a subdirectory. The specific
NB. file used here is system\examples\data\tdata.csv.
NB.
NB. verbs:
NB.   txconnect    connect to text driver
NB.   txcolumns    columns of tdata
NB.   txreadsel    read a selection from tdata
NB.
NB.   demo         run a demo
NB.
NB. (There is no update verb.)

require 'dd'

demo=: 3 : 0
out=. 1!:2 & 2
empty txconnect''
out 'columns:',": txcolumns''
out 'read all:',": txreadall''
out 'read selection:', ": txreadsel''
empty dddis CH
)

NB. =====================================================
NB. txconnect
NB. connect to text driver,
NB. define and return connection handle CH
txconnect=: 3 : 0
CH=: ddcheck ddcon 'dsn=jtext'
)

NB. =====================================================
NB. txcolumns
NB. read columns of tdata
txcolumns=: 3 : 0
ddcheck 'tdata.csv' ddcol CH
)

NB. =====================================================
NB. txreadall
NB. read all tdata
txreadall=: 3 : 0
sel=. 'select * from tdata.csv'
sh=. ddcheck sel ddsel CH
if. #sh do.
  r=. ddfch sh,_1
  ddend sh
  r
end.
)

NB. =====================================================
NB. txreadsel
NB. read a selection from tdata
txreadsel=: 3 : 0
sel=. 'select * from tdata.csv where sex=''M'''
sh=. ddcheck sel ddsel CH
if. #sh do.
  r=. ddfch sh,_1
  ddend sh
  r
end.
)
