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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
|
/* Copyright (C) 2000-2004 DRIZZLE AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL as it
is applied to this software. View the full text of the exception in file
EXCEPTIONS-CLIENT in the directory of this software distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <my_global.h>
#include <my_sys.h>
#include <my_time.h>
#include <mysys_err.h>
#include <m_string.h>
#include <m_ctype.h>
#include "drizzle.h"
#include "drizzle_version.h"
#include "mysqld_error.h"
#include "errmsg.h"
#include <violite.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_SELECT_H
#include <select.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include <my_pthread.h> /* because of signal() */
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#include <sql_common.h>
#include "client_settings.h"
#undef net_buffer_length
#undef max_allowed_packet
uint32_t net_buffer_length= 8192;
uint32_t max_allowed_packet= 1024L*1024L*1024L;
#include <errno.h>
#define SOCKET_ERROR -1
/*
If allowed through some configuration, then this needs to
be changed
*/
#define MAX_LONG_DATA_LENGTH 8192
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
static void append_wild(char *to,char *end,const char *wild);
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
{&max_allowed_packet, &net_buffer_length, 0};
DRIZZLE_PARAMETERS *STDCALL drizzle_get_parameters(void)
{
return &drizzle_internal_parameters;
}
my_bool STDCALL drizzle_thread_init()
{
return my_thread_init();
}
void STDCALL drizzle_thread_end()
{
my_thread_end();
}
/*
Expand wildcard to a sql string
*/
static void
append_wild(char *to, char *end, const char *wild)
{
end-=5; /* Some extra */
if (wild && wild[0])
{
to=strmov(to," like '");
while (*wild && to < end)
{
if (*wild == '\\' || *wild == '\'')
*to++='\\';
*to++= *wild++;
}
if (*wild) /* Too small buffer */
*to++='%'; /* Nicer this way */
to[0]='\'';
to[1]=0;
}
}
/**************************************************************************
Ignore SIGPIPE handler
ARGSUSED
**************************************************************************/
sig_handler
my_pipe_sig_handler(int sig __attribute__((unused)))
{
#ifdef DONT_REMEMBER_SIGNAL
(void) signal(SIGPIPE, my_pipe_sig_handler);
#endif
}
/**************************************************************************
Connect to sql server
If host == 0 then use localhost
**************************************************************************/
#ifdef USE_OLD_FUNCTIONS
DRIZZLE * STDCALL
drizzle_connect(DRIZZLE *drizzle,const char *host,
const char *user, const char *passwd)
{
DRIZZLE *res;
drizzle=drizzle_init(drizzle); /* Make it thread safe */
{
if (!(res=drizzle_connect(drizzle,host,user,passwd,NullS,0,NullS,0)))
{
if (drizzle->free_me)
my_free((uchar*) drizzle,MYF(0));
}
drizzle->reconnect= 1;
return(res);
}
}
#endif
/**************************************************************************
Change user and database
**************************************************************************/
int cli_read_change_user_result(DRIZZLE *drizzle, char *buff, const char *passwd)
{
(void)buff;
(void)passwd;
ulong pkt_length;
pkt_length= cli_safe_read(drizzle);
if (pkt_length == packet_error)
return 1;
return 0;
}
my_bool STDCALL drizzle_change_user(DRIZZLE *drizzle, const char *user,
const char *passwd, const char *db)
{
char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
char *end= buff;
int rc;
CHARSET_INFO *saved_cs= drizzle->charset;
/* Get the connection-default character set. */
if (drizzle_init_character_set(drizzle))
{
drizzle->charset= saved_cs;
return(true);
}
/* Use an empty string instead of NULL. */
if (!user)
user="";
if (!passwd)
passwd="";
/* Store user into the buffer */
end= strmake(end, user, USERNAME_LENGTH) + 1;
/* write scrambled password according to server capabilities */
if (passwd[0])
{
{
*end++= SCRAMBLE_LENGTH;
scramble(end, drizzle->scramble, passwd);
end+= SCRAMBLE_LENGTH;
}
}
else
*end++= '\0'; /* empty password */
/* Add database if needed */
end= strmake(end, db ? db : "", NAME_LEN) + 1;
/* Add character set number. */
if (drizzle->server_capabilities & CLIENT_SECURE_CONNECTION)
{
int2store(end, (ushort) drizzle->charset->number);
end+= 2;
}
/* Write authentication package */
(void)simple_command(drizzle,COM_CHANGE_USER, (uchar*) buff, (ulong) (end-buff), 1);
rc= (*drizzle->methods->read_change_user_result)(drizzle, buff, passwd);
if (rc == 0)
{
/* Free old connect information */
my_free(drizzle->user,MYF(MY_ALLOW_ZERO_PTR));
my_free(drizzle->passwd,MYF(MY_ALLOW_ZERO_PTR));
my_free(drizzle->db,MYF(MY_ALLOW_ZERO_PTR));
/* alloc new connect information */
drizzle->user= my_strdup(user,MYF(MY_WME));
drizzle->passwd=my_strdup(passwd,MYF(MY_WME));
drizzle->db= db ? my_strdup(db,MYF(MY_WME)) : 0;
}
else
{
drizzle->charset= saved_cs;
}
return(rc);
}
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
struct passwd *getpwuid(uid_t);
char* getlogin(void);
#endif
void read_user_name(char *name)
{
if (geteuid() == 0)
(void) strmov(name,"root"); /* allow use of surun */
else
{
#ifdef HAVE_GETPWUID
struct passwd *skr;
const char *str;
if ((str=getlogin()) == NULL)
{
if ((skr=getpwuid(geteuid())) != NULL)
str=skr->pw_name;
else if (!(str=getenv("USER")) && !(str=getenv("LOGNAME")) &&
!(str=getenv("LOGIN")))
str="UNKNOWN_USER";
}
(void) strmake(name,str,USERNAME_LENGTH);
#elif HAVE_CUSERID
(void) cuserid(name);
#else
strmov(name,"UNKNOWN_USER");
#endif
}
return;
}
my_bool handle_local_infile(DRIZZLE *drizzle, const char *net_filename)
{
my_bool result= 1;
uint packet_length=MY_ALIGN(drizzle->net.max_packet-16,IO_SIZE);
NET *net= &drizzle->net;
int readcount;
void *li_ptr; /* pass state to local_infile functions */
char *buf; /* buffer to be filled by local_infile_read */
struct st_drizzle_options *options= &drizzle->options;
/* check that we've got valid callback functions */
if (!(options->local_infile_init &&
options->local_infile_read &&
options->local_infile_end &&
options->local_infile_error))
{
/* if any of the functions is invalid, set the default */
drizzle_set_local_infile_default(drizzle);
}
/* copy filename into local memory and allocate read buffer */
if (!(buf=my_malloc(packet_length, MYF(0))))
{
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
return(1);
}
/* initialize local infile (open file, usually) */
if ((*options->local_infile_init)(&li_ptr, net_filename,
options->local_infile_userdata))
{
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
net_flush(net);
strmov(net->sqlstate, unknown_sqlstate);
net->last_errno=
(*options->local_infile_error)(li_ptr,
net->last_error,
sizeof(net->last_error)-1);
goto err;
}
/* read blocks of data from local infile callback */
while ((readcount =
(*options->local_infile_read)(li_ptr, buf,
packet_length)) > 0)
{
if (my_net_write(net, (uchar*) buf, readcount))
{
goto err;
}
}
/* Send empty packet to mark end of file */
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
{
set_drizzle_error(drizzle, CR_SERVER_LOST, unknown_sqlstate);
goto err;
}
if (readcount < 0)
{
net->last_errno=
(*options->local_infile_error)(li_ptr,
net->last_error,
sizeof(net->last_error)-1);
goto err;
}
result=0; /* Ok */
err:
/* free up memory allocated with _init, usually */
(*options->local_infile_end)(li_ptr);
my_free(buf, MYF(0));
return(result);
}
/****************************************************************************
Default handlers for LOAD LOCAL INFILE
****************************************************************************/
typedef struct st_default_local_infile
{
int fd;
int error_num;
const char *filename;
char error_msg[LOCAL_INFILE_ERROR_LEN];
} default_local_infile_data;
/*
Open file for LOAD LOCAL INFILE
SYNOPSIS
default_local_infile_init()
ptr Store pointer to internal data here
filename File name to open. This may be in unix format !
NOTES
Even if this function returns an error, the load data interface
guarantees that default_local_infile_end() is called.
RETURN
0 ok
1 error
*/
static int default_local_infile_init(void **ptr, const char *filename,
void *userdata __attribute__ ((unused)))
{
default_local_infile_data *data;
char tmp_name[FN_REFLEN];
if (!(*ptr= data= ((default_local_infile_data *)
my_malloc(sizeof(default_local_infile_data), MYF(0)))))
return 1; /* out of memory */
data->error_msg[0]= 0;
data->error_num= 0;
data->filename= filename;
fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
{
data->error_num= my_errno;
snprintf(data->error_msg, sizeof(data->error_msg)-1,
EE(EE_FILENOTFOUND), tmp_name, data->error_num);
return 1;
}
return 0; /* ok */
}
/*
Read data for LOAD LOCAL INFILE
SYNOPSIS
default_local_infile_read()
ptr Points to handle allocated by _init
buf Read data here
buf_len Ammount of data to read
RETURN
> 0 number of bytes read
== 0 End of data
< 0 Error
*/
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
{
int count;
default_local_infile_data*data = (default_local_infile_data *) ptr;
if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
{
data->error_num= EE_READ; /* the errmsg for not entire file read */
snprintf(data->error_msg, sizeof(data->error_msg)-1,
EE(EE_READ),
data->filename, my_errno);
}
return count;
}
/*
Read data for LOAD LOCAL INFILE
SYNOPSIS
default_local_infile_end()
ptr Points to handle allocated by _init
May be NULL if _init failed!
RETURN
*/
static void default_local_infile_end(void *ptr)
{
default_local_infile_data *data= (default_local_infile_data *) ptr;
if (data) /* If not error on open */
{
if (data->fd >= 0)
my_close(data->fd, MYF(MY_WME));
my_free(ptr, MYF(MY_WME));
}
}
/*
Return error from LOAD LOCAL INFILE
SYNOPSIS
default_local_infile_end()
ptr Points to handle allocated by _init
May be NULL if _init failed!
error_msg Store error text here
error_msg_len Max lenght of error_msg
RETURN
error message number
*/
static int
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
{
default_local_infile_data *data = (default_local_infile_data *) ptr;
if (data) /* If not error on open */
{
strmake(error_msg, data->error_msg, error_msg_len);
return data->error_num;
}
/* This can only happen if we got error on malloc of handle */
strmov(error_msg, ER(CR_OUT_OF_MEMORY));
return CR_OUT_OF_MEMORY;
}
void
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
int (*local_infile_init)(void **, const char *,
void *),
int (*local_infile_read)(void *, char *, uint),
void (*local_infile_end)(void *),
int (*local_infile_error)(void *, char *, uint),
void *userdata)
{
drizzle->options.local_infile_init= local_infile_init;
drizzle->options.local_infile_read= local_infile_read;
drizzle->options.local_infile_end= local_infile_end;
drizzle->options.local_infile_error= local_infile_error;
drizzle->options.local_infile_userdata = userdata;
}
void drizzle_set_local_infile_default(DRIZZLE *drizzle)
{
drizzle->options.local_infile_init= default_local_infile_init;
drizzle->options.local_infile_read= default_local_infile_read;
drizzle->options.local_infile_end= default_local_infile_end;
drizzle->options.local_infile_error= default_local_infile_error;
}
/**************************************************************************
Do a query. If query returned rows, free old rows.
Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
**************************************************************************/
int STDCALL
drizzle_query(DRIZZLE *drizzle, const char *query)
{
return drizzle_real_query(drizzle,query, (uint) strlen(query));
}
/**************************************************************************
Return next field of the query results
**************************************************************************/
DRIZZLE_FIELD * STDCALL
drizzle_fetch_field(DRIZZLE_RES *result)
{
if (result->current_field >= result->field_count)
return(NULL);
return &result->fields[result->current_field++];
}
/**************************************************************************
Move to a specific row and column
**************************************************************************/
void STDCALL
drizzle_data_seek(DRIZZLE_RES *result, uint64_t row)
{
DRIZZLE_ROWS *tmp=0;
if (result->data)
for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
result->current_row=0;
result->data_cursor = tmp;
}
/*************************************************************************
put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
This doesn't restore any data. The next drizzle_fetch_row or
drizzle_fetch_field will return the next row or field after the last used
*************************************************************************/
DRIZZLE_ROW_OFFSET STDCALL
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
{
DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
result->current_row= 0;
result->data_cursor= row;
return return_value;
}
DRIZZLE_FIELD_OFFSET STDCALL
drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
{
DRIZZLE_FIELD_OFFSET return_value=result->current_field;
result->current_field=field_offset;
return return_value;
}
/*****************************************************************************
List all databases
*****************************************************************************/
DRIZZLE_RES * STDCALL
drizzle_list_dbs(DRIZZLE *drizzle, const char *wild)
{
char buff[255];
append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild);
if (drizzle_query(drizzle,buff))
return(0);
return (drizzle_store_result(drizzle));
}
/*****************************************************************************
List all tables in a database
If wild is given then only the tables matching wild is returned
*****************************************************************************/
DRIZZLE_RES * STDCALL
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
{
char buff[255];
append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild);
if (drizzle_query(drizzle,buff))
return(0);
return (drizzle_store_result(drizzle));
}
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
{
DRIZZLE_DATA *query;
if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0,
protocol_41(drizzle) ? 8 : 6)))
return NULL;
drizzle->field_count= (uint) query->rows;
return unpack_fields(query,&drizzle->field_alloc,
drizzle->field_count, 1, drizzle->server_capabilities);
}
/**************************************************************************
List all fields in a table
If wild is given then only the fields matching wild is returned
Instead of this use query:
show fields in 'table' like "wild"
**************************************************************************/
DRIZZLE_RES * STDCALL
drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
{
DRIZZLE_RES *result;
DRIZZLE_FIELD *fields;
char buff[257],*end;
end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128);
free_old_query(drizzle);
if (simple_command(drizzle, COM_FIELD_LIST, (uchar*) buff,
(ulong) (end-buff), 1) ||
!(fields= (*drizzle->methods->list_fields)(drizzle)))
return(NULL);
if (!(result = (DRIZZLE_RES *) my_malloc(sizeof(DRIZZLE_RES),
MYF(MY_WME | MY_ZEROFILL))))
return(NULL);
result->methods= drizzle->methods;
result->field_alloc=drizzle->field_alloc;
drizzle->fields=0;
result->field_count = drizzle->field_count;
result->fields= fields;
result->eof=1;
return(result);
}
/* List all running processes (threads) in server */
DRIZZLE_RES * STDCALL
drizzle_list_processes(DRIZZLE *drizzle)
{
DRIZZLE_DATA *fields;
uint field_count;
uchar *pos;
if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
return(0);
free_old_query(drizzle);
pos=(uchar*) drizzle->net.read_pos;
field_count=(uint) net_field_length(&pos);
if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0,
protocol_41(drizzle) ? 7 : 5)))
return(NULL);
if (!(drizzle->fields=unpack_fields(fields,&drizzle->field_alloc,field_count,0,
drizzle->server_capabilities)))
return(0);
drizzle->status=DRIZZLE_STATUS_GET_RESULT;
drizzle->field_count=field_count;
return(drizzle_store_result(drizzle));
}
#ifdef USE_OLD_FUNCTIONS
int STDCALL
drizzle_create_db(DRIZZLE *drizzle, const char *db)
{
return(simple_command(drizzle,COM_CREATE_DB,db, (ulong) strlen(db),0));
}
int STDCALL
drizzle_drop_db(DRIZZLE *drizzle, const char *db)
{
return(simple_command(drizzle,COM_DROP_DB,db,(ulong) strlen(db),0));
}
#endif
int STDCALL
drizzle_shutdown(DRIZZLE *drizzle, enum drizzle_enum_shutdown_level shutdown_level)
{
uchar level[1];
level[0]= (uchar) shutdown_level;
return(simple_command(drizzle, COM_SHUTDOWN, level, 1, 0));
}
int STDCALL
drizzle_refresh(DRIZZLE *drizzle,uint options)
{
uchar bits[1];
bits[0]= (uchar) options;
return(simple_command(drizzle, COM_REFRESH, bits, 1, 0));
}
int32_t STDCALL
drizzle_kill(DRIZZLE *drizzle, uint32_t pid)
{
uchar buff[4];
int4store(buff,pid);
return(simple_command(drizzle,COM_PROCESS_KILL,buff,sizeof(buff),0));
}
int STDCALL
drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
{
uchar buff[2];
int2store(buff, (uint) option);
return(simple_command(drizzle, COM_SET_OPTION, buff, sizeof(buff), 0));
}
const char *cli_read_statistics(DRIZZLE *drizzle)
{
drizzle->net.read_pos[drizzle->packet_length]=0; /* End of stat string */
if (!drizzle->net.read_pos[0])
{
set_drizzle_error(drizzle, CR_WRONG_HOST_INFO, unknown_sqlstate);
return drizzle->net.last_error;
}
return (char*) drizzle->net.read_pos;
}
const char * STDCALL
drizzle_stat(DRIZZLE *drizzle)
{
if (simple_command(drizzle,COM_STATISTICS,0,0,0))
return(drizzle->net.last_error);
return((*drizzle->methods->read_statistics)(drizzle));
}
int STDCALL
drizzle_ping(DRIZZLE *drizzle)
{
int res;
res= simple_command(drizzle,COM_PING,0,0,0);
if (res == CR_SERVER_LOST && drizzle->reconnect)
res= simple_command(drizzle,COM_PING,0,0,0);
return(res);
}
const char * STDCALL
drizzle_get_server_info(DRIZZLE *drizzle)
{
return((char*) drizzle->server_version);
}
const char * STDCALL
drizzle_get_host_info(DRIZZLE *drizzle)
{
return(drizzle->host_info);
}
uint STDCALL
drizzle_get_proto_info(DRIZZLE *drizzle)
{
return (drizzle->protocol_version);
}
const char * STDCALL
drizzle_get_client_info(void)
{
return (char*) MYSQL_SERVER_VERSION;
}
uint32_t STDCALL drizzle_get_client_version(void)
{
return MYSQL_VERSION_ID;
}
my_bool STDCALL drizzle_eof(DRIZZLE_RES *res)
{
return res->eof;
}
DRIZZLE_FIELD * STDCALL drizzle_fetch_field_direct(DRIZZLE_RES *res,uint fieldnr)
{
return &(res)->fields[fieldnr];
}
DRIZZLE_FIELD * STDCALL drizzle_fetch_fields(DRIZZLE_RES *res)
{
return (res)->fields;
}
DRIZZLE_ROW_OFFSET STDCALL DRIZZLE_ROW_tell(DRIZZLE_RES *res)
{
return res->data_cursor;
}
DRIZZLE_FIELD_OFFSET STDCALL drizzle_field_tell(DRIZZLE_RES *res)
{
return (res)->current_field;
}
/* DRIZZLE */
unsigned int STDCALL drizzle_field_count(DRIZZLE *drizzle)
{
return drizzle->field_count;
}
uint64_t STDCALL drizzle_affected_rows(DRIZZLE *drizzle)
{
return drizzle->affected_rows;
}
uint64_t STDCALL drizzle_insert_id(DRIZZLE *drizzle)
{
return drizzle->insert_id;
}
const char *STDCALL drizzle_sqlstate(DRIZZLE *drizzle)
{
return drizzle ? drizzle->net.sqlstate : cant_connect_sqlstate;
}
uint32_t STDCALL drizzle_warning_count(DRIZZLE *drizzle)
{
return drizzle->warning_count;
}
const char *STDCALL drizzle_info(DRIZZLE *drizzle)
{
return drizzle->info;
}
uint32_t STDCALL drizzle_thread_id(DRIZZLE *drizzle)
{
return (drizzle)->thread_id;
}
const char * STDCALL drizzle_character_set_name(DRIZZLE *drizzle)
{
return drizzle->charset->csname;
}
void STDCALL drizzle_get_character_set_info(DRIZZLE *drizzle, MY_CHARSET_INFO *csinfo)
{
csinfo->number = drizzle->charset->number;
csinfo->state = drizzle->charset->state;
csinfo->csname = drizzle->charset->csname;
csinfo->name = drizzle->charset->name;
csinfo->comment = drizzle->charset->comment;
csinfo->mbminlen = drizzle->charset->mbminlen;
csinfo->mbmaxlen = drizzle->charset->mbmaxlen;
if (drizzle->options.charset_dir)
csinfo->dir = drizzle->options.charset_dir;
else
csinfo->dir = charsets_dir;
}
uint STDCALL drizzle_thread_safe(void)
{
return 1;
}
my_bool STDCALL drizzle_embedded(void)
{
#ifdef EMBEDDED_LIBRARY
return 1;
#else
return 0;
#endif
}
/****************************************************************************
Some support functions
****************************************************************************/
/*
Functions called my my_net_init() to set some application specific variables
*/
void my_net_local_init(NET *net)
{
net->max_packet= (uint) net_buffer_length;
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
net->retry_count= 1;
net->max_packet_size= max(net_buffer_length, max_allowed_packet);
}
/*
This function is used to create HEX string that you
can use in a SQL statement in of the either ways:
INSERT INTO blob_column VALUES (0xAABBCC); (any DRIZZLE version)
INSERT INTO blob_column VALUES (X'AABBCC'); (4.1 and higher)
The string in "from" is encoded to a HEX string.
The result is placed in "to" and a terminating null byte is appended.
The string pointed to by "from" must be "length" bytes long.
You must allocate the "to" buffer to be at least length*2+1 bytes long.
Each character needs two bytes, and you need room for the terminating
null byte. When drizzle_hex_string() returns, the contents of "to" will
be a null-terminated string. The return value is the length of the
encoded string, not including the terminating null character.
The return value does not contain any leading 0x or a leading X' and
trailing '. The caller must supply whichever of those is desired.
*/
uint32_t STDCALL
drizzle_hex_string(char *to, const char *from, uint32_t length)
{
char *to0= to;
const char *end;
for (end= from + length; from < end; from++)
{
*to++= _dig_vec_upper[((unsigned char) *from) >> 4];
*to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
}
*to= '\0';
return (uint32_t) (to-to0);
}
/*
Add escape characters to a string (blob?) to make it suitable for a insert
to should at least have place for length*2+1 chars
Returns the length of the to string
*/
uint32_t STDCALL
drizzle_escape_string(char *to,const char *from, uint32_t length)
{
return escape_string_for_mysql(default_charset_info, to, 0, from, length);
}
uint32_t STDCALL
drizzle_real_escape_string(DRIZZLE *drizzle, char *to,const char *from,
uint32_t length)
{
if (drizzle->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
return escape_quotes_for_mysql(drizzle->charset, to, 0, from, length);
return escape_string_for_mysql(drizzle->charset, to, 0, from, length);
}
void STDCALL
myodbc_remove_escape(DRIZZLE *drizzle,char *name)
{
char *to;
#ifdef USE_MB
my_bool use_mb_flag=use_mb(drizzle->charset);
char *end=NULL;
if (use_mb_flag)
for (end=name; *end ; end++) ;
#endif
for (to=name ; *name ; name++)
{
#ifdef USE_MB
int l;
if (use_mb_flag && (l = my_ismbchar( drizzle->charset, name , end ) ) )
{
while (l--)
*to++ = *name++;
name--;
continue;
}
#endif
if (*name == '\\' && name[1])
name++;
*to++= *name;
}
*to=0;
}
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
{
if (packet_error == cli_safe_read(drizzle))
return 1;
*row= ((drizzle->net.read_pos[0] == 254) ? NULL :
(char*) (drizzle->net.read_pos+1));
return 0;
}
/********************************************************************
Transactional APIs
*********************************************************************/
/*
Commit the current transaction
*/
my_bool STDCALL drizzle_commit(DRIZZLE *drizzle)
{
return((my_bool) drizzle_real_query(drizzle, "commit", 6));
}
/*
Rollback the current transaction
*/
my_bool STDCALL drizzle_rollback(DRIZZLE *drizzle)
{
return((my_bool) drizzle_real_query(drizzle, "rollback", 8));
}
/*
Set autocommit to either true or false
*/
my_bool STDCALL drizzle_autocommit(DRIZZLE *drizzle, my_bool auto_mode)
{
return((my_bool) drizzle_real_query(drizzle, auto_mode ?
"set autocommit=1":"set autocommit=0",
16));
}
/********************************************************************
Multi query execution + SPs APIs
*********************************************************************/
/*
Returns true/false to indicate whether any more query results exist
to be read using drizzle_next_result()
*/
my_bool STDCALL drizzle_more_results(DRIZZLE *drizzle)
{
my_bool res;
res= ((drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? 1: 0);
return(res);
}
/*
Reads and returns the next query results
*/
int STDCALL drizzle_next_result(DRIZZLE *drizzle)
{
if (drizzle->status != DRIZZLE_STATUS_READY)
{
set_drizzle_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
return(1);
}
net_clear_error(&drizzle->net);
drizzle->affected_rows= ~(uint64_t) 0;
if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
return((*drizzle->methods->next_result)(drizzle));
return(-1); /* No more results */
}
DRIZZLE_RES * STDCALL drizzle_use_result(DRIZZLE *drizzle)
{
return (*drizzle->methods->use_result)(drizzle);
}
my_bool STDCALL drizzle_read_query_result(DRIZZLE *drizzle)
{
return (*drizzle->methods->read_query_result)(drizzle);
}
|