1/************************************************************************
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public
14 License along with this library; if not, write to the Free
15 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 MA 02111-1301, USA
17
18 Part of this code includes code from PHP's mysqlnd extension
19 (written by Andrey Hristov, Georg Richter and Ulf Wendel), freely
20 available from http://www.php.net/software
21
22*************************************************************************/
23
24#define MYSQL_NO_DATA 100
25#define MYSQL_DATA_TRUNCATED 101
26#define MYSQL_DEFAULT_PREFETCH_ROWS (unsigned long) 1
27
28/* Bind flags */
29#define MADB_BIND_DUMMY 1
30
31#define MARIADB_STMT_BULK_SUPPORTED(stmt)\
32 ((stmt)->mysql && \
33 (!((stmt)->mysql->server_capabilities & CLIENT_MYSQL) &&\
34 ((stmt)->mysql->extension->mariadb_server_capabilities & \
35 (MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32))))
36
37#define SET_CLIENT_STMT_ERROR(a, b, c, d) \
38{ \
39 (a)->last_errno= (b);\
40 strncpy((a)->sqlstate, (c), SQLSTATE_LENGTH);\
41 (a)->sqlstate[SQLSTATE_LENGTH]= 0;\
42 strncpy((a)->last_error, (d) ? (d) : ER((b)), MYSQL_ERRMSG_SIZE);\
43 (a)->last_error[MYSQL_ERRMSG_SIZE - 1]= 0;\
44}
45
46#define CLEAR_CLIENT_STMT_ERROR(a) \
47{ \
48 (a)->last_errno= 0;\
49 strcpy((a)->sqlstate, "00000");\
50 (a)->last_error[0]= 0;\
51}
52
53#define MYSQL_PS_SKIP_RESULT_W_LEN -1
54#define MYSQL_PS_SKIP_RESULT_STR -2
55#define STMT_ID_LENGTH 4
56
57
58typedef struct st_mysql_stmt MYSQL_STMT;
59
60typedef MYSQL_RES* (*mysql_stmt_use_or_store_func)(MYSQL_STMT *);
61
62enum enum_stmt_attr_type
63{
64 STMT_ATTR_UPDATE_MAX_LENGTH,
65 STMT_ATTR_CURSOR_TYPE,
66 STMT_ATTR_PREFETCH_ROWS,
67
68 /* MariaDB only */
69 STMT_ATTR_PREBIND_PARAMS=200,
70 STMT_ATTR_ARRAY_SIZE,
71 STMT_ATTR_ROW_SIZE,
72 STMT_ATTR_STATE,
73 STMT_ATTR_CB_USER_DATA,
74 STMT_ATTR_CB_PARAM,
75 STMT_ATTR_CB_RESULT
76};
77
78enum enum_cursor_type
79{
80 CURSOR_TYPE_NO_CURSOR= 0,
81 CURSOR_TYPE_READ_ONLY= 1,
82 CURSOR_TYPE_FOR_UPDATE= 2,
83 CURSOR_TYPE_SCROLLABLE= 4
84};
85
86enum enum_indicator_type
87{
88 STMT_INDICATOR_NTS=-1,
89 STMT_INDICATOR_NONE=0,
90 STMT_INDICATOR_NULL=1,
91 STMT_INDICATOR_DEFAULT=2,
92 STMT_INDICATOR_IGNORE=3,
93 STMT_INDICATOR_IGNORE_ROW=4
94};
95
96/*
97 bulk PS flags
98*/
99#define STMT_BULK_FLAG_CLIENT_SEND_TYPES 128
100#define STMT_BULK_FLAG_INSERT_ID_REQUEST 64
101
102typedef enum mysql_stmt_state
103{
104 MYSQL_STMT_INITTED = 0,
105 MYSQL_STMT_PREPARED,
106 MYSQL_STMT_EXECUTED,
107 MYSQL_STMT_WAITING_USE_OR_STORE,
108 MYSQL_STMT_USE_OR_STORE_CALLED,
109 MYSQL_STMT_USER_FETCHING, /* fetch_row_buff or fetch_row_unbuf */
110 MYSQL_STMT_FETCH_DONE
111} enum_mysqlnd_stmt_state;
112
113typedef struct st_mysql_bind
114{
115 unsigned long *length; /* output length pointer */
116 my_bool *is_null; /* Pointer to null indicator */
117 void *buffer; /* buffer to get/put data */
118 /* set this if you want to track data truncations happened during fetch */
119 my_bool *error;
120 union {
121 unsigned char *row_ptr; /* for the current data position */
122 char *indicator; /* indicator variable */
123 } u;
124 void (*store_param_func)(NET *net, struct st_mysql_bind *param);
125 void (*fetch_result)(struct st_mysql_bind *, MYSQL_FIELD *,
126 unsigned char **row);
127 void (*skip_result)(struct st_mysql_bind *, MYSQL_FIELD *,
128 unsigned char **row);
129 /* output buffer length, must be set when fetching str/binary */
130 unsigned long buffer_length;
131 unsigned long offset; /* offset position for char/binary fetch */
132 unsigned long length_value; /* Used if length is 0 */
133 unsigned int flags; /* special flags, e.g. for dummy bind */
134 unsigned int pack_length; /* Internal length for packed data */
135 enum enum_field_types buffer_type; /* buffer type */
136 my_bool error_value; /* used if error is 0 */
137 my_bool is_unsigned; /* set if integer type is unsigned */
138 my_bool long_data_used; /* If used with mysql_send_long_data */
139 my_bool is_null_value; /* Used if is_null is 0 */
140 void *extension;
141} MYSQL_BIND;
142
143typedef struct st_mysqlnd_upsert_result
144{
145 unsigned int warning_count;
146 unsigned int server_status;
147 unsigned long long affected_rows;
148 unsigned long long last_insert_id;
149} mysql_upsert_status;
150
151typedef struct st_mysql_cmd_buffer
152{
153 unsigned char *buffer;
154 size_t length;
155} MYSQL_CMD_BUFFER;
156
157typedef struct st_mysql_error_info
158{
159 unsigned int error_no;
160 char error[MYSQL_ERRMSG_SIZE+1];
161 char sqlstate[SQLSTATE_LENGTH + 1];
162} mysql_error_info;
163
164
165struct st_mysqlnd_stmt_methods
166{
167 my_bool (*prepare)(const MYSQL_STMT * stmt, const char * const query, size_t query_len);
168 my_bool (*execute)(const MYSQL_STMT * stmt);
169 MYSQL_RES * (*use_result)(const MYSQL_STMT * stmt);
170 MYSQL_RES * (*store_result)(const MYSQL_STMT * stmt);
171 MYSQL_RES * (*get_result)(const MYSQL_STMT * stmt);
172 my_bool (*free_result)(const MYSQL_STMT * stmt);
173 my_bool (*seek_data)(const MYSQL_STMT * stmt, unsigned long long row);
174 my_bool (*reset)(const MYSQL_STMT * stmt);
175 my_bool (*close)(const MYSQL_STMT * stmt); /* private */
176 my_bool (*dtor)(const MYSQL_STMT * stmt); /* use this for mysqlnd_stmt_close */
177
178 my_bool (*fetch)(const MYSQL_STMT * stmt, my_bool * const fetched_anything);
179
180 my_bool (*bind_param)(const MYSQL_STMT * stmt, const MYSQL_BIND bind);
181 my_bool (*refresh_bind_param)(const MYSQL_STMT * stmt);
182 my_bool (*bind_result)(const MYSQL_STMT * stmt, const MYSQL_BIND *bind);
183 my_bool (*send_long_data)(const MYSQL_STMT * stmt, unsigned int param_num,
184 const char * const data, size_t length);
185 MYSQL_RES *(*get_parameter_metadata)(const MYSQL_STMT * stmt);
186 MYSQL_RES *(*get_result_metadata)(const MYSQL_STMT * stmt);
187 unsigned long long (*get_last_insert_id)(const MYSQL_STMT * stmt);
188 unsigned long long (*get_affected_rows)(const MYSQL_STMT * stmt);
189 unsigned long long (*get_num_rows)(const MYSQL_STMT * stmt);
190
191 unsigned int (*get_param_count)(const MYSQL_STMT * stmt);
192 unsigned int (*get_field_count)(const MYSQL_STMT * stmt);
193 unsigned int (*get_warning_count)(const MYSQL_STMT * stmt);
194
195 unsigned int (*get_error_no)(const MYSQL_STMT * stmt);
196 const char * (*get_error_str)(const MYSQL_STMT * stmt);
197 const char * (*get_sqlstate)(const MYSQL_STMT * stmt);
198
199 my_bool (*get_attribute)(const MYSQL_STMT * stmt, enum enum_stmt_attr_type attr_type, const void * value);
200 my_bool (*set_attribute)(const MYSQL_STMT * stmt, enum enum_stmt_attr_type attr_type, const void * value);
201 void (*set_error)(MYSQL_STMT *stmt, unsigned int error_nr, const char *sqlstate, const char *format, ...);
202};
203
204typedef int (*mysql_stmt_fetch_row_func)(MYSQL_STMT *stmt, unsigned char **row);
205typedef void (*ps_result_callback)(void *data, unsigned int column, unsigned char **row);
206typedef my_bool *(*ps_param_callback)(void *data, MYSQL_BIND *bind, unsigned int row_nr);
207
208struct st_mysql_stmt
209{
210 MA_MEM_ROOT mem_root;
211 MYSQL *mysql;
212 unsigned long stmt_id;
213 unsigned long flags;/* cursor is set here */
214 enum_mysqlnd_stmt_state state;
215 MYSQL_FIELD *fields;
216 unsigned int field_count;
217 unsigned int param_count;
218 unsigned char send_types_to_server;
219 MYSQL_BIND *params;
220 MYSQL_BIND *bind;
221 MYSQL_DATA result; /* we don't use mysqlnd's result set logic */
222 MYSQL_ROWS *result_cursor;
223 my_bool bind_result_done;
224 my_bool bind_param_done;
225
226 mysql_upsert_status upsert_status;
227
228 unsigned int last_errno;
229 char last_error[MYSQL_ERRMSG_SIZE+1];
230 char sqlstate[SQLSTATE_LENGTH + 1];
231
232 my_bool update_max_length;
233 unsigned long prefetch_rows;
234 LIST list;
235
236 my_bool cursor_exists;
237
238 void *extension;
239 mysql_stmt_fetch_row_func fetch_row_func;
240 unsigned int execute_count;/* count how many times the stmt was executed */
241 mysql_stmt_use_or_store_func default_rset_handler;
242 struct st_mysqlnd_stmt_methods *m;
243 unsigned int array_size;
244 size_t row_size;
245 unsigned int prebind_params;
246 void *user_data;
247 ps_result_callback result_callback;
248 ps_param_callback param_callback;
249};
250
251typedef void (*ps_field_fetch_func)(MYSQL_BIND *r_param, const MYSQL_FIELD * field, unsigned char **row);
252typedef struct st_mysql_perm_bind {
253 ps_field_fetch_func func;
254 /* should be signed int */
255 int pack_len;
256 unsigned long max_len;
257} MYSQL_PS_CONVERSION;
258
259extern MYSQL_PS_CONVERSION mysql_ps_fetch_functions[MYSQL_TYPE_GEOMETRY + 1];
260unsigned long ma_net_safe_read(MYSQL *mysql);
261void mysql_init_ps_subsystem(void);
262unsigned long net_field_length(unsigned char **packet);
263int ma_simple_command(MYSQL *mysql,enum enum_server_command command, const char *arg,
264 size_t length, my_bool skipp_check, void *opt_arg);
265/*
266 * function prototypes
267 */
268MYSQL_STMT * STDCALL mysql_stmt_init(MYSQL *mysql);
269int STDCALL mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, unsigned long length);
270int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt);
271int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt);
272int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind_arg, unsigned int column, unsigned long offset);
273int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt);
274unsigned long STDCALL mysql_stmt_param_count(MYSQL_STMT * stmt);
275my_bool STDCALL mysql_stmt_attr_set(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, const void *attr);
276my_bool STDCALL mysql_stmt_attr_get(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, void *attr);
277my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
278my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
279my_bool STDCALL mysql_stmt_close(MYSQL_STMT * stmt);
280my_bool STDCALL mysql_stmt_reset(MYSQL_STMT * stmt);
281my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt);
282my_bool STDCALL mysql_stmt_send_long_data(MYSQL_STMT *stmt, unsigned int param_number, const char *data, unsigned long length);
283MYSQL_RES *STDCALL mysql_stmt_result_metadata(MYSQL_STMT *stmt);
284MYSQL_RES *STDCALL mysql_stmt_param_metadata(MYSQL_STMT *stmt);
285unsigned int STDCALL mysql_stmt_errno(MYSQL_STMT * stmt);
286const char *STDCALL mysql_stmt_error(MYSQL_STMT * stmt);
287const char *STDCALL mysql_stmt_sqlstate(MYSQL_STMT * stmt);
288MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET offset);
289MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_tell(MYSQL_STMT *stmt);
290void STDCALL mysql_stmt_data_seek(MYSQL_STMT *stmt, unsigned long long offset);
291unsigned long long STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt);
292unsigned long long STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt);
293unsigned long long STDCALL mysql_stmt_insert_id(MYSQL_STMT *stmt);
294unsigned int STDCALL mysql_stmt_field_count(MYSQL_STMT *stmt);
295int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt);
296my_bool STDCALL mysql_stmt_more_results(MYSQL_STMT *stmt);
297int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, const char *stmt_str, size_t length);
298MYSQL_FIELD * STDCALL mariadb_stmt_fetch_fields(MYSQL_STMT *stmt);
299