Search in sources :

Example 1 with Row

use of com.mysql.cj.result.Row 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 Row

use of com.mysql.cj.result.Row 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 Row

use of com.mysql.cj.result.Row 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 Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class NativeProtocol method appendResultSetSlashGStyle.

private StringBuilder appendResultSetSlashGStyle(StringBuilder appendTo, Resultset rs) {
    Field[] fields = rs.getColumnDefinition().getFields();
    int maxWidth = 0;
    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getColumnLabel().length() > maxWidth) {
            maxWidth = fields[i].getColumnLabel().length();
        }
    }
    int rowCount = 1;
    Row r;
    while ((r = rs.getRows().next()) != null) {
        appendTo.append("*************************** ");
        appendTo.append(rowCount++);
        appendTo.append(". row ***************************\n");
        for (int i = 0; i < fields.length; i++) {
            int leftPad = maxWidth - fields[i].getColumnLabel().length();
            for (int j = 0; j < leftPad; j++) {
                appendTo.append(" ");
            }
            appendTo.append(fields[i].getColumnLabel()).append(": ");
            String stringVal = r.getValue(i, new StringValueFactory(this.propertySet));
            appendTo.append(stringVal != null ? stringVal : "NULL").append("\n");
        }
        appendTo.append("\n");
    }
    return appendTo;
}
Also used : Field(com.mysql.cj.result.Field) StringValueFactory(com.mysql.cj.result.StringValueFactory) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row) LazyString(com.mysql.cj.util.LazyString)

Example 5 with Row

use of com.mysql.cj.result.Row 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();
        }
    }
}
Also used : SQLWarning(java.sql.SQLWarning) StringValueFactory(com.mysql.cj.result.StringValueFactory) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) LazyString(com.mysql.cj.util.LazyString) IOException(java.io.IOException) ResultsetRows(com.mysql.cj.protocol.ResultsetRows) DataTruncation(java.sql.DataTruncation) MysqlDataTruncation(com.mysql.cj.jdbc.exceptions.MysqlDataTruncation) Resultset(com.mysql.cj.protocol.Resultset) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row)

Aggregations

Row (com.mysql.cj.result.Row)144 ArrayList (java.util.ArrayList)81 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)78 ByteArrayRow (com.mysql.cj.protocol.a.result.ByteArrayRow)72 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)69 Field (com.mysql.cj.result.Field)69 ResultsetRowsStatic (com.mysql.cj.protocol.a.result.ResultsetRowsStatic)66 StringValueFactory (com.mysql.cj.result.StringValueFactory)48 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)36 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)33 Statement (java.sql.Statement)33 CJException (com.mysql.cj.exceptions.CJException)30 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)30 AssertionFailedException (com.mysql.cj.exceptions.AssertionFailedException)27 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)27 Test (org.junit.jupiter.api.Test)27 Resultset (com.mysql.cj.protocol.Resultset)21 IOException (java.io.IOException)21