LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/sql - declare.pgc (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 113 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 5 0
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.3.2-1