c NAME
c	gdi_f77_array
c 
c FILE 
c	gdi_f77_array.f
c
c SYNOPSIS
c       test the postgres array data types
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 /'%W% %G%'/

      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, 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

C     ==================  Output variables ============================
      CHARACTER*16  RELNAME(10)
      INTEGER*4     INDRELID(10)
      INTEGER*2     INDKEY (8,10)
      INTEGER*4     INDCLASS (8,10)

      INTEGER       I

      VENDOR = 'postgres'
      DATABASE = 'jta'
      PRGNAM = 'gdi_f77_array'

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
         CALL GDI_ERROR_GET(DBCONN, ERRCDE, ERRTXT, MAXTXT, 
     +        STATUS, SEVERITY)
         WRITE (6,*) 'GDI_OPEN Failed: 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 =========================
      WRITE (QUERY, 5000)
 5000 FORMAT('retrieve (r1.relname, i.indrelid, i.indkey, i.indclass) ',
     &     'from r1 in pg_class, r2 in pg_class, i in pg_index ',
     &     'where r1.oid = i.indexrelid and r2.oid = i.indrelid ',
     &     'and r1.relkind = "i"',
     &     'and r2.relname="pg_attribute" ')


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.
c     or array in this case.

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


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

c     one element is 8 INT*4's
      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'indkey', INDKEY, GDI_INT2, 0, 8)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF

      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'indclass', INDCLASS, GDI_INT4, 0, 8)
      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_AFFECTED, ' rows satisfied the query.'
      WRITE (6,*) ROWS_RETRIEVED, ' rows were retrieved.'
      DO 10 I = 1, ROWS_RETRIEVED
         WRITE (6,*) RELNAME(I), INDRELID(I),
     &    INDKEY(1,I), INDKEY(2,I), INDKEY(3,I), INDKEY(4,I),
     &    INDKEY(5,I), INDKEY(6,I), INDKEY(7,I), INDKEY(8,I),
     &    INDCLASS(1,I), INDCLASS(2,I), INDCLASS(3,I), INDCLASS(4,I),
     &    INDCLASS(5,I), INDCLASS(6,I), INDCLASS(7,I), INDCLASS(8,I)
         
 10   CONTINUE
      
      IF (MORE_DATA) THEN
         ROWS_LEFT = ROWS_AFFECTED - ROWS_RETRIEVED
         WRITE (6,*) ROWS_LEFT, ' 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
