2020import io .asyncer .r2dbc .mysql .constant .SslMode ;
2121import io .asyncer .r2dbc .mysql .constant .ZeroDateOption ;
2222import io .asyncer .r2dbc .mysql .extension .Extension ;
23+ import io .asyncer .r2dbc .mysql .internal .util .InternalArrays ;
2324import io .netty .handler .ssl .SslContextBuilder ;
2425import org .jetbrains .annotations .Nullable ;
2526import org .reactivestreams .Publisher ;
@@ -94,6 +95,8 @@ public final class MySqlConnectionConfiguration {
9495 @ Nullable
9596 private final Predicate <String > preferPrepareStatement ;
9697
98+ private final List <String > sessionVariables ;
99+
97100 @ Nullable
98101 private final Path loadLocalInfilePath ;
99102
@@ -120,6 +123,7 @@ private MySqlConnectionConfiguration(
120123 ZeroDateOption zeroDateOption , @ Nullable ZoneId serverZoneId ,
121124 String user , @ Nullable CharSequence password , @ Nullable String database ,
122125 boolean createDatabaseIfNotExist , @ Nullable Predicate <String > preferPrepareStatement ,
126+ List <String > sessionVariables ,
123127 @ Nullable Path loadLocalInfilePath , int localInfileBufferSize ,
124128 int queryCacheSize , int prepareCacheSize ,
125129 Set <CompressionAlgorithm > compressionAlgorithms , int zstdCompressionLevel ,
@@ -140,13 +144,14 @@ private MySqlConnectionConfiguration(
140144 this .database = database == null || database .isEmpty () ? "" : database ;
141145 this .createDatabaseIfNotExist = createDatabaseIfNotExist ;
142146 this .preferPrepareStatement = preferPrepareStatement ;
147+ this .sessionVariables = sessionVariables ;
143148 this .loadLocalInfilePath = loadLocalInfilePath ;
144149 this .localInfileBufferSize = localInfileBufferSize ;
145150 this .queryCacheSize = queryCacheSize ;
146151 this .prepareCacheSize = prepareCacheSize ;
147152 this .compressionAlgorithms = compressionAlgorithms ;
148153 this .zstdCompressionLevel = zstdCompressionLevel ;
149- this .loopResources = loopResources == null ? TcpResources .get () : loopResources ;
154+ this .loopResources = loopResources == null ? TcpResources .get () : loopResources ;
150155 this .extensions = extensions ;
151156 this .passwordPublisher = passwordPublisher ;
152157 }
@@ -220,6 +225,10 @@ Predicate<String> getPreferPrepareStatement() {
220225 return preferPrepareStatement ;
221226 }
222227
228+ List <String > getSessionVariables () {
229+ return sessionVariables ;
230+ }
231+
223232 @ Nullable
224233 Path getLoadLocalInfilePath () {
225234 return loadLocalInfilePath ;
@@ -281,6 +290,7 @@ public boolean equals(Object o) {
281290 database .equals (that .database ) &&
282291 createDatabaseIfNotExist == that .createDatabaseIfNotExist &&
283292 Objects .equals (preferPrepareStatement , that .preferPrepareStatement ) &&
293+ sessionVariables .equals (that .sessionVariables ) &&
284294 Objects .equals (loadLocalInfilePath , that .loadLocalInfilePath ) &&
285295 localInfileBufferSize == that .localInfileBufferSize &&
286296 queryCacheSize == that .queryCacheSize &&
@@ -296,9 +306,9 @@ public boolean equals(Object o) {
296306 public int hashCode () {
297307 return Objects .hash (isHost , domain , port , ssl , tcpKeepAlive , tcpNoDelay , connectTimeout ,
298308 serverZoneId , zeroDateOption , user , password , database , createDatabaseIfNotExist ,
299- preferPrepareStatement , loadLocalInfilePath , localInfileBufferSize , queryCacheSize ,
300- prepareCacheSize , compressionAlgorithms , zstdCompressionLevel , loopResources ,
301- extensions , passwordPublisher );
309+ preferPrepareStatement , sessionVariables , loadLocalInfilePath ,
310+ localInfileBufferSize , queryCacheSize , prepareCacheSize , compressionAlgorithms ,
311+ zstdCompressionLevel , loopResources , extensions , passwordPublisher );
302312 }
303313
304314 @ Override
@@ -310,6 +320,7 @@ public String toString() {
310320 ", zeroDateOption=" + zeroDateOption + ", user='" + user + "', password=" + password +
311321 ", database='" + database + "', createDatabaseIfNotExist=" + createDatabaseIfNotExist +
312322 ", preferPrepareStatement=" + preferPrepareStatement +
323+ ", sessionVariables=" + sessionVariables +
313324 ", loadLocalInfilePath=" + loadLocalInfilePath +
314325 ", localInfileBufferSize=" + localInfileBufferSize +
315326 ", queryCacheSize=" + queryCacheSize + ", prepareCacheSize=" + prepareCacheSize +
@@ -324,6 +335,7 @@ public String toString() {
324335 ", zeroDateOption=" + zeroDateOption + ", user='" + user + "', password=" + password +
325336 ", database='" + database + "', createDatabaseIfNotExist=" + createDatabaseIfNotExist +
326337 ", preferPrepareStatement=" + preferPrepareStatement +
338+ ", sessionVariables=" + sessionVariables +
327339 ", loadLocalInfilePath=" + loadLocalInfilePath +
328340 ", localInfileBufferSize=" + localInfileBufferSize +
329341 ", queryCacheSize=" + queryCacheSize +
@@ -393,6 +405,8 @@ public static final class Builder {
393405 @ Nullable
394406 private Predicate <String > preferPrepareStatement ;
395407
408+ private List <String > sessionVariables = Collections .emptyList ();
409+
396410 @ Nullable
397411 private Path loadLocalInfilePath ;
398412
@@ -440,7 +454,7 @@ public MySqlConnectionConfiguration build() {
440454 sslCa , sslKey , sslKeyPassword , sslCert , sslContextBuilderCustomizer );
441455 return new MySqlConnectionConfiguration (isHost , domain , port , ssl , tcpKeepAlive , tcpNoDelay ,
442456 connectTimeout , zeroDateOption , serverZoneId , user , password , database ,
443- createDatabaseIfNotExist , preferPrepareStatement , loadLocalInfilePath ,
457+ createDatabaseIfNotExist , preferPrepareStatement , sessionVariables , loadLocalInfilePath ,
444458 localInfileBufferSize , queryCacheSize , prepareCacheSize ,
445459 compressionAlgorithms , zstdCompressionLevel , loopResources ,
446460 Extensions .from (extensions , autodetectExtensions ), passwordPublisher );
@@ -801,6 +815,23 @@ public Builder useServerPrepareStatement(Predicate<String> preferPrepareStatemen
801815 return this ;
802816 }
803817
818+ /**
819+ * Configure the session variables, used to set session variables immediately after login. Default no
820+ * session variables to set. It should be a list of key-value pairs. e.g.
821+ * {@code ["sql_mode='ANSI_QUOTES,STRICT_TRANS_TABLES'", "time_zone=00:00"]}.
822+ *
823+ * @param sessionVariables the session variables to set.
824+ * @return {@link Builder this}
825+ * @throws IllegalArgumentException if {@code sessionVariables} is {@code null}.
826+ * @since 1.1.2
827+ */
828+ public Builder sessionVariables (String ... sessionVariables ) {
829+ requireNonNull (sessionVariables , "sessionVariables must not be null" );
830+
831+ this .sessionVariables = InternalArrays .toImmutableList (sessionVariables );
832+ return this ;
833+ }
834+
804835 /**
805836 * Configures to allow the {@code LOAD DATA LOCAL INFILE} statement in the given {@code path} or
806837 * disallow the statement. Default to {@code null} which means not allow the statement.
@@ -917,9 +948,9 @@ public Builder compressionAlgorithms(CompressionAlgorithm... compressionAlgorith
917948 * @param level the compression level.
918949 * @return {@link Builder this}.
919950 * @throws IllegalArgumentException if {@code level} is not between 1 and 22.
920- * @since 1.1.2
921951 * @see <a href="https://dev.mysql.com/doc/refman/8.0/en/connection-options.html">
922952 * MySQL Connection Options --zstd-compression-level</a>
953+ * @since 1.1.2
923954 */
924955 public Builder zstdCompressionLevel (int level ) {
925956 require (level >= 1 && level <= 22 , "level must be between 1 and 22" );
@@ -929,8 +960,9 @@ public Builder zstdCompressionLevel(int level) {
929960 }
930961
931962 /**
932- * Configures the {@link LoopResources} for the driver.
933- * Default to {@link TcpResources#get() global tcp resources}.
963+ * Configures the {@link LoopResources} for the driver. Default to
964+ * {@link TcpResources#get() global tcp resources}.
965+ *
934966 * @param loopResources the {@link LoopResources}.
935967 * @return this {@link Builder}.
936968 * @throws IllegalArgumentException if {@code loopResources} is {@code null}.
0 commit comments