| 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 |
static void sql_check(const char *fn, const char *caller, int ignore) |
| 11 |
{ |
| 12 |
char errorstring[255]; |
| 13 |
|
| 14 |
if (SQLCODE == ignore) |
| 15 |
return; |
| 16 |
else |
| 17 |
{ |
| 18 |
if (SQLCODE != 0) |
| 19 |
{ |
| 20 |
|
| 21 |
sprintf(errorstring, "**SQL error %ld doing '%s' in function '%s'. [%s]", |
| 22 |
SQLCODE, caller, fn, sqlca.sqlerrm.sqlerrmc); |
| 23 |
fprintf(stderr, "%s", errorstring); |
| 24 |
printf("%s\n", errorstring); |
| 25 |
|
| 26 |
/* attempt a ROLLBACK */ |
| 27 |
EXEC SQL rollback; |
| 28 |
|
| 29 |
if (SQLCODE == 0) |
| 30 |
{ |
| 31 |
sprintf(errorstring, "Rollback successful.\n"); |
| 32 |
} else { |
| 33 |
sprintf(errorstring, "Rollback failed with code %ld.\n", SQLCODE); |
| 34 |
} |
| 35 |
|
| 36 |
fprintf(stderr, "%s", errorstring); |
| 37 |
printf("%s\n", errorstring); |
| 38 |
|
| 39 |
exit(1); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
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 |
ECPGdebug(1, stderr); |
| 59 |
|
| 60 |
strcpy(dbname, "ecpg1_regression"); |
| 61 |
EXEC SQL connect to :dbname; |
| 62 |
sql_check("main", "connect", 0); |
| 63 |
|
| 64 |
EXEC SQL SET DateStyle TO 'DMY'; |
| 65 |
|
| 66 |
EXEC SQL create table history (customerid integer, timestamp timestamp without time zone, action_taken char(5), narrative varchar(100)); |
| 67 |
sql_check("main", "create", 0); |
| 68 |
|
| 69 |
EXEC SQL insert into history |
| 70 |
(customerid, timestamp, action_taken, narrative) |
| 71 |
values(1, '2003-05-07 13:28:34 CEST', 'test', 'test'); |
| 72 |
sql_check("main", "insert", 0); |
| 73 |
|
| 74 |
EXEC SQL select max(timestamp) |
| 75 |
into :maxd |
| 76 |
from history; |
| 77 |
sql_check("main", "select max", 100); |
| 78 |
|
| 79 |
EXEC SQL select customerid, timestamp |
| 80 |
into :c, :d |
| 81 |
from history |
| 82 |
where timestamp = :maxd |
| 83 |
limit 1; |
| 84 |
sql_check("main", "select", 0); |
| 85 |
|
| 86 |
printf("Read in customer %d\n", c); |
| 87 |
|
| 88 |
intvl = PGTYPESinterval_from_asc("1 day 2 hours 24 minutes 65 seconds", NULL); |
| 89 |
PGTYPEStimestamp_add_interval(&d, intvl, &e); |
| 90 |
free(intvl); |
| 91 |
c++; |
| 92 |
|
| 93 |
EXEC SQL insert into history |
| 94 |
(customerid, timestamp, action_taken, narrative) |
| 95 |
values(:c, :e, 'test', 'test'); |
| 96 |
sql_check("main", "update", 0); |
| 97 |
|
| 98 |
EXEC SQL commit; |
| 99 |
|
| 100 |
EXEC SQL drop table history; |
| 101 |
sql_check("main", "drop", 0); |
| 102 |
|
| 103 |
EXEC SQL commit; |
| 104 |
|
| 105 |
EXEC SQL disconnect; |
| 106 |
sql_check("main", "disconnect", 0); |
| 107 |
|
| 108 |
printf("All OK!\n"); |
| 109 |
|
| 110 |
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 |
} |