LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/sql - declare.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 100.0 % 108 108
Test Date: 2026-07-25 10:15:40 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 54.2 % 96 52

             Branch data     Line data    Source code
       1                 :             : #include <locale.h>
       2                 :             : #include <string.h>
       3                 :             : #include <stdlib.h>
       4                 :             : 
       5                 :             : EXEC SQL WHENEVER SQLERROR SQLPRINT;
       6                 :             : 
       7                 :             : EXEC SQL INCLUDE sqlca;
       8                 :             : EXEC SQL INCLUDE ../regression;
       9                 :             : 
      10                 :             : #define ARRAY_SIZE 2
      11                 :             : 
      12                 :             : void execute_test(void);
      13                 :             : void commitTable(void);
      14                 :             : void reset(void);
      15                 :             : void printResult(char *tc_name, int loop);
      16                 :             : 
      17                 :             : EXEC SQL BEGIN DECLARE SECTION;
      18                 :             : int f1[ARRAY_SIZE];
      19                 :             : int f2[ARRAY_SIZE];
      20                 :             : char f3[ARRAY_SIZE][20];
      21                 :             : EXEC SQL END DECLARE SECTION;
      22                 :             : 
      23                 :           2 : int main(void)
      24                 :             : {
      25                 :           2 :     setlocale(LC_ALL, "C");
      26                 :             : 
      27                 :           2 :     ECPGdebug(1, stderr);
      28                 :             : 
      29                 :           2 :     EXEC SQL CONNECT TO REGRESSDB1 AS con1;
      30         [ -  + ]:           2 :     EXEC SQL CONNECT TO REGRESSDB2 AS con2;
      31         [ -  + ]:           2 : 
      32                 :           2 :     EXEC SQL AT con1 CREATE TABLE source(f1 integer, f2 integer, f3 varchar(20));
      33         [ -  + ]:           2 :     EXEC SQL AT con2 CREATE TABLE source(f1 integer, f2 integer, f3 varchar(20));
      34         [ -  + ]:           2 : 
      35                 :           2 :     EXEC SQL AT con1 INSERT INTO source VALUES(1, 10, 'db on con1');
      36         [ -  + ]:           2 :     EXEC SQL AT con1 INSERT INTO source VALUES(2, 20, 'db on con1');
      37         [ -  + ]:           2 : 
      38                 :           2 :     EXEC SQL AT con2 INSERT INTO source VALUES(1, 10, 'db on con2');
      39         [ -  + ]:           2 :     EXEC SQL AT con2 INSERT INTO source VALUES(2, 20, 'db on con2');
      40         [ -  + ]:           2 : 
      41                 :           2 :     commitTable();
      42                 :             : 
      43                 :           2 :     execute_test();
      44                 :             : 
      45                 :           2 :     EXEC SQL AT con1 DROP TABLE IF EXISTS source;
      46         [ -  + ]:           2 :     EXEC SQL AT con2 DROP TABLE IF EXISTS source;
      47         [ -  + ]:           2 : 
      48                 :           2 :     commitTable();
      49                 :             : 
      50                 :           2 :     EXEC SQL DISCONNECT ALL;
      51         [ -  + ]:           2 : 
      52                 :           2 :     return 0;
      53                 :             : }
      54                 :             : 
      55                 :             : /*
      56                 :             :  * default connection: con2
      57                 :             :  * Non-default connection: con1
      58                 :             :  *
      59                 :             :  */
      60                 :           2 : void execute_test(void)
      61                 :             : {
      62                 :             :     EXEC SQL BEGIN DECLARE SECTION;
      63                 :             :     int i, count, length;
      64                 :           2 :     char *selectString = "SELECT f1,f2,f3 FROM source";
      65                 :             :     EXEC SQL END DECLARE SECTION;
      66                 :             : 
      67                 :             :     /*
      68                 :             :      * testcase1. using DECLARE STATEMENT without using AT clause,
      69                 :             :      * using PREPARE and CURSOR statement without using AT clause
      70                 :             :      */
      71                 :           2 :     reset();
      72                 :             : 
      73                 :             :     EXEC SQL DECLARE stmt_1 STATEMENT;
      74                 :           2 :     EXEC SQL PREPARE stmt_1 FROM :selectString;
      75         [ -  + ]:           2 :     EXEC SQL DECLARE cur_1 CURSOR FOR stmt_1;
      76                 :           2 :     EXEC SQL OPEN cur_1;
      77         [ -  + ]:           2 : 
      78                 :             :     EXEC SQL WHENEVER NOT FOUND DO BREAK;
      79                 :           2 :     i = 0;
      80                 :             :     while (1)
      81                 :             :     {
      82                 :           6 :         EXEC SQL FETCH cur_1 INTO :f1[i], :f2[i], :f3[i];
      83   [ +  +  -  + ]:           6 :         i++;
      84                 :             :     }
      85                 :           2 :     EXEC SQL CLOSE cur_1;
      86         [ -  + ]:           2 :     EXEC SQL DEALLOCATE PREPARE stmt_1;
      87         [ -  + ]:           8 :     EXEC SQL WHENEVER NOT FOUND CONTINUE;
      88                 :             : 
      89                 :           2 :     printResult("testcase1", 2);
      90                 :             : 
      91                 :             : 
      92                 :             :     /*
      93                 :             :      * testcase2. using DECLARE STATEMENT at con1,
      94                 :             :      * using PREPARE and CURSOR statement without using AT clause
      95                 :             :      */
      96                 :           2 :     reset();
      97                 :             : 
      98                 :             :     EXEC SQL AT con1 DECLARE stmt_2 STATEMENT;
      99                 :           2 :     EXEC SQL PREPARE stmt_2 FROM :selectString;
     100         [ -  + ]:           2 :     EXEC SQL DECLARE cur_2 CURSOR FOR stmt_2;
     101                 :           2 :     EXEC SQL OPEN cur_2;
     102         [ -  + ]:           2 : 
     103                 :             :     EXEC SQL WHENEVER NOT FOUND DO BREAK;
     104                 :           2 :     i = 0;
     105                 :             :     while (1)
     106                 :             :     {
     107                 :           6 :         EXEC SQL FETCH cur_2 INTO :f1[i], :f2[i], :f3[i];
     108   [ +  +  -  + ]:           6 :         i++;
     109                 :             :     }
     110                 :           2 :     EXEC SQL CLOSE cur_2;
     111         [ -  + ]:           2 :     EXEC SQL DEALLOCATE PREPARE stmt_2;
     112         [ -  + ]:           8 :     EXEC SQL WHENEVER NOT FOUND CONTINUE;
     113                 :             : 
     114                 :           2 :     printResult("testcase2", 2);
     115                 :             : 
     116                 :             :     /*
     117                 :             :      * testcase3. using DECLARE STATEMENT without using AT clause,
     118                 :             :      * using PREPARE and EXECUTE statement without using AT clause
     119                 :             :      */
     120                 :           2 :     reset();
     121                 :             : 
     122                 :             :     EXEC SQL DECLARE stmt_3 STATEMENT;
     123                 :           2 :     EXEC SQL PREPARE stmt_3 FROM :selectString;
     124         [ -  + ]:           2 :     EXEC SQL EXECUTE stmt_3 INTO :f1, :f2, :f3;
     125         [ -  + ]:           2 : 
     126                 :           2 :     EXEC SQL DEALLOCATE PREPARE stmt_3;
     127         [ -  + ]:           2 : 
     128                 :           2 :     printResult("testcase3", 2);
     129                 :             : 
     130                 :             :     /*
     131                 :             :      * testcase4. using DECLARE STATEMENT without using AT clause,
     132                 :             :      * using PREPARE and CURSOR statement at con2
     133                 :             :      */
     134                 :           2 :     reset();
     135                 :             : 
     136                 :             :     EXEC SQL DECLARE stmt_4 STATEMENT;
     137                 :           2 :     EXEC SQL AT con2 PREPARE stmt_4 FROM :selectString;
     138         [ -  + ]:           2 :     EXEC SQL AT con2 DECLARE cur_4 CURSOR FOR stmt_4;
     139                 :           2 :     EXEC SQL AT con2 OPEN cur_4;
     140         [ -  + ]:           2 : 
     141                 :             :     EXEC SQL WHENEVER NOT FOUND DO BREAK;
     142                 :           2 :     i = 0;
     143                 :             :     while (1)
     144                 :             :     {
     145                 :           6 :         EXEC SQL AT con2 FETCH cur_4 INTO :f1[i], :f2[i], :f3[i];
     146   [ +  +  -  + ]:           6 :         i++;
     147                 :             :     }
     148                 :           2 :     EXEC SQL AT con2 CLOSE cur_4;
     149         [ -  + ]:           2 :     EXEC SQL AT con2 DEALLOCATE PREPARE stmt_4;
     150         [ -  + ]:           8 :     EXEC SQL WHENEVER NOT FOUND CONTINUE;
     151                 :             : 
     152                 :           2 :     printResult("testcase4", 2);
     153                 :             : 
     154                 :             :     /*
     155                 :             :      * DESCRIBE statement is also supported.
     156                 :             :      */
     157                 :             :     EXEC SQL AT con1 DECLARE stmt_desc STATEMENT;
     158                 :           2 :     EXEC SQL PREPARE stmt_desc FROM :selectString;
     159         [ -  + ]:           2 :     EXEC SQL DECLARE cur_desc CURSOR FOR stmt_desc;
     160                 :           2 :     EXEC SQL OPEN cur_desc;
     161         [ -  + ]:           2 : 
     162                 :             :     /* descriptor can be used for describe statement */
     163                 :           2 :     EXEC SQL AT con1 ALLOCATE DESCRIPTOR desc_for_describe;
     164         [ -  + ]:           2 :     EXEC SQL DESCRIBE stmt_desc INTO SQL DESCRIPTOR desc_for_describe;
     165                 :             : 
     166                 :           2 :     EXEC SQL AT con1 GET DESCRIPTOR desc_for_describe :count = COUNT;
     167         [ -  + ]:           2 :     EXEC SQL AT con1 GET DESCRIPTOR desc_for_describe VALUE 3 :length = LENGTH;
     168         [ -  + ]:           2 : 
     169                 :           2 :     EXEC SQL AT con1 DEALLOCATE DESCRIPTOR desc_for_describe;
     170         [ -  + ]:           2 : 
     171                 :             :     /* for fetch statement */
     172                 :           2 :     EXEC SQL AT con1 ALLOCATE DESCRIPTOR desc_for_fetch;
     173         [ -  + ]:           2 :     EXEC SQL FETCH cur_desc INTO SQL DESCRIPTOR desc_for_fetch;
     174         [ -  + ]:           2 : 
     175                 :           2 :     EXEC SQL AT con1 GET DESCRIPTOR desc_for_fetch VALUE 3 :f3[0] = DATA;
     176         [ -  + ]:           2 : 
     177                 :           2 :     EXEC SQL AT con1 DEALLOCATE DESCRIPTOR desc_for_fetch;
     178         [ -  + ]:           2 :     EXEC SQL CLOSE cur_desc;
     179         [ -  + ]:           2 :     EXEC SQL DEALLOCATE stmt_desc;
     180         [ -  + ]:           2 : 
     181                 :           2 :     printf("****descriptor results****\n");
     182                 :           2 :     printf("count: %d, length: %d, data: %s\n", count, length, f3[0]);
     183                 :           2 : }
     184                 :             : 
     185                 :           4 : void commitTable(void)
     186                 :             : {
     187                 :           4 :     EXEC SQL AT con1 COMMIT;
     188         [ -  + ]:           4 :     EXEC SQL AT con2 COMMIT;
     189         [ -  + ]:           4 : }
     190                 :             : 
     191                 :             : /*
     192                 :             :  * reset all the output variables
     193                 :             :  */
     194                 :           8 : void reset(void)
     195                 :             : {
     196                 :           8 :     memset(f1, 0, sizeof(f1));
     197                 :           8 :     memset(f2, 0, sizeof(f2));
     198                 :           8 :     memset(f3, 0, sizeof(f3));
     199                 :           8 : }
     200                 :             : 
     201                 :           8 : void printResult(char *tc_name, int loop)
     202                 :             : {
     203                 :             :     int i;
     204                 :             : 
     205         [ +  - ]:           8 :     if (tc_name)
     206                 :           8 :         printf("****%s test results:****\n", tc_name);
     207                 :             : 
     208         [ +  + ]:          24 :     for (i = 0; i < loop; i++)
     209                 :          16 :         printf("f1=%d, f2=%d, f3=%s\n", f1[i], f2[i], f3[i]);
     210                 :             : 
     211                 :           8 :     printf("\n");
     212                 :           8 : }
        

Generated by: LCOV version 2.0-1