LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/sql - sqlda.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 96.1 % 152 146
Test Date: 2026-07-25 10:15:40 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 57.5 % 73 42

             Branch data     Line data    Source code
       1                 :             : #include <stdlib.h>
       2                 :             : #include <string.h>
       3                 :             : #include <limits.h>
       4                 :             : #include "ecpg_config.h"
       5                 :             : 
       6                 :             : exec sql include ../regression;
       7                 :             : exec sql include sqlda.h;
       8                 :             : exec sql include pgtypes_numeric.h;
       9                 :             : 
      10                 :             : exec sql whenever sqlerror stop;
      11                 :             : 
      12                 :             : /* These shouldn't be under DECLARE SECTION */
      13                 :             : sqlda_t *inp_sqlda, *outp_sqlda, *outp_sqlda1;
      14                 :             : 
      15                 :             : static void
      16                 :          24 : dump_sqlda(sqlda_t *sqlda)
      17                 :             : {
      18                 :             :     int i;
      19                 :             : 
      20         [ -  + ]:          24 :     if (sqlda == NULL)
      21                 :             :     {
      22                 :           0 :         printf("dump_sqlda called with NULL sqlda\n");
      23                 :           0 :         return;
      24                 :             :     }
      25                 :             : 
      26         [ +  + ]:         168 :     for (i = 0; i < sqlda->sqld; i++)
      27                 :             :     {
      28   [ +  -  +  + ]:         144 :         if (sqlda->sqlvar[i].sqlind && *(sqlda->sqlvar[i].sqlind) == -1)
      29                 :          20 :             printf("name sqlda descriptor: '%s' value NULL'\n", sqlda->sqlvar[i].sqlname.data);
      30                 :             :         else
      31   [ +  +  +  -  :         124 :         switch (sqlda->sqlvar[i].sqltype)
                +  +  - ]
      32                 :             :         {
      33                 :          40 :         case ECPGt_char:
      34                 :          40 :             printf("name sqlda descriptor: '%s' value '%s'\n", sqlda->sqlvar[i].sqlname.data, sqlda->sqlvar[i].sqldata);
      35                 :          40 :             break;
      36                 :          24 :         case ECPGt_int:
      37                 :          24 :             printf("name sqlda descriptor: '%s' value %d\n", sqlda->sqlvar[i].sqlname.data, *(int *)sqlda->sqlvar[i].sqldata);
      38                 :          24 :             break;
      39                 :          20 :         case ECPGt_long:
      40                 :          20 :             printf("name sqlda descriptor: '%s' value %ld\n", sqlda->sqlvar[i].sqlname.data, *(long int *)sqlda->sqlvar[i].sqldata);
      41                 :          20 :             break;
      42                 :           0 :         case ECPGt_long_long:
      43                 :           0 :             printf("name sqlda descriptor: '%s' value %lld\n",
      44                 :           0 :                    sqlda->sqlvar[i].sqlname.data, *(long long int *)sqlda->sqlvar[i].sqldata);
      45                 :           0 :             break;
      46                 :          20 :         case ECPGt_double:
      47                 :          20 :             printf("name sqlda descriptor: '%s' value %f\n", sqlda->sqlvar[i].sqlname.data, *(double *)sqlda->sqlvar[i].sqldata);
      48                 :          20 :             break;
      49                 :          20 :         case ECPGt_numeric:
      50                 :             :             {
      51                 :             :                 char    *val;
      52                 :             : 
      53                 :          20 :                 val = PGTYPESnumeric_to_asc((numeric*)sqlda->sqlvar[i].sqldata, -1);
      54                 :          20 :                 printf("name sqlda descriptor: '%s' value NUMERIC '%s'\n", sqlda->sqlvar[i].sqlname.data, val);
      55                 :          20 :                 PGTYPESchar_free(val);
      56                 :          20 :                 break;
      57                 :             :             }
      58                 :             :         }
      59                 :             :     }
      60                 :             : }
      61                 :             : 
      62                 :             : int
      63                 :           2 : main (void)
      64                 :             : {
      65                 :             : exec sql begin declare section;
      66                 :           2 :     char    *stmt1 = "SELECT * FROM t1";
      67                 :           2 :     char    *stmt2 = "SELECT * FROM t1 WHERE id = ?";
      68                 :             :     int rec;
      69                 :             :     int id;
      70                 :             : exec sql end declare section;
      71                 :             : 
      72                 :             :     char msg[128];
      73                 :             : 
      74                 :           2 :     ECPGdebug(1, stderr);
      75                 :             : 
      76                 :           2 :     strcpy(msg, "connect");
      77                 :           2 :     exec sql connect to REGRESSDB1 as regress1;
      78         [ -  + ]:           2 : 
      79                 :           2 :     strcpy(msg, "set");
      80                 :           2 :     exec sql set datestyle to iso;
      81         [ -  + ]:           2 : 
      82                 :           2 :     strcpy(msg, "create");
      83                 :           2 :     exec sql create table t1(
      84                 :             :         id integer,
      85                 :             :         t text,
      86                 :             :         d1 numeric,
      87                 :             :         d2 float8,
      88                 :             :         c char(10),
      89                 :             :         big bigint
      90                 :             :         );
      91         [ -  + ]:           2 : 
      92                 :           2 :     strcpy(msg, "insert");
      93                 :           2 :     exec sql insert into t1 values
      94                 :             :         (1, 'a', 1.0, 1, 'a',1111111111111111111),
      95                 :             :         (2, null, null, null, null,null),
      96                 :             :         (3, 'c', 0.0, 3, 'c',3333333333333333333),
      97                 :             :         (4, 'd', 'NaN', 4, 'd',4444444444444444444),
      98                 :             :         (5, 'e', 0.001234, 5, 'e',5555555555555555555);
      99         [ -  + ]:           2 : 
     100                 :           2 :     strcpy(msg, "commit");
     101                 :           2 :     exec sql commit;
     102         [ -  + ]:           2 : 
     103                 :             :     /* SQLDA test for getting all records from a table */
     104                 :             : 
     105                 :           2 :     outp_sqlda = NULL;
     106                 :             : 
     107                 :           2 :     strcpy(msg, "prepare");
     108                 :           2 :     exec sql prepare st_id1 from :stmt1;
     109         [ -  + ]:           2 : 
     110                 :           2 :     strcpy(msg, "declare");
     111                 :             :     exec sql declare mycur1 cursor for st_id1;
     112                 :             : 
     113                 :           2 :     strcpy(msg, "open");
     114                 :           2 :     exec sql open mycur1;
     115         [ -  + ]:           2 : 
     116                 :             :     exec sql whenever not found do break;
     117                 :             : 
     118                 :           2 :     rec = 0;
     119                 :             :     while (1)
     120                 :             :     {
     121                 :          12 :         strcpy(msg, "fetch");
     122                 :          12 :         exec sql fetch 1 from mycur1 into descriptor outp_sqlda;
     123   [ +  +  -  + ]:          12 : 
     124                 :          10 :         printf("FETCH RECORD %d\n", ++rec);
     125                 :          10 :         dump_sqlda(outp_sqlda);
     126                 :             :     }
     127                 :             : 
     128                 :             :     exec sql whenever not found continue;
     129                 :             : 
     130                 :           2 :     strcpy(msg, "close");
     131                 :           2 :     exec sql close mycur1;
     132         [ -  + ]:           2 : 
     133                 :           2 :     strcpy(msg, "deallocate");
     134                 :           2 :     exec sql deallocate prepare st_id1;
     135         [ -  + ]:           2 : 
     136                 :           2 :     free(outp_sqlda);
     137                 :             : 
     138                 :             :     /* SQLDA test for getting ALL records into the sqlda list */
     139                 :             : 
     140                 :           2 :     outp_sqlda = NULL;
     141                 :             : 
     142                 :           2 :     strcpy(msg, "prepare");
     143                 :           2 :     exec sql prepare st_id2 from :stmt1;
     144         [ -  + ]:           2 : 
     145                 :           2 :     strcpy(msg, "declare");
     146                 :             :     exec sql declare mycur2 cursor for st_id2;
     147                 :             : 
     148                 :           2 :     strcpy(msg, "open");
     149                 :           2 :     exec sql open mycur2;
     150         [ -  + ]:           2 : 
     151                 :           2 :     strcpy(msg, "fetch");
     152                 :           2 :     exec sql fetch all from mycur2 into descriptor outp_sqlda;
     153         [ -  + ]:           2 : 
     154                 :           2 :     outp_sqlda1 = outp_sqlda;
     155                 :           2 :     rec = 0;
     156         [ +  + ]:          12 :     while (outp_sqlda1)
     157                 :             :     {
     158                 :             :         sqlda_t *ptr;
     159                 :          10 :         printf("FETCH RECORD %d\n", ++rec);
     160                 :          10 :         dump_sqlda(outp_sqlda1);
     161                 :             : 
     162                 :          10 :         ptr = outp_sqlda1;
     163                 :          10 :         outp_sqlda1 = outp_sqlda1->desc_next;
     164                 :          10 :         free(ptr);
     165                 :             :     }
     166                 :             : 
     167                 :           2 :     strcpy(msg, "close");
     168                 :           2 :     exec sql close mycur2;
     169         [ -  + ]:           2 : 
     170                 :           2 :     strcpy(msg, "deallocate");
     171                 :           2 :     exec sql deallocate prepare st_id2;
     172         [ -  + ]:           2 : 
     173                 :             :     /* SQLDA test for getting one record using an input descriptor */
     174                 :             : 
     175                 :             :     /*
     176                 :             :      * Input sqlda has to be built manually
     177                 :             :      * sqlda_t contains 1 sqlvar_t structure already.
     178                 :             :      */
     179                 :           2 :     inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t));
     180                 :           2 :     memset(inp_sqlda, 0, sizeof(sqlda_t));
     181                 :           2 :     inp_sqlda->sqln = 1;
     182                 :             : 
     183                 :           2 :     inp_sqlda->sqlvar[0].sqltype = ECPGt_int;
     184                 :           2 :     inp_sqlda->sqlvar[0].sqldata = (char *)&id;
     185                 :             : 
     186                 :           2 :     printf("EXECUTE RECORD 4\n");
     187                 :             : 
     188                 :           2 :     id = 4;
     189                 :             : 
     190                 :           2 :     outp_sqlda = NULL;
     191                 :             : 
     192                 :           2 :     strcpy(msg, "prepare");
     193                 :           2 :     exec sql prepare st_id3 FROM :stmt2;
     194         [ -  + ]:           2 : 
     195                 :           2 :     strcpy(msg, "execute");
     196                 :           2 :     exec sql execute st_id3 using descriptor inp_sqlda into descriptor outp_sqlda;
     197         [ -  + ]:           2 : 
     198                 :           2 :     dump_sqlda(outp_sqlda);
     199                 :             : 
     200                 :           2 :     strcpy(msg, "deallocate");
     201                 :           2 :     exec sql deallocate prepare st_id3;
     202         [ -  + ]:           2 : 
     203                 :           2 :     free(inp_sqlda);
     204                 :           2 :     free(outp_sqlda);
     205                 :             : 
     206                 :             :     /* SQLDA test for getting one record using an input descriptor
     207                 :             :      * on a named connection
     208                 :             :      */
     209                 :             : 
     210                 :           2 :     exec sql connect to REGRESSDB1 as con2;
     211         [ -  + ]:           2 : 
     212                 :             :     /*
     213                 :             :      * Input sqlda has to be built manually
     214                 :             :      * sqlda_t contains 1 sqlvar_t structure already.
     215                 :             :      */
     216                 :           2 :     inp_sqlda = (sqlda_t *)malloc(sizeof(sqlda_t));
     217                 :           2 :     memset(inp_sqlda, 0, sizeof(sqlda_t));
     218                 :           2 :     inp_sqlda->sqln = 1;
     219                 :             : 
     220                 :           2 :     inp_sqlda->sqlvar[0].sqltype = ECPGt_int;
     221                 :           2 :     inp_sqlda->sqlvar[0].sqldata = (char *)&id;
     222                 :             : 
     223                 :           2 :     printf("EXECUTE RECORD 4\n");
     224                 :             : 
     225                 :           2 :     id = 4;
     226                 :             : 
     227                 :           2 :     outp_sqlda = NULL;
     228                 :             : 
     229                 :           2 :     strcpy(msg, "prepare");
     230                 :           2 :     exec sql at con2 prepare st_id4 FROM :stmt2;
     231         [ -  + ]:           2 : 
     232                 :           2 :     strcpy(msg, "execute");
     233                 :           2 :     exec sql at con2 execute st_id4 using descriptor inp_sqlda into descriptor outp_sqlda;
     234         [ -  + ]:           2 : 
     235                 :           2 :     dump_sqlda(outp_sqlda);
     236                 :             : 
     237                 :           2 :     strcpy(msg, "commit");
     238                 :           2 :     exec sql at con2 commit;
     239         [ -  + ]:           2 : 
     240                 :           2 :     strcpy(msg, "deallocate");
     241                 :           2 :     exec sql deallocate prepare st_id4;
     242         [ -  + ]:           2 : 
     243                 :           2 :     free(inp_sqlda);
     244                 :           2 :     free(outp_sqlda);
     245                 :             : 
     246                 :           2 :     strcpy(msg, "disconnect");
     247                 :           2 :     exec sql disconnect con2;
     248         [ -  + ]:           2 : 
     249                 :             :     /* End test */
     250                 :             : 
     251                 :           2 :     strcpy(msg, "drop");
     252                 :           2 :     exec sql drop table t1;
     253         [ -  + ]:           2 : 
     254                 :           2 :     strcpy(msg, "commit");
     255                 :           2 :     exec sql commit;
     256         [ -  + ]:           2 : 
     257                 :           2 :     strcpy(msg, "disconnect");
     258                 :           2 :     exec sql disconnect;
     259         [ -  + ]:           2 : 
     260                 :           2 :     return 0;
     261                 :             : }
        

Generated by: LCOV version 2.0-1