LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/sql - execute.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 66 66 100.0 %
Date: 2024-12-12 16:15:21 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include <stdlib.h>
       2             : #include <string.h>
       3             : #include <stdlib.h>
       4             : #include <stdio.h>
       5             : 
       6             : exec sql include ../regression;
       7             : 
       8             : exec sql whenever sqlerror sqlprint;
       9             : 
      10             : int
      11           4 : main(void)
      12             : {
      13             : exec sql begin declare section;
      14             :     int amount[8];
      15           4 :     int increment=100;
      16             :     char name[8][8];
      17             :     char letter[8][1];
      18             :     char command[128];
      19             : exec sql end declare section;
      20             :     int i,j;
      21             : 
      22           4 :     ECPGdebug(1, stderr);
      23             : 
      24           4 :     exec sql connect to REGRESSDB1 as main;
      25           4 :     exec sql create table test (name char(8), amount int, letter char(1));
      26           4 :     exec sql commit;
      27           4 : 
      28             :     /* test handling of embedded quotes in EXECUTE IMMEDIATE "literal" */
      29           4 :     exec sql execute immediate "insert into test (name, \042amount\042, letter) values ('db: ''r1''', 1, 'f')";
      30           4 : 
      31           4 :     sprintf(command, "insert into test (name, amount, letter) values ('db: ''r1''', 2, 't')");
      32           4 :     exec sql execute immediate :command;
      33           4 : 
      34           4 :     sprintf(command, "insert into test (name, amount, letter) select name, amount+10, letter from test");
      35           4 :     exec sql execute immediate :command;
      36           4 : 
      37           4 :     printf("Inserted %ld tuples via execute immediate\n", sqlca.sqlerrd[2]);
      38             : 
      39           4 :     sprintf(command, "insert into test (name, amount, letter) select name, amount+$1, letter from test");
      40           4 :     exec sql prepare I from :command;
      41           4 :     exec sql execute I using :increment;
      42           4 : 
      43           4 :     printf("Inserted %ld tuples via prepared execute\n", sqlca.sqlerrd[2]);
      44             : 
      45           4 :     exec sql commit;
      46           4 : 
      47           4 :     sprintf (command, "select * from test");
      48             : 
      49           4 :     exec sql prepare f from :command;
      50           4 :     exec sql declare CUR cursor for f;
      51             : 
      52           4 :     exec sql open CUR;
      53           4 :     exec sql fetch 8 in CUR into :name, :amount, :letter;
      54           4 : 
      55          36 :     for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
      56             :     {
      57             :         exec sql begin declare section;
      58          32 :         char n[8], l = letter[i][0];
      59          32 :         int a = amount[i];
      60             :         exec sql end declare section;
      61             : 
      62          32 :         strncpy(n, name[i], 8);
      63          32 :         printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, n, i, a, i, l);
      64             :     }
      65             : 
      66           4 :     exec sql close CUR;
      67           4 :     exec sql deallocate f;
      68           4 : 
      69           4 :     sprintf (command, "select * from test where amount = $1");
      70             : 
      71           4 :     exec sql prepare f from :command;
      72           4 :     exec sql declare CUR2 cursor for f;
      73             : 
      74           4 :     exec sql open CUR2 using 1;
      75           4 :     exec sql fetch in CUR2 into :name, :amount, :letter;
      76           4 : 
      77           8 :     for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
      78             :     {
      79             :         exec sql begin declare section;
      80           4 :         char n[8], l = letter[i][0];
      81           4 :         int a = amount[i];
      82             :         exec sql end declare section;
      83             : 
      84           4 :         strncpy(n, name[i], 8);
      85           4 :         printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, n, i, a, i, l);
      86             :     }
      87             : 
      88           4 :     exec sql close CUR2;
      89           4 :     exec sql deallocate f;
      90           4 : 
      91           4 :     sprintf (command, "select * from test where amount = $1");
      92             : 
      93           4 :     exec sql prepare f from :command;
      94           4 :     exec sql execute f using 2 into :name, :amount, :letter;
      95           4 : 
      96           8 :     for (i=0, j=sqlca.sqlerrd[2]; i<j; i++)
      97             :     {
      98             :         exec sql begin declare section;
      99           4 :         char n[8], l = letter[i][0];
     100           4 :         int a = amount[i];
     101             :         exec sql end declare section;
     102             : 
     103           4 :         strncpy(n, name[i], 8);
     104           4 :         printf("name[%d]=%8.8s\tamount[%d]=%d\tletter[%d]=%c\n", i, n, i, a, i, l);
     105             :     }
     106             : 
     107           4 :     exec sql deallocate f;
     108           4 :     exec sql drop table test;
     109           4 :     exec sql commit;
     110           4 :     exec sql disconnect;
     111           4 : 
     112           4 :     return 0;
     113             : }

Generated by: LCOV version 1.14