-
Notifications
You must be signed in to change notification settings - Fork 376
/
bind.c
197 lines (193 loc) · 5.31 KB
/
bind.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
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
/*
* Copyright 2010-2019, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``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
* <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 "bind.h"
#include "errcode.h"
#include "small/region.h"
#include "sql/sqlInt.h"
#include "sql/sqlLimit.h"
#include "sql/vdbe.h"
const char *
sql_bind_name(const struct sql_bind *bind)
{
if (bind->name)
return tt_sprintf("'%.*s'", bind->name_len, bind->name);
else
return tt_sprintf("%d", (int) bind->pos);
}
int
sql_bind_decode(struct sql_bind *bind, int i, const char **packet)
{
bind->pos = i + 1;
if (mp_typeof(**packet) == MP_MAP) {
uint32_t len = mp_decode_map(packet);
/*
* A named parameter is an MP_MAP with
* one key - {'name': value}.
* Report parse error otherwise.
*/
if (len != 1 || mp_typeof(**packet) != MP_STR) {
diag_set(ClientError, ER_INVALID_MSGPACK,
"SQL bind parameter");
return -1;
}
bind->name = mp_decode_str(packet, &bind->name_len);
} else {
bind->name = NULL;
bind->name_len = 0;
}
enum mp_type type = mp_typeof(**packet);
switch (type) {
case MP_UINT: {
uint64_t n = mp_decode_uint(packet);
bind->u64 = n;
bind->bytes = sizeof(bind->u64);
break;
}
case MP_INT:
bind->i64 = mp_decode_int(packet);
bind->bytes = sizeof(bind->i64);
break;
case MP_STR:
bind->s = mp_decode_str(packet, &bind->bytes);
break;
case MP_DOUBLE:
bind->d = mp_decode_double(packet);
bind->bytes = sizeof(bind->d);
break;
case MP_FLOAT:
bind->d = mp_decode_float(packet);
bind->bytes = sizeof(bind->d);
break;
case MP_NIL:
mp_decode_nil(packet);
bind->bytes = 1;
break;
case MP_BOOL:
bind->b = mp_decode_bool(packet);
bind->bytes = sizeof(bind->b);
break;
case MP_BIN:
bind->s = mp_decode_bin(packet, &bind->bytes);
break;
case MP_EXT:
bind->s = *packet;
mp_next(packet);
bind->bytes = *packet - bind->s;
break;
case MP_ARRAY:
diag_set(ClientError, ER_SQL_BIND_TYPE, "ARRAY",
sql_bind_name(bind));
return -1;
case MP_MAP:
diag_set(ClientError, ER_SQL_BIND_TYPE, "MAP",
sql_bind_name(bind));
return -1;
default:
unreachable();
}
bind->type = type;
return 0;
}
int
sql_bind_list_decode(const char *data, struct sql_bind **out_bind)
{
assert(data != NULL);
if (mp_typeof(*data) != MP_ARRAY) {
diag_set(ClientError, ER_INVALID_MSGPACK, "SQL parameter list");
return -1;
}
uint32_t bind_count = mp_decode_array(&data);
if (bind_count == 0)
return 0;
if (bind_count > SQL_BIND_PARAMETER_MAX) {
diag_set(ClientError, ER_SQL_BIND_PARAMETER_MAX,
(int) bind_count);
return -1;
}
struct region *region = &fiber()->gc;
uint32_t used = region_used(region);
size_t size = sizeof(struct sql_bind) * bind_count;
struct sql_bind *bind = (struct sql_bind *) region_alloc(region, size);
if (bind == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "struct sql_bind");
return -1;
}
for (uint32_t i = 0; i < bind_count; ++i) {
if (sql_bind_decode(&bind[i], i, &data) != 0) {
region_truncate(region, used);
return -1;
}
}
*out_bind = bind;
return bind_count;
}
int
sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p,
uint32_t pos)
{
if (p->name != NULL) {
pos = sql_bind_parameter_lindex(stmt, p->name, p->name_len);
if (pos == 0) {
diag_set(ClientError, ER_SQL_BIND_NOT_FOUND,
sql_bind_name(p));
return -1;
}
}
switch (p->type) {
case MP_INT:
return sql_bind_int64(stmt, pos, p->i64);
case MP_UINT:
return sql_bind_uint64(stmt, pos, p->u64);
case MP_BOOL:
return sql_bind_boolean(stmt, pos, p->b);
case MP_DOUBLE:
case MP_FLOAT:
return sql_bind_double(stmt, pos, p->d);
case MP_STR:
/*
* Parameters are allocated within message pack,
* received from the iproto thread. IProto thread
* now is waiting for the response and it will not
* free the packet until sql_finalize. So
* there is no need to copy the packet and we can
* use SQL_STATIC.
*/
return sql_bind_text64(stmt, pos, p->s, p->bytes, SQL_STATIC);
case MP_NIL:
return sql_bind_null(stmt, pos);
case MP_BIN:
return sql_bind_blob64(stmt, pos, (const void *) p->s, p->bytes,
SQL_STATIC);
default:
unreachable();
}
return 0;
}