Search in sources :

Example 1 with WrongArgumentException

use of com.mysql.cj.exceptions.WrongArgumentException in project ABC by RuiPinto96274.

the class SyncMessageReader method readMessageLocal.

@SuppressWarnings("unchecked")
private <T extends GeneratedMessageV3> T readMessageLocal(Class<T> messageClass, boolean fromQueue) {
    XMessageHeader header;
    if (fromQueue) {
        header = this.headersQueue.poll();
        T msg = (T) this.messagesQueue.poll();
        if (msg != null) {
            return msg;
        }
    } else {
        header = this.headersQueue.getLast();
    }
    Parser<T> parser = (Parser<T>) MessageConstants.MESSAGE_CLASS_TO_PARSER.get(messageClass);
    byte[] packet = new byte[header.getMessageSize()];
    try {
        this.inputStream.readFully(packet);
    } catch (IOException ex) {
        // TODO close socket?
        throw new CJCommunicationsException("Cannot read packet payload", ex);
    }
    try {
        T msg = parser.parseFrom(packet);
        if (msg instanceof Frame && ((Frame) msg).getType() == Frame.Type.WARNING_VALUE && ((Frame) msg).getScope() == Frame.Scope.GLOBAL) {
            XWarning w = new XWarning((Frame) msg);
            int code = (int) w.getCode();
            if (code == MysqlErrorNumbers.ER_SERVER_SHUTDOWN || code == MysqlErrorNumbers.ER_IO_READ_ERROR || code == MysqlErrorNumbers.ER_SESSION_WAS_KILLED) {
                CJCommunicationsException ex = new CJCommunicationsException(w.getMessage());
                ex.setVendorCode(code);
                if (this.protocolEventHandler != null) {
                    this.protocolEventHandler.invokeListeners(code == MysqlErrorNumbers.ER_SERVER_SHUTDOWN ? EventType.SERVER_SHUTDOWN : EventType.SERVER_CLOSED_SESSION, ex);
                }
                throw ex;
            }
        }
        return msg;
    } catch (InvalidProtocolBufferException ex) {
        throw new WrongArgumentException(ex);
    }
}
Also used : XWarning(com.mysql.cj.protocol.x.Notice.XWarning) Frame(com.mysql.cj.x.protobuf.MysqlxNotice.Frame) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) IOException(java.io.IOException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) Parser(com.google.protobuf.Parser)

Example 2 with WrongArgumentException

use of com.mysql.cj.exceptions.WrongArgumentException in project ABC by RuiPinto96274.

the class FieldFactory method columnMetaDataToField.

/**
 * Convert a X Protocol {@link ColumnMetaData} message to a C/J {@link Field} object.
 *
 * @param col
 *            the message from the server
 * @param characterSet
 *            the encoding of the strings in the message
 * @return {@link Field}
 */
