c NAME
c	tst_str_array
c 
c FILE 
c	tst_str_array.f
c
c SYNOPSIS
c       test the postgres array string data type
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 /'@(#)tst_str_arrays.f	1.1 8/17/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, 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  STR2_NAMES(10,20)
      CHARACTER*50  STR2_TEXT(10,20)

      INTEGER       I

      VENDOR = 'postgres'
      DATABASE = 'jta'
      PRGNAM = 'tst_str_arrays'

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

      DO 3 I = 1, 20
         DO 4 J = 1, 10
            STR2_NAMES(I,J) = "aaaaaaaaaaaaaaaa"
            STR2_TEXT(I,J)   = "AAAAAAAAAAAAAAAA"
 4       CONTINUE
 3    CONTINUE

      MAXRECS = 10
      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 =========================
      QUERY = "retrieve (test_strings2.all)"


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, 
     &     'str2_names', STR2_NAMES, GDI_STRING, 16, 10)
      IF (STATUS .NE. GDI_SUCCESS) THEN
         GOTO 999
      END IF


      STATUS = GDI_ADD_MAP_FIELD(DBCONN, MAP_ID, 
     +     'str2_text', STR2_TEXT, GDI_STRING, 50, 10)
      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
         DO 5 J = 1, 10
            WRITE (6,*) STR2_NAMES(J,I),'  ', STR2_TEXT(J,I)
 5       CONTINUE
            WRITE (6,*) ' '
            WRITE (6,*) ' '
 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
