LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/compat_oracle - char_array.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 50 50 100.0 %
Date: 2024-11-21 08:14:44 Functions: 2 2 100.0 %
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          12 : static void warn(void)
      12             : {
      13          12 :   fprintf(stderr, "Warning: At least one column was truncated\n");
      14          12 : }
      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           4 : int main() {
      21             : 
      22             :   EXEC SQL WHENEVER SQLWARNING do warn();
      23             :   EXEC SQL WHENEVER SQLERROR STOP;
      24             : 
      25           4 :   const char *ppppp = "XXXXX";
      26             :   int loopcount;
      27           4 :   sqlda_t *sqlda = NULL;
      28             : 
      29             :   EXEC SQL BEGIN DECLARE SECTION;
      30             :   char shortstr[5];
      31             :   char bigstr[11];
      32           4 :   short shstr_ind = 0;
      33           4 :   short bigstr_ind = 0;
      34             :   EXEC SQL END DECLARE SECTION;
      35             : 
      36           4 :   ECPGdebug(1, stderr);
      37           4 :   EXEC SQL CONNECT TO REGRESSDB1;
      38           4 : 
      39           4 :   EXEC SQL CREATE TABLE strdbase (strval varchar(10));
      40           4 :   EXEC SQL INSERT INTO strdbase values ('');
      41           4 :   EXEC SQL INSERT INTO strdbase values ('AB');
      42           4 :   EXEC SQL INSERT INTO strdbase values ('ABCD');
      43           4 :   EXEC SQL INSERT INTO strdbase values ('ABCDE');
      44           4 :   EXEC SQL INSERT INTO strdbase values ('ABCDEF');
      45           4 :   EXEC SQL INSERT INTO strdbase values ('ABCDEFGHIJ');
      46           4 : 
      47             :   EXEC SQL declare C cursor for select strval, strval from strdbase;
      48           4 :   EXEC SQL OPEN C;
      49           4 : 
      50             :   EXEC SQL WHENEVER NOT FOUND DO BREAK;
      51             : 
      52           4 :   printf("Full Str.  :  Short  Ind.\n");
      53          28 :   for (loopcount = 0; loopcount < 100; loopcount++) {
      54          28 :     strncpy(shortstr, ppppp, sizeof shortstr);
      55          28 :     memset(bigstr, 0, sizeof bigstr);
      56          28 :     EXEC SQL FETCH C into :bigstr :bigstr_ind, :shortstr :shstr_ind;
      57          28 :     printf("\"%s\": \"%s\"  %d\n", bigstr, shortstr, shstr_ind);
      58             :   }
      59             : 
      60           4 :   EXEC SQL CLOSE C;
      61           4 :   EXEC SQL DROP TABLE strdbase;
      62           4 :   EXEC SQL COMMIT WORK;
      63           4 : 
      64             :   /* SQLDA handling */
      65             :   EXEC SQL WHENEVER SQLWARNING SQLPRINT;
      66             :   EXEC SQL WHENEVER NOT FOUND STOP;
      67           4 :   EXEC SQL PREPARE stmt1 FROM "SELECT 123::numeric(3,0), 't'::varchar(2)";
      68           4 :   EXEC SQL DECLARE cur1 CURSOR FOR stmt1;
      69           4 :   EXEC SQL OPEN cur1;
      70           4 :   EXEC SQL FETCH NEXT FROM cur1 INTO DESCRIPTOR sqlda;
      71           4 : 
      72           4 :   printf("\n-----------------\ntype    : data\n");
      73          12 :   for (int i = 0 ; i < sqlda->sqld ; i++)
      74             :   {
      75           8 :       sqlvar_t v = sqlda->sqlvar[i];
      76           8 :       char *sqldata = v.sqldata;
      77             : 
      78           8 :       if (v.sqltype == ECPGt_numeric)
      79             :           sqldata =
      80           4 :               PGTYPESnumeric_to_asc((numeric*) sqlda->sqlvar[i].sqldata, -1);
      81             : 
      82           8 :       printf("%-8s: \"%s\"\n", v.sqlname.data, sqldata);
      83             :   }
      84             : 
      85           4 :   EXEC SQL CLOSE cur1;
      86           4 :   EXEC SQL COMMIT WORK;
      87           4 : 
      88           4 :   printf("\nGOOD-BYE!!\n\n");
      89             : 
      90           4 :   EXEC SQL DISCONNECT ALL;
      91           4 : 
      92           4 :   return 0;
      93             : }

Generated by: LCOV version 1.14