]> granicus.if.org Git - postgresql/blob - src/interfaces/ecpg/test/compat_oracle/char_array.pgc
Fix whitespace
[postgresql] / src / interfaces / ecpg / test / compat_oracle / char_array.pgc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 EXEC SQL INCLUDE ../regression;
6
7 static void warn(void)
8 {
9   fprintf(stderr, "Warning: At least one column was truncated\n");
10 }
11
12 /* Compatible handling of char array to retrieve varchar field to char array
13    should be fixed-length, blank-padded, then null-terminated.
14    Conforms to the ANSI Fixed Character type. */
15
16 int main() {
17
18   EXEC SQL WHENEVER SQLWARNING do warn();
19   EXEC SQL WHENEVER SQLERROR SQLPRINT;
20
21   const char *ppppp = "XXXXX";
22
23   EXEC SQL BEGIN DECLARE SECTION;
24   char shortstr[5];
25   char bigstr[11];
26   short shstr_ind = 0;
27   short bigstr_ind = 0;
28   EXEC SQL END DECLARE SECTION;
29
30   ECPGdebug(1, stderr);
31   EXEC SQL CONNECT TO REGRESSDB1;
32
33   EXEC SQL CREATE TABLE strdbase (strval varchar(10));
34   EXEC SQL INSERT INTO strdbase values ('');
35   EXEC SQL INSERT INTO strdbase values ('AB');
36   EXEC SQL INSERT INTO strdbase values ('ABCD');
37   EXEC SQL INSERT INTO strdbase values ('ABCDE');
38   EXEC SQL INSERT INTO strdbase values ('ABCDEF');
39   EXEC SQL INSERT INTO strdbase values ('ABCDEFGHIJ');
40
41   EXEC SQL declare C cursor for select strval, strval from strdbase;
42   EXEC SQL OPEN C;
43
44   EXEC SQL WHENEVER NOT FOUND DO BREAK;
45
46   printf("Full Str.  :  Short  Ind.\n");
47   while(1) {
48     strncpy(shortstr, ppppp, sizeof shortstr);
49     memset(bigstr, 0, sizeof bigstr);
50     EXEC SQL FETCH C into :bigstr :bigstr_ind, :shortstr :shstr_ind;
51     printf("\"%s\": \"%s\"  %d\n", bigstr, shortstr, shstr_ind);
52   }
53
54   EXEC SQL CLOSE C;
55   EXEC SQL DROP TABLE strdbase;
56
57   printf("\nGOOD-BYE!!\n\n");
58
59   EXEC SQL COMMIT WORK;
60
61   EXEC SQL DISCONNECT ALL;
62
63   return 0;
64 }