Search in sources :

Example 1 with ResultsetFactory

use of com.mysql.cj.protocol.a.ResultsetFactory 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);
    }
}
Also used : LongValueFactory(com.mysql.cj.result.LongValueFactory) Resultset(com.mysql.cj.protocol.Resultset) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) Row(com.mysql.cj.result.Row) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory)

Example 2 with ResultsetFactory

use of com.mysql.cj.protocol.a.ResultsetFactory 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);
    }
}
Also used : StringValueFactory(com.mysql.cj.result.StringValueFactory) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory) LongValueFactory(com.mysql.cj.result.LongValueFactory) Resultset(com.mysql.cj.protocol.Resultset) Row(com.mysql.cj.result.Row)

Example 3 with ResultsetFactory

use of com.mysql.cj.protocol.a.ResultsetFactory 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));
    }
}
Also used : StringValueFactory(com.mysql.cj.result.StringValueFactory) HashMap(java.util.HashMap) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory) Field(com.mysql.cj.result.Field) Resultset(com.mysql.cj.protocol.Resultset) Row(com.mysql.cj.result.Row)

Example 4 with ResultsetFactory

use of com.mysql.cj.protocol.a.ResultsetFactory in project JavaSegundasQuintas by ecteruel.

the class NativeCharsetSettings method buildCollationMapping.

/**
 * Builds the map needed for 4.1.0 and newer servers that maps field-level
 * charset/collation info to a java character encoding name.
 */
