Java Code Examples for com.mysql.jdbc.log.LogUtils

The following examples show how to use com.mysql.jdbc.log.LogUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: r-course   Source File: StatementImpl.java    License: MIT License 4 votes vote down vote up
/**
 * Constructor for a Statement.
 * 
 * @param c
 *            the Connection instance that creates us
 * @param catalog
 *            the database name in use when we were created
 * 
 * @throws SQLException
 *             if an error occurs.
 */
public StatementImpl(MySQLConnection c, String catalog) throws SQLException {
    if ((c == null) || c.isClosed()) {
        throw SQLError.createSQLException(Messages.getString("Statement.0"), SQLError.SQL_STATE_CONNECTION_NOT_OPEN, null);
    }

    this.connection = c;
    this.connectionId = this.connection.getId();
    this.exceptionInterceptor = this.connection.getExceptionInterceptor();

    this.currentCatalog = catalog;
    this.pedantic = this.connection.getPedantic();
    this.continueBatchOnError = this.connection.getContinueBatchOnError();
    this.useLegacyDatetimeCode = this.connection.getUseLegacyDatetimeCode();
    this.sendFractionalSeconds = this.connection.getSendFractionalSeconds();
    this.doEscapeProcessing = this.connection.getEnableEscapeProcessing();

    if (!this.connection.getDontTrackOpenResources()) {
        this.connection.registerStatement(this);
    }

    this.maxFieldSize = this.connection.getMaxAllowedPacket();

    int defaultFetchSize = this.connection.getDefaultFetchSize();
    if (defaultFetchSize != 0) {
        setFetchSize(defaultFetchSize);
    }

    if (this.connection.getUseUnicode()) {
        this.charEncoding = this.connection.getEncoding();
        this.charConverter = this.connection.getCharsetConverter(this.charEncoding);
    }

    boolean profiling = this.connection.getProfileSql() || this.connection.getUseUsageAdvisor() || this.connection.getLogSlowQueries();
    if (this.connection.getAutoGenerateTestcaseScript() || profiling) {
        this.statementId = statementCounter++;
    }
    if (profiling) {
        this.pointOfOrigin = LogUtils.findCallingClassAndMethod(new Throwable());
        this.profileSQL = this.connection.getProfileSql();
        this.useUsageAdvisor = this.connection.getUseUsageAdvisor();
        this.eventSink = ProfilerEventHandlerFactory.getInstance(this.connection);
    }

    int maxRowsConn = this.connection.getMaxRows();
    if (maxRowsConn != -1) {
        setMaxRows(maxRowsConn);
    }

    this.holdResultsOpenOverClose = this.connection.getHoldResultsOpenOverStatementClose();

    this.version5013OrNewer = this.connection.versionMeetsMinimum(5, 0, 13);
}
 
