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 32 : static void sql_check(const char *fn, const char *caller, int ignore)
11 : {
12 : char errorstring[255];
13 :
14 32 : if (SQLCODE == ignore)
15 28 : return;
16 : else
17 : {
18 4 : if (SQLCODE != 0)
19 : {
20 :
21 0 : sprintf(errorstring, "**SQL error %ld doing '%s' in function '%s'. [%s]",
22 0 : 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 : } 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 : }
43 :
44 4 : int main(void)
45 : {
46 : EXEC SQL BEGIN DECLARE SECTION;
47 : int c;
48 : timestamp d;
49 : timestamp e;
50 : timestamp maxd;
51 : char dbname[30];
52 : EXEC SQL END DECLARE SECTION;
53 :
54 : interval *intvl;
55 :
56 : EXEC SQL whenever sqlerror stop;
57 :
58 4 : ECPGdebug(1, stderr);
59 :
60 4 : strcpy(dbname, "ecpg1_regression");
61 4 : EXEC SQL connect to :dbname;
62 4 : sql_check("main", "connect", 0);
63 :
64 4 : EXEC SQL SET DateStyle TO 'DMY';
65 4 :
66 4 : EXEC SQL create table history (customerid integer, timestamp timestamp without time zone, action_taken char(5), narrative varchar(100));
67 4 : sql_check("main", "create", 0);
68 :
69 4 : EXEC SQL insert into history
70 : (customerid, timestamp, action_taken, narrative)
71 : values(1, '2003-05-07 13:28:34 CEST', 'test', 'test');
72 4 : sql_check("main", "insert", 0);
73 :
74 4 : EXEC SQL select max(timestamp)
75 : into :maxd
76 : from history;
77 4 : sql_check("main", "select max", 100);
78 :
79 4 : EXEC SQL select customerid, timestamp
80 : into :c, :d
81 : from history
82 : where timestamp = :maxd
83 : limit 1;
84 4 : sql_check("main", "select", 0);
85 :
86 4 : printf("Read in customer %d\n", c);
87 :
88 4 : intvl = PGTYPESinterval_from_asc("1 day 2 hours 24 minutes 65 seconds", NULL);
89 4 : PGTYPEStimestamp_add_interval(&d, intvl, &e);
90 4 : free(intvl);
91 4 : c++;
92 :
93 4 : EXEC SQL insert into history
94 : (customerid, timestamp, action_taken, narrative)
95 : values(:c, :e, 'test', 'test');
96 4 : sql_check("main", "update", 0);
97 :
98 4 : EXEC SQL commit;
99 4 :
100 4 : EXEC SQL drop table history;
101 4 : sql_check("main", "drop", 0);
102 :
103 4 : EXEC SQL commit;
104 4 :
105 4 : EXEC SQL disconnect;
106 4 : sql_check("main", "disconnect", 0);
107 :
108 4 : printf("All OK!\n");
109 :
110 4 : 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 : }
|