Search in sources :

Example 21 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class SqlParser method parseBindVariableBlockComment.

protected void parseBindVariableBlockComment() {
    String variableName = tokenType.extract(token);
    if (variableName.isEmpty()) {
        throw new JdbcException(Message.DOMA2120, sql, tokenizer.getLineNumber(), tokenizer.getPosition(), token);
    }
    BindVariableNode node = new BindVariableNode(getLocation(), variableName, token);
    appendNode(node);
    push(node);
}
Also used : JdbcException(org.seasar.doma.jdbc.JdbcException) BindVariableNode(org.seasar.doma.internal.jdbc.sql.node.BindVariableNode)

Example 22 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class SqlTokenizer method peekOneChar.

protected void peekOneChar(char c) {
    if (isWhitespace(c)) {
        type = WHITESPACE;
    } else if (c == '(') {
        type = OPENED_PARENS;
    } else if (c == ')') {
        type = CLOSED_PARENS;
    } else if (c == ';') {
        type = DELIMITER;
    } else if (c == '\'') {
        type = QUOTE;
        boolean closed = false;
        while (buf.hasRemaining()) {
            char c2 = buf.get();
            if (c2 == '\'') {
                if (buf.hasRemaining()) {
                    buf.mark();
                    char c3 = buf.get();
                    if (c3 != '\'') {
                        buf.reset();
                        closed = true;
                        break;
                    }
                } else {
                    closed = true;
                }
            }
        }
        if (closed) {
            return;
        }
        int pos = buf.position() - lineStartPosition;
        throw new JdbcException(Message.DOMA2101, sql, lineNumber, pos);
    } else if (isWordStart(c)) {
        type = WORD;
        while (buf.hasRemaining()) {
            buf.mark();
            char c2 = buf.get();
            if (c2 == '\'') {
                boolean closed = false;
                while (buf.hasRemaining()) {
                    char c3 = buf.get();
                    if (c3 == '\'') {
                        if (buf.hasRemaining()) {
                            buf.mark();
                            char c4 = buf.get();
                            if (c4 != '\'') {
                                buf.reset();
                                closed = true;
                                break;
                            }
                        } else {
                            closed = true;
                        }
                    }
                }
                if (closed) {
                    return;
                }
                int pos = buf.position() - lineStartPosition;
                throw new JdbcException(Message.DOMA2101, sql, lineNumber, pos);
            }
            if (!isWordPart(c2)) {
                buf.reset();
                return;
            }
        }
    } else if (c == '\r' || c == '\n') {
        type = EOL;
        currentLineNumber++;
    } else {
        type = OTHER;
    }
}
Also used : JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 23 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class SqlTokenizer method peekTwoChars.

