001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2011-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.server.monitor;
011
012import mondrian.server.Locus;
013import mondrian.server.Statement;
014
015/**
016 * Event concerning an SQL statement.
017 */
018public abstract class SqlStatementEvent extends Event {
019    /**
020     * Identifier of the statement. Unique for the lifetime of the JVM.
021     */
022    public final long sqlStatementId;
023
024    /**
025     * Locus of event. From this you can glean the statement and session.
026     */
027    public final Locus locus;
028
029    /**
030     * SQL text of statement.
031     */
032    public final String sql;
033
034    /**
035     * Purpose of executing this SQL statement.
036     */
037    public final Purpose purpose;
038
039    /**
040     * Creates a SqlStatementEvent.
041     *
042     * @param timestamp Timestamp
043     * @param sqlStatementId SQL statement id
044     * @param locus Locus of event
045     * @param sql SQL
046     * @param purpose Why Mondrian is executing this statement
047     */
048    public SqlStatementEvent(
049        long timestamp,
050        long sqlStatementId,
051        Locus locus,
052        String sql,
053        Purpose purpose)
054    {
055        super(timestamp);
056        assert locus != null;
057        assert sql != null;
058        assert purpose != null;
059        this.locus = locus;
060        this.sqlStatementId = sqlStatementId;
061        this.sql = sql;
062        this.purpose = purpose;
063    }
064
065    public long getStatementId() {
066        if (locus.execution != null) {
067            final Statement mondrianStatement =
068                locus.execution.getMondrianStatement();
069            if (mondrianStatement != null) {
070                return mondrianStatement.getId();
071            }
072        }
073        return -1;
074    }
075
076    /**
077     * Reason why Mondrian is executing this SQL statement.
078     */
079    public enum Purpose {
080        DRILL_THROUGH,
081        CELL_SEGMENT,
082        TUPLES,
083        OTHER
084    }
085}
086
087// End SqlStatementEvent.java