Skip to content

Commit 2b7486b

Browse files
committed
CONCPP-146 Adding initSql option for queries to be run at connection
time. This can be single query or a ';' separated list of queries. The testcase hase been added.
1 parent bc8b4c6 commit 2b7486b

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

README

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ prepStmtCacheSqlLimit This is the maximum length of a (SQL query length + sch
165165
connectionAttributes If performance_schema is enabled, permits to send server
166166
some client information in a key:value pair format
167167
(example: connectionAttributes=key1:value1,key2,value2) string
168-
log` The logging level. Setting it to non-zero effectively turns
168+
log The logging level. Setting it to non-zero effectively turns
169169
on the logging. The levels are 1- error, 2- warning, 3- info,
170170
4 - debug, 5-trace uint
171171
logname Name of the file to write the log in. If the name is set,
@@ -174,6 +174,8 @@ logname Name of the file to write the log in. If the name is se
174174
default name and location for the log file. The name is
175175
mariadbccpp.log, the location is %TMP% or %USERPROFILE% or
176176
current dir on Windows, or $HOME or /tmp on other systems. string
177+
initSql One query or a semicolon separated list of queried to be executed at the
178+
connection time. string
177179

178180

179181
Properties is map of strings, and is another way to pass optional parameters.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ The list of supported options:
112112
| **`connectionAttributes`** |If performance_schema is enabled, permits to send server some client information in a key:value pair format (example: connectionAttributes=key1:value1,key2,value2) This information can be retrieved on server within tables performance_schema.session_connect_attrs and performance_schema.session_account_connect_attrs. This allows an identification of client/application on server|*string* |||
113113
| **`log`**|The logging level. Setting it to non-zero effectively turns on the logging. The levels are 1- error, 2- warning, 3- info, 4 - debug, 5-trace|*uint*|0||
114114
| **`logname`** |Name of the file to write the log in. If the name is set, and the log level is not, the level will be set to 'error'. The option does not have default value, but the logger has default name and location for the log file. The name is mariadbccpp.log, the location is %TMP% or %USERPROFILE% or current dir on Windows, or $HOME or /tmp on other systems.|*string*|||
115+
| **`initSql`** |One query or a semicolon separated list of queried to be executed at the connection time.|*string*|||
115116

116117

117118
Properties is map of strings, and is another way to pass optional parameters.

src/options/DefaultOptions.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************************************************
2-
Copyright (C) 2020, 2023 MariaDB Corporation AB
2+
Copyright (C) 2020, 2025 MariaDB Corporation plc
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Library General Public
@@ -403,8 +403,7 @@ namespace sql
403403
"tlsCRLPath", {"tlsCRLPath",
404404
"0.9.2",
405405
"A path to a directory that contains one or more PEM files that should each contain one revoked X509 certificate.\n"
406-
"The directory specified by this option needs to be run through the openssl rehash command.\n"
407-
" ",
406+
"The directory specified by this option needs to be run through the openssl rehash command.",
408407
false,
409408
""}
410409
},
@@ -714,6 +713,13 @@ namespace sql
714713
"Default authentication client-side plugin to use",
715714
false,
716715
""}
716+
},
717+
{
718+
"initSql", {"initSql",
719+
"1.1.7",
720+
"SQL command(s) to run at connection time",
721+
false,
722+
""}
717723
}
718724
};
719725

src/options/Options.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ namespace mariadb
140140
OPTIONS_FIELD(useResetConnection),
141141
OPTIONS_FIELD(useReadAheadInput),
142142
OPTIONS_FIELD(serverRsaPublicKeyFile),
143-
OPTIONS_FIELD(tlsPeerFP)
143+
OPTIONS_FIELD(tlsPeerFP),
144+
OPTIONS_FIELD(initSql)
144145
};
145146

146147

@@ -525,6 +526,9 @@ namespace mariadb
525526
if (!(tlsPeerFPList.compare(opt->tlsPeerFPList) == 0)) {
526527
return false;
527528
}
529+
if (!(initSql.compare(opt->initSql) == 0)) {
530+
return false;
531+
}
528532
return minPoolSize == opt->minPoolSize;
529533
}
530534

@@ -618,9 +622,11 @@ namespace mariadb
618622
result= 31 *result + (autocommit ? 1 : 0);
619623
result= 31 *result + (!credentialType.empty() ? credentialType.hashCode() : 0);
620624

621-
result= 31 *result + (!nonMappedOptions.empty() ? hashProps(nonMappedOptions) : 0);
625+
result= 31*result + (!nonMappedOptions.empty() ? hashProps(nonMappedOptions) : 0);
626+
627+
result= 31*result + (!tlsPeerFPList.empty() ? tlsPeerFPList.hashCode() : 0);
628+
result= 31*result + (!initSql.empty() ? initSql.hashCode() : 0);
622629

623-
result= 31 *result + (!tlsPeerFPList.empty() ? tlsPeerFPList.hashCode() : 0);
624630
return result;
625631
}
626632

src/options/Options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/************************************************************************************
2-
Copyright (C) 2020,2024 MariaDB Corporation plc
2+
Copyright (C) 2020,2025 MariaDB Corporation plc
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Library General Public
@@ -138,6 +138,7 @@ struct Options
138138
bool useReadAheadInput= true;
139139
SQLString serverRsaPublicKeyFile;
140140
SQLString tlsPeerFP;
141+
SQLString initSql;
141142

142143
SQLString toString() const;
143144
bool equals(Options* obj);

src/protocol/capi/ConnectProtocol.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ namespace capi
455455
mysql_optionsv(connection.get(), MYSQL_REPORT_DATA_TRUNCATION, &uintOptionSelected);
456456
mysql_optionsv(connection.get(), MYSQL_OPT_LOCAL_INFILE, (options->allowLocalInfile ? &uintOptionSelected : &uintOptionNotSelected));
457457

458+
if (!options.get()->initSql.empty()) {
459+
static const SQLString initSqlDelim(";");
460+
auto Query= split(options.get()->initSql, initSqlDelim);
461+
for (auto& sql : *Query) {
462+
mysql_optionsv(connection.get(), MYSQL_INIT_COMMAND, sql.c_str());
463+
}
464+
}
458465
if (mysql_real_connect(connection.get(), NULL, NULL, NULL, NULL, 0, NULL, CLIENT_MULTI_STATEMENTS) == nullptr)
459466
{
460467
throw SQLException(mysql_error(connection.get()), mysql_sqlstate(connection.get()), mysql_errno(connection.get()));

test/unit/classes/connection.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,6 +3523,25 @@ void connection::concpp112_connection_attributes()
35233523
con.reset();
35243524
}
35253525

3526+
3527+
void connection::concpp146_initSQL()
3528+
{
3529+
sql::Properties p{{"user", user}, {"password", passwd}};
3530+
con.reset(driver->connect(addOptions2url("initSql=SET @myVar='YourVar'"), p));
3531+
stmt.reset(con->createStatement());
3532+
res.reset(stmt->executeQuery("SELECT @myVar"));
3533+
ASSERT(res->next());
3534+
ASSERT_EQUALS("YourVar", res->getString(1));
3535+
3536+
con.reset(driver->connect(addOptions2url("&initSql=SET @myVar='YourVar';SET @myVar2='YourVar2'"), p));
3537+
stmt.reset(con->createStatement());
3538+
res.reset(stmt->executeQuery("SELECT @myVar, @myVar2"));
3539+
ASSERT(res->next());
3540+
ASSERT_EQUALS("YourVar", res->getString(1));
3541+
ASSERT_EQUALS("YourVar2", res->getString(2));
3542+
}
3543+
3544+
35263545
void connection::setUp()
35273546
{
35283547
super::setUp();

test/unit/classes/connectiontest.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
3-
* 2020, 2022 MariaDB Corporation AB
3+
* 2020, 2025 MariaDB Corporation plc
44
*
55
* This program is free software; you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License, version 2.0, as
@@ -96,6 +96,7 @@ class connection : public unit_fixture
9696
TEST_CASE(concpp4_sequentialfailover);
9797
TEST_CASE(concpp105_conn_concurrency);
9898
TEST_CASE(concpp112_connection_attributes);
99+
TEST_CASE(concpp146_initSQL);
99100
}
100101

101102
/**
@@ -297,6 +298,9 @@ class connection : public unit_fixture
297298
/* Setting of connection attributes for perfschema */
298299
void concpp112_connection_attributes();
299300

301+
/* Test of initSql option */
302+
void concpp146_initSQL();
303+
300304
void setUp();
301305
};
302306

0 commit comments

Comments
 (0)