private Field columnMetaDataToField(ColumnMetaData col, String characterSet) {
    try {
        LazyString databaseName = new LazyString(col.getSchema().toString(characterSet));
        LazyString tableName = new LazyString(col.getTable().toString(characterSet));
        LazyString originalTableName = new LazyString(col.getOriginalTable().toString(characterSet));
        LazyString columnName = new LazyString(col.getName().toString(characterSet));
        LazyString originalColumnName = new LazyString(col.getOriginalName().toString(characterSet));
        long length = Integer.toUnsignedLong(col.getLength());
        int decimals = col.getFractionalDigits();
        int collationIndex = 0;
        if (col.hasCollation()) {
            // TODO: support custom character set
            collationIndex = (int) col.getCollation();
        }
        String encoding = CharsetMapping.getStaticJavaEncodingForCollationIndex(collationIndex);
        MysqlType mysqlType = findMysqlType(col.getType(), col.getContentType(), col.getFlags(), collationIndex);
        int mysqlTypeId = xProtocolTypeToMysqlType(col.getType(), col.getContentType());
        // flags translation; unsigned is handled in Field by checking the MysqlType, so here we check others
        short flags = (short) 0;
        if (col.getType().equals(FieldType.UINT) && 0 < (col.getFlags() & XPROTOCOL_COLUMN_FLAGS_UINT_ZEROFILL)) {
            flags |= MysqlType.FIELD_FLAG_ZEROFILL;
        } else if (col.getType().equals(FieldType.BYTES) && 0 < (col.getFlags() & XPROTOCOL_COLUMN_FLAGS_BYTES_RIGHTPAD)) {
            mysqlType = MysqlType.CHAR;
        } else if (col.getType().equals(FieldType.DATETIME) && 0 < (col.getFlags() & XPROTOCOL_COLUMN_FLAGS_DATETIME_TIMESTAMP)) {
            mysqlType = MysqlType.TIMESTAMP;
        }
        if ((col.getFlags() & XPROTOCOL_COLUMN_FLAGS_NOT_NULL) > 0) {
            flags |= MysqlType.FIELD_FLAG_NOT_NULL;
        }
        if ((col.getFlags() & XPROTOCOL_COLUMN_FLAGS_PRIMARY_KEY) > 0) {
            flags |= MysqlType.FIELD_FLAG_PRIMARY_KEY;
        }
        if ((col.getFlags() & XPROTOCOL_COLUMN_FLAGS_UNIQUE_KEY) > 0) {
            flags |= MysqlType.FIELD_FLAG_UNIQUE_KEY;
        }
        if ((col.getFlags() & XPROTOCOL_COLUMN_FLAGS_MULTIPLE_KEY) > 0) {
            flags |= MysqlType.FIELD_FLAG_MULTIPLE_KEY;
        }
        if ((col.getFlags() & XPROTOCOL_COLUMN_FLAGS_AUTO_INCREMENT) > 0) {
            flags |= MysqlType.FIELD_FLAG_AUTO_INCREMENT;
        }
        // It's probably a mistake that it's exposed by protocol as a decimals and it should be replaced with 0.
        switch(mysqlType) {
            case FLOAT:
            case FLOAT_UNSIGNED:
            case DOUBLE:
            case DOUBLE_UNSIGNED:
                if (decimals == 31) {
                    decimals = 0;
                }
                break;
            default:
                break;
        }
        Field f = new Field(databaseName, tableName, originalTableName, columnName, originalColumnName, length, mysqlTypeId, flags, decimals, collationIndex, encoding, mysqlType);
        return f;
    } catch (UnsupportedEncodingException ex) {
        throw new WrongArgumentException("Unable to decode metadata strings", ex);
    }
}
Also used : MysqlType(com.mysql.cj.MysqlType) Field(com.mysql.cj.result.Field) LazyString(com.mysql.cj.util.LazyString) UnsupportedEncodingException(java.io.UnsupportedEncodingException) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) LazyString(com.mysql.cj.util.LazyString)

Example 3 with WrongArgumentException

use of com.mysql.cj.exceptions.WrongArgumentException in project ABC by RuiPinto96274.

the class ConnectionUrlTest method testConnectionUrl.

/**
 * Tests the {@link ConnectionUrl} with close to one million of different connection string variations.
 */
@Test
public void testConnectionUrl() {
    Properties props = new Properties();
    props.setProperty("propKey", "propValue");
    for (ConnectionStringGenerator.UrlMode urlMode : ConnectionStringGenerator.UrlMode.values()) {
        ConnectionStringGenerator csg = new ConnectionStringGenerator(urlMode);
        for (String cs : csg) {
            try {
                ConnectionUrl.getConnectionUrlInstance(cs, props);
            } catch (WrongArgumentException e) {
                // X plugin connections ("mysqlx:") don't allow different credentials in different hosts and the generator doesn't account for that.
                assertEquals(ConnectionUrl.Type.XDEVAPI_SESSION.getScheme(), csg.getProtocol(), cs);
                boolean first = true;
                boolean ok = false;
                String lastUi = "";
                for (int hostIndex = 0; hostIndex < urlMode.getHostsCount() && !ok; hostIndex++) {
                    if (first) {
                        first = false;
                        lastUi = csg.getUserInfo(hostIndex);
                    } else if (!lastUi.equals(csg.getUserInfo(hostIndex))) {
                        ok = true;
                    }
                }
                assertTrue(ok, cs + ": unexpected " + e.getClass().getName() + " thrown with message: " + e.getMessage());
            }
        }
    }
}
Also used : WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 4 with WrongArgumentException

use of com.mysql.cj.exceptions.WrongArgumentException in project ABC by RuiPinto96274.

the class ExprParser method ilriExpr.

