LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/preproc - pointer_to_struct.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 100.0 % 50 50
Test Date: 2026-07-11 14:15:42 Functions: 100.0 % 1 1
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 56.5 % 62 35

             Branch data     Line data    Source code
       1                 :             : #include <stdio.h>
       2                 :             : #include <stdlib.h>
       3                 :             : 
       4                 :             : exec sql include ../regression;
       5                 :             : 
       6                 :             : EXEC SQL WHENEVER sqlerror sqlprint;
       7                 :             : EXEC SQL WHENEVER sqlwarning sqlprint;
       8                 :             : EXEC SQL WHENEVER not found sqlprint;
       9                 :             : 
      10                 :             : EXEC SQL TYPE customer IS
      11                 :             :     struct
      12                 :             :     {
      13                 :             :         varchar name[50];
      14                 :             :         int     phone;
      15                 :             :     };
      16                 :             : 
      17                 :             : EXEC SQL TYPE cust_ind IS
      18                 :             :     struct ind
      19                 :             :     {
      20                 :             :         short   name_ind;
      21                 :             :         short   phone_ind;
      22                 :             :     };
      23                 :             : 
      24                 :             : EXEC SQL TYPE company IS
      25                 :             :     struct
      26                 :             :     {
      27                 :             :         customer *customers;
      28                 :             :     };
      29                 :             : 
      30                 :           2 : int main(void)
      31                 :             : {
      32                 :             :     EXEC SQL begin declare section;
      33                 :           2 :       customer  *custs1 = (customer *) malloc(sizeof(customer) * 10);
      34                 :           2 :       cust_ind  *inds = (cust_ind *) malloc(sizeof(cust_ind) * 10);
      35                 :             :       typedef struct
      36                 :             :       {
      37                 :             :         varchar name[50];
      38                 :             :         int     phone;
      39                 :             :       } customer2;
      40                 :           2 :       customer2  *custs2 = (customer2 *) malloc(sizeof(customer2) * 10);
      41                 :             : 
      42                 :             :       struct customer3
      43                 :             :       {
      44                 :             :         char name[50];
      45                 :             :         int     phone;
      46                 :           2 :       } *custs3 = (struct customer3 *) malloc(sizeof(struct customer3) * 10);
      47                 :             : 
      48                 :             :       struct customer4
      49                 :             :       {
      50                 :             :         varchar name[50];
      51                 :             :         int     phone;
      52                 :           2 :       } *custs4 = (struct customer4 *) malloc(sizeof(struct customer4));
      53                 :             : 
      54                 :             :       company acme;
      55                 :             : 
      56                 :             :       int r;
      57                 :             :       varchar onlyname[2][50];
      58                 :             :     EXEC SQL end declare section;
      59                 :             : 
      60                 :           2 :     ECPGdebug(1, stderr);
      61                 :             : 
      62                 :           2 :     acme.customers = (customer *) malloc(sizeof(customer) * 10);
      63                 :             : 
      64                 :           2 :     EXEC SQL connect to REGRESSDB1;
      65   [ -  +  -  + ]:           2 : 
      66                 :           2 :     EXEC SQL create table customers (c varchar(50), p int);
      67   [ -  +  -  + ]:           2 : 
      68                 :             :     /* First we'll insert some data using C variable references */
      69                 :           2 :     strcpy(custs1[0].name.arr, "John Doe");
      70                 :           2 :     custs1[0].name.len = strlen(custs1[0].name.arr);
      71                 :           2 :     custs1[0].phone = 12345;
      72                 :             : 
      73                 :           2 :     strcpy(acme.customers[1].name.arr, "Jane Doe");
      74                 :           2 :     acme.customers[1].name.len = strlen(acme.customers[1].name.arr);
      75                 :           2 :     acme.customers[1].phone = 67890;
      76                 :             : 
      77                 :           2 :     EXEC SQL insert into customers values (:custs1->name,
      78                 :             :                                            :custs1[0].phone);
      79   [ -  +  -  +  :           2 :     EXEC SQL insert into customers values (:acme.customers[1].name,
                   -  + ]
      80                 :           2 :                                            :acme.customers[1].phone);
      81   [ -  +  -  +  :           2 : 
                   -  + ]
      82                 :           2 :     /* Clear the array, to be sure reading back into it actually gets data */
      83                 :           2 :     memset(custs1, 0, sizeof(customer) * 10);
      84                 :             : 
      85                 :             :     /* Now read back the data */
      86                 :           2 :     EXEC SQL select * INTO :custs1:inds from customers limit 2;
      87   [ -  +  -  +  :           2 :     printf("custs1:\n");
                   -  + ]
      88         [ +  + ]:           6 :     for (r = 0; r < 2; r++)
      89                 :             :     {
      90                 :           4 :         printf( "name  - %s\n", custs1[r].name.arr );
      91                 :           4 :         printf( "phone - %d\n", custs1[r].phone );
      92                 :             :     }
      93                 :             : 
      94                 :           2 :     EXEC SQL select * INTO :custs2:inds from customers limit 2;
      95   [ -  +  -  +  :           2 :     printf("\ncusts2:\n");
                   -  + ]
      96         [ +  + ]:           6 :     for (r = 0; r < 2; r++)
      97                 :             :     {
      98                 :           4 :         printf( "name  - %s\n", custs2[r].name.arr );
      99                 :           4 :         printf( "phone - %d\n", custs2[r].phone );
     100                 :             :     }
     101                 :             : 
     102                 :           2 :     EXEC SQL select * INTO :custs3:inds from customers limit 2;
     103   [ -  +  -  +  :           2 :     printf("\ncusts3:\n");
                   -  + ]
     104         [ +  + ]:           6 :     for (r = 0; r < 2; r++)
     105                 :             :     {
     106                 :           4 :         printf( "name  - %s\n", custs3[r].name );
     107                 :           4 :         printf( "phone - %d\n", custs3[r].phone );
     108                 :             :     }
     109                 :             : 
     110                 :           2 :     EXEC SQL select * INTO :custs4:inds from customers limit 1;
     111   [ -  +  -  +  :           2 :     printf("\ncusts4:\n");
                   -  + ]
     112                 :           2 :     printf( "name  - %s\n", custs4->name.arr );
     113                 :           2 :     printf( "phone - %d\n", custs4->phone );
     114                 :             : 
     115                 :           2 :     EXEC SQL select c INTO :onlyname from customers limit 2;
     116   [ -  +  -  +  :           2 :     printf("\nname:\n");
                   -  + ]
     117         [ +  + ]:           6 :     for (r = 0; r < 2; r++)
     118                 :             :     {
     119                 :           4 :         printf( "name  - %s\n", onlyname[r].arr );
     120                 :             :     }
     121                 :             : 
     122                 :           2 :     EXEC SQL disconnect all;
     123   [ -  +  -  + ]:           2 : 
     124                 :             :     /* All the memory will anyway be freed at the end */
     125                 :           2 :     return 0;
     126                 :             : }
        

Generated by: LCOV version 2.0-1