private void buildCollationMapping() {
    Map<Integer, String> customCollationIndexToCollationName = null;
    Map<String, Integer> customCollationNameToCollationIndex = null;
    Map<Integer, String> customCollationIndexToCharsetName = null;
    Map<String, Integer> customCharsetNameToMblen = null;
    Map<String, String> customCharsetNameToJavaEncoding = new HashMap<>();
    Map<String, String> customJavaEncodingUcToCharsetName = new HashMap<>();
    Map<String, Integer> customCharsetNameToCollationIndex = new HashMap<>();
    Set<String> customMultibyteEncodings = new HashSet<>();
    String databaseURL = this.session.getHostInfo().getDatabaseUrl();
    if (this.cacheServerConfiguration.getValue()) {
        synchronized (customCollationIndexToCharsetNameByUrl) {
            customCollationIndexToCollationName = customCollationIndexToCollationNameByUrl.get(databaseURL);
            customCollationNameToCollationIndex = customCollationNameToCollationIndexByUrl.get(databaseURL);
            customCollationIndexToCharsetName = customCollationIndexToCharsetNameByUrl.get(databaseURL);
            customCharsetNameToMblen = customCharsetNameToMblenByUrl.get(databaseURL);
            customCharsetNameToJavaEncoding = customCharsetNameToJavaEncodingByUrl.get(databaseURL);
            customJavaEncodingUcToCharsetName = customJavaEncodingUcToCharsetNameByUrl.get(databaseURL);
            customCharsetNameToCollationIndex = customCharsetNameToCollationIndexByUrl.get(databaseURL);
            customMultibyteEncodings = customMultibyteEncodingsByUrl.get(databaseURL);
        }
    }
    if (customCollationIndexToCharsetName == null && this.session.getPropertySet().getBooleanProperty(PropertyKey.detectCustomCollations).getValue()) {
        customCollationIndexToCollationName = new HashMap<>();
        customCollationNameToCollationIndex = new HashMap<>();
        customCollationIndexToCharsetName = new HashMap<>();
        customCharsetNameToMblen = new HashMap<>();
        customCharsetNameToJavaEncoding = new HashMap<>();
        customJavaEncodingUcToCharsetName = new HashMap<>();
        customCharsetNameToCollationIndex = new HashMap<>();
        customMultibyteEncodings = new HashSet<>();
        String customCharsetMapping = this.session.getPropertySet().getStringProperty(PropertyKey.customCharsetMapping).getValue();
        if (customCharsetMapping != null) {
            String[] pairs = customCharsetMapping.split(",");
            for (String pair : pairs) {
                int keyEnd = pair.indexOf(":");
                if (keyEnd > 0 && (keyEnd + 1) < pair.length()) {
                    String charset = pair.substring(0, keyEnd);
                    String encoding = pair.substring(keyEnd + 1);
                    customCharsetNameToJavaEncoding.put(charset, encoding);
                    customJavaEncodingUcToCharsetName.put(encoding.toUpperCase(Locale.ENGLISH), charset);
                }
            }
        }
        ValueFactory<Integer> ivf = new IntegerValueFactory(this.session.getPropertySet());
        try {
            NativePacketPayload resultPacket = this.session.sendCommand(getCommandBuilder().buildComQuery(null, "select c.COLLATION_NAME, c.CHARACTER_SET_NAME, c.ID, cs.MAXLEN, c.IS_DEFAULT='Yes' from INFORMATION_SCHEMA.COLLATIONS as c left join" + " INFORMATION_SCHEMA.CHARACTER_SETS as cs on cs.CHARACTER_SET_NAME=c.CHARACTER_SET_NAME"), false, 0);
            Resultset rs = this.session.getProtocol().readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            ValueFactory<String> svf = new StringValueFactory(this.session.getPropertySet());
            Row r;
            while ((r = rs.getRows().next()) != null) {
                String collationName = r.getValue(0, svf);
                String charsetName = r.getValue(1, svf);
                int collationIndex = ((Number) r.getValue(2, ivf)).intValue();
                int maxlen = ((Number) r.getValue(3, ivf)).intValue();
                boolean isDefault = ((Number) r.getValue(4, ivf)).intValue() > 0;
                if (// 
                collationIndex >= MAP_SIZE || !collationName.equals(getStaticCollationNameForCollationIndex(collationIndex)) || !charsetName.equals(getStaticMysqlCharsetNameForCollationIndex(collationIndex))) {
                    customCollationIndexToCollationName.put(collationIndex, collationName);
                    customCollationNameToCollationIndex.put(collationName, collationIndex);
                    customCollationIndexToCharsetName.put(collationIndex, charsetName);
                    if (isDefault) {
                        customCharsetNameToCollationIndex.put(charsetName, collationIndex);
                    } else {
                        customCharsetNameToCollationIndex.putIfAbsent(charsetName, collationIndex);
                    }
                }
                // if no static map for charsetName adding to custom map
                if (getStaticMysqlCharsetByName(charsetName) == null) {
                    customCharsetNameToMblen.put(charsetName, maxlen);
                    if (maxlen > 1) {
                        String enc = customCharsetNameToJavaEncoding.get(charsetName);
                        if (enc != null) {
                            customMultibyteEncodings.add(enc.toUpperCase(Locale.ENGLISH));
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw ExceptionFactory.createException(e.getMessage(), e, this.session.getExceptionInterceptor());
        }
        if (this.cacheServerConfiguration.getValue()) {
            synchronized (customCollationIndexToCharsetNameByUrl) {
                customCollationIndexToCollationNameByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationIndexToCollationName));
                customCollationNameToCollationIndexByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationNameToCollationIndex));
                customCollationIndexToCharsetNameByUrl.put(databaseURL, Collections.unmodifiableMap(customCollationIndexToCharsetName));
                customCharsetNameToMblenByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToMblen));
                customCharsetNameToJavaEncodingByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToJavaEncoding));
                customJavaEncodingUcToCharsetNameByUrl.put(databaseURL, Collections.unmodifiableMap(customJavaEncodingUcToCharsetName));
                customCharsetNameToCollationIndexByUrl.put(databaseURL, Collections.unmodifiableMap(customCharsetNameToCollationIndex));
                customMultibyteEncodingsByUrl.put(databaseURL, Collections.unmodifiableSet(customMultibyteEncodings));
            }
        }
    }
    if (customCollationIndexToCharsetName != null) {
        this.collationIndexToCollationName = customCollationIndexToCollationName;
        this.collationNameToCollationIndex = customCollationNameToCollationIndex;
        this.collationIndexToCharsetName = customCollationIndexToCharsetName;
        this.charsetNameToMblen = customCharsetNameToMblen;
        this.charsetNameToJavaEncoding = customCharsetNameToJavaEncoding;
        this.javaEncodingUcToCharsetName = customJavaEncodingUcToCharsetName;
        this.charsetNameToCollationIndex = customCharsetNameToCollationIndex;
        this.multibyteEncodings = customMultibyteEncodings;
    }
}
Also used : StringValueFactory(com.mysql.cj.result.StringValueFactory) HashMap(java.util.HashMap) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory) Resultset(com.mysql.cj.protocol.Resultset) Row(com.mysql.cj.result.Row) HashSet(java.util.HashSet)

Example 5 with ResultsetFactory

use of com.mysql.cj.protocol.a.ResultsetFactory in project JavaSegundasQuintas by ecteruel.

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);
    }
}
Also used : LongValueFactory(com.mysql.cj.result.LongValueFactory) Resultset(com.mysql.cj.protocol.Resultset) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) Row(com.mysql.cj.result.Row) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory)

Aggregations

Resultset (com.mysql.cj.protocol.Resultset)15 NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)15 ResultsetFactory (com.mysql.cj.protocol.a.ResultsetFactory)15 Row (com.mysql.cj.result.Row)15 IOException (java.io.IOException)15 NativeProtocol (com.mysql.cj.protocol.a.NativeProtocol)12 StringValueFactory (com.mysql.cj.result.StringValueFactory)12 LongValueFactory (com.mysql.cj.result.LongValueFactory)6 HashMap (java.util.HashMap)6 Field (com.mysql.cj.result.Field)3 IntegerValueFactory (com.mysql.cj.result.IntegerValueFactory)3 HashSet (java.util.HashSet)3