Expr ilriExpr() {
    Expr lhs = compExpr();
    List<TokenType> expected = Arrays.asList(new TokenType[] { TokenType.IS, TokenType.IN, TokenType.LIKE, TokenType.BETWEEN, TokenType.REGEXP, TokenType.NOT, TokenType.OVERLAPS });
    while (this.tokenPos < this.tokens.size() && expected.contains(this.tokens.get(this.tokenPos).type)) {
        boolean isNot = false;
        if (currentTokenTypeEquals(TokenType.NOT)) {
            consumeToken(TokenType.NOT);
            isNot = true;
        }
        if (this.tokenPos < this.tokens.size()) {
            List<Expr> params = new ArrayList<>();
            params.add(lhs);
            String opName = this.tokens.get(this.tokenPos).value.toLowerCase();
            switch(this.tokens.get(this.tokenPos).type) {
                case // for IS, NOT comes AFTER
                IS:
                    consumeToken(TokenType.IS);
                    if (currentTokenTypeEquals(TokenType.NOT)) {
                        consumeToken(TokenType.NOT);
                        opName = "is_not";
                    }
                    params.add(compExpr());
                    break;
                case IN:
                    consumeToken(TokenType.IN);
                    if (currentTokenTypeEquals(TokenType.LPAREN)) {
                        params.addAll(parenExprList());
                    } else {
                        opName = "cont_in";
                        params.add(compExpr());
                    }
                    break;
                case LIKE:
                    consumeToken(TokenType.LIKE);
                    params.add(compExpr());
                    if (currentTokenTypeEquals(TokenType.ESCAPE)) {
                        consumeToken(TokenType.ESCAPE);
                        // add as a third (optional) param
                        params.add(compExpr());
                    }
                    break;
                case BETWEEN:
                    consumeToken(TokenType.BETWEEN);
                    params.add(compExpr());
                    assertTokenAt(this.tokenPos, TokenType.AND);
                    consumeToken(TokenType.AND);
                    params.add(compExpr());
                    break;
                case REGEXP:
                    consumeToken(TokenType.REGEXP);
                    params.add(compExpr());
                    break;
                case OVERLAPS:
                    consumeToken(TokenType.OVERLAPS);
                    params.add(compExpr());
                    break;
                default:
                    throw new WrongArgumentException("Unknown token after NOT at pos: " + this.tokenPos);
            }
            if (isNot) {
                opName = "not_" + opName;
            }
            Operator.Builder builder = Operator.newBuilder().setName(opName).addAllParam(params);
            lhs = Expr.newBuilder().setType(Expr.Type.OPERATOR).setOperator(builder.build()).build();
        }
    }
    return lhs;
}
Also used : Operator(com.mysql.cj.x.protobuf.MysqlxExpr.Operator) Expr(com.mysql.cj.x.protobuf.MysqlxExpr.Expr) ArrayList(java.util.ArrayList) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException)

Example 5 with WrongArgumentException

use of com.mysql.cj.exceptions.WrongArgumentException in project ABC by RuiPinto96274.

the class ExprParser method lex.

/**
 * Lexer for X DevAPI expression language.
 */