protected void peekTwoChars(char c, char c2) {
    if ((c == 'o' || c == 'O') && (c2 == 'r' || c2 == 'R')) {
        type = OR_WORD;
        if (isWordTerminated()) {
            return;
        }
    } else if (c == '/' && c2 == '*') {
        type = BLOCK_COMMENT;
        if (buf.hasRemaining()) {
            char c3 = buf.get();
            if (ExpressionUtil.isExpressionIdentifierStart(c3)) {
                type = BIND_VARIABLE_BLOCK_COMMENT;
            } else if (c3 == '^') {
                type = LITERAL_VARIABLE_BLOCK_COMMENT;
            } else if (c3 == '#') {
                type = EMBEDDED_VARIABLE_BLOCK_COMMENT;
            } else if (c3 == '%') {
                if (buf.hasRemaining()) {
                    char c4 = buf.get();
                    if (buf.hasRemaining()) {
                        char c5 = buf.get();
                        if (c4 == 'i' && c5 == 'f') {
                            if (isBlockCommentDirectiveTerminated()) {
                                type = IF_BLOCK_COMMENT;
                            }
                        } else if (buf.hasRemaining()) {
                            char c6 = buf.get();
                            if (c4 == 'f' && c5 == 'o' && c6 == 'r') {
                                if (isBlockCommentDirectiveTerminated()) {
                                    type = FOR_BLOCK_COMMENT;
                                }
                            } else if (c4 == 'e' && c5 == 'n' && c6 == 'd') {
                                if (isBlockCommentDirectiveTerminated()) {
                                    type = END_BLOCK_COMMENT;
                                }
                            } else if (buf.hasRemaining()) {
                                char c7 = buf.get();
                                if (c4 == 'e' && c5 == 'l' && c6 == 's' && c7 == 'e') {
                                    if (isBlockCommentDirectiveTerminated()) {
                                        type = ELSE_BLOCK_COMMENT;
                                    } else {
                                        if (buf.hasRemaining()) {
                                            char c8 = buf.get();
                                            if (buf.hasRemaining()) {
                                                char c9 = buf.get();
                                                if (c8 == 'i' && c9 == 'f') {
                                                    if (isBlockCommentDirectiveTerminated()) {
                                                        type = ELSEIF_BLOCK_COMMENT;
                                                    }
                                                } else {
                                                    buf.position(buf.position() - 6);
                                                }
                                            } else {
                                                buf.position(buf.position() - 5);
                                            }
                                        }
                                    }
                                } else if (buf.hasRemaining()) {
                                    char c8 = buf.get();
                                    if (buf.hasRemaining()) {
                                        char c9 = buf.get();
                                        if (c4 == 'e' && c5 == 'x' && c6 == 'p' && c7 == 'a' && c8 == 'n' && c9 == 'd') {
                                            if (isBlockCommentDirectiveTerminated()) {
                                                type = EXPAND_BLOCK_COMMENT;
                                            }
                                        } else if (buf.hasRemaining()) {
                                            char c10 = buf.get();
                                            if (buf.hasRemaining()) {
                                                char c11 = buf.get();
                                                if (c4 == 'p' && c5 == 'o' && c6 == 'p' && c7 == 'u' && c8 == 'l' && c9 == 'a' && c10 == 't' && c11 == 'e') {
                                                    if (isBlockCommentDirectiveTerminated()) {
                                                        type = POPULATE_BLOCK_COMMENT;
                                                    }
                                                } else {
                                                    buf.position(buf.position() - 8);
                                                }
                                            } else {
                                                buf.position(buf.position() - 7);
                                            }
                                        } else {
                                            buf.position(buf.position() - 6);
                                        }
                                    } else {
                                        buf.position(buf.position() - 5);
                                    }
                                } else {
                                    buf.position(buf.position() - 4);
                                }
                            } else {
                                buf.position(buf.position() - 3);
                            }
                        } else {
                            buf.position(buf.position() - 2);
                        }
                    } else {
                        buf.position(buf.position() - 1);
                    }
                }
                if (type != IF_BLOCK_COMMENT && type != FOR_BLOCK_COMMENT && type != END_BLOCK_COMMENT && type != ELSE_BLOCK_COMMENT && type != ELSEIF_BLOCK_COMMENT && type != EXPAND_BLOCK_COMMENT && type != POPULATE_BLOCK_COMMENT) {
                    int pos = buf.position() - lineStartPosition;
                    throw new JdbcException(Message.DOMA2119, sql, lineNumber, pos);
                }
            }
            buf.position(buf.position() - 1);
        }
        while (buf.hasRemaining()) {
            char c3 = buf.get();
            if (buf.hasRemaining()) {
                buf.mark();
                char c4 = buf.get();
                if (c3 == '*' && c4 == '/') {
                    return;
                }
                if ((c3 == '\r' || c3 == '\n')) {
                    currentLineNumber++;
                }
                buf.reset();
            }
        }
        int pos = buf.position() - lineStartPosition;
        throw new JdbcException(Message.DOMA2102, sql, lineNumber, pos);
    } else if (c == '-' && c2 == '-') {
        type = LINE_COMMENT;
        while (buf.hasRemaining()) {
            buf.mark();
            char c3 = buf.get();
            if (c3 == '\r' || c3 == '\n') {
                buf.reset();
                return;
            }
        }
        return;
    } else if (c == '\r' && c2 == '\n') {
        type = EOL;
        currentLineNumber++;
        return;
    }
    buf.position(buf.position() - 1);
    peekOneChar(c);
}
Also used : JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 24 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class KeepAliveLocalTransactionTest method testBegin_failedToSetTransactionIsolation.

@Test
public void testBegin_failedToSetTransactionIsolation() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getKeepAliveLocalTransaction(jdbcLogger);
    try {
        transaction.begin(TransactionIsolationLevel.READ_COMMITTED);
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 25 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class LocalTransactionTest method testBegin_failedToSetTransactionIsolation.

@Test
public void testBegin_failedToSetTransactionIsolation() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setTransactionIsolation(int level) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getLocalTransaction(jdbcLogger);
    try {
        transaction.begin(TransactionIsolationLevel.READ_COMMITTED);
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcException (org.seasar.doma.jdbc.JdbcException)69 Test (org.junit.jupiter.api.Test)35 SqlNode (org.seasar.doma.jdbc.SqlNode)18 SQLException (java.sql.SQLException)15 BigDecimal (java.math.BigDecimal)9 Value (org.seasar.doma.internal.expr.Value)9 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)8 SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)7 SelectOptions (org.seasar.doma.jdbc.SelectOptions)7 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)6 Connection (java.sql.Connection)5 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)5 PreparedStatement (java.sql.PreparedStatement)4 Savepoint (java.sql.Savepoint)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 DomaNullPointerException (org.seasar.doma.DomaNullPointerException)4 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)4 MockDataSource (org.seasar.doma.internal.jdbc.mock.MockDataSource)4 IfBlockNode (org.seasar.doma.internal.jdbc.sql.node.IfBlockNode)4