forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLConnectionImpl.java
More file actions
109 lines (96 loc) · 3.76 KB
/
MySQLConnectionImpl.java
File metadata and controls
109 lines (96 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.mysqlclient.impl;
import io.vertx.core.Context;
import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.internal.ContextInternal;
import io.vertx.mysqlclient.MySQLAuthOptions;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLConnection;
import io.vertx.mysqlclient.MySQLSetOption;
import io.vertx.mysqlclient.impl.command.*;
import io.vertx.mysqlclient.spi.MySQLDriver;
import io.vertx.sqlclient.internal.SqlConnectionBase;
import io.vertx.sqlclient.spi.connection.Connection;
import io.vertx.sqlclient.spi.connection.ConnectionFactory;
import static io.vertx.sqlclient.impl.ConnectionFactoryBase.UDS_NOT_SUPPORTED;
public class MySQLConnectionImpl extends SqlConnectionBase<MySQLConnectionImpl> implements MySQLConnection {
public static Future<MySQLConnection> connect(ContextInternal ctx, MySQLConnectOptions options) {
if (options.isUsingDomainSocket() && !ctx.owner().transport().supportsDomainSockets()) {
return ctx.failedFuture(UDS_NOT_SUPPORTED);
}
MySQLConnectionFactory client;
try {
client = new MySQLConnectionFactory(ctx.owner());
} catch (Exception e) {
return ctx.failedFuture(e);
}
return client.connect((Context)ctx, options).map(conn -> {
MySQLConnectionImpl impl = new MySQLConnectionImpl(ctx, client, conn);
conn.init(impl);
prepareForClose(ctx, impl);
return impl;
});
}
public MySQLConnectionImpl(ContextInternal context, ConnectionFactory factory, Connection conn) {
super(context, factory, conn, MySQLDriver.INSTANCE);
}
@Override
public Future<Void> ping() {
return schedule(context, new PingCommand());
}
@Override
public Future<Void> specifySchema(String schemaName) {
return schedule(context, new InitDbCommand(schemaName));
}
@Override
public Future<String> getInternalStatistics() {
return schedule(context, new StatisticsCommand());
}
@Override
public Future<Void> setOption(MySQLSetOption option) {
return schedule(context, new SetOptionCommand(option));
}
@Override
public Future<Void> resetConnection() {
return schedule(context, new ResetConnectionCommand());
}
@Override
public Future<Void> debug() {
return schedule(context, new DebugCommand());
}
@Override
public Future<Void> changeUser(MySQLAuthOptions options) {
MySQLCollation collation;
if (options.getCollation() != null) {
// override the collation if configured
collation = MySQLCollation.valueOfName(options.getCollation());
} else {
String charset = options.getCharset();
if (charset == null) {
collation = MySQLCollation.DEFAULT_COLLATION;
} else {
collation = MySQLCollation.valueOfName(MySQLCollation.getDefaultCollationFromCharsetName(charset));
}
}
Buffer serverRsaPublicKey = null;
if (options.getServerRsaPublicKeyValue() != null) {
serverRsaPublicKey = options.getServerRsaPublicKeyValue();
} else {
if (options.getServerRsaPublicKeyPath() != null) {
serverRsaPublicKey = context.owner().fileSystem().readFileBlocking(options.getServerRsaPublicKeyPath());
}
}
ChangeUserCommand cmd = new ChangeUserCommand(options.getUser(), options.getPassword(), options.getDatabase(), collation, serverRsaPublicKey, options.getProperties());
return schedule(context, cmd);
}
}