forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmysql.cc
4563 lines (3956 loc) · 146 KB
/
libmysql.cc
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
/* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
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, version 2.0, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "my_config.h"
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <sys/types.h>
#include "m_ctype.h"
#include "m_string.h"
#include "my_alloc.h"
#include "my_sys.h"
#include "my_time.h"
#include "mysys_err.h"
#ifndef _WIN32
#include <netdb.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include "errmsg.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_double2ulonglong.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_pointer_arithmetic.h"
#include "my_thread_local.h"
#include "mysql.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_com.h"
#include "mysql_version.h"
#include "mysqld_error.h"
#include "template_utils.h"
#include "violite.h"
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_POLL
#include <poll.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#if !defined(_WIN32)
#include "my_thread.h" /* because of signal() */
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#include <memory>
#include "client_settings.h"
#include "mysql_trace.h"
#include "sql_common.h"
/*
Temporary replacement for COM_SHUTDOWN. This will be removed once
mysql_shutdown C API is removed.
*/
#define COM_SHUTDOWN_DEPRECATED 8
static void append_wild(char *to, char *end, const char *wild);
static bool mysql_client_init = 0;
static bool org_my_init_done = 0;
struct MYSQL_STMT_EXT {
MEM_ROOT fields_mem_root;
};
/*
Initialize the MySQL client library
SYNOPSIS
mysql_server_init()
NOTES
Should be called before doing any other calls to the MySQL
client library to initialize thread specific variables etc.
It's called by mysql_init() to ensure that things will work for
old not threaded applications that doesn't call mysql_server_init()
directly.
RETURN
0 ok
1 could not initialize environment (out of memory or thread keys)
*/
int STDCALL mysql_server_init(int argc MY_ATTRIBUTE((unused)),
char **argv MY_ATTRIBUTE((unused)),
char **groups MY_ATTRIBUTE((unused))) {
int result = 0;
if (!mysql_client_init) {
mysql_client_init = 1;
org_my_init_done = my_init_done;
if (my_init()) /* Will init threads */
return 1;
init_client_errs();
if (mysql_client_plugin_init()) return 1;
#if defined(HAVE_OPENSSL)
ssl_start();
#endif
if (!mysql_port) {
char *env;
struct servent *serv_ptr MY_ATTRIBUTE((unused));
mysql_port = MYSQL_PORT;
/*
if builder specifically requested a default port, use that
(even if it coincides with our factory default).
only if they didn't do we check /etc/services (and, failing
on that, fall back to the factory default of 3306).
either default can be overridden by the environment variable
MYSQL_TCP_PORT, which in turn can be overridden with command
line options.
*/
#if MYSQL_PORT_DEFAULT == 0
if ((serv_ptr = getservbyname("mysql", "tcp")))
mysql_port = (uint)ntohs((ushort)serv_ptr->s_port);
#endif
if ((env = getenv("MYSQL_TCP_PORT"))) mysql_port = (uint)atoi(env);
}
if (!mysql_unix_port) {
char *env;
#ifdef _WIN32
mysql_unix_port = (char *)MYSQL_NAMEDPIPE;
#else
mysql_unix_port = (char *)MYSQL_UNIX_ADDR;
#endif
if ((env = getenv("MYSQL_UNIX_PORT"))) mysql_unix_port = env;
}
mysql_debug(NullS);
#if defined(SIGPIPE) && !defined(_WIN32)
(void)signal(SIGPIPE, SIG_IGN);
#endif
} else
result = (int)my_thread_init(); /* Init if new thread */
return result;
}
/*
Free all memory and resources used by the client library
NOTES
When calling this there should not be any other threads using
the library.
To make things simpler when used with windows dll's (which calls this
function automaticly), it's safe to call this function multiple times.
*/
void STDCALL mysql_server_end() {
if (!mysql_client_init) return;
mysql_client_plugin_deinit();
finish_client_errs();
vio_end();
/* If library called my_init(), free memory allocated by it */
if (!org_my_init_done) {
my_end(0);
} else {
mysql_thread_end();
}
mysql_client_init = org_my_init_done = 0;
}
bool STDCALL mysql_thread_init() { return my_thread_init(); }
void STDCALL mysql_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 = my_stpcpy(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;
}
}
/**************************************************************************
Init debugging if MYSQL_DEBUG environment variable is found
**************************************************************************/
void STDCALL mysql_debug(const char *debug MY_ATTRIBUTE((unused))) {
#ifndef DBUG_OFF
char *env;
if (debug) {
DBUG_PUSH(debug);
} else if ((env = getenv("MYSQL_DEBUG"))) {
DBUG_PUSH(env);
#if !defined(_WINVER) && !defined(WINVER)
puts("\n-------------------------------------------------------");
puts("MYSQL_DEBUG found. libmysql started with the following:");
puts(env);
puts("-------------------------------------------------------\n");
#else
{
char buff[80];
buff[sizeof(buff) - 1] = 0;
strxnmov(buff, sizeof(buff) - 1, "libmysql: ", env, NullS);
MessageBox((HWND)0, "Debugging variable MYSQL_DEBUG used", buff, MB_OK);
}
#endif
}
#endif
}
/**************************************************************************
Change user and database
**************************************************************************/
bool STDCALL mysql_change_user(MYSQL *mysql, const char *user,
const char *passwd, const char *db) {
int rc;
CHARSET_INFO *saved_cs = mysql->charset;
char *saved_user = mysql->user;
char *saved_passwd = mysql->passwd;
char *saved_db = mysql->db;
DBUG_ENTER("mysql_change_user");
/* Get the connection-default character set. */
if (mysql_init_character_set(mysql)) {
mysql->charset = saved_cs;
DBUG_RETURN(true);
}
/*
Use an empty string instead of NULL.
Alloc user and password on heap because mysql_reconnect()
calls mysql_close() on success.
*/
mysql->user = my_strdup(PSI_NOT_INSTRUMENTED, user ? user : "", MYF(MY_WME));
mysql->passwd =
my_strdup(PSI_NOT_INSTRUMENTED, passwd ? passwd : "", MYF(MY_WME));
mysql->db = 0;
rc = run_plugin_auth(mysql, 0, 0, 0, db);
MYSQL_TRACE_STAGE(mysql, READY_FOR_COMMAND);
/*
The server will close all statements no matter was the attempt
to change user successful or not.
*/
mysql_detach_stmt_list(&mysql->stmts, "mysql_change_user");
if (rc == 0) {
/* Free old connect information */
my_free(saved_user);
my_free(saved_passwd);
my_free(saved_db);
/* alloc new connect information */
mysql->db = db ? my_strdup(PSI_NOT_INSTRUMENTED, db, MYF(MY_WME)) : 0;
} else {
/* Free temporary connect information */
my_free(mysql->user);
my_free(mysql->passwd);
my_free(mysql->db);
/* Restore saved state */
mysql->charset = saved_cs;
mysql->user = saved_user;
mysql->passwd = saved_passwd;
mysql->db = saved_db;
}
DBUG_RETURN(rc);
}
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
struct passwd *getpwuid(uid_t);
char *getlogin(void);
#endif
#if !defined(_WIN32)
void read_user_name(char *name) {
DBUG_ENTER("read_user_name");
if (geteuid() == 0)
(void)my_stpcpy(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
my_stpcpy(name, "UNKNOWN_USER");
#endif
}
DBUG_VOID_RETURN;
}
#else /* If Windows */
void read_user_name(char *name) {
char *str = getenv("USER"); /* ODBC will send user variable */
strmake(name, str ? str : "ODBC", USERNAME_LENGTH);
}
#endif
bool handle_local_infile(MYSQL *mysql, const char *net_filename) {
bool result = 1;
uint packet_length = MY_ALIGN(mysql->net.max_packet - 16, IO_SIZE);
NET *net = &mysql->net;
int readcount;
void *li_ptr; /* pass state to local_infile functions */
char *buf; /* buffer to be filled by local_infile_read */
struct st_mysql_options *options = &mysql->options;
DBUG_ENTER("handle_local_infile");
/* 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 */
mysql_set_local_infile_default(mysql);
}
/* copy filename into local memory and allocate read buffer */
if (!(buf = pointer_cast<char *>(
my_malloc(PSI_NOT_INSTRUMENTED, packet_length, MYF(0))))) {
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
DBUG_RETURN(1);
}
/* initialize local infile (open file, usually) */
if ((*options->local_infile_init)(&li_ptr, net_filename,
options->local_infile_userdata)) {
MYSQL_TRACE(SEND_FILE, mysql, (0, NULL));
(void)my_net_write(net, (const uchar *)"", 0); /* Server needs one packet */
net_flush(net);
MYSQL_TRACE(PACKET_SENT, mysql, (0));
my_stpcpy(net->sqlstate, unknown_sqlstate);
net->last_errno = (*options->local_infile_error)(
li_ptr, net->last_error, sizeof(net->last_error) - 1);
MYSQL_TRACE(ERROR, mysql, ());
goto err;
}
/* read blocks of data from local infile callback */
while ((readcount =
(*options->local_infile_read)(li_ptr, buf, packet_length)) > 0) {
MYSQL_TRACE(SEND_FILE, mysql,
((size_t)readcount, (const unsigned char *)buf));
if (my_net_write(net, (uchar *)buf, readcount)) {
DBUG_PRINT(
"error",
("Lost connection to MySQL server during LOAD DATA of local file"));
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
goto err;
}
MYSQL_TRACE(PACKET_SENT, mysql, (static_cast<size_t>(readcount)));
}
/* Send empty packet to mark end of file */
MYSQL_TRACE(SEND_FILE, mysql, (0, NULL));
if (my_net_write(net, (const uchar *)"", 0) || net_flush(net)) {
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
goto err;
}
MYSQL_TRACE(PACKET_SENT, mysql, (0));
if (readcount < 0) {
net->last_errno = (*options->local_infile_error)(
li_ptr, net->last_error, sizeof(net->last_error) - 1);
MYSQL_TRACE(ERROR, mysql, ());
goto err;
}
result = 0; /* Ok */
err:
/* free up memory allocated with _init, usually */
(*options->local_infile_end)(li_ptr);
my_free(buf);
DBUG_RETURN(result);
}
/****************************************************************************
Default handlers for LOAD LOCAL INFILE
****************************************************************************/
struct default_local_infile_data {
int fd;
int error_num;
const char *filename;
char error_msg[LOCAL_INFILE_ERROR_LEN];
};
/*
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 MY_ATTRIBUTE((unused))) {
default_local_infile_data *data;
char tmp_name[FN_REFLEN];
if (!(*ptr = data = ((default_local_infile_data *)my_malloc(
PSI_NOT_INSTRUMENTED, 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) {
char errbuf[MYSYS_STRERROR_SIZE];
data->error_num = my_errno();
snprintf(data->error_msg, sizeof(data->error_msg) - 1, EE(EE_FILENOTFOUND),
tmp_name, data->error_num,
my_strerror(errbuf, sizeof(errbuf), 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) {
char errbuf[MYSYS_STRERROR_SIZE];
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(),
my_strerror(errbuf, sizeof(errbuf), 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);
}
}
/*
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 */
my_stpcpy(error_msg, ER_CLIENT(CR_OUT_OF_MEMORY));
return CR_OUT_OF_MEMORY;
}
/*
Explicit extern "C" because otherwise solaris studio thinks
that the function pointer arguments have C++ linkage,
and then it overloads the declaration in include/mysql.h
*/
extern "C" void mysql_set_local_infile_handler(
MYSQL *mysql, 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) {
mysql->options.local_infile_init = local_infile_init;
mysql->options.local_infile_read = local_infile_read;
mysql->options.local_infile_end = local_infile_end;
mysql->options.local_infile_error = local_infile_error;
mysql->options.local_infile_userdata = userdata;
}
void mysql_set_local_infile_default(MYSQL *mysql) {
mysql->options.local_infile_init = default_local_infile_init;
mysql->options.local_infile_read = default_local_infile_read;
mysql->options.local_infile_end = default_local_infile_end;
mysql->options.local_infile_error = default_local_infile_error;
}
/**************************************************************************
Do a query. If query returned rows, free old rows.
Read data by mysql_store_result or by repeat call of mysql_fetch_row
**************************************************************************/
int STDCALL mysql_query(MYSQL *mysql, const char *query) {
return mysql_real_query(mysql, query, (ulong)strlen(query));
}
/**************************************************************************
Return next field of the query results
**************************************************************************/
MYSQL_FIELD *STDCALL mysql_fetch_field(MYSQL_RES *result) {
if (result->current_field >= result->field_count || !result->fields)
return (NULL);
return &result->fields[result->current_field++];
}
/**************************************************************************
Move to a specific row and column
**************************************************************************/
void STDCALL mysql_data_seek(MYSQL_RES *result, my_ulonglong row) {
MYSQL_ROWS *tmp = 0;
DBUG_PRINT("info", ("mysql_data_seek(%ld)", (long)row));
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 mysql_row_tell()
This doesn't restore any data. The next mysql_fetch_row or
mysql_fetch_field will return the next row or field after the last used
*************************************************************************/
MYSQL_ROW_OFFSET STDCALL mysql_row_seek(MYSQL_RES *result,
MYSQL_ROW_OFFSET row) {
MYSQL_ROW_OFFSET return_value = result->data_cursor;
result->current_row = 0;
result->data_cursor = row;
return return_value;
}
MYSQL_FIELD_OFFSET STDCALL mysql_field_seek(MYSQL_RES *result,
MYSQL_FIELD_OFFSET field_offset) {
MYSQL_FIELD_OFFSET return_value = result->current_field;
result->current_field = field_offset;
return return_value;
}
/*****************************************************************************
List all databases
*****************************************************************************/
MYSQL_RES *STDCALL mysql_list_dbs(MYSQL *mysql, const char *wild) {
char buff[255];
DBUG_ENTER("mysql_list_dbs");
append_wild(my_stpcpy(buff, "show databases"), buff + sizeof(buff), wild);
if (mysql_query(mysql, buff)) DBUG_RETURN(0);
DBUG_RETURN(mysql_store_result(mysql));
}
/*****************************************************************************
List all tables in a database
If wild is given then only the tables matching wild is returned
*****************************************************************************/
MYSQL_RES *STDCALL mysql_list_tables(MYSQL *mysql, const char *wild) {
char buff[255];
DBUG_ENTER("mysql_list_tables");
append_wild(my_stpcpy(buff, "show tables"), buff + sizeof(buff), wild);
if (mysql_query(mysql, buff)) DBUG_RETURN(0);
DBUG_RETURN(mysql_store_result(mysql));
}
MYSQL_FIELD *cli_list_fields(MYSQL *mysql) {
MYSQL_DATA *query;
MYSQL_FIELD *result;
MYSQL_TRACE_STAGE(mysql, WAIT_FOR_FIELD_DEF);
query = cli_read_rows(mysql, (MYSQL_FIELD *)0, protocol_41(mysql) ? 8 : 6);
MYSQL_TRACE_STAGE(mysql, READY_FOR_COMMAND);
if (!query) return NULL;
mysql->field_count = (uint)query->rows;
result = unpack_fields(mysql, query->data, mysql->field_alloc,
mysql->field_count, 1, mysql->server_capabilities);
free_rows(query);
return result;
}
/**************************************************************************
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"
**************************************************************************/
MYSQL_RES *STDCALL mysql_list_fields(MYSQL *mysql, const char *table,
const char *wild) {
MYSQL_RES *result;
MYSQL_FIELD *fields;
MEM_ROOT *new_root;
char buff[258], *end;
DBUG_ENTER("mysql_list_fields");
DBUG_PRINT("enter", ("table: '%s' wild: '%s'", table, wild ? wild : ""));
end = strmake(strmake(buff, table, 128) + 1, wild ? wild : "", 128);
free_old_query(mysql);
if (simple_command(mysql, COM_FIELD_LIST, (uchar *)buff, (ulong)(end - buff),
1) ||
!(fields = (*mysql->methods->list_fields)(mysql)))
DBUG_RETURN(NULL);
if (!(new_root = (MEM_ROOT *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(MEM_ROOT),
MYF(MY_WME | MY_ZEROFILL))))
DBUG_RETURN(NULL);
if (!(result = (MYSQL_RES *)my_malloc(PSI_NOT_INSTRUMENTED, sizeof(MYSQL_RES),
MYF(MY_WME | MY_ZEROFILL)))) {
my_free(new_root);
DBUG_RETURN(NULL);
}
result->methods = mysql->methods;
result->field_alloc = mysql->field_alloc;
mysql->fields = 0;
mysql->field_alloc = new_root;
result->field_count = mysql->field_count;
result->fields = fields;
result->eof = 1;
DBUG_RETURN(result);
}
/* List all running processes (threads) in server */
MYSQL_RES *STDCALL mysql_list_processes(MYSQL *mysql) {
uint field_count;
uchar *pos;
DBUG_ENTER("mysql_list_processes");
if (simple_command(mysql, COM_PROCESS_INFO, 0, 0, 0)) DBUG_RETURN(0);
free_old_query(mysql);
pos = (uchar *)mysql->net.read_pos;
field_count = (uint)net_field_length(&pos);
if (!(mysql->fields =
cli_read_metadata(mysql, field_count, protocol_41(mysql) ? 7 : 5)))
DBUG_RETURN(NULL);
mysql->status = MYSQL_STATUS_GET_RESULT;
mysql->field_count = field_count;
DBUG_RETURN(mysql_store_result(mysql));
}
int STDCALL mysql_shutdown(MYSQL *mysql,
enum mysql_enum_shutdown_level shutdown_level
MY_ATTRIBUTE((unused))) {
if (mysql_get_server_version(mysql) < 50709)
return simple_command(mysql, COM_DEPRECATED_1, 0, 1, 0);
else
return mysql_real_query(mysql, C_STRING_WITH_LEN("shutdown"));
}
int STDCALL mysql_refresh(MYSQL *mysql, uint options) {
uchar bits[1];
DBUG_ENTER("mysql_refresh");
bits[0] = (uchar)options;
DBUG_RETURN(simple_command(mysql, COM_REFRESH, bits, 1, 0));
}
int STDCALL mysql_kill(MYSQL *mysql, ulong pid) {
uchar buff[4];
DBUG_ENTER("mysql_kill");
/*
Sanity check: if ulong is 64-bits, user can submit a PID here that
overflows our 32-bit parameter to the somewhat obsolete COM_PROCESS_KILL.
If this is the case, we'll flag an error here.
The SQL statement KILL CONNECTION is the safer option here.
There is an analog of this failsafe in the server as we might see old
libmysql connection to a new server as well as the other way around.
*/
if (pid & (~0xfffffffful)) DBUG_RETURN(CR_INVALID_CONN_HANDLE);
int4store(buff, pid);
DBUG_RETURN(simple_command(mysql, COM_PROCESS_KILL, buff, sizeof(buff), 0));
}
int STDCALL mysql_set_server_option(MYSQL *mysql,
enum enum_mysql_set_option option) {
uchar buff[2];
DBUG_ENTER("mysql_set_server_option");
int2store(buff, (uint)option);
DBUG_RETURN(simple_command(mysql, COM_SET_OPTION, buff, sizeof(buff), 0));
}
int STDCALL mysql_dump_debug_info(MYSQL *mysql) {
DBUG_ENTER("mysql_dump_debug_info");
DBUG_RETURN(simple_command(mysql, COM_DEBUG, 0, 0, 0));
}
const char *cli_read_statistics(MYSQL *mysql) {
mysql->net.read_pos[mysql->packet_length] = 0; /* End of stat string */
if (!mysql->net.read_pos[0]) {
set_mysql_error(mysql, CR_WRONG_HOST_INFO, unknown_sqlstate);
return mysql->net.last_error;
}
/*
After reading the single packet with reply to COM_STATISTICS
we are ready for new commands.
*/
MYSQL_TRACE_STAGE(mysql, READY_FOR_COMMAND);
return (char *)mysql->net.read_pos;
}
const char *STDCALL mysql_stat(MYSQL *mysql) {
DBUG_ENTER("mysql_stat");
if (simple_command(mysql, COM_STATISTICS, 0, 0, 0))
DBUG_RETURN(mysql->net.last_error);
DBUG_RETURN((*mysql->methods->read_statistics)(mysql));
}
int STDCALL mysql_ping(MYSQL *mysql) {
int res;
DBUG_ENTER("mysql_ping");
res = simple_command(mysql, COM_PING, 0, 0, 0);
if (res == CR_SERVER_LOST && mysql->reconnect)
res = simple_command(mysql, COM_PING, 0, 0, 0);
DBUG_RETURN(res);
}
const char *STDCALL mysql_get_server_info(MYSQL *mysql) {
return ((char *)mysql->server_version);
}
const char *STDCALL mysql_get_host_info(MYSQL *mysql) {
return (mysql->host_info);
}
uint STDCALL mysql_get_proto_info(MYSQL *mysql) {
return (mysql->protocol_version);
}
const char *STDCALL mysql_get_client_info(void) {
return (char *)MYSQL_SERVER_VERSION;
}
ulong STDCALL mysql_get_client_version(void) { return MYSQL_VERSION_ID; }
bool STDCALL mysql_eof(MYSQL_RES *res) { return res->eof; }
MYSQL_FIELD *STDCALL mysql_fetch_field_direct(MYSQL_RES *res, uint fieldnr) {
if (fieldnr >= res->field_count || !res->fields) return (NULL);
return &(res)->fields[fieldnr];
}
MYSQL_FIELD *STDCALL mysql_fetch_fields(MYSQL_RES *res) {
return (res)->fields;
}
MYSQL_ROW_OFFSET STDCALL mysql_row_tell(MYSQL_RES *res) {
return res->data_cursor;
}
MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(MYSQL_RES *res) {
return (res)->current_field;
}
enum_resultset_metadata STDCALL mysql_result_metadata(MYSQL_RES *result) {
return result->metadata;
}
/* MYSQL */
unsigned int STDCALL mysql_field_count(MYSQL *mysql) {
return mysql->field_count;
}
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql) {
return mysql->affected_rows;
}
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql) { return mysql->insert_id; }
const char *STDCALL mysql_sqlstate(MYSQL *mysql) {
return mysql ? mysql->net.sqlstate : cant_connect_sqlstate;
}
uint STDCALL mysql_warning_count(MYSQL *mysql) { return mysql->warning_count; }
const char *STDCALL mysql_info(MYSQL *mysql) {
if (!mysql) {
#if defined(CLIENT_PROTOCOL_TRACING)
return "protocol tracing enabled";
#else
return NULL;
#endif
}
return mysql->info;
}
ulong STDCALL mysql_thread_id(MYSQL *mysql) {
/*
ulong may be 64-bit, but we currently only transmit 32-bit.
mysql_thread_id() is usually used in conjunction with mysql_kill()
which is similarly limited (and obsolete).
SELECTION CONNECTION_ID() / KILL CONNECTION avoid this issue.
*/
return (mysql)->thread_id;
}
const char *STDCALL mysql_character_set_name(MYSQL *mysql) {
return mysql->charset->csname;
}
void STDCALL mysql_get_character_set_info(MYSQL *mysql,
MY_CHARSET_INFO *csinfo) {
csinfo->number = mysql->charset->number;
csinfo->state = mysql->charset->state;
csinfo->csname = mysql->charset->csname;
csinfo->name = mysql->charset->name;
csinfo->comment = mysql->charset->comment;
csinfo->mbminlen = mysql->charset->mbminlen;
csinfo->mbmaxlen = mysql->charset->mbmaxlen;
if (mysql->options.charset_dir)
csinfo->dir = mysql->options.charset_dir;
else
csinfo->dir = charsets_dir;
}
uint STDCALL mysql_thread_safe(void) { return 1; }
/****************************************************************************
Some support functions
****************************************************************************/
/*
Functions called my my_net_init() to set some application specific variables
*/
void my_net_local_init(NET *net) {
ulong local_net_buffer_length = 0;
ulong local_max_allowed_packet = 0;
(void)mysql_get_option(NULL, MYSQL_OPT_MAX_ALLOWED_PACKET,
&local_max_allowed_packet);
(void)mysql_get_option(NULL, MYSQL_OPT_NET_BUFFER_LENGTH,
&local_net_buffer_length);
net->max_packet = (uint)local_net_buffer_length;
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
my_net_set_retry_count(net, CLIENT_NET_RETRY_COUNT);
net->max_packet_size =
MY_MAX(local_net_buffer_length, local_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 MySQL 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 mysql_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.
*/
ulong STDCALL mysql_hex_string(char *to, const char *from, ulong 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 (ulong)(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
*/
ulong STDCALL mysql_escape_string(char *to, const char *from, ulong length) {
return (uint)escape_string_for_mysql(default_charset_info, to, 0, from,
length);
}
/**