Skip to content

Commit 689bed1

Browse files
Hossamfc9cvicentiu
authored andcommitted
MDEV-23818 mysql option --script-dir
This commit introduces an additional command line option to the mariadb client. --script-dir=<directory> will cause the `source` command to look for files initially in CWD, then in <script-dir> if not found in CWD.
1 parent 3a81664 commit 689bed1

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

client/mysql.cc

+15-2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static STATUS status;
280280
static ulong select_limit,max_join_size,opt_connect_timeout=0;
281281
static char mysql_charsets_dir[FN_REFLEN+1];
282282
static char *opt_plugin_dir= 0, *opt_default_auth= 0;
283+
static char *script_dir = NULL;
283284
static const char *xmlmeta[] = {
284285
"&", "&amp;",
285286
"<", "&lt;",
@@ -1838,6 +1839,8 @@ static struct my_option my_long_options[] =
18381839
&safe_updates, &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
18391840
{"sandbox", 0, "Disallow commands that access the file system (except \\P without an argument and \\e).",
18401841
&status.sandbox, &status.sandbox, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1842+
{"script-dir", 0, "Set an alternative directory path for searching scripts invoked via the source command.",
1843+
&script_dir, &script_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
18411844
{"secure-auth", 0, "Refuse client connecting to server if it"
18421845
" uses old (pre-4.1.1) protocol.", &opt_secure_auth,
18431846
&opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
@@ -4692,6 +4695,7 @@ static int com_connect(String *buffer, char *line)
46924695
static int com_source(String *, char *line)
46934696
{
46944697
char source_name[FN_REFLEN], *end, *param;
4698+
char full_path[FN_REFLEN];
46954699
LINE_BUFFER *line_buff;
46964700
int error;
46974701
STATUS old_status;
@@ -4718,8 +4722,17 @@ static int com_source(String *, char *line)
47184722
/* open file name */
47194723
if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
47204724
{
4721-
char buff[FN_REFLEN+60];
4722-
sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
4725+
if (script_dir)
4726+
{
4727+
snprintf(full_path, sizeof(full_path), "%s/%s", script_dir, source_name);
4728+
sql_file = my_fopen(full_path, O_RDONLY | O_BINARY, MYF(0));
4729+
}
4730+
}
4731+
4732+
if (!sql_file)
4733+
{
4734+
char buff[FN_REFLEN + 60];
4735+
sprintf(buff, "Failed to open file '%s', error: %d", source_name, errno);
47234736
return put_info(buff, INFO_ERROR, 0);
47244737
}
47254738

mysql-test/main/client.result

+20
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,23 @@ drop table t1;
6060
#
6161
# End of 10.5 tests
6262
#
63+
#
64+
# MDEV-23818: mysql option --script-dir
65+
#
66+
# test 1: can't find the file at all
67+
ERROR at line 1: Failed to open file 'file1', error: 2
68+
# test 2: file in the current working directory
69+
1
70+
1
71+
# test 3: file is present in CWD and also in script-dir
72+
hello from file1
73+
hello from file1
74+
# test 4: file is only present in the script-dir
75+
hello from dir1/file1.sql
76+
hello from dir1/file1.sql
77+
# test 5: script-dir file has source command that references CWD
78+
hello from file2.sql
79+
hello from file2.sql
80+
# test 6: script-dir file has source command that references script-dir
81+
hello from file2.sql
82+
hello from file2.sql

mysql-test/main/client.test

+65
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,68 @@ drop table t1;
4444
--echo #
4545
--echo # End of 10.5 tests
4646
--echo #
47+
48+
--echo #
49+
--echo # MDEV-23818: mysql option --script-dir
50+
--echo #
51+
52+
--echo # test 1: can't find the file at all
53+
--mkdir $MYSQLTEST_VARDIR/dir1
54+
--error 1
55+
--exec echo "source file1;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
56+
--rmdir $MYSQLTEST_VARDIR/dir1
57+
58+
--echo # test 2: file in the current working directory
59+
--mkdir $MYSQLTEST_VARDIR/dir1
60+
--write_file $MYSQLTEST_VARDIR/file1.sql
61+
select 1;
62+
EOF
63+
--exec echo "source $MYSQLTEST_VARDIR/file1.sql;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
64+
--remove_file $MYSQLTEST_VARDIR/file1.sql
65+
--rmdir $MYSQLTEST_VARDIR/dir1
66+
67+
--echo # test 3: file is present in CWD and also in script-dir
68+
--mkdir $MYSQLTEST_VARDIR/dir1
69+
--write_file $MYSQLTEST_VARDIR/file1.sql
70+
select 'hello from file1'
71+
EOF
72+
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
73+
select 'hello from dir1/file1.sql';
74+
EOF
75+
--exec echo "source $MYSQLTEST_VARDIR/file1.sql;" | $MYSQL --script-dir=./dir1/ 2>&1
76+
--remove_file $MYSQLTEST_VARDIR/file1.sql
77+
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
78+
--rmdir $MYSQLTEST_VARDIR/dir1
79+
80+
--echo # test 4: file is only present in the script-dir
81+
--mkdir $MYSQLTEST_VARDIR/dir1
82+
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
83+
select 'hello from dir1/file1.sql';
84+
EOF
85+
--exec echo "source file1.sql;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
86+
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
87+
--rmdir $MYSQLTEST_VARDIR/dir1
88+
89+
--echo # test 5: script-dir file has source command that references CWD
90+
--mkdir $MYSQLTEST_VARDIR/dir1
91+
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
92+
source file2.sql;
93+
EOF
94+
--exec echo "select 'hello from file2.sql'" > $MYSQLTEST_VARDIR/file2.sql
95+
--exec cd $MYSQLTEST_VARDIR && $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ -e "source file1.sql;" 2>&1
96+
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
97+
--remove_file $MYSQLTEST_VARDIR/file2.sql
98+
--rmdir $MYSQLTEST_VARDIR/dir1
99+
100+
--echo # test 6: script-dir file has source command that references script-dir
101+
--mkdir $MYSQLTEST_VARDIR/dir1
102+
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
103+
source file2.sql;
104+
EOF
105+
--write_file $MYSQLTEST_VARDIR/dir1/file2.sql
106+
select 'hello from file2.sql';
107+
EOF
108+
--exec echo "source file1.sql" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
109+
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
110+
--remove_file $MYSQLTEST_VARDIR/dir1/file2.sql
111+
--rmdir $MYSQLTEST_VARDIR/dir1

0 commit comments

Comments
 (0)