Example 2
Source Project: r-course   Source File: ServerPreparedStatement.java    License: MIT License 4 votes vote down vote up
private void serverPrepare(String sql) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        MysqlIO mysql = this.connection.getIO();

        if (this.connection.getAutoGenerateTestcaseScript()) {
            dumpPrepareForTestcase();
        }

        try {
            long begin = 0;

            if (StringUtils.startsWithIgnoreCaseAndWs(sql, "LOAD DATA")) {
                this.isLoadDataQuery = true;
            } else {
                this.isLoadDataQuery = false;
            }

            if (this.connection.getProfileSql()) {
                begin = System.currentTimeMillis();
            }

            String characterEncoding = null;
            String connectionEncoding = this.connection.getEncoding();

            if (!this.isLoadDataQuery && this.connection.getUseUnicode() && (connectionEncoding != null)) {
                characterEncoding = connectionEncoding;
            }

            Buffer prepareResultPacket = mysql.sendCommand(MysqlDefs.COM_PREPARE, sql, null, false, characterEncoding, 0);

            if (this.connection.versionMeetsMinimum(4, 1, 1)) {
                // 4.1.1 and newer use the first byte as an 'ok' or 'error' flag, so move the buffer pointer past it to start reading the statement id.
                prepareResultPacket.setPosition(1);
            } else {
                // 4.1.0 doesn't use the first byte as an 'ok' or 'error' flag
                prepareResultPacket.setPosition(0);
            }

            this.serverStatementId = prepareResultPacket.readLong();
            this.fieldCount = prepareResultPacket.readInt();
            this.parameterCount = prepareResultPacket.readInt();
            this.parameterBindings = new BindValue[this.parameterCount];

            for (int i = 0; i < this.parameterCount; i++) {
                this.parameterBindings[i] = new BindValue();
            }

            this.connection.incrementNumberOfPrepares();

            if (this.profileSQL) {
                this.eventSink.consumeEvent(new ProfilerEvent(ProfilerEvent.TYPE_PREPARE, "", this.currentCatalog, this.connectionId, this.statementId, -1,
                        System.currentTimeMillis(), mysql.getCurrentTimeNanosOrMillis() - begin, mysql.getQueryTimingUnits(), null,
                        LogUtils.findCallingClassAndMethod(new Throwable()), truncateQueryToLog(sql)));
            }

            boolean checkEOF = !mysql.isEOFDeprecated();

            if (this.parameterCount > 0 && this.connection.versionMeetsMinimum(4, 1, 2) && !mysql.isVersion(5, 0, 0)) {
                this.parameterFields = new Field[this.parameterCount];

                Buffer metaDataPacket;
                for (int i = 0; i < this.parameterCount; i++) {
                    metaDataPacket = mysql.readPacket();
                    this.parameterFields[i] = mysql.unpackField(metaDataPacket, false);
                }
                if (checkEOF) { // Skip the following EOF packet.
                    mysql.readPacket();
                }
            }

            // Read in the result set column information
            if (this.fieldCount > 0) {
                this.resultFields = new Field[this.fieldCount];

                Buffer fieldPacket;
                for (int i = 0; i < this.fieldCount; i++) {
                    fieldPacket = mysql.readPacket();
                    this.resultFields[i] = mysql.unpackField(fieldPacket, false);
                }
                if (checkEOF) { // Skip the following EOF packet.
                    mysql.readPacket();
                }
            }
        } catch (SQLException sqlEx) {
            if (this.connection.getDumpQueriesOnException()) {
                StringBuilder messageBuf = new StringBuilder(this.originalSql.length() + 32);
                messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
                messageBuf.append(this.originalSql);

                sqlEx = ConnectionImpl.appendMessageToException(sqlEx, messageBuf.toString(), getExceptionInterceptor());
            }

            throw sqlEx;
        } finally {
            // Leave the I/O channel in a known state...there might be packets out there that we're not interested in
            this.connection.getIO().clearInputStream();
        }
    }
}
 
Example 3
Source Project: Komondor   Source File: StatementImpl.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructor for a Statement.
 * 
 * @param c
 *            the Connection instance that creates us
 * @param catalog
 *            the database name in use when we were created
 * 
 * @throws SQLException
 *             if an error occurs.
 */
public StatementImpl(MySQLConnection c, String catalog) throws SQLException {
    if ((c == null) || c.isClosed()) {
        throw SQLError.createSQLException(Messages.getString("Statement.0"), SQLError.SQL_STATE_CONNECTION_NOT_OPEN, null);
    }

    this.connection = c;
    this.connectionId = this.connection.getId();
    this.exceptionInterceptor = this.connection.getExceptionInterceptor();

    this.currentCatalog = catalog;
    this.pedantic = this.connection.getPedantic();
    this.continueBatchOnError = this.connection.getContinueBatchOnError();
    this.useLegacyDatetimeCode = this.connection.getUseLegacyDatetimeCode();
    this.sendFractionalSeconds = this.connection.getSendFractionalSeconds();
    this.doEscapeProcessing = this.connection.getEnableEscapeProcessing();

    if (!this.connection.getDontTrackOpenResources()) {
        this.connection.registerStatement(this);
    }

    this.maxFieldSize = this.connection.getMaxAllowedPacket();

    int defaultFetchSize = this.connection.getDefaultFetchSize();
    if (defaultFetchSize != 0) {
        setFetchSize(defaultFetchSize);
    }

    if (this.connection.getUseUnicode()) {
        this.charEncoding = this.connection.getEncoding();
        this.charConverter = this.connection.getCharsetConverter(this.charEncoding);
    }

    boolean profiling = this.connection.getProfileSql() || this.connection.getUseUsageAdvisor() || this.connection.getLogSlowQueries();
    if (this.connection.getAutoGenerateTestcaseScript() || profiling) {
        this.statementId = statementCounter++;
    }
    if (profiling) {
        this.pointOfOrigin = LogUtils.findCallingClassAndMethod(new Throwable());
        this.profileSQL = this.connection.getProfileSql();
        this.useUsageAdvisor = this.connection.getUseUsageAdvisor();
        this.eventSink = ProfilerEventHandlerFactory.getInstance(this.connection);
    }

    int maxRowsConn = this.connection.getMaxRows();
    if (maxRowsConn != -1) {
        setMaxRows(maxRowsConn);
    }

    this.holdResultsOpenOverClose = this.connection.getHoldResultsOpenOverStatementClose();

    this.version5013OrNewer = this.connection.versionMeetsMinimum(5, 0, 13);
}
 
