use of com.mysql.cj.protocol.Resultset in project ABC by RuiPinto96274.
the class NativeSession method getProcessHost.
public String getProcessHost() {
try {
long threadId = getThreadId();
String processHost = findProcessHost(threadId);
if (processHost == null) {
// http://bugs.mysql.com/bug.php?id=44167 - connection ids on the wire wrap at 4 bytes even though they're 64-bit numbers
this.log.logWarn(String.format("Connection id %d not found in \"SHOW PROCESSLIST\", assuming 32-bit overflow, using SELECT CONNECTION_ID() instead", threadId));
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, "SELECT CONNECTION_ID()"), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<Long> lvf = new LongValueFactory(getPropertySet());
Row r;
if ((r = rs.getRows().next()) != null) {
threadId = r.getValue(0, lvf);
processHost = findProcessHost(threadId);
} else {
this.log.logError("No rows returned for statement \"SELECT CONNECTION_ID()\", local connection check will most likely be incorrect");
}
}
if (processHost == null) {
this.log.logWarn(String.format("Cannot find process listing for connection %d in SHOW PROCESSLIST output, unable to determine if locally connected", threadId));
}
return processHost;
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e);
}
}
use of com.mysql.cj.protocol.Resultset in project ABC by RuiPinto96274.
the class NativeSession method findProcessHost.
private String findProcessHost(long threadId) {
try {
String processHost = null;
String ps = this.protocol.getServerSession().getServerVariable("performance_schema");
NativePacketPayload resultPacket = // performance_schema.threads in MySQL 5.5 does not contain PROCESSLIST_HOST column
versionMeetsMinimum(5, 6, 0) && ps != null && ("1".contentEquals(ps) || "ON".contentEquals(ps)) ? sendCommand(this.commandBuilder.buildComQuery(null, "select PROCESSLIST_ID, PROCESSLIST_USER, PROCESSLIST_HOST from performance_schema.threads where PROCESSLIST_ID=" + threadId), false, 0) : sendCommand(this.commandBuilder.buildComQuery(null, "SHOW PROCESSLIST"), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<Long> lvf = new LongValueFactory(getPropertySet());
ValueFactory<String> svf = new StringValueFactory(this.propertySet);
Row r;
while ((r = rs.getRows().next()) != null) {
long id = r.getValue(0, lvf);
if (threadId == id) {
processHost = r.getValue(2, svf);
break;
}
}
return processHost;
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e);
}
}
use of com.mysql.cj.protocol.Resultset in project ABC by RuiPinto96274.
the class NativeSession method loadServerVariables.
/**
* Loads the result of 'SHOW VARIABLES' into the serverVariables field so
* that the driver can configure itself.
*
* @param syncMutex
* synchronization mutex
* @param version
* driver version string
*/
public void loadServerVariables(Object syncMutex, String version) {
if (this.cacheServerConfiguration.getValue()) {
createConfigCacheIfNeeded(syncMutex);
Map<String, String> cachedVariableMap = this.serverConfigCache.get(this.hostInfo.getDatabaseUrl());
if (cachedVariableMap != null) {
String cachedServerVersion = cachedVariableMap.get(SERVER_VERSION_STRING_VAR_NAME);
if (cachedServerVersion != null && getServerSession().getServerVersion() != null && cachedServerVersion.equals(getServerSession().getServerVersion().toString())) {
Map<String, String> localVariableMap = this.protocol.getServerSession().getServerVariables();
Map<String, String> newLocalVariableMap = new HashMap<>();
newLocalVariableMap.putAll(cachedVariableMap);
// preserving variables already configured on previous session initialization steps
newLocalVariableMap.putAll(localVariableMap);
this.protocol.getServerSession().setServerVariables(newLocalVariableMap);
return;
}
this.serverConfigCache.invalidate(this.hostInfo.getDatabaseUrl());
}
}
try {
if (version != null && version.indexOf('*') != -1) {
StringBuilder buf = new StringBuilder(version.length() + 10);
for (int i = 0; i < version.length(); i++) {
char c = version.charAt(i);
buf.append(c == '*' ? "[star]" : c);
}
version = buf.toString();
}
String versionComment = (this.propertySet.getBooleanProperty(PropertyKey.paranoid).getValue() || version == null) ? "" : "/* " + version + " */";
this.protocol.getServerSession().setServerVariables(new HashMap<String, String>());
if (versionMeetsMinimum(5, 1, 0)) {
StringBuilder queryBuf = new StringBuilder(versionComment).append("SELECT");
queryBuf.append(" @@session.auto_increment_increment AS auto_increment_increment");
queryBuf.append(", @@character_set_client AS character_set_client");
queryBuf.append(", @@character_set_connection AS character_set_connection");
queryBuf.append(", @@character_set_results AS character_set_results");
queryBuf.append(", @@character_set_server AS character_set_server");
queryBuf.append(", @@collation_server AS collation_server");
queryBuf.append(", @@collation_connection AS collation_connection");
queryBuf.append(", @@init_connect AS init_connect");
queryBuf.append(", @@interactive_timeout AS interactive_timeout");
if (!versionMeetsMinimum(5, 5, 0)) {
queryBuf.append(", @@language AS language");
}
queryBuf.append(", @@license AS license");
queryBuf.append(", @@lower_case_table_names AS lower_case_table_names");
queryBuf.append(", @@max_allowed_packet AS max_allowed_packet");
queryBuf.append(", @@net_write_timeout AS net_write_timeout");
queryBuf.append(", @@performance_schema AS performance_schema");
if (!versionMeetsMinimum(8, 0, 3)) {
queryBuf.append(", @@query_cache_size AS query_cache_size");
queryBuf.append(", @@query_cache_type AS query_cache_type");
}
queryBuf.append(", @@sql_mode AS sql_mode");
queryBuf.append(", @@system_time_zone AS system_time_zone");
queryBuf.append(", @@time_zone AS time_zone");
if (versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0))) {
queryBuf.append(", @@transaction_isolation AS transaction_isolation");
} else {
queryBuf.append(", @@tx_isolation AS transaction_isolation");
}
queryBuf.append(", @@wait_timeout AS wait_timeout");
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, queryBuf.toString()), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
Field[] f = rs.getColumnDefinition().getFields();
if (f.length > 0) {
ValueFactory<String> vf = new StringValueFactory(this.propertySet);
Row r;
if ((r = rs.getRows().next()) != null) {
for (int i = 0; i < f.length; i++) {
String value = r.getValue(i, vf);
this.protocol.getServerSession().getServerVariables().put(f[i].getColumnLabel(), // recent server versions return "utf8mb3" instead of "utf8"
"utf8mb3".equalsIgnoreCase(value) ? "utf8" : value);
}
}
}
} else {
NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, versionComment + "SHOW VARIABLES"), false, 0);
Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
ValueFactory<String> vf = new StringValueFactory(this.propertySet);
Row r;
while ((r = rs.getRows().next()) != null) {
this.protocol.getServerSession().getServerVariables().put(r.getValue(0, vf), r.getValue(1, vf));
}
}
} catch (IOException e) {
throw ExceptionFactory.createException(e.getMessage(), e);
}
if (this.cacheServerConfiguration.getValue()) {
this.protocol.getServerSession().getServerVariables().put(SERVER_VERSION_STRING_VAR_NAME, getServerSession().getServerVersion().toString());
Map<String, String> localVariableMap = new HashMap<>();
localVariableMap.putAll(this.protocol.getServerSession().getServerVariables());
this.serverConfigCache.put(this.hostInfo.getDatabaseUrl(), Collections.unmodifiableMap(localVariableMap));
}
}
use of com.mysql.cj.protocol.Resultset in project ABC by RuiPinto96274.
the class NativeProtocol method explainSlowQuery.
/**
* Runs an 'EXPLAIN' on the given query and dumps the results to the log
*
* @param query
* full query string
* @param truncatedQuery
* query string truncated for profiling
*/
public void explainSlowQuery(String query, String truncatedQuery) {
if (StringUtils.startsWithIgnoreCaseAndWs(truncatedQuery, EXPLAINABLE_STATEMENT) || (versionMeetsMinimum(5, 6, 3) && StringUtils.startsWithIgnoreCaseAndWs(truncatedQuery, EXPLAINABLE_STATEMENT_EXTENSION) != -1)) {
try {
NativePacketPayload resultPacket = sendCommand(getCommandBuilder().buildComQuery(getSharedSendPacket(), "EXPLAIN " + query), false, 0);
Resultset rs = readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
StringBuilder explainResults = new StringBuilder(Messages.getString("Protocol.6"));
explainResults.append(truncatedQuery);
explainResults.append(Messages.getString("Protocol.7"));
appendResultSetSlashGStyle(explainResults, rs);
this.log.logWarn(explainResults.toString());
} catch (CJException sqlEx) {
throw sqlEx;
} catch (Exception ex) {
throw ExceptionFactory.createException(ex.getMessage(), ex, getExceptionInterceptor());
}
}
}
use of com.mysql.cj.protocol.Resultset in project ABC by RuiPinto96274.
the class NativeProtocol method convertShowWarningsToSQLWarnings.
/**
* Turns output of 'SHOW WARNINGS' into JDBC SQLWarning instances.
*
* If 'forTruncationOnly' is true, only looks for truncation warnings, and
* actually throws DataTruncation as an exception.
*
* @param warningCountIfKnown
* the warning count (if known), otherwise set it to 0.
* @param forTruncationOnly
* if this method should only scan for data truncation warnings
*
* @return the SQLWarning chain (or null if no warnings)
*/
public SQLWarning convertShowWarningsToSQLWarnings(int warningCountIfKnown, boolean forTruncationOnly) {
SQLWarning currentWarning = null;
ResultsetRows rows = null;
try {
/*
* +---------+------+---------------------------------------------+
* | Level ..| Code | Message ....................................|
* +---------+------+---------------------------------------------+
* | Warning | 1265 | Data truncated for column 'field1' at row 1 |
* +---------+------+---------------------------------------------+
*/
NativePacketPayload resultPacket = sendCommand(getCommandBuilder().buildComQuery(getSharedSendPacket(), "SHOW WARNINGS"), false, 0);
Resultset warnRs = readAllResults(-1, warningCountIfKnown > 99, /* stream large warning counts */
resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, Concurrency.READ_ONLY));
int codeFieldIndex = warnRs.getColumnDefinition().findColumn("Code", false, 1) - 1;
int messageFieldIndex = warnRs.getColumnDefinition().findColumn("Message", false, 1) - 1;
ValueFactory<String> svf = new StringValueFactory(this.propertySet);
ValueFactory<Integer> ivf = new IntegerValueFactory(this.propertySet);
rows = warnRs.getRows();
Row r;
while ((r = rows.next()) != null) {
int code = r.getValue(codeFieldIndex, ivf);
if (forTruncationOnly) {
if (code == MysqlErrorNumbers.ER_WARN_DATA_TRUNCATED || code == MysqlErrorNumbers.ER_WARN_DATA_OUT_OF_RANGE) {
DataTruncation newTruncation = new MysqlDataTruncation(r.getValue(messageFieldIndex, svf), 0, false, false, 0, 0, code);
if (currentWarning == null) {
currentWarning = newTruncation;
} else {
currentWarning.setNextWarning(newTruncation);
}
}
} else {
// String level = warnRs.getString("Level");
String message = r.getValue(messageFieldIndex, svf);
SQLWarning newWarning = new SQLWarning(message, MysqlErrorNumbers.mysqlToSqlState(code), code);
if (currentWarning == null) {
currentWarning = newWarning;
} else {
currentWarning.setNextWarning(newWarning);
}
}
}
if (forTruncationOnly && (currentWarning != null)) {
throw ExceptionFactory.createException(currentWarning.getMessage(), currentWarning);
}
return currentWarning;
} catch (IOException ex) {
throw ExceptionFactory.createException(ex.getMessage(), ex);
} finally {
if (rows != null) {
rows.close();
}
}
}
Aggregations