-
Notifications
You must be signed in to change notification settings - Fork 2
/
slev.c
142 lines (115 loc) · 3.51 KB
/
slev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* -*-C-*-
* slev.pc
* corresponds to A.5 in appendix A
*/
#include <string.h>
#include <stdio.h>
#include <sqlite3.h>
#include "spt_proc.h"
#include "tpc.h"
extern sqlite3 **ctx;
extern sqlite3_stmt ***stmt;
/*
* the stock level transaction
*/
int slev( int t_num,
int w_id_arg, /* warehouse id */
int d_id_arg, /* district id */
int level_arg /* stock level */
)
{
int ret;
int w_id = w_id_arg;
int d_id = d_id_arg;
int level = level_arg;
int d_next_o_id;
int i_count;
int ol_i_id;
sqlite3_stmt *sqlite_stmt;
sqlite3_stmt *sqlite_stmt2;
int num_cols;
/*EXEC SQL WHENEVER NOT FOUND GOTO sqlerr;*/
/*EXEC SQL WHENEVER SQLERROR GOTO sqlerr;*/
/* find the next order id */
#ifdef DEBUG
printf("select 1\n");
#endif
/*EXEC_SQL SELECT d_next_o_id
INTO :d_next_o_id
FROM district
WHERE d_id = :d_id
AND d_w_id = :w_id;*/
sqlite_stmt = stmt[t_num][32];
sqlite3_bind_int64(sqlite_stmt, 1, d_id);
sqlite3_bind_int64(sqlite_stmt, 2, w_id);
ret = sqlite3_step(sqlite_stmt);
if (ret != SQLITE_DONE) {
if (ret != SQLITE_ROW) goto sqlerr;
num_cols = sqlite3_column_count(sqlite_stmt);
if (num_cols != 1) goto sqlerr;
d_next_o_id = sqlite3_column_int64(sqlite_stmt, 0);
}
sqlite3_reset(sqlite_stmt);
/* find the most recent 20 orders for this district */
/*EXEC_SQL DECLARE ord_line CURSOR FOR
SELECT DISTINCT ol_i_id
FROM order_line
WHERE ol_w_id = :w_id
AND ol_d_id = :d_id
AND ol_o_id < :d_next_o_id
AND ol_o_id >= (:d_next_o_id - 20);
EXEC_SQL OPEN ord_line;
EXEC SQL WHENEVER NOT FOUND GOTO done;*/
sqlite_stmt = stmt[t_num][33];
sqlite3_bind_int64(sqlite_stmt, 1, d_id);
sqlite3_bind_int64(sqlite_stmt, 2, w_id);
sqlite3_bind_int64(sqlite_stmt, 3, d_next_o_id);
sqlite3_bind_int64(sqlite_stmt, 4, d_next_o_id);
while (sqlite3_step(sqlite_stmt) != SQLITE_DONE) {
num_cols = sqlite3_column_count(sqlite_stmt);
if (num_cols != 1) goto sqlerr;
ol_i_id = sqlite3_column_int64(sqlite_stmt, 0);
/*EXEC_SQL SELECT count(*) INTO :i_count
FROM stock
WHERE s_w_id = :w_id
AND s_i_id = :ol_i_id
AND s_quantity < :level;*/
sqlite_stmt2 = stmt[t_num][34];
sqlite3_bind_int64(sqlite_stmt, 1, w_id);
sqlite3_bind_int64(sqlite_stmt, 2, ol_i_id);
sqlite3_bind_int64(sqlite_stmt, 3, level);
ret = sqlite3_step(sqlite_stmt2);
if (ret != SQLITE_DONE) {
if (ret != SQLITE_ROW) goto sqlerr;
num_cols = sqlite3_column_count(sqlite_stmt2);
if (num_cols != 1) goto sqlerr;
i_count = sqlite3_column_int64(sqlite_stmt2, 0);
}
sqlite3_reset(sqlite_stmt2);
}
sqlite3_reset(sqlite_stmt);
done:
/*EXEC_SQL CLOSE ord_line;*/
/*EXEC_SQL COMMIT WORK;*/
//if( sqlite3_exec(ctx[t_num], "COMMIT;", NULL, NULL, NULL) != SQLITE_OK) goto sqlerr;
return (1);
sqlerr:
fprintf(stderr,"slev\n");
printf("%s: error: %s\n", __func__, sqlite3_errmsg(ctx[t_num]));
//error(ctx[t_num],mysql_stmt);
/*EXEC SQL WHENEVER SQLERROR GOTO sqlerrerr;*/
/*EXEC_SQL ROLLBACK WORK;*/
sqlite3_exec(ctx[t_num], "ROLLBACK;", NULL, NULL, NULL);
return (0);
sqlerr2:
fprintf(stderr,"slev\n");
printf("%s: error: %s\n", __func__, sqlite3_errmsg(ctx[t_num]));
//error(ctx[t_num],mysql_stmt2);
/*EXEC SQL WHENEVER SQLERROR GOTO sqlerrerr;*/
/*EXEC_SQL ROLLBACK WORK;*/
//mysql_stmt_free_result(mysql_stmt);
//mysql_rollback(ctx[t_num]);
sqlite3_exec(ctx[t_num], "ROLLBACK;", NULL, NULL, NULL);
return (0);
}