1 module hunt.database.driver.mysql.MySQLConnectOptions; 2 3 import hunt.database.base.SqlConnectOptions; 4 5 import hunt.collection.Collections; 6 import hunt.collection.HashMap; 7 import hunt.collection.Map; 8 import hunt.collection.Set; 9 import hunt.net.OpenSSLEngineOptions; 10 import hunt.net.ProxyOptions; 11 import hunt.net.util.HttpURI; 12 import hunt.net.util.UrlEncoded; 13 import hunt.Exceptions; 14 15 import core.time; 16 import std.array; 17 18 /** 19 * Connect options for configuring {@link MySQLConnection} or {@link MySQLPool}. 20 */ 21 class MySQLConnectOptions : SqlConnectOptions { 22 23 /** 24 * Provide a {@link MySQLConnectOptions} configured from a connection URI. 25 * 26 * @param connectionUri the connection URI to configure from 27 * @return a {@link MySQLConnectOptions} parsed from the connection URI 28 * @throws IllegalArgumentException when the {@code connectionUri} is in an invalid format 29 */ 30 static MySQLConnectOptions fromUri(string connectionUri) { 31 return new MySQLConnectOptions(new HttpURI(connectionUri)); 32 } 33 34 enum string DEFAULT_HOST = "localhost"; 35 enum int DEFAULT_PORT = 3306; 36 enum string DEFAULT_USER = "root"; 37 enum string DEFAULT_PASSWORD = ""; 38 enum string DEFAULT_SCHEMA = ""; 39 enum string DEFAULT_COLLATION = "utf8mb4_general_ci"; 40 enum string[string] DEFAULT_CONNECTION_ATTRIBUTES = [ 41 "_client_name" : "hunt-mysql-client", 42 "_client_version" : "1.0.0"]; 43 44 private string collation; 45 46 this() { 47 super(); 48 this.collation = DEFAULT_COLLATION; 49 } 50 51 this(HttpURI uri) { 52 super(uri); 53 this.collation = DEFAULT_COLLATION; 54 UrlEncoded maps = new UrlEncoded(uri.getQuery()); 55 string charset = maps.getValue("charset"); 56 if(!charset.empty()) { 57 import hunt.database.driver.mysql.impl.MySQLCollation; 58 this.collation = MySQLCollation.getDefaultCollationFromCharsetName(charset); 59 } 60 } 61 62 this(MySQLConnectOptions other) { 63 super(other); 64 this.collation = other.collation; 65 } 66 67 /** 68 * Get the collation for the connection. 69 * 70 * @return the MySQL collation 71 */ 72 string getCollation() { 73 return collation; 74 } 75 76 /** 77 * Set the collation for the connection. 78 * 79 * @param collation the collation to set 80 * @return a reference to this, so the API can be used fluently 81 */ 82 MySQLConnectOptions setCollation(string collation) { 83 this.collation = collation; 84 return this; 85 } 86 87 override 88 MySQLConnectOptions setHost(string host) { 89 return cast(MySQLConnectOptions) super.setHost(host); 90 } 91 92 override 93 MySQLConnectOptions setPort(int port) { 94 return cast(MySQLConnectOptions) super.setPort(port); 95 } 96 97 override 98 MySQLConnectOptions setUser(string user) { 99 return cast(MySQLConnectOptions) super.setUser(user); 100 } 101 102 override 103 MySQLConnectOptions setPassword(string password) { 104 return cast(MySQLConnectOptions) super.setPassword(password); 105 } 106 107 override 108 MySQLConnectOptions setDatabase(string database) { 109 return cast(MySQLConnectOptions) super.setDatabase(database); 110 } 111 112 override 113 MySQLConnectOptions setCachePreparedStatements(bool cachePreparedStatements) { 114 return cast(MySQLConnectOptions) super.setCachePreparedStatements(cachePreparedStatements); 115 } 116 117 override 118 MySQLConnectOptions setPreparedStatementCacheMaxSize(int preparedStatementCacheMaxSize) { 119 return cast(MySQLConnectOptions) super.setPreparedStatementCacheMaxSize(preparedStatementCacheMaxSize); 120 } 121 122 override 123 MySQLConnectOptions setPreparedStatementCacheSqlLimit(int preparedStatementCacheSqlLimit) { 124 return cast(MySQLConnectOptions) super.setPreparedStatementCacheSqlLimit(preparedStatementCacheSqlLimit); 125 } 126 127 override 128 MySQLConnectOptions setProperties(Map!(string, string) properties) { 129 return cast(MySQLConnectOptions) super.setProperties(properties); 130 } 131 132 override 133 MySQLConnectOptions addProperty(string key, string value) { 134 return cast(MySQLConnectOptions) super.addProperty(key, value); 135 } 136 137 override 138 MySQLConnectOptions setSendBufferSize(int sendBufferSize) { 139 return cast(MySQLConnectOptions) super.setSendBufferSize(sendBufferSize); 140 } 141 142 override 143 MySQLConnectOptions setReceiveBufferSize(int receiveBufferSize) { 144 return cast(MySQLConnectOptions) super.setReceiveBufferSize(receiveBufferSize); 145 } 146 override 147 MySQLConnectOptions setDecoderBufferSize(int size) { 148 return cast(MySQLConnectOptions) super.setDecoderBufferSize(size); 149 } 150 151 override 152 MySQLConnectOptions setEncoderBufferSize(int size) { 153 return cast(MySQLConnectOptions) super.setEncoderBufferSize(size); 154 } 155 156 override 157 MySQLConnectOptions setReuseAddress(bool reuseAddress) { 158 return cast(MySQLConnectOptions) super.setReuseAddress(reuseAddress); 159 } 160 161 override 162 MySQLConnectOptions setReusePort(bool reusePort) { 163 return cast(MySQLConnectOptions) super.setReusePort(reusePort); 164 } 165 166 override 167 MySQLConnectOptions setTrafficClass(int trafficClass) { 168 return cast(MySQLConnectOptions) super.setTrafficClass(trafficClass); 169 } 170 171 override 172 MySQLConnectOptions setTcpNoDelay(bool tcpNoDelay) { 173 return cast(MySQLConnectOptions) super.setTcpNoDelay(tcpNoDelay); 174 } 175 176 override 177 MySQLConnectOptions setTcpKeepAlive(bool tcpKeepAlive) { 178 return cast(MySQLConnectOptions) super.setTcpKeepAlive(tcpKeepAlive); 179 } 180 181 override 182 MySQLConnectOptions setSoLinger(int soLinger) { 183 return cast(MySQLConnectOptions) super.setSoLinger(soLinger); 184 } 185 186 override 187 MySQLConnectOptions setIdleTimeout(Duration idleTimeout) { 188 return cast(MySQLConnectOptions) super.setIdleTimeout(idleTimeout); 189 } 190 191 // override 192 // MySQLConnectOptions setKeyCertOptions(KeyCertOptions options) { 193 // return cast(MySQLConnectOptions) super.setKeyCertOptions(options); 194 // } 195 196 // override 197 // MySQLConnectOptions setKeyStoreOptions(JksOptions options) { 198 // return cast(MySQLConnectOptions) super.setKeyStoreOptions(options); 199 // } 200 201 // override 202 // MySQLConnectOptions setPfxKeyCertOptions(PfxOptions options) { 203 // return cast(MySQLConnectOptions) super.setPfxKeyCertOptions(options); 204 // } 205 206 // override 207 // MySQLConnectOptions setPemKeyCertOptions(PemKeyCertOptions options) { 208 // return cast(MySQLConnectOptions) super.setPemKeyCertOptions(options); 209 // } 210 211 // override 212 // MySQLConnectOptions setTrustOptions(TrustOptions options) { 213 // return cast(MySQLConnectOptions) super.setTrustOptions(options); 214 // } 215 216 // override 217 // MySQLConnectOptions setTrustStoreOptions(JksOptions options) { 218 // return cast(MySQLConnectOptions) super.setTrustStoreOptions(options); 219 // } 220 221 // override 222 // MySQLConnectOptions setPemTrustOptions(PemTrustOptions options) { 223 // return cast(MySQLConnectOptions) super.setPemTrustOptions(options); 224 // } 225 226 // override 227 // MySQLConnectOptions setPfxTrustOptions(PfxOptions options) { 228 // return cast(MySQLConnectOptions) super.setPfxTrustOptions(options); 229 // } 230 231 // override 232 // MySQLConnectOptions addEnabledCipherSuite(string suite) { 233 // return cast(MySQLConnectOptions) super.addEnabledCipherSuite(suite); 234 // } 235 236 // override 237 // MySQLConnectOptions addEnabledSecureTransportProtocol(string protocol) { 238 // return cast(MySQLConnectOptions) super.addEnabledSecureTransportProtocol(protocol); 239 // } 240 241 // override 242 // MySQLConnectOptions removeEnabledSecureTransportProtocol(string protocol) { 243 // return cast(MySQLConnectOptions) super.removeEnabledSecureTransportProtocol(protocol); 244 // } 245 246 override 247 MySQLConnectOptions setUseAlpn(bool useAlpn) { 248 return cast(MySQLConnectOptions) super.setUseAlpn(useAlpn); 249 } 250 251 // override 252 // MySQLConnectOptions setSslEngineOptions(SSLEngineOptions sslEngineOptions) { 253 // return cast(MySQLConnectOptions) super.setSslEngineOptions(sslEngineOptions); 254 // } 255 256 // override 257 // MySQLConnectOptions setJdkSslEngineOptions(JdkSSLEngineOptions sslEngineOptions) { 258 // return cast(MySQLConnectOptions) super.setJdkSslEngineOptions(sslEngineOptions); 259 // } 260 261 override 262 MySQLConnectOptions setTcpFastOpen(bool tcpFastOpen) { 263 return cast(MySQLConnectOptions) super.setTcpFastOpen(tcpFastOpen); 264 } 265 266 override 267 MySQLConnectOptions setTcpCork(bool tcpCork) { 268 return cast(MySQLConnectOptions) super.setTcpCork(tcpCork); 269 } 270 271 override 272 MySQLConnectOptions setTcpQuickAck(bool tcpQuickAck) { 273 return cast(MySQLConnectOptions) super.setTcpQuickAck(tcpQuickAck); 274 } 275 276 // override 277 // ClientOptionsBase setOpenSslEngineOptions(OpenSSLEngineOptions sslEngineOptions) { 278 // return super.setOpenSslEngineOptions(sslEngineOptions); 279 // } 280 281 // override 282 // MySQLConnectOptions addCrlPath(string crlPath) { 283 // return cast(MySQLConnectOptions) super.addCrlPath(crlPath); 284 // } 285 286 // override 287 // MySQLConnectOptions addCrlValue(Buffer crlValue) { 288 // return cast(MySQLConnectOptions) super.addCrlValue(crlValue); 289 // } 290 291 override 292 MySQLConnectOptions setTrustAll(bool trustAll) { 293 return cast(MySQLConnectOptions) super.setTrustAll(trustAll); 294 } 295 296 override 297 MySQLConnectOptions setConnectTimeout(Duration connectTimeout) { 298 return cast(MySQLConnectOptions) super.setConnectTimeout(connectTimeout); 299 } 300 301 override 302 MySQLConnectOptions setMetricsName(string metricsName) { 303 return cast(MySQLConnectOptions) super.setMetricsName(metricsName); 304 } 305 306 override 307 MySQLConnectOptions setReconnectAttempts(int attempts) { 308 return cast(MySQLConnectOptions) super.setReconnectAttempts(attempts); 309 } 310 311 override 312 MySQLConnectOptions setReconnectInterval(Duration interval) { 313 return cast(MySQLConnectOptions) super.setReconnectInterval(interval); 314 } 315 316 override 317 MySQLConnectOptions setHostnameVerificationAlgorithm(string hostnameVerificationAlgorithm) { 318 return cast(MySQLConnectOptions) super.setHostnameVerificationAlgorithm(hostnameVerificationAlgorithm); 319 } 320 321 override 322 MySQLConnectOptions setLogActivity(bool logEnabled) { 323 return cast(MySQLConnectOptions) super.setLogActivity(logEnabled); 324 } 325 326 override 327 MySQLConnectOptions setProxyOptions(ProxyOptions proxyOptions) { 328 return cast(MySQLConnectOptions) super.setProxyOptions(proxyOptions); 329 } 330 331 override 332 MySQLConnectOptions setLocalAddress(string localAddress) { 333 return cast(MySQLConnectOptions) super.setLocalAddress(localAddress); 334 } 335 336 // override 337 // MySQLConnectOptions setEnabledSecureTransportProtocols(Set!(string) enabledSecureTransportProtocols) { 338 // return cast(MySQLConnectOptions) super.setEnabledSecureTransportProtocols(enabledSecureTransportProtocols); 339 // } 340 341 override 342 MySQLConnectOptions setSslHandshakeTimeout(Duration sslHandshakeTimeout) { 343 return cast(MySQLConnectOptions) super.setSslHandshakeTimeout(sslHandshakeTimeout); 344 } 345 346 /** 347 * Initialize with the default options. 348 */ 349 override protected void initialize() { 350 this.setHost(DEFAULT_HOST); 351 this.setPort(DEFAULT_PORT); 352 this.setUser(DEFAULT_USER); 353 this.setPassword(DEFAULT_PASSWORD); 354 this.setDatabase(DEFAULT_SCHEMA); 355 this.setProperties(new HashMap!(string, string)(DEFAULT_CONNECTION_ATTRIBUTES)); 356 } 357 358 // override 359 // JsonObject toJson() { 360 // JsonObject json = super.toJson(); 361 // MySQLConnectOptionsConverter.toJson(this, json); 362 // return json; 363 // } 364 }