c NAME
c	gdi_f77_test
c 
c FILE 
c	gdi_f77_test.f
c
c SYNOPSIS
c       test the FORTRAN GDI interface
c
c DESCRIPTION
c     This file contains the test driver for the FORTRAN interface to the GDI.
c     
c AUTHOR
c	Howard Turner
c
c     ===============  Sample POSTGRES program =====================

      common /sccsfilename/ sccsid
      character*80 sccsid
      data sccsid /'@(#)gdi_f77_pg_test.f	1.4	7/7/93'/

      include '../../include/gdi_f77_refdefs.h'

C     define local variables

C     ==================  Connect to database ==================
      CHARACTER*10  VENDOR, DATABASE, NA
      CHARACTER*16  PRGNAM
      INTEGER       DBCONN

C     ==================  Error Handling Variables =============
      CHARACTER*80  ERRTXT
      INTEGER       MAXTXT
      INTEGER       STATUS, SEVERITY, ERRCDE

C     ==================  Query variables ======================
      INTEGER*4     MAP_ID
      CHARACTER*80  QUERY
      INTEGER       MAXRECS, ROWS_RETRIEVED, ROWS_AFFECTED
      LOGICAL*4     MORE_DATA

C     ==================  Output variables =====================
      REAL*8        TIME(20)
      INTEGER       NSAMP(20)
      CHARACTER*16  STA(20)
      INTEGER       I

      VENDOR = 'postgres'
      DATABASE = 'jta'
      PRGNAM = 'gdi_f77_pg_test'

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

      MAXRECS = 20
      MAXTXT = 80

C     Initialize the GDI.

      STATUS = GDI_INIT(PRGNAM)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         WRITE (6,*) 'GDI_INIT Failed.  Program exiting.'
         GOTO 999
      END IF

C     Open a connection to the database.

      DBCONN = GDI_OPEN(VENDOR, NA, NA, DATABASE, NA, PRGNAM)

      IF (DBCONN .EQ. GDI_NOCONN) THEN
         WRITE (6,*) 'GDI_OPEN Failed.  Program exiting.'
         CALL GDI_ERROR_GET(DBCONN, ERRCDE, ERRTXT, MAXTXT, 
     +        STATUS, SEVERITY)
         WRITE (6,*) 'Severity ',SEVERITY, '  Stat ', STATUS, 
     +               '  Error Code ', ERRCDE
         WRITE (6,*) ERRTXT
         GOTO 999
      END IF

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.
      QUERY = 'retrieve (w.time, w.nsamp, w.sta) from w in wfdisc'

C     Create a query mapping
      MAP_ID = GDI_OPEN_MAP(DBCONN)
      IF (MAP_ID .EQ. GDI_NOMAP) THEN
         GOTO 999
      END IF

C     Map each attribute being retrieved to a FORTRAN variable.

      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'time', TIME, GDI_REAL8, 0, 0)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF

      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'nsamp', NSAMP, GDI_INT4, 0, 0)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF

      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'sta', STA, GDI_STRING, 16, 0)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF

      CALL GDI_CLOSE_MAP(DBCONN, MAP_ID)

C     Execute the query
      STATUS = GDI_SUBMIT(DBCONN, MAP_ID, QUERY, MAXRECS,
     +     ROWS_RETRIEVED, ROWS_AFFECTED, MORE_DATA)

      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF

C     Print out the retrieved data.
      WRITE (6,*) ROWS_RETRIEVED, ' rows were retrieved.'
      DO 10 I = 1, ROWS_RETRIEVED
         WRITE (6,*) STA(I), TIME(I), NSAMP(I)
 10   CONTINUE

      IF (MORE_DATA) THEN
         WRITE (6,*) 'More rows are available.'
      ELSE
         WRITE (6,*) 'No more data exists in the database.'
      END IF

C     Destroy query mapping.
      CALL GDI_DESTROY_MAP(DBCONN, MAP_ID)

 999  STATUS = GDI_CLOSE(DBCONN)
      
      END
