LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/compat_oracle - char_array.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 100.0 % 50 50
Test Date: 2026-03-03 18:14:56 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit

            Line data    Source code
       1              : #include <stdio.h>
       2              : #include <stdlib.h>
       3              : #include <string.h>
       4              : 
       5              : #include <pgtypes_numeric.h>
       6              : 
       7              : EXEC SQL INCLUDE sqlda.h;
       8              : 
       9              : EXEC SQL INCLUDE ../regression;
      10              : 
      11            6 : static void warn(void)
      12              : {
      13            6 :   fprintf(stderr, "Warning: At least one column was truncated\n");
      14            6 : }
      15              : 
      16              : /* Compatible handling of char array to retrieve varchar field to char array
      17              :    should be fixed-length, blank-padded, then null-terminated.
      18              :    Conforms to the ANSI Fixed Character type. */
      19              : 
      20            2 : int main() {
      21              : 
      22              :   EXEC SQL WHENEVER SQLWARNING do warn();
      23              :   EXEC SQL WHENEVER SQLERROR STOP;
      24              : 
      25            2 :   const char *ppppp = "XXXXX";
      26              :   int loopcount;
      27            2 :   sqlda_t *sqlda = NULL;
      28              : 
      29              :   EXEC SQL BEGIN DECLARE SECTION;
      30              :   char shortstr[5];
      31              :   char bigstr[11];
      32            2 :   short shstr_ind = 0;
      33            2 :   short bigstr_ind = 0;
      34              :   EXEC SQL END DECLARE SECTION;
      35              : 
      36            2 :   ECPGdebug(1, stderr);
      37            2 :   EXEC SQL CONNECT TO REGRESSDB1;
      38            2 : 
      39            2 :   EXEC SQL CREATE TABLE strdbase (strval varchar(10));
      40            2 :   EXEC SQL INSERT INTO strdbase values ('');
      41            2 :   EXEC SQL INSERT INTO strdbase values ('AB');
      42            2 :   EXEC SQL INSERT INTO strdbase values ('ABCD');
      43            2 :   EXEC SQL INSERT INTO strdbase values ('ABCDE');
      44            2 :   EXEC SQL INSERT INTO strdbase values ('ABCDEF');
      45            2 :   EXEC SQL INSERT INTO strdbase values ('ABCDEFGHIJ');
      46            2 : 
      47              :   EXEC SQL declare C cursor for select strval, strval from strdbase;
      48            2 :   EXEC SQL OPEN C;
      49            2 : 
      50              :   EXEC SQL WHENEVER NOT FOUND DO BREAK;
      51              : 
      52            2 :   printf("Full Str.  :  Short  Ind.\n");
      53           14 :   for (loopcount = 0; loopcount < 100; loopcount++) {
      54           14 :     strncpy(shortstr, ppppp, sizeof shortstr);
      55           14 :     memset(bigstr, 0, sizeof bigstr);
      56           14 :     EXEC SQL FETCH C into :bigstr :bigstr_ind, :shortstr :shstr_ind;
      57           14 :     printf("\"%s\": \"%s\"  %d\n", bigstr, shortstr, shstr_ind);
      58              :   }
      59              : 
      60            2 :   EXEC SQL CLOSE C;
      61            2 :   EXEC SQL DROP TABLE strdbase;
      62            2 :   EXEC SQL COMMIT WORK;
      63            2 : 
      64              :   /* SQLDA handling */
      65              :   EXEC SQL WHENEVER SQLWARNING SQLPRINT;
      66              :   EXEC SQL WHENEVER NOT FOUND STOP;
      67            2 :   EXEC SQL PREPARE stmt1 FROM "SELECT 123::numeric(3,0), 't'::varchar(2)";
      68            2 :   EXEC SQL DECLARE cur1 CURSOR FOR stmt1;
      69            2 :   EXEC SQL OPEN cur1;
      70            2 :   EXEC SQL FETCH NEXT FROM cur1 INTO DESCRIPTOR sqlda;
      71            2 : 
      72            2 :   printf("\n-----------------\ntype    : data\n");
      73            6 :   for (int i = 0 ; i < sqlda->sqld ; i++)
      74              :   {
      75            4 :       sqlvar_t v = sqlda->sqlvar[i];
      76            4 :       char *sqldata = v.sqldata;
      77              : 
      78            4 :       if (v.sqltype == ECPGt_numeric)
      79              :           sqldata =
      80            2 :               PGTYPESnumeric_to_asc((numeric*) sqlda->sqlvar[i].sqldata, -1);
      81              : 
      82            4 :       printf("%-8s: \"%s\"\n", v.sqlname.data, sqldata);
      83              :   }
      84              : 
      85            2 :   EXEC SQL CLOSE cur1;
      86            2 :   EXEC SQL COMMIT WORK;
      87            2 : 
      88            2 :   printf("\nGOOD-BYE!!\n\n");
      89              : 
      90            2 :   EXEC SQL DISCONNECT ALL;
      91            2 : 
      92            2 :   return 0;
      93              : }
        

Generated by: LCOV version 2.0-1