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_DATA_MALFORMED 102
27
28#define MYSQL_DEFAULT_PREFETCH_ROWS (unsigned long) 1
29
30/* Bind flags */
31#define MADB_BIND_DUMMY 1
32
33#define MARIADB_STMT_BULK_SUPPORTED(stmt)\
34 ((stmt)->mysql && \
35 (!((stmt)->mysql->server_capabilities & CLIENT_MYSQL) &&\
36 ((stmt)->mysql->extension->mariadb_server_capabilities & \
37 (MARIADB_CLIENT_STMT_BULK_OPERATIONS >> 32))))
38
39#define MARIADB_STMT_BULK_UNIT_RESULTS_SUPPORTED(stmt)\
40 ((stmt)->mysql && \
41 (!((stmt)->mysql->server_capabilities & CLIENT_MYSQL) &&\
42 ((stmt)->mysql->extension->mariadb_client_flag & \
43 (MARIADB_CLIENT_BULK_UNIT_RESULTS >> 32))))
44
45#define CLEAR_CLIENT_STMT_ERROR(a) \
46do { \
47 (a)->last_errno= 0;\
48 strcpy((a)->sqlstate, "00000");\
49 (a)->last_error[0]= 0;\
50} while (0)
51
52#define MYSQL_PS_SKIP_RESULT_W_LEN -1
53#define MYSQL_PS_SKIP_RESULT_STR -2
54#define STMT_ID_LENGTH 4
55
56
57typedef struct st_mysql_stmt MYSQL_STMT;
58
59typedef MYSQL_RES* (*mysql_stmt_use_or_store_func)(MYSQL_STMT *);
60
61enum enum_stmt_attr_type
62{
63 STMT_ATTR_UPDATE_MAX_LENGTH,
64 STMT_ATTR_CURSOR_TYPE,
65 STMT_ATTR_PREFETCH_ROWS,
66
67 /* MariaDB only */
68 STMT_ATTR_PREBIND_PARAMS=200,
69 STMT_ATTR_ARRAY_SIZE,
70 STMT_ATTR_ROW_SIZE,
71 STMT_ATTR_STATE,
72 STMT_ATTR_CB_USER_DATA,
73 STMT_ATTR_CB_PARAM,
74 STMT_ATTR_CB_RESULT,
75 STMT_ATTR_SQL_STATEMENT,
76 STMT_ATTR_CB_RESULT_EX
77};
78
79enum enum_cursor_type
80{
81 CURSOR_TYPE_NO_CURSOR= 0,
82 CURSOR_TYPE_READ_ONLY= 1,
83 CURSOR_TYPE_FOR_UPDATE= 2,
84 CURSOR_TYPE_SCROLLABLE= 4
85};
86
87enum enum_indicator_type
88{
89 STMT_INDICATOR_NTS=-1,
90 STMT_INDICATOR_NONE=0,
91 STMT_INDICATOR_NULL=1,
92 STMT_INDICATOR_DEFAULT=2,
93 STMT_INDICATOR_IGNORE=3,
94 STMT_INDICATOR_IGNORE_ROW=4
95};
96
97/*
98 bulk PS flags
99*/
100#define STMT_BULK_FLAG_CLIENT_SEND_TYPES 128
101#define STMT_BULK_FLAG_SEND_UNIT_RESULTS 64
102
103typedef enum mysql_stmt_state
104{
105 MYSQL_STMT_INITTED = 0,
106 MYSQL_STMT_PREPARED,
107 MYSQL_STMT_EXECUTED,
108 MYSQL_STMT_WAITING_USE_OR_STORE,
109 MYSQL_STMT_USE_OR_STORE_CALLED,
110 MYSQL_STMT_USER_FETCHING, /* fetch_row_buff or fetch_row_unbuf */
111 MYSQL_STMT_FETCH_DONE
112} enum_mysqlnd_stmt_state;
113
114typedef struct st_mysql_bind
115{
116 unsigned long *length; /* output length pointer */
117 my_bool *is_null; /* Pointer to null indicator */
118 void *buffer; /* buffer to get/put data */
119 /* set this if you want to track data truncations happened during fetch */
120 my_bool *error;
121 union {
122 unsigned char *row_ptr; /* for the current data position */
123 char *indicator; /* indicator variable */
124 } u;
125 void (*store_param_func)(NET *net, struct st_mysql_bind *param);
126 void (*fetch_result)(struct st_mysql_bind *, MYSQL_FIELD *,
127 unsigned char **row);
128 void (*skip_result)(struct st_mysql_bind *, MYSQL_FIELD *,
129 unsigned char **row);
130 /* output buffer length, must be set when fetching str/binary */
131 unsigned long buffer_length;
132 unsigned long offset; /* offset position for char/binary fetch */
133 unsigned long length_value; /* Used if length is 0 */
134 unsigned int flags; /* special flags, e.g. for dummy bind */
135 unsigned int pack_length; /* Internal length for packed data */
136 enum enum_field_types buffer_type; /* buffer type */
137 my_bool error_value; /* used if error is 0 */
138 my_bool is_unsigned; /* set if integer type is unsigned */
139 my_bool long_data_used; /* If used with mysql_send_long_data */
140 my_bool is_null_value; /* Used if is_null is 0 */
141 void *extension;
142} MYSQL_BIND;
143
144typedef struct st_mysqlnd_upsert_result
145{
146 unsigned int warning_count;
147 unsigned int server_status;
148 unsigned long long affected_rows;
149 unsigned long long last_insert_id;
150} mysql_upsert_status;
151
152typedef struct st_mysql_cmd_buffer
153{
154 unsigned char *buffer;
155 size_t length;
156} MYSQL_CMD_BUFFER;
157
158typedef struct st_mysql_error_info
159{
160 unsigned int error_no;
161 char error[MYSQL_ERRMSG_SIZE+1];
162 char sqlstate[SQLSTATE_LENGTH + 1];
163} mysql_error_info;
164
165typedef int (*mysql_stmt_fetch_row_func)(MYSQL_STMT *stmt, unsigned char **row, unsigned long *length);
166typedef void (*ps_result_callback)(void *data, unsigned int column, unsigned char **row);
167typedef int (*ps_result_callback_ex)(void *data, unsigned int column, unsigned char **row, const unsigned char *end);
168typedef my_bool (*ps_param_callback)(void *data, MYSQL_BIND *bind, unsigned int row_nr);
169
170struct st_mysql_stmt
171{
172 MA_MEM_ROOT mem_root;
173 MYSQL *mysql;
174 unsigned long stmt_id;
175 unsigned long flags;/* cursor is set here */
176 enum_mysqlnd_stmt_state state;
177 MYSQL_FIELD *fields;
178 unsigned int field_count;
179 unsigned int param_count;
180 unsigned char send_types_to_server;
181 MYSQL_BIND *params;
182 MYSQL_BIND *bind;
183 MYSQL_DATA result; /* we don't use mysqlnd's result set logic */
184 MYSQL_ROWS *result_cursor;
185 my_bool bind_result_done;
186 my_bool bind_param_done;
187
188 mysql_upsert_status upsert_status;
189
190 unsigned int last_errno;
191 char last_error[MYSQL_ERRMSG_SIZE+1];
192 char sqlstate[SQLSTATE_LENGTH + 1];
193
194 my_bool update_max_length;
195 unsigned long prefetch_rows;
196 LIST list;
197
198 my_bool cursor_exists;
199
200 void *extension;
201 mysql_stmt_fetch_row_func fetch_row_func;
202 unsigned int execute_count;/* count how many times the stmt was executed */
203 mysql_stmt_use_or_store_func default_rset_handler;
204 unsigned char *request_buffer;
205 unsigned int array_size;
206 size_t row_size;
207 unsigned int prebind_params;
208 void *user_data;
209 union {
210 ps_result_callback result_callback;
211 ps_result_callback_ex result_callback_ex;
212 } u;
213 ps_param_callback param_callback;
214 size_t request_length;
215 unsigned char result_callback_type;
216 MARIADB_CONST_STRING sql;
217};
218
219typedef void (*ps_field_fetch_func)(MYSQL_BIND *r_param, const MYSQL_FIELD * field, unsigned char **row);
220typedef struct st_mysql_perm_bind {
221 ps_field_fetch_func func;
222 /* should be signed int */
223 int pack_len;
224 unsigned long max_len;
225} MYSQL_PS_CONVERSION;
226
227extern MYSQL_PS_CONVERSION mysql_ps_fetch_functions[MYSQL_TYPE_GEOMETRY + 1];
228unsigned long ma_net_safe_read(MYSQL *mysql);
229void mysql_init_ps_subsystem(void);
230unsigned long net_field_length(unsigned char **packet);
231int ma_simple_command(MYSQL *mysql,enum enum_server_command command, const char *arg,
232 size_t length, my_bool skipp_check, void *opt_arg);
233void stmt_set_error(MYSQL_STMT *stmt,
234 unsigned int error_nr,
235 const char *sqlstate,
236 const char *format,
237 ...);
238/*
239 * function prototypes
240 */
241MYSQL_STMT * STDCALL mysql_stmt_init(MYSQL *mysql);
242int STDCALL mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, unsigned long length);
243int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt);
244int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt);
245int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind_arg, unsigned int column, unsigned long offset);
246int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt);
247unsigned long STDCALL mysql_stmt_param_count(MYSQL_STMT * stmt);
248my_bool STDCALL mysql_stmt_attr_set(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, const void *attr);
249my_bool STDCALL mysql_stmt_attr_get(MYSQL_STMT *stmt, enum enum_stmt_attr_type attr_type, void *attr);
250my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
251my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bnd);
252my_bool STDCALL mysql_stmt_close(MYSQL_STMT * stmt);
253my_bool STDCALL mysql_stmt_reset(MYSQL_STMT * stmt);
254my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt);
255my_bool STDCALL mysql_stmt_send_long_data(MYSQL_STMT *stmt, unsigned int param_number, const char *data, unsigned long length);
256MYSQL_RES *STDCALL mysql_stmt_result_metadata(MYSQL_STMT *stmt);
257MYSQL_RES *STDCALL mysql_stmt_param_metadata(MYSQL_STMT *stmt);
258unsigned int STDCALL mysql_stmt_errno(MYSQL_STMT * stmt);
259const char *STDCALL mysql_stmt_error(MYSQL_STMT * stmt);
260const char *STDCALL mysql_stmt_sqlstate(MYSQL_STMT * stmt);
261MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET offset);
262MYSQL_ROW_OFFSET STDCALL mysql_stmt_row_tell(MYSQL_STMT *stmt);
263void STDCALL mysql_stmt_data_seek(MYSQL_STMT *stmt, unsigned long long offset);
264unsigned long long STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt);
265unsigned long long STDCALL mysql_stmt_affected_rows(MYSQL_STMT *stmt);
266unsigned long long STDCALL mysql_stmt_insert_id(MYSQL_STMT *stmt);
267unsigned int STDCALL mysql_stmt_field_count(MYSQL_STMT *stmt);
268int STDCALL mysql_stmt_next_result(MYSQL_STMT *stmt);
269my_bool STDCALL mysql_stmt_more_results(MYSQL_STMT *stmt);
270int STDCALL mariadb_stmt_execute_direct(MYSQL_STMT *stmt, const char *stmt_str, size_t length);
271MYSQL_FIELD * STDCALL mariadb_stmt_fetch_fields(MYSQL_STMT *stmt);
272