21
This file is included by both libdrizzle.c (the DRIZZLE client C API)
21
This file is included by both libdrizzleclient.c (the DRIZZLE client C API)
22
22
and the drizzled server to connect to another DRIZZLE server.
24
24
The differences for the two cases are:
26
26
- Things that only works for the client:
27
27
- Trying to automaticly determinate user name if not supplied to
28
drizzleclient_connect()
29
29
- Support for reading local file with LOAD DATA LOCAL
30
30
- SHARED memory handling
31
31
- Prepared statements
33
33
- Things that only works for the server
34
34
- Alarm handling on connect
36
36
In all other cases, the code should be idential for the client and
41
#include "libdrizzle_priv.h"
45
#include "drizzle_methods.h"
40
47
#include <stdarg.h>
42
#include <libdrizzle/libdrizzle.h>
43
#include <libdrizzle/net_serv.h>
44
#include "libdrizzle_priv.h"
46
48
#include <sys/poll.h>
47
49
#include <sys/ioctl.h>
76
76
#include <sys/un.h>
79
#define SOCKET_ERROR -1
82
#include <drizzled/version.h>
83
#include <libdrizzle/sql_common.h>
84
#include <libdrizzle/gettext.h>
85
#include "local_infile.h"
81
#include <drizzled/gettext.h>
82
#include "libdrizzle.h"
94
86
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
95
87
struct passwd *getpwuid(uid_t);
101
92
/*****************************************************************************
102
93
Read a packet from server. Give error message if socket was down
103
94
or packet is an error message
104
95
*****************************************************************************/
106
uint32_t cli_safe_read(DRIZZLE *drizzle)
97
/* I'm not sure if this is even used anymore, but now that libdrizzleclient is
98
server only, this is safe to set here. */
99
extern "C" bool safe_read_error_impl(NET *net);
101
bool safe_read_error_impl(NET *net)
104
return drizzleclient_vio_was_interrupted(net->vio);
109
safe_read_error_hook_func safe_read_error_hook= safe_read_error_impl;
111
uint32_t drizzleclient_cli_safe_read(DRIZZLE *drizzle)
108
113
NET *net= &drizzle->net;
111
116
if (net->vio != 0)
112
len=my_net_read(net);
117
len=drizzleclient_net_read(net);
114
119
if (len == packet_error || len == 0)
116
#ifdef DRIZZLE_SERVER
117
if (net->vio && vio_was_interrupted(net->vio))
118
return (packet_error);
119
#endif /*DRIZZLE_SERVER*/
120
drizzle_disconnect(drizzle);
121
drizzle_set_error(drizzle, net->last_errno == CR_NET_PACKET_TOO_LARGE ?
121
if (safe_read_error_hook != NULL)
122
if (safe_read_error_hook(net))
123
return (packet_error);
124
drizzleclient_disconnect(drizzle);
125
drizzleclient_set_error(drizzle, net->last_errno == CR_NET_PACKET_TOO_LARGE ?
122
126
CR_NET_PACKET_TOO_LARGE : CR_SERVER_LOST,
123
sqlstate_get_unknown());
127
drizzleclient_sqlstate_get_unknown());
124
128
return (packet_error);
126
130
if (net->read_pos[0] == 255)
134
138
if (pos[0] == '#')
136
strncpy(net->sqlstate, pos+1, SQLSTATE_LENGTH);
137
pos+= SQLSTATE_LENGTH+1;
140
strncpy(net->sqlstate, pos+1, LIBDRIZZLE_SQLSTATE_LENGTH);
141
pos+= LIBDRIZZLE_SQLSTATE_LENGTH+1;
143
147
(unknown error sql state).
146
strcpy(net->sqlstate, sqlstate_get_unknown());
150
strcpy(net->sqlstate, drizzleclient_sqlstate_get_unknown());
149
153
strncpy(net->last_error,(char*) pos, min((uint32_t) len,
150
154
(uint32_t) sizeof(net->last_error)-1));
153
drizzle_set_error(drizzle, CR_UNKNOWN_ERROR, sqlstate_get_unknown());
157
drizzleclient_set_error(drizzle, CR_UNKNOWN_ERROR, drizzleclient_sqlstate_get_unknown());
155
159
Cover a protocol design error: error packet does not
156
160
contain the server status. Therefore, the client has no way
171
cli_advanced_command(DRIZZLE *drizzle, enum enum_server_command command,
175
drizzleclient_cli_advanced_command(DRIZZLE *drizzle, enum enum_server_command command,
172
176
const unsigned char *header, uint32_t header_length,
173
177
const unsigned char *arg, uint32_t arg_length, bool skip_check)
179
183
if (drizzle->net.vio == 0)
180
184
{ /* Do reconnect if possible */
181
if (drizzle_reconnect(drizzle) || stmt_skip)
185
if (drizzleclient_reconnect(drizzle) || stmt_skip)
184
188
if (drizzle->status != DRIZZLE_STATUS_READY ||
185
189
drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
187
drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC,
188
sqlstate_get_unknown());
191
drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC,
192
drizzleclient_sqlstate_get_unknown());
192
net_clear_error(net);
196
drizzleclient_drizzleclient_net_clear_error(net);
194
198
drizzle->affected_rows= ~(uint64_t) 0;
197
201
the previous command was a shutdown command, we may have the
198
202
response for the COM_QUIT already in the communication buffer
200
net_clear(&drizzle->net, (command != COM_QUIT));
204
drizzleclient_net_clear(&drizzle->net, (command != COM_QUIT));
202
if (net_write_command(net,(unsigned char) command, header, header_length,
206
if (drizzleclient_net_write_command(net,(unsigned char) command, header, header_length,
203
207
arg, arg_length))
205
209
if (net->last_errno == CR_NET_PACKET_TOO_LARGE)
207
drizzle_set_error(drizzle, CR_NET_PACKET_TOO_LARGE, sqlstate_get_unknown());
211
drizzleclient_set_error(drizzle, CR_NET_PACKET_TOO_LARGE, drizzleclient_sqlstate_get_unknown());
210
drizzle_disconnect(drizzle);
211
if (drizzle_reconnect(drizzle) || stmt_skip)
214
drizzleclient_disconnect(drizzle);
215
if (drizzleclient_reconnect(drizzle) || stmt_skip)
213
if (net_write_command(net,(unsigned char) command, header, header_length,
217
if (drizzleclient_net_write_command(net,(unsigned char) command, header, header_length,
214
218
arg, arg_length))
216
drizzle_set_error(drizzle, CR_SERVER_GONE_ERROR, sqlstate_get_unknown());
220
drizzleclient_set_error(drizzle, CR_SERVER_GONE_ERROR, drizzleclient_sqlstate_get_unknown());
222
result= ((drizzle->packet_length=cli_safe_read(drizzle)) == packet_error ?
226
result= ((drizzle->packet_length=drizzleclient_cli_safe_read(drizzle)) == packet_error ?
228
void free_old_query(DRIZZLE *drizzle)
232
void drizzleclient_free_old_query(DRIZZLE *drizzle)
230
234
if (drizzle->fields)
284
288
/* Read all rows (fields or data) from server */
286
DRIZZLE_DATA *cli_read_rows(DRIZZLE *drizzle, DRIZZLE_FIELD *DRIZZLE_FIELDs, uint32_t fields)
290
DRIZZLE_DATA *drizzleclient_cli_read_rows(DRIZZLE *drizzle, DRIZZLE_FIELD *DRIZZLE_FIELDs, uint32_t fields)
289
293
uint32_t pkt_len;
294
298
DRIZZLE_ROWS **prev_ptr,*cur;
295
299
NET *net = &drizzle->net;
297
if ((pkt_len= cli_safe_read(drizzle)) == packet_error)
301
if ((pkt_len= drizzleclient_cli_safe_read(drizzle)) == packet_error)
299
303
if (!(result=(DRIZZLE_DATA*) malloc(sizeof(DRIZZLE_DATA))))
301
drizzle_set_error(drizzle, CR_OUT_OF_MEMORY,
302
sqlstate_get_unknown());
305
drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY,
306
drizzleclient_sqlstate_get_unknown());
305
309
memset(result, 0, sizeof(DRIZZLE_DATA));
311
315
The last EOF packet is either a 254 (0xFE) character followed by 1-7 status bytes.
313
317
This doesn't conflict with normal usage of 254 which stands for a
314
string where the length of the string is 8 bytes. (see net_field_length())
318
string where the length of the string is 8 bytes. (see drizzleclient_net_field_length())
317
321
while (*(cp=net->read_pos) != DRIZZLE_PROTOCOL_NO_MORE_DATA || pkt_len >= 8)
320
324
if (!(cur= (DRIZZLE_ROWS*) malloc(sizeof(DRIZZLE_ROWS))) ||
321
325
!(cur->data= ((DRIZZLE_ROW) malloc((fields+1)*sizeof(char *)+pkt_len))))
324
drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
327
drizzleclient_free_rows(result);
328
drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
339
343
cur->data[field] = to;
340
344
if (len > (uint32_t) (end_to - to))
343
drizzle_set_error(drizzle, CR_MALFORMED_PACKET,
344
sqlstate_get_unknown());
346
drizzleclient_free_rows(result);
347
drizzleclient_set_error(drizzle, CR_MALFORMED_PACKET,
348
drizzleclient_sqlstate_get_unknown());
347
351
memcpy(to, cp, len);
385
389
unsigned char *pos, *prev_pos, *end_pos;
386
390
NET *net= &drizzle->net;
388
if ((pkt_len=cli_safe_read(drizzle)) == packet_error)
392
if ((pkt_len=drizzleclient_cli_safe_read(drizzle)) == packet_error)
390
394
if (pkt_len <= 8 && net->read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA)
411
415
if (len > (uint32_t) (end_pos - pos))
413
drizzle_set_error(drizzle, CR_UNKNOWN_ERROR,
414
sqlstate_get_unknown());
417
drizzleclient_set_error(drizzle, CR_UNKNOWN_ERROR,
418
drizzleclient_sqlstate_get_unknown());
417
421
row[field] = (char*) pos;
442
446
DRIZZLE *drizzle= res->handle;
443
447
if (drizzle->status != DRIZZLE_STATUS_USE_RESULT)
445
drizzle_set_error(drizzle,
449
drizzleclient_set_error(drizzle,
446
450
res->unbuffered_fetch_cancelled ?
447
451
CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
448
sqlstate_get_unknown());
452
drizzleclient_sqlstate_get_unknown());
450
454
else if (!(read_one_row(drizzle, res->field_count, res->row, res->lengths)))
461
465
if (drizzle->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
462
466
drizzle->unbuffered_fetch_owner= 0;
463
/* Don't clear handle in drizzle_free_result */
467
/* Don't clear handle in drizzleclient_free_result */
466
470
return((DRIZZLE_ROW) NULL);
481
485
/**************************************************************************
482
486
Get column lengths of the current row
483
If one uses drizzle_use_result, res->lengths contains the length information,
487
If one uses drizzleclient_use_result, res->lengths contains the length information,
484
488
else the lengths are calculated from the offset between pointers.
485
489
**************************************************************************/
488
drizzle_fetch_lengths(DRIZZLE_RES *res)
492
drizzleclient_fetch_lengths(DRIZZLE_RES *res)
490
494
DRIZZLE_ROW column;
501
drizzle_options(DRIZZLE *drizzle,enum drizzle_option option, const void *arg)
505
drizzleclient_options(DRIZZLE *drizzle,enum drizzle_option option, const void *arg)
503
507
switch (option) {
504
508
case DRIZZLE_OPT_CONNECT_TIMEOUT:
514
518
drizzle->options.compress= 1; /* Remember for connect */
515
519
drizzle->options.client_flag|= CLIENT_COMPRESS;
517
case DRIZZLE_OPT_LOCAL_INFILE: /* Allow LOAD DATA LOCAL ?*/
518
if (!arg || (*(uint32_t*) arg) ? 1 : 0)
519
drizzle->options.client_flag|= CLIENT_LOCAL_FILES;
521
drizzle->options.client_flag&= ~CLIENT_LOCAL_FILES;
523
521
case DRIZZLE_READ_DEFAULT_FILE:
524
522
if (drizzle->options.my_cnf_file != NULL)
525
523
free(drizzle->options.my_cnf_file);
526
drizzle->options.my_cnf_file=strdup(arg);
524
drizzle->options.my_cnf_file=strdup((char *)arg);
528
526
case DRIZZLE_READ_DEFAULT_GROUP:
529
527
if (drizzle->options.my_cnf_group != NULL)
530
528
free(drizzle->options.my_cnf_group);
531
drizzle->options.my_cnf_group=strdup(arg);
529
drizzle->options.my_cnf_group=strdup((char *)arg);
533
531
case DRIZZLE_OPT_PROTOCOL:
537
535
drizzle->options.methods_to_use= option;
539
537
case DRIZZLE_SET_CLIENT_IP:
540
drizzle->options.client_ip= strdup(arg);
538
drizzle->options.client_ip= strdup((char *)arg);
542
540
case DRIZZLE_SECURE_AUTH:
543
541
drizzle->options.secure_auth= *(const bool *) arg;
567
565
****************************************************************************/
569
567
/* DRIZZLE_RES */
570
uint64_t drizzle_num_rows(const DRIZZLE_RES *res)
568
uint64_t drizzleclient_num_rows(const DRIZZLE_RES *res)
572
570
return res->row_count;
575
unsigned int drizzle_num_fields(const DRIZZLE_RES *res)
573
unsigned int drizzleclient_num_fields(const DRIZZLE_RES *res)
577
575
return res->field_count;
599
drizzle_get_server_version(const DRIZZLE *drizzle)
597
drizzleclient_get_server_version(const DRIZZLE *drizzle)
601
599
uint32_t major, minor, version;
602
600
char *pos= drizzle->server_version, *end_pos;