void lex() {
    for (int i = 0; i < this.string.length(); ++i) {
        // for routines that consume more than one char
        int start = i;
        char c = this.string.charAt(i);
        if (Character.isWhitespace(c)) {
        // ignore
        } else if (Character.isDigit(c)) {
            i = lexNumber(i);
        } else if (!(c == '_' || Character.isUnicodeIdentifierStart(c))) {
            // non-identifier, e.g. operator or quoted literal
            switch(c) {
                case ':':
                    this.tokens.add(new Token(TokenType.COLON, c));
                    break;
                case '+':
                    this.tokens.add(new Token(TokenType.PLUS, c));
                    break;
                case '-':
                    if (nextCharEquals(i, '>')) {
                        i++;
                        this.tokens.add(new Token(TokenType.COLDOCPATH, "->"));
                    } else {
                        this.tokens.add(new Token(TokenType.MINUS, c));
                    }
                    break;
                case '*':
                    if (nextCharEquals(i, '*')) {
                        i++;
                        this.tokens.add(new Token(TokenType.DOUBLESTAR, "**"));
                    } else {
                        this.tokens.add(new Token(TokenType.STAR, c));
                    }
                    break;
                case '/':
                    this.tokens.add(new Token(TokenType.SLASH, c));
                    break;
                case '$':
                    this.tokens.add(new Token(TokenType.DOLLAR, c));
                    break;
                case '%':
                    this.tokens.add(new Token(TokenType.MOD, c));
                    break;
                case '=':
                    if (nextCharEquals(i, '=')) {
                        i++;
                    }
                    this.tokens.add(new Token(TokenType.EQ, "=="));
                    break;
                case '&':
                    if (nextCharEquals(i, '&')) {
                        i++;
                        this.tokens.add(new Token(TokenType.ANDAND, "&&"));
                    } else {
                        this.tokens.add(new Token(TokenType.BITAND, c));
                    }
                    break;
                case '|':
                    if (nextCharEquals(i, '|')) {
                        i++;
                        this.tokens.add(new Token(TokenType.OROR, "||"));
                    } else {
                        this.tokens.add(new Token(TokenType.BITOR, c));
                    }
                    break;
                case '^':
                    this.tokens.add(new Token(TokenType.BITXOR, c));
                    break;
                case '(':
                    this.tokens.add(new Token(TokenType.LPAREN, c));
                    break;
                case ')':
                    this.tokens.add(new Token(TokenType.RPAREN, c));
                    break;
                case '[':
                    this.tokens.add(new Token(TokenType.LSQBRACKET, c));
                    break;
                case ']':
                    this.tokens.add(new Token(TokenType.RSQBRACKET, c));
                    break;
                case '{':
                    this.tokens.add(new Token(TokenType.LCURLY, c));
                    break;
                case '}':
                    this.tokens.add(new Token(TokenType.RCURLY, c));
                    break;
                case '~':
                    this.tokens.add(new Token(TokenType.NEG, c));
                    break;
                case ',':
                    this.tokens.add(new Token(TokenType.COMMA, c));
                    break;
                case '!':
                    if (nextCharEquals(i, '=')) {
                        i++;
                        this.tokens.add(new Token(TokenType.NE, "!="));
                    } else {
                        this.tokens.add(new Token(TokenType.BANG, c));
                    }
                    break;
                case '?':
                    this.tokens.add(new Token(TokenType.EROTEME, c));
                    break;
                case '<':
                    if (nextCharEquals(i, '<')) {
                        i++;
                        this.tokens.add(new Token(TokenType.LSHIFT, "<<"));
                    } else if (nextCharEquals(i, '=')) {
                        i++;
                        this.tokens.add(new Token(TokenType.LE, "<="));
                    } else {
                        this.tokens.add(new Token(TokenType.LT, c));
                    }
                    break;
                case '>':
                    if (nextCharEquals(i, '>')) {
                        i++;
                        this.tokens.add(new Token(TokenType.RSHIFT, ">>"));
                    } else if (nextCharEquals(i, '=')) {
                        i++;
                        this.tokens.add(new Token(TokenType.GE, ">="));
                    } else {
                        this.tokens.add(new Token(TokenType.GT, c));
                    }
                    break;
                case '.':
                    if (nextCharEquals(i, '*')) {
                        i++;
                        this.tokens.add(new Token(TokenType.DOTSTAR, ".*"));
                    } else if (i + 1 < this.string.length() && Character.isDigit(this.string.charAt(i + 1))) {
                        i = lexNumber(i);
                    } else {
                        this.tokens.add(new Token(TokenType.DOT, c));
                    }
                    break;
                case '"':
                case '\'':
                case '`':
                    char quoteChar = c;
                    StringBuilder val = new StringBuilder();
                    try {
                        for (c = this.string.charAt(++i); c != quoteChar || (i + 1 < this.string.length() && this.string.charAt(i + 1) == quoteChar); c = this.string.charAt(++i)) {
                            if (c == '\\' || c == quoteChar) {
                                ++i;
                            }
                            val.append(this.string.charAt(i));
                        }
                    } catch (StringIndexOutOfBoundsException ex) {
                        throw new WrongArgumentException("Unterminated string starting at " + start);
                    }
                    this.tokens.add(new Token(quoteChar == '`' ? TokenType.IDENT : TokenType.LSTRING, val.toString()));
                    break;
                default:
                    throw new WrongArgumentException("Can't parse at pos: " + i);
            }
        } else {
            // otherwise, it's an identifier
            for (; i < this.string.length() && Character.isUnicodeIdentifierPart(this.string.charAt(i)); ++i) {
            }
            String val = this.string.substring(start, i);
            String valLower = val.toLowerCase();
            if (i < this.string.length()) {
                // last char, this logic is artifact of the preceding loop
                --i;
            }
            if (reservedWords.containsKey(valLower)) {
                // Map operator names to values the server understands
                if ("and".equals(valLower)) {
                    this.tokens.add(new Token(reservedWords.get(valLower), "&&"));
                } else if ("or".equals(valLower)) {
                    this.tokens.add(new Token(reservedWords.get(valLower), "||"));
                } else {
                    // we case-normalize reserved words
                    this.tokens.add(new Token(reservedWords.get(valLower), valLower));
                }
            } else {
                this.tokens.add(new Token(TokenType.IDENT, val));
            }
        }
    }
}
Also used : WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException)

Aggregations

WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)46 IOException (java.io.IOException)10 DocumentPathItem (com.mysql.cj.x.protobuf.MysqlxExpr.DocumentPathItem)9 Expr (com.mysql.cj.x.protobuf.MysqlxExpr.Expr)9 Operator (com.mysql.cj.x.protobuf.MysqlxExpr.Operator)9 Test (org.junit.jupiter.api.Test)9 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)7 Error (com.mysql.cj.x.protobuf.Mysqlx.Error)6 ColumnIdentifier (com.mysql.cj.x.protobuf.MysqlxExpr.ColumnIdentifier)6 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 Parser (com.google.protobuf.Parser)4 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)3 MysqlType (com.mysql.cj.MysqlType)3 AuthMech (com.mysql.cj.conf.PropertyDefinitions.AuthMech)3 FullReadInputStream (com.mysql.cj.protocol.FullReadInputStream)3 XWarning (com.mysql.cj.protocol.x.Notice.XWarning)3 Field (com.mysql.cj.result.Field)3 Row (com.mysql.cj.result.Row)3