Skip to content

Commit e1e1853

Browse files
committed
support local_infile_dir option
1 parent 2076d16 commit e1e1853

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Diff for: doc/user_guide.rst

+11
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,17 @@ connect(parameters...)
396396
server_public_key_path
397397
specifies path to a RSA public key used by caching sha2 password authentication.
398398
See https://dev.mysql.com/doc/refman/9.0/en/caching-sha2-pluggable-authentication.html
399+
400+
local_infile
401+
sets ``MYSQL_OPT_LOCAL_INFILE`` in ``mysql_options()`` enabling LOAD LOCAL INFILE from any path; zero disables;
402+
403+
*This must be a keyword parameter.*
404+
405+
local_infile_dir
406+
sets ``MYSQL_OPT_LOAD_DATA_LOCAL_DIR`` in ``mysql_options()`` enabling LOAD LOCAL INFILE from any path;
407+
if ``local_infile`` is set to ``True`` then this is ignored;
408+
409+
*This must be a keyword parameter.*
399410

400411
.. _mysql_ssl_set: http://dev.mysql.com/doc/refman/en/mysql-ssl-set.html
401412

Diff for: src/MySQLdb/_mysql.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ PERFORMANCE OF THIS SOFTWARE.
4848
#define HAVE_MYSQL_SERVER_PUBLIC_KEY
4949
#endif
5050

51+
#if !defined(MARIADB_VERSION_ID) && MYSQL_VERSION_ID >= 80021
52+
#define HAVE_MYSQL_OPT_LOCAL_INFILE_DIR
53+
#endif
54+
5155
#define PY_SSIZE_T_CLEAN 1
5256
#include "Python.h"
5357

@@ -436,7 +440,7 @@ _mysql_ConnectionObject_Initialize(
436440
"client_flag", "ssl", "ssl_mode",
437441
"local_infile",
438442
"read_timeout", "write_timeout", "charset",
439-
"auth_plugin", "server_public_key_path",
443+
"auth_plugin", "server_public_key_path", "local_infile_dir",
440444
NULL } ;
441445
int connect_timeout = 0;
442446
int read_timeout = 0;
@@ -448,14 +452,15 @@ _mysql_ConnectionObject_Initialize(
448452
*read_default_group=NULL,
449453
*charset=NULL,
450454
*auth_plugin=NULL,
451-
*server_public_key_path=NULL;
455+
*server_public_key_path=NULL,
456+
*local_infile_dir=NULL;
452457

453458
self->converter = NULL;
454459
self->open = false;
455460
self->reconnect = false;
456461

457462
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
458-
"|ssssisOiiisssiOsiiisss:connect",
463+
"|ssssisOiiisssiOsiiissss:connect",
459464
kwlist,
460465
&host, &user, &passwd, &db,
461466
&port, &unix_socket, &conv,
@@ -469,7 +474,8 @@ _mysql_ConnectionObject_Initialize(
469474
&write_timeout,
470475
&charset,
471476
&auth_plugin,
472-
&server_public_key_path
477+
&server_public_key_path,
478+
&local_infile_dir
473479
))
474480
return -1;
475481

@@ -479,6 +485,13 @@ _mysql_ConnectionObject_Initialize(
479485
return -1;
480486
}
481487
#endif
488+
489+
#ifndef HAVE_MYSQL_OPT_LOCAL_INFILE_DIR
490+
if (local_infile_dir) {
491+
PyErr_SetString(_mysql_NotSupportedError, "local_infile_dir is not supported");
492+
return -1;
493+
}
494+
#endif
482495
// For compatibility with PyPy, we need to keep strong reference
483496
// to unicode objects until we use UTF8.
484497
#define _stringsuck(d,t,s) {t=PyMapping_GetItemString(s,#d);\
@@ -599,6 +612,12 @@ _mysql_ConnectionObject_Initialize(
599612
}
600613
#endif
601614

615+
#ifdef HAVE_MYSQL_OPT_LOCAL_INFILE_DIR
616+
if (local_infile_dir) {
617+
mysql_options(&(self->connection), MYSQL_OPT_LOAD_DATA_LOCAL_DIR, local_infile_dir);
618+
}
619+
#endif
620+
602621
Py_BEGIN_ALLOW_THREADS
603622
conn = mysql_real_connect(&(self->connection), host, user, passwd, db,
604623
port, unix_socket, client_flag);

Diff for: src/MySQLdb/connections.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ class object, used to create cursors (keyword only)
142142
See https://dev.mysql.com/doc/refman/9.0/en/caching-sha2-pluggable-authentication.html
143143
144144
:param bool local_infile:
145-
enables LOAD LOCAL INFILE; zero disables
145+
sets ``MYSQL_OPT_LOCAL_INFILE`` in ``mysql_options()`` enabling LOAD LOCAL INFILE from any path; zero disables;
146+
147+
:param str local_infile_dir:
148+
sets ``MYSQL_OPT_LOAD_DATA_LOCAL_DIR`` in ``mysql_options()`` enabling LOAD LOCAL INFILE from any path;
149+
if ``local_infile`` is set to ``True`` then this is ignored;
150+
151+
supported for mysql version >= 8.0.21
146152
147153
:param bool autocommit:
148154
If False (default), autocommit is disabled.

0 commit comments

Comments
 (0)