LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/sql - describe.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 112 128 87.5 %
Date: 2024-12-12 17:14:55 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <stdlib.h>
       2             : #include <string.h>
       3             : 
       4             : exec sql include ../regression;
       5             : exec sql include sqlda.h;
       6             : 
       7             : exec sql whenever sqlerror stop;
       8             : 
       9             : sqlda_t *sqlda1, *sqlda2, *sqlda3;
      10             : 
      11             : int
      12           4 : main (void)
      13             : {
      14             : exec sql begin declare section;
      15           4 :     char    *stmt1 = "SELECT id, t FROM descr_t2";
      16           4 :     char    *stmt2 = "SELECT id, t FROM descr_t2 WHERE id = -1";
      17             :     int i, count1, count2;
      18           4 :     char    field_name1[30] = "not set";
      19           4 :     char    field_name2[30] = "not set";
      20             : exec sql end declare section;
      21             : 
      22             :     char msg[128];
      23             : 
      24           4 :     ECPGdebug(1, stderr);
      25             : 
      26           4 :     strcpy(msg, "connect");
      27           4 :     exec sql connect to REGRESSDB1;
      28           4 : 
      29           4 :     strcpy(msg, "set");
      30           4 :     exec sql set datestyle to iso;
      31           4 : 
      32           4 :     strcpy(msg, "create");
      33           4 :     exec sql create table descr_t2(id serial primary key, t text);
      34           4 : 
      35           4 :     strcpy(msg, "insert");
      36           4 :     exec sql insert into descr_t2(id, t) values (default, 'a');
      37           4 :     exec sql insert into descr_t2(id, t) values (default, 'b');
      38           4 :     exec sql insert into descr_t2(id, t) values (default, 'c');
      39           4 :     exec sql insert into descr_t2(id, t) values (default, 'd');
      40           4 : 
      41           4 :     strcpy(msg, "commit");
      42           4 :     exec sql commit;
      43           4 : 
      44             :     /*
      45             :      * Test DESCRIBE with a query producing tuples.
      46             :      * DESCRIPTOR and SQL DESCRIPTOR are NOT the same in
      47             :      * Informix-compat mode.
      48             :      */
      49             : 
      50           4 :     strcpy(msg, "allocate");
      51           4 :     exec sql allocate descriptor desc1;
      52           4 :     exec sql allocate descriptor desc2;
      53           4 : 
      54           4 :     strcpy(msg, "prepare");
      55           4 :     exec sql prepare st_id1 FROM :stmt1;
      56           4 : 
      57           4 :     sqlda1 = sqlda2 = sqlda3 = NULL;
      58             : 
      59           4 :     strcpy(msg, "describe");
      60           4 :     exec sql describe st_id1 into sql descriptor desc1;
      61           4 :     exec sql describe st_id1 using sql descriptor desc2;
      62             : 
      63           4 :     exec sql describe st_id1 into descriptor sqlda1;
      64           4 :     exec sql describe st_id1 using descriptor sqlda2;
      65           4 :     exec sql describe st_id1 into sqlda3;
      66             : 
      67           4 :     if (sqlda1 == NULL)
      68             :     {
      69           0 :         printf("sqlda1 NULL\n");
      70           0 :         exit(1);
      71             :     }
      72             : 
      73           4 :     if (sqlda2 == NULL)
      74             :     {
      75           0 :         printf("sqlda2 NULL\n");
      76           0 :         exit(1);
      77             :     }
      78             : 
      79           4 :     if (sqlda3 == NULL)
      80             :     {
      81           0 :         printf("sqlda3 NULL\n");
      82           0 :         exit(1);
      83             :     }
      84             : 
      85           4 :     strcpy(msg, "get descriptor");
      86           4 :     exec sql get descriptor desc1 :count1 = count;
      87           4 :     exec sql get descriptor desc1 :count2 = count;
      88           4 : 
      89           4 :     if (count1 != count2)
      90             :     {
      91           0 :         printf("count1 (%d) != count2 (%d)\n", count1, count2);
      92           0 :         exit(1);
      93             :     }
      94             : 
      95           4 :     if (count1 != sqlda1->sqld)
      96             :     {
      97           0 :         printf("count1 (%d) != sqlda1->sqld (%d)\n", count1, sqlda1->sqld);
      98           0 :         exit(1);
      99             :     }
     100             : 
     101           4 :     if (count1 != sqlda2->sqld)
     102             :     {
     103           0 :         printf("count1 (%d) != sqlda2->sqld (%d)\n", count1, sqlda2->sqld);
     104           0 :         exit(1);
     105             :     }
     106             : 
     107           4 :     if (count1 != sqlda3->sqld)
     108             :     {
     109           0 :         printf("count1 (%d) != sqlda3->sqld (%d)\n", count1, sqlda3->sqld);
     110           0 :         exit(1);
     111             :     }
     112             : 
     113          12 :     for (i = 1; i <= count1; i++)
     114             :     {
     115           8 :         exec sql get descriptor desc1 value :i :field_name1 = name;
     116           8 :         exec sql get descriptor desc2 value :i :field_name2 = name;
     117           8 :         printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
     118             :             "sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
     119             :             i, field_name1, field_name2,
     120           8 :             sqlda1->sqlvar[i-1].sqlname.data,
     121           8 :             sqlda2->sqlvar[i-1].sqlname.data,
     122           8 :             sqlda3->sqlvar[i-1].sqlname.data);
     123             :     }
     124             : 
     125           4 :     strcpy(msg, "deallocate");
     126           4 :     exec sql deallocate descriptor desc1;
     127           4 :     exec sql deallocate descriptor desc2;
     128           4 :     free(sqlda1);
     129           4 :     free(sqlda2);
     130           4 :     free(sqlda3);
     131             : 
     132           4 :     exec sql deallocate prepare st_id1;
     133           4 : 
     134             :     /* Test DESCRIBE with a query not producing tuples */
     135             : 
     136           4 :     strcpy(msg, "allocate");
     137           4 :     exec sql allocate descriptor desc1;
     138           4 :     exec sql allocate descriptor desc2;
     139           4 : 
     140           4 :     strcpy(msg, "prepare");
     141           4 :     exec sql prepare st_id2 FROM :stmt2;
     142           4 : 
     143           4 :     sqlda1 = sqlda2 = sqlda3 = NULL;
     144             : 
     145           4 :     strcpy(msg, "describe");
     146           4 :     exec sql describe st_id2 into sql descriptor desc1;
     147           4 :     exec sql describe st_id2 using sql descriptor desc2;
     148             : 
     149           4 :     exec sql describe st_id2 into descriptor sqlda1;
     150           4 :     exec sql describe st_id2 using descriptor sqlda2;
     151           4 :     exec sql describe st_id2 into sqlda3;
     152             : 
     153           4 :     if (sqlda1 == NULL || sqlda2 == NULL || sqlda3 == NULL)
     154           0 :         exit(1);
     155             : 
     156           4 :     strcpy(msg, "get descriptor");
     157           4 :     exec sql get descriptor desc1 :count1 = count;
     158           4 :     exec sql get descriptor desc1 :count2 = count;
     159           4 : 
     160           4 :     if (!(  count1 == count2 &&
     161           4 :         count1 == sqlda1->sqld &&
     162           4 :         count1 == sqlda2->sqld &&
     163           4 :         count1 == sqlda3->sqld))
     164           0 :         exit(1);
     165             : 
     166          12 :     for (i = 1; i <= count1; i++)
     167             :     {
     168           8 :         exec sql get descriptor desc1 value :i :field_name1 = name;
     169           8 :         exec sql get descriptor desc2 value :i :field_name2 = name;
     170           8 :         printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
     171             :             "sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
     172             :             i, field_name1, field_name2,
     173           8 :             sqlda1->sqlvar[i-1].sqlname.data,
     174           8 :             sqlda2->sqlvar[i-1].sqlname.data,
     175           8 :             sqlda3->sqlvar[i-1].sqlname.data);
     176             :     }
     177             : 
     178           4 :     strcpy(msg, "deallocate");
     179           4 :     exec sql deallocate descriptor desc1;
     180           4 :     exec sql deallocate descriptor desc2;
     181           4 :     free(sqlda1);
     182           4 :     free(sqlda2);
     183           4 :     free(sqlda3);
     184             : 
     185           4 :     exec sql deallocate prepare st_id2;
     186           4 : 
     187             :     /* End test */
     188             : 
     189           4 :     strcpy(msg, "drop");
     190           4 :     exec sql drop table descr_t2;
     191           4 : 
     192           4 :     strcpy(msg, "commit");
     193           4 :     exec sql commit;
     194           4 : 
     195           4 :     strcpy(msg, "disconnect");
     196           4 :     exec sql disconnect;
     197           4 : 
     198           4 :     return 0;
     199             : }

Generated by: LCOV version 1.14