55
51
#include <stdlib.h>
56
52
#include <string.h>
57
#include <mystrings/utf8.h>
54
#include <sql_common.h>
55
#include <drizzled/version.h>
57
/* Borrowed from libicu header */
59
#define U8_IS_SINGLE(c) (((c)&0x80)==0)
60
#define U8_LENGTH(c) \
61
((uint32_t)(c)<=0x7f ? 1 : \
62
((uint32_t)(c)<=0x7ff ? 2 : \
63
((uint32_t)(c)<=0xd7ff ? 3 : \
64
((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
65
((uint32_t)(c)<=0xffff ? 3 : 4)\
60
72
#undef net_buffer_length
66
78
unsigned int drizzle_port=0;
81
#define SOCKET_ERROR -1
84
If allowed through some configuration, then this needs to
87
#define MAX_LONG_DATA_LENGTH 8192
88
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
71
91
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
72
92
{&max_allowed_packet, &net_buffer_length, 0};
74
const DRIZZLE_PARAMETERS * drizzleclient_get_parameters(void)
94
const DRIZZLE_PARAMETERS * drizzle_get_parameters(void)
76
96
return &drizzle_internal_parameters;
79
unsigned int drizzleclient_get_default_port(void)
99
unsigned int drizzle_get_default_port(void)
81
101
return drizzle_port;
84
void drizzleclient_set_default_port(unsigned int port)
104
void drizzle_set_default_port(unsigned int port)
86
106
drizzle_port= port;
116
136
Change user and database
117
137
**************************************************************************/
119
int drizzleclient_cli_read_change_user_result(DRIZZLE *drizzle)
139
int cli_read_change_user_result(DRIZZLE *drizzle)
121
141
uint32_t pkt_length;
123
pkt_length= drizzleclient_cli_safe_read(drizzle);
143
pkt_length= cli_safe_read(drizzle);
125
145
if (pkt_length == packet_error)
131
bool drizzleclient_change_user(DRIZZLE *drizzle, const char *user,
151
bool drizzle_change_user(DRIZZLE *drizzle, const char *user,
132
152
const char *passwd, const char *db)
134
154
char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
197
217
/**************************************************************************
198
218
Do a query. If query returned rows, free old rows.
199
Read data by drizzleclient_store_result or by repeat call of drizzleclient_fetch_row
219
Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
200
220
**************************************************************************/
203
drizzleclient_query(DRIZZLE *drizzle, const char *query)
223
drizzle_query(DRIZZLE *drizzle, const char *query)
205
return drizzleclient_real_query(drizzle,query, (uint32_t) strlen(query));
225
return drizzle_real_query(drizzle,query, (uint32_t) strlen(query));
237
257
/*************************************************************************
238
258
put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
239
This doesn't restore any data. The next drizzleclient_fetch_row or
240
drizzleclient_fetch_field will return the next row or field after the last used
259
This doesn't restore any data. The next drizzle_fetch_row or
260
drizzle_fetch_field will return the next row or field after the last used
241
261
*************************************************************************/
243
263
DRIZZLE_ROW_OFFSET
244
drizzleclient_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
264
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
246
266
DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
247
267
result->current_row= 0;
265
285
*****************************************************************************/
268
drizzleclient_list_tables(DRIZZLE *drizzle, const char *wild)
288
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
271
291
char *ptr= strcpy(buff, "show tables");
272
292
ptr+= 11; /* strlen("show tables"); */
274
294
append_wild(ptr,buff+sizeof(buff),wild);
275
if (drizzleclient_query(drizzle,buff))
295
if (drizzle_query(drizzle,buff))
277
return (drizzleclient_store_result(drizzle));
297
return (drizzle_store_result(drizzle));
281
DRIZZLE_FIELD *drizzleclient_cli_list_fields(DRIZZLE *drizzle)
301
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
283
303
DRIZZLE_DATA *query;
284
if (!(query= drizzleclient_cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
304
if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
287
307
drizzle->field_count= (uint32_t) query->rows;
288
return drizzleclient_unpack_fields(query, drizzle->field_count, 1);
308
return unpack_fields(query, drizzle->field_count, 1);
306
326
end= strncpy(buff, table, 128) + 128;
307
327
end= strncpy(end+1, wild ? wild : "", 128) + 128;
309
drizzleclient_free_old_query(drizzle);
329
free_old_query(drizzle);
310
330
if (simple_command(drizzle, COM_FIELD_LIST, (unsigned char*) buff,
311
331
(uint32_t) (end-buff), 1) ||
312
332
!(fields= (*drizzle->methods->list_fields)(drizzle)))
337
357
if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
339
drizzleclient_free_old_query(drizzle);
359
free_old_query(drizzle);
340
360
pos=(unsigned char*) drizzle->net.read_pos;
341
field_count=(uint32_t) drizzleclient_net_field_length(&pos);
361
field_count=(uint32_t) net_field_length(&pos);
342
362
if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0, 7)))
344
if (!(drizzle->fields=drizzleclient_unpack_fields(fields, field_count, 0)))
364
if (!(drizzle->fields=unpack_fields(fields, field_count, 0)))
346
366
drizzle->status=DRIZZLE_STATUS_GET_RESULT;
347
367
drizzle->field_count=field_count;
348
return(drizzleclient_store_result(drizzle));
368
return(drizzle_store_result(drizzle));
353
drizzleclient_shutdown(DRIZZLE *drizzle)
373
drizzle_shutdown(DRIZZLE *drizzle)
355
375
return(simple_command(drizzle, COM_SHUTDOWN, 0, 0, 0));
360
drizzleclient_refresh(DRIZZLE *drizzle, uint32_t options)
380
drizzle_refresh(DRIZZLE *drizzle, uint32_t options)
362
382
unsigned char bits[1];
363
383
bits[0]= (unsigned char) options;
386
const char *drizzleclient_cli_read_statistics(DRIZZLE *drizzle)
406
const char *cli_read_statistics(DRIZZLE *drizzle)
388
408
drizzle->net.read_pos[drizzle->packet_length]=0; /* End of stat string */
389
409
if (!drizzle->net.read_pos[0])
391
drizzleclient_set_error(drizzle, CR_WRONG_HOST_INFO, drizzleclient_sqlstate_get_unknown());
411
drizzle_set_error(drizzle, CR_WRONG_HOST_INFO, sqlstate_get_unknown());
392
412
return drizzle->net.last_error;
394
414
return (char*) drizzle->net.read_pos;
410
drizzleclient_get_server_info(const DRIZZLE *drizzle)
430
drizzle_get_server_info(const DRIZZLE *drizzle)
412
432
return((char*) drizzle->server_version);
417
drizzleclient_get_host_info(const DRIZZLE *drizzle)
437
drizzle_get_host_info(const DRIZZLE *drizzle)
419
439
return(drizzle->host_info);
424
drizzleclient_get_proto_info(const DRIZZLE *drizzle)
444
drizzle_get_proto_info(const DRIZZLE *drizzle)
426
446
return (drizzle->protocol_version);
430
drizzleclient_get_client_info(void)
450
drizzle_get_client_info(void)
432
return (char*) VERSION;
452
return (char*) DRIZZLE_SERVER_VERSION;
435
uint32_t drizzleclient_get_client_version(void)
455
uint32_t drizzle_get_client_version(void)
437
457
return DRIZZLE_VERSION_ID;
440
bool drizzleclient_eof(const DRIZZLE_RES *res)
460
bool drizzle_eof(const DRIZZLE_RES *res)
445
const DRIZZLE_FIELD * drizzleclient_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
465
const DRIZZLE_FIELD * drizzle_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
447
467
return &(res)->fields[fieldnr];
450
const DRIZZLE_FIELD * drizzleclient_fetch_fields(const DRIZZLE_RES *res)
470
const DRIZZLE_FIELD * drizzle_fetch_fields(const DRIZZLE_RES *res)
452
472
return res->fields;
455
DRIZZLE_ROW_OFFSET drizzleclient_row_tell(const DRIZZLE_RES *res)
475
DRIZZLE_ROW_OFFSET drizzle_row_tell(const DRIZZLE_RES *res)
457
477
return res->data_cursor;
460
DRIZZLE_FIELD_OFFSET drizzleclient_field_tell(const DRIZZLE_RES *res)
480
DRIZZLE_FIELD_OFFSET drizzle_field_tell(const DRIZZLE_RES *res)
462
482
return res->current_field;
467
unsigned int drizzleclient_field_count(const DRIZZLE *drizzle)
487
unsigned int drizzle_field_count(const DRIZZLE *drizzle)
469
489
return drizzle->field_count;
472
uint64_t drizzleclient_affected_rows(const DRIZZLE *drizzle)
492
uint64_t drizzle_affected_rows(const DRIZZLE *drizzle)
474
494
return drizzle->affected_rows;
477
uint64_t drizzleclient_insert_id(const DRIZZLE *drizzle)
497
uint64_t drizzle_insert_id(const DRIZZLE *drizzle)
479
499
return drizzle->insert_id;
482
const char * drizzleclient_sqlstate(const DRIZZLE *drizzle)
502
const char * drizzle_sqlstate(const DRIZZLE *drizzle)
484
return drizzle ? drizzle->net.sqlstate : drizzleclient_sqlstate_get_cant_connect();
504
return drizzle ? drizzle->net.sqlstate : sqlstate_get_cant_connect();
487
uint32_t drizzleclient_warning_count(const DRIZZLE *drizzle)
507
uint32_t drizzle_warning_count(const DRIZZLE *drizzle)
489
509
return drizzle->warning_count;
492
const char * drizzleclient_info(const DRIZZLE *drizzle)
512
const char * drizzle_info(const DRIZZLE *drizzle)
494
514
return drizzle->info;
497
uint32_t drizzleclient_thread_id(const DRIZZLE *drizzle)
517
uint32_t drizzle_thread_id(const DRIZZLE *drizzle)
499
519
return drizzle->thread_id;
504
524
****************************************************************************/
507
Functions called my drizzleclient_net_init() to set some application specific variables
527
Functions called my my_net_init() to set some application specific variables
510
void drizzleclient_net_local_init(NET *net)
530
void my_net_local_init(NET *net)
512
532
net->max_packet= (uint32_t) net_buffer_length;
513
drizzleclient_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
514
drizzleclient_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
533
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
534
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
515
535
net->retry_count= 1;
516
536
net->max_packet_size= (net_buffer_length > max_allowed_packet) ?
517
537
net_buffer_length : max_allowed_packet;
541
This function is used to create HEX string that you
542
can use in a SQL statement in of the either ways:
543
INSERT INTO blob_column VALUES (0xAABBCC); (any DRIZZLE version)
544
INSERT INTO blob_column VALUES (X'AABBCC');
546
The string in "from" is encoded to a HEX string.
547
The result is placed in "to" and a terminating null byte is appended.
549
The string pointed to by "from" must be "length" bytes long.
550
You must allocate the "to" buffer to be at least length*2+1 bytes long.
551
Each character needs two bytes, and you need room for the terminating
552
null byte. When drizzle_hex_string() returns, the contents of "to" will
553
be a null-terminated string. The return value is the length of the
554
encoded string, not including the terminating null character. The return value does not contain any leading 0x or a leading X' and
555
trailing '. The caller must supply whichever of those is desired.
559
drizzle_hex_string(char *to, const char *from, uint32_t length)
564
for (end= from + length; from < end; from++)
566
*to++= _dig_vec_upper[((unsigned char) *from) >> 4];
567
*to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
570
return (uint32_t) (to-to0);
521
574
Add escape characters to a string (blob?) to make it suitable for a insert
522
575
to should at least have place for length*2+1 chars
523
576
Returns the length of the to string
527
drizzleclient_escape_string(char *to,const char *from, uint32_t length)
580
drizzle_escape_string(char *to,const char *from, uint32_t length)
529
582
const char *to_start= to;
530
583
const char *end, *to_end=to_start + 2*length;
593
646
return overflow ? (size_t) -1 : (size_t) (to - to_start);
596
int drizzleclient_cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
649
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
598
if (packet_error == drizzleclient_cli_safe_read(drizzle))
651
if (packet_error == cli_safe_read(drizzle))
601
654
*row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
611
664
Commit the current transaction
614
bool drizzleclient_commit(DRIZZLE *drizzle)
667
bool drizzle_commit(DRIZZLE *drizzle)
616
return((bool) drizzleclient_real_query(drizzle, "commit", 6));
669
return((bool) drizzle_real_query(drizzle, "commit", 6));
620
673
Rollback the current transaction
623
bool drizzleclient_rollback(DRIZZLE *drizzle)
676
bool drizzle_rollback(DRIZZLE *drizzle)
625
return((bool) drizzleclient_real_query(drizzle, "rollback", 8));
678
return((bool) drizzle_real_query(drizzle, "rollback", 8));
630
683
Set autocommit to either true or false
633
bool drizzleclient_autocommit(DRIZZLE *drizzle, bool auto_mode)
686
bool drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode)
635
return((bool) drizzleclient_real_query(drizzle, auto_mode ?
688
return((bool) drizzle_real_query(drizzle, auto_mode ?
636
689
"set autocommit=1":"set autocommit=0",
657
710
Reads and returns the next query results
659
int drizzleclient_next_result(DRIZZLE *drizzle)
712
int drizzle_next_result(DRIZZLE *drizzle)
661
714
if (drizzle->status != DRIZZLE_STATUS_READY)
663
drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, drizzleclient_sqlstate_get_unknown());
716
drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
667
drizzleclient_drizzleclient_net_clear_error(&drizzle->net);
720
net_clear_error(&drizzle->net);
668
721
drizzle->affected_rows= ~(uint64_t) 0;
670
723
if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)