Example 4
private void serverPrepare(String sql) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        MysqlIO mysql = this.connection.getIO();

        if (this.connection.getAutoGenerateTestcaseScript()) {
            dumpPrepareForTestcase();
        }

        try {
            long begin = 0;

            if (StringUtils.startsWithIgnoreCaseAndWs(sql, "LOAD DATA")) {
                this.isLoadDataQuery = true;
            } else {
                this.isLoadDataQuery = false;
            }

            if (this.connection.getProfileSql()) {
                begin = System.currentTimeMillis();
            }

            String characterEncoding = null;
            String connectionEncoding = this.connection.getEncoding();

            if (!this.isLoadDataQuery && this.connection.getUseUnicode() && (connectionEncoding != null)) {
                characterEncoding = connectionEncoding;
            }

            Buffer prepareResultPacket = mysql.sendCommand(MysqlDefs.COM_PREPARE, sql, null, false, characterEncoding, 0);

            if (this.connection.versionMeetsMinimum(4, 1, 1)) {
                // 4.1.1 and newer use the first byte as an 'ok' or 'error' flag, so move the buffer pointer past it to start reading the statement id.
                prepareResultPacket.setPosition(1);
            } else {
                // 4.1.0 doesn't use the first byte as an 'ok' or 'error' flag
                prepareResultPacket.setPosition(0);
            }

            this.serverStatementId = prepareResultPacket.readLong();
            this.fieldCount = prepareResultPacket.readInt();
            this.parameterCount = prepareResultPacket.readInt();
            this.parameterBindings = new BindValue[this.parameterCount];

            for (int i = 0; i < this.parameterCount; i++) {
                this.parameterBindings[i] = new BindValue();
            }

            this.connection.incrementNumberOfPrepares();

            if (this.profileSQL) {
                this.eventSink.consumeEvent(new ProfilerEvent(ProfilerEvent.TYPE_PREPARE, "", this.currentCatalog, this.connectionId, this.statementId, -1,
                        System.currentTimeMillis(), mysql.getCurrentTimeNanosOrMillis() - begin, mysql.getQueryTimingUnits(), null,
                        LogUtils.findCallingClassAndMethod(new Throwable()), truncateQueryToLog(sql)));
            }

            boolean checkEOF = !mysql.isEOFDeprecated();

            if (this.parameterCount > 0 && this.connection.versionMeetsMinimum(4, 1, 2) && !mysql.isVersion(5, 0, 0)) {
                this.parameterFields = new Field[this.parameterCount];

                Buffer metaDataPacket;
                for (int i = 0; i < this.parameterCount; i++) {
                    metaDataPacket = mysql.readPacket();
                    this.parameterFields[i] = mysql.unpackField(metaDataPacket, false);
                }
                if (checkEOF) { // Skip the following EOF packet.
                    mysql.readPacket();
                }
            }

            // Read in the result set column information
            if (this.fieldCount > 0) {
                this.resultFields = new Field[this.fieldCount];

                Buffer fieldPacket;
                for (int i = 0; i < this.fieldCount; i++) {
                    fieldPacket = mysql.readPacket();
                    this.resultFields[i] = mysql.unpackField(fieldPacket, false);
                }
                if (checkEOF) { // Skip the following EOF packet.
                    mysql.readPacket();
                }
            }
        } catch (SQLException sqlEx) {
            if (this.connection.getDumpQueriesOnException()) {
                StringBuilder messageBuf = new StringBuilder(this.originalSql.length() + 32);
                messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
                messageBuf.append(this.originalSql);

                sqlEx = ConnectionImpl.appendMessageToException(sqlEx, messageBuf.toString(), getExceptionInterceptor());
            }

            throw sqlEx;
        } finally {
            // Leave the I/O channel in a known state...there might be packets out there that we're not interested in
            this.connection.getIO().clearInputStream();
        }
    }
}