Search in sources :

Example 1 with IntegerValueFactory

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

Example 2 with IntegerValueFactory

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

the class MysqlxSessionTest method testGenericQuery.

@Test
public void testGenericQuery() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    XMessageBuilder builder = (XMessageBuilder) this.session.<XMessage>getMessageBuilder();
    List<Integer> ints = this.session.query(builder.buildSqlStatement("select 2 union select 1"), null, r -> r.getValue(0, new IntegerValueFactory(new DefaultPropertySet())), Collectors.toList());
    assertEquals(2, ints.size());
    assertEquals(new Integer(2), ints.get(0));
    assertEquals(new Integer(1), ints.get(1));
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) Test(org.junit.jupiter.api.Test)

Example 3 with IntegerValueFactory

use of com.mysql.cj.result.IntegerValueFactory in project JavaSegundasQuintas by ecteruel.

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)

Example 4 with IntegerValueFactory

use of com.mysql.cj.result.IntegerValueFactory in project JavaSegundasQuintas by ecteruel.

the class MysqlxSessionTest method testGenericQuery.

@Test
public void testGenericQuery() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    XMessageBuilder builder = (XMessageBuilder) this.session.<XMessage>getMessageBuilder();
    List<Integer> ints = this.session.query(builder.buildSqlStatement("select 2 union select 1"), null, r -> r.getValue(0, new IntegerValueFactory(new DefaultPropertySet())), Collectors.toList());
    assertEquals(2, ints.size());
    assertEquals(new Integer(2), ints.get(0));
    assertEquals(new Integer(1), ints.get(1));
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) IntegerValueFactory(com.mysql.cj.result.IntegerValueFactory) Test(org.junit.jupiter.api.Test)

Example 5 with IntegerValueFactory

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

Aggregations

IntegerValueFactory (com.mysql.cj.result.IntegerValueFactory)9 Resultset (com.mysql.cj.protocol.Resultset)6 Row (com.mysql.cj.result.Row)6 StringValueFactory (com.mysql.cj.result.StringValueFactory)6 IOException (java.io.IOException)6 DefaultPropertySet (com.mysql.cj.conf.DefaultPropertySet)3 MysqlDataTruncation (com.mysql.cj.jdbc.exceptions.MysqlDataTruncation)3 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)3 ResultsetRows (com.mysql.cj.protocol.ResultsetRows)3 NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)3 ResultsetFactory (com.mysql.cj.protocol.a.ResultsetFactory)3 XMessageBuilder (com.mysql.cj.protocol.x.XMessageBuilder)3 LazyString (com.mysql.cj.util.LazyString)3 DataTruncation (java.sql.DataTruncation)3 SQLWarning (java.sql.SQLWarning)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Test (org.junit.jupiter.api.Test)3