Line data Source code
1 : #include <stdio.h>
2 : #include <stdlib.h>
3 : #include "sqltypes.h"
4 :
5 : EXEC SQL include sqlca.h;
6 : EXEC SQL include ../regression;
7 : EXEC SQL DEFINE MAXDBLEN 30;
8 :
9 : /* Check SQLCODE, and produce a "standard error" if it's wrong! */
10 0 : static void sql_check(const char *fn, const char *caller, int ignore)
11 : {
12 0 : char errorstring[255];
13 :
14 0 : if (SQLCODE == ignore)
15 0 : return;
16 : else
17 : {
18 0 : if (SQLCODE != 0)
19 : {
20 :
21 0 : sprintf(errorstring, "**SQL error %ld doing '%s' in function '%s'. [%s]",
22 : SQLCODE, caller, fn, sqlca.sqlerrm.sqlerrmc);
23 0 : fprintf(stderr, "%s", errorstring);
24 0 : printf("%s\n", errorstring);
25 :
26 : /* attempt a ROLLBACK */
27 0 : EXEC SQL rollback;
28 :
29 0 : if (SQLCODE == 0)
30 : {
31 0 : sprintf(errorstring, "Rollback successful.\n");
32 0 : } else {
33 0 : sprintf(errorstring, "Rollback failed with code %ld.\n", SQLCODE);
34 : }
35 :
36 0 : fprintf(stderr, "%s", errorstring);
37 0 : printf("%s\n", errorstring);
38 :
39 0 : exit(1);
40 : }
41 : }
42 0 : }
43 :
44 0 : int main(void)
45 : {
46 : EXEC SQL BEGIN DECLARE SECTION;
47 0 : int c;
48 0 : timestamp d;
49 0 : timestamp e;
50 0 : timestamp maxd;
51 0 : char dbname[30];
52 : EXEC SQL END DECLARE SECTION;
53 :
54 0 : interval *intvl;
55 :
56 : EXEC SQL whenever sqlerror stop;
57 :
58 0 : ECPGdebug(1, stderr);
59 :
60 0 : strcpy(dbname, "ecpg1_regression");
61 0 : EXEC SQL connect to :dbname;
62 0 : sql_check("main", "connect", 0);
63 :
64 0 : EXEC SQL SET DateStyle TO 'DMY';
65 0 :
66 0 : EXEC SQL create table history (customerid integer, timestamp timestamp without time zone, action_taken char(5), narrative varchar(100));
67 0 : sql_check("main", "create", 0);
68 :
69 0 : EXEC SQL insert into history
70 : (customerid, timestamp, action_taken, narrative)
71 : values(1, '2003-05-07 13:28:34 CEST', 'test', 'test');
72 0 : sql_check("main", "insert", 0);
73 :
74 0 : EXEC SQL select max(timestamp)
75 : into :maxd
76 : from history;
77 0 : sql_check("main", "select max", 100);
78 :
79 0 : EXEC SQL select customerid, timestamp
80 : into :c, :d
81 : from history
82 : where timestamp = :maxd
83 : limit 1;
84 0 : sql_check("main", "select", 0);
85 :
86 0 : printf("Read in customer %d\n", c);
87 :
88 0 : intvl = PGTYPESinterval_from_asc("1 day 2 hours 24 minutes 65 seconds", NULL);
89 0 : PGTYPEStimestamp_add_interval(&d, intvl, &e);
90 0 : free(intvl);
91 0 : c++;
92 :
93 0 : EXEC SQL insert into history
94 : (customerid, timestamp, action_taken, narrative)
95 : values(:c, :e, 'test', 'test');
96 0 : sql_check("main", "update", 0);
97 :
98 0 : EXEC SQL commit;
99 0 :
100 0 : EXEC SQL drop table history;
101 0 : sql_check("main", "drop", 0);
102 :
103 0 : EXEC SQL commit;
104 0 :
105 0 : EXEC SQL disconnect;
106 0 : sql_check("main", "disconnect", 0);
107 :
108 0 : printf("All OK!\n");
109 :
110 0 : exit(0);
111 :
112 : /*
113 : Table "public.history"
114 : Column | Type | Nullable
115 : --------------+-----------------------------+----------
116 : customerid | integer | not null
117 : timestamp | timestamp without time zone | not null
118 : action_taken | character(5) | not null
119 : narrative | character varying(100) |
120 : */
121 :
122 : }
|