-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.c
More file actions
247 lines (179 loc) · 7.94 KB
/
sequence.c
File metadata and controls
247 lines (179 loc) · 7.94 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
BSD 3-Clause License
Copyright (c) 2017, Sylvain Pointeau (sylvain.pointeau@gmail.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
int is_temp_table_created = 0;
void sp_seq_init(sqlite3_context *context, int argc, sqlite3_value **argv) {
int rc = 0;
sqlite3_stmt *stmt;
sqlite3 *db = sqlite3_context_db_handle(context);
const unsigned char* seq_name = sqlite3_value_text(argv[0]);
long seq_init_val = sqlite3_value_int64(argv[1]);
long seq_inc_val = argc < 3 ? 1 : sqlite3_value_int64(argv[2]);
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS SP_SEQUENCE ( " \
" SEQ_NAME TEXT NOT NULL PRIMARY KEY, " \
" SEQ_VAL INTEGER, " \
" SEQ_INIT INTEGER NOT NULL, " \
" SEQ_INC INTEGER NOT NULL CHECK (SEQ_INC<>0) " \
" )", 0, 0, 0);
if( rc != SQLITE_OK ) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
sqlite3_prepare_v2(db, "insert or replace into sp_sequence (seq_name, seq_val, seq_init, seq_inc) values (?, ?, ?, ?)", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 2, seq_init_val-seq_inc_val);
sqlite3_bind_int64(stmt, 3, seq_init_val);
sqlite3_bind_int64(stmt, 4, seq_inc_val);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
// init means that we wait for a nextval before a currval
sqlite3_prepare_v2(db, "delete from SP_TEMP_SEQ_CURRVAL where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
// we don't really care if there is any error on the TEMP table
sqlite3_result_int64( context, seq_init_val );
}
void sp_seq_nextval(sqlite3_context *context, int argc, sqlite3_value **argv) {
int rc = 0;
int update_row_count = 0;
sqlite3_stmt *stmt;
long nextval = 0;
sqlite3 *db = sqlite3_context_db_handle(context);
const unsigned char* seq_name = sqlite3_value_text(argv[0]);
sqlite3_prepare_v2(db, "update sp_sequence set seq_val = seq_val + seq_inc where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
sqlite3_prepare_v2(db, "select seq_val from sp_sequence where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if( rc == SQLITE_ROW) {
nextval = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
if (rc != SQLITE_ROW) {
if( rc == SQLITE_DONE ) sqlite3_result_error(context, "sequence name does not exist", -1);
else sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
if( is_temp_table_created == 0 ) {
rc = sqlite3_exec(db, "CREATE TEMPORARY TABLE IF NOT EXISTS SP_TEMP_SEQ_CURRVAL ( " \
" SEQ_NAME TEXT PRIMARY KEY, " \
" CURRVAL INTEGER " \
" )", 0, 0, 0);
if( rc != SQLITE_OK ) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
is_temp_table_created = 1;
}
sqlite3_prepare_v2(db, "update SP_TEMP_SEQ_CURRVAL set currval = ? where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_int64(stmt, 1, nextval);
sqlite3_bind_text(stmt, 2, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
update_row_count = sqlite3_changes(db);
if (update_row_count == 0) {
// update not done, value has to be inserted
sqlite3_prepare_v2(db, "insert into SP_TEMP_SEQ_CURRVAL (seq_name, currval) values (?,?)", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 2, nextval);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
}
sqlite3_result_int64( context, nextval );
}
void sp_seq_currval(sqlite3_context *context, int argc, sqlite3_value **argv) {
int rc = 0;
sqlite3_stmt *stmt;
long currval = 0;
sqlite3 *db = sqlite3_context_db_handle(context);
const unsigned char* seq_name = sqlite3_value_text(argv[0]);
sqlite3_prepare_v2(db, "select currval from SP_TEMP_SEQ_CURRVAL where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if( rc == SQLITE_ROW) {
currval = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
if (rc != SQLITE_ROW) {
sqlite3_result_error(context, "currval is not yet defined in this session for this sequence", -1);
return;
}
sqlite3_result_int64( context, currval );
}
void sp_seq_drop(sqlite3_context *context, int argc, sqlite3_value **argv) {
int rc = 0;
sqlite3_stmt *stmt;
long currval = 0;
sqlite3 *db = sqlite3_context_db_handle(context);
const unsigned char* seq_name = sqlite3_value_text(argv[0]);
sqlite3_prepare_v2(db, "delete from sp_sequence where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
if (rc != SQLITE_DONE) {
sqlite3_result_error(context, sqlite3_errmsg(db), -1);
return;
}
sqlite3_prepare_v2(db, "delete from SP_TEMP_SEQ_CURRVAL where seq_name = ?", -1, &stmt, 0);
sqlite3_bind_text(stmt, 1, seq_name, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
// we don't really care if there is any error on the TEMP table
sqlite3_result_null( context );
}
int sqlite3_extension_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
SQLITE_EXTENSION_INIT2(pApi)
sqlite3_create_function(db, "seq_init", 3, SQLITE_UTF8, 0, sp_seq_init, 0, 0);
sqlite3_create_function(db, "seq_init", 2, SQLITE_UTF8, 0, sp_seq_init, 0, 0);
sqlite3_create_function(db, "seq_nextval", 1, SQLITE_UTF8, 0, sp_seq_nextval, 0, 0);
sqlite3_create_function(db, "seq_currval", 1, SQLITE_UTF8, 0, sp_seq_currval, 0, 0);
sqlite3_create_function(db, "seq_drop", 1, SQLITE_UTF8, 0, sp_seq_drop, 0, 0);
return 0;
}