c******************************************************************************
c* filename:	demo1.f
c*
c* purpose:	sample program to read from one class/database.  the database/
c*		class and host will be specified by the user.
c*
c* user input: (note: enter the following input in the file demo1.com)
c*
c*					max length	examples
c*
c*	1) program filename 		16 characters	(demo1)
c*	2) database name		20 chars	(demo)
c*	3) database host name		30 chars	(aeolus.s2k.ucsd.edu)
c*	4) max no. of recs to output for 1st query	(20)
c*
c* directory:	/kidlat1/users/bainto/GDIDEMO/demo1.f
c* date: 	8/17/93 -- ecv-b
c*******************************************************************************

	program demo1

      	include 	'/POSTGRES/gdi/include/gdi_f77_refdefs.h'

C..... define local variables

c..................  CONNECT TO DATABASE ..................

      	character*20  	vendor, database1, database2, na
      	character*16  	prgnam
      	character*30  	dbhost1, dbhost2
      	integer       	dbconn

c..................  ERROR HANDLING vARIABLES .............

      	character*80  	errtxt
      	integer       	maxtxt, status, severity, errcde

c..................  QUERY VARIABLES ......................

      	integer*4     	map_id
      	character*300 	query
      	integer       	maxrecs, rows_retrieved, rows_affected, 
     &        		rows_left
      	logical*4     	more_data

	integer*2	maxtemp(30), mintemp(30), precip(30), 
     &                  snowfall(30), evap(30)
	integer*4	date(30)
	character	ncdcstn(30)*8

c........................ b e g i n	p r o g r a m ..........................

c..... initialize some variables

      	vendor = 'postgres'

c..... user input 

 50	format(a)

	write(*,*) '  enter program name w/o extension (max: 16 chars)'        
	read(*,50) prgnam
	write(*,50) prgnam

	write(*,*) '  enter database name (max: 20 characters)'
	read(*,50) database1
	write(*,50) database1

	write(*,*) '  enter database host name (max: 30 characters)'
	read(*,50) dbhost1
	write(*,50) dbhost1

	write(*,*) '  enter maximum no. of records to output'
	read(*,*)  maxrecs
	write(*,*) maxrecs

C..... Some GDI_OPEN arguments are Not Applicable (NA) to POSTGRES

      	na = ' '

      	maxtxt = 80					! max text length

C.................  Initialize the GDI. ..........................

      	status = gdi_init(prgnam)
       	if (status .ne. gdi_success) then
          write(*,*) 'GDI_INIT Failed.  Program exiting.'
          goto 999
      	endif

C............ Open a connection to the first database. ...................

      	dbconn = gdi_open(vendor, na, na, database1, dbhost1, prgnam)

      	if (dbconn .eq. gdi_noconn) then
          call gdi_error_get (dbconn, errcde, errtxt, maxtxt, 
     +                        status, severity)
          write(*,*) 'GDI_OPEN Failed: Error Code ', errcde
          write(*,*) errtxt
          goto 999
      	endif

C..... Set debug and threshold flags. GDI_DEBUG_ON prints errors to the screen.

      	call gdi_error_init (dbconn, gdi_debug_on, gdi_warning, 
     +                       reserved1, reserved2)

C...............  Build a query .........................

      	write (query, 5000)
 5000 	format('retrieve (m.all) from m in ca10clim where m.ncdcstn =',        
     &         ' \"04471302\" and m.precip >= 600 sort by date')

C...........  Create a query mapping ...............................

       	map_id = gdi_open_map(dbconn)
      	if (map_id .eq. gdi_nomap) then
          goto 999
      	endif

C..... Map each attribute being retrieved to a FORTRAN variable or array
c..... (array for multiple records)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'ncdcstn', ncdcstn, GDI_STRING, 8, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'date', date, GDI_INT4, 0, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'maxtemp', maxtemp, GDI_INT2, 0, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'mintemp', mintemp, GDI_INT2, 0, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'precip', precip, GDI_INT2, 0, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'snowfall', snowfall, GDI_INT2, 0, 0)

      	status = gdi_add_map_field(dbconn, map_id, 
     +           'evap', evap, GDI_INT2, 0, 0)

      	if (status .ne. gdi_success) then
          goto 999
      	endif

c..... close mapping

      	call gdi_close_map(dbconn, map_id)

C................ Execute the query on the first database ...........

      	status = gdi_submit(dbconn, map_id, query, maxrecs,
     +           rows_retrieved, rows_affected, more_data)

        if (status .ne. gdi_success) then
          goto 999
      	endif

C................ Print out the retrieved data. .....................

      	write(*,*) '  found ',rows_retrieved,' records:'

      	do 10 i = 1, rows_retrieved
          write(*,*) ncdcstn(i), date(i), precip(i)
 10   	continue
      
      	write(*,*) ' '

      	if (more_data) then
          rows_left = rows_affected - rows_retrieved
          write(*,*) rows_left, ' more rows are available.'
      	endif

C................    Destroy query mapping.   ..........................

      	call gdi_destroy_map(dbconn, map_id)

c..... close connection to the database

 999  	status = gdi_close(dbconn)
   
	stop
	end
