View Javadoc
1   /*
2    * Tentackle - https://tentackle.org
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  
19  package org.tentackle.dbms;
20  
21  import org.tentackle.log.StatisticsResult;
22  import org.tentackle.misc.TimeKeeper;
23  
24  /**
25   * Statistics result for SQL statements.<br>
26   * Optionally extends {@link StatisticsResult} by fetch duration for the result set and result set size.
27   */
28  public class StatementStatisticsResult extends StatisticsResult {
29  
30    private StatisticsResult fetchResult;     // != null if result set involved
31    private int minRows = Integer.MAX_VALUE;  // minimum number of returned rows
32    private int maxRows = Integer.MIN_VALUE;  // maximum number of returned rows
33    private long totalRows;                   // total number of returned rows
34  
35  
36    /**
37     * Creates a statement statistics result.
38     */
39    public StatementStatisticsResult() {
40      // see -Xlint:missing-explicit-ctor since Java 16
41    }
42  
43    /**
44     * Counts the fetch-statistics.
45     *
46     * @param duration the duration fetching the result from the database
47     * @param rows the number of rows fetched
48     */
49    public void countFetch(TimeKeeper duration, int rows) {
50      if (fetchResult == null) {
51        fetchResult = new StatisticsResult();
52      }
53      fetchResult.count(duration);
54  
55      if (rows < minRows) {
56        minRows = rows;
57      }
58      if (rows > maxRows) {
59        maxRows = rows;
60      }
61      totalRows += rows;
62    }
63  
64    /**
65     * Returns whether the statement involved fetching from a result set.
66     *
67     * @return true if fetch statistics are valid
68     */
69    public boolean isFetchResultValid() {
70      return fetchResult != null;
71    }
72  
73    /**
74     * Returns the result for fetching the result set from the database.
75     *
76     * @return the fetch statistics, null if no result set involved
77     */
78    public StatisticsResult getFetchResult() {
79      return fetchResult;
80    }
81  
82    /**
83     * Gets the minimum number of returned rows.
84     *
85     * @return the row count, {@link Integer#MAX_VALUE} if no result set involved
86     */
87    public int getMinRows() {
88      return minRows;
89    }
90  
91    /**
92     * Gets the maximum number of returned rows.
93     *
94     * @return the row count, {@link Integer#MIN_VALUE} if no result set involved
95     */
96    public int getMaxRows() {
97      return maxRows;
98    }
99  
100   /**
101    * Gets the total number of returned rows.
102    *
103    * @return the total number of rows fetched
104    */
105   public long getTotalRows() {
106     return totalRows;
107   }
108 
109   @Override
110   public String toString() {
111     if (isValid()) {
112       StringBuilder buf = new StringBuilder();
113       buf.append(getMinDuration().millisToString()).append(' ')
114          .append(getMaxDuration().millisToString()).append(' ')
115          .append(getTotalDuration().millisToString());
116       if (isFetchResultValid()) {
117         buf.append(' ')
118            .append(minRows).append(' ')
119            .append(maxRows).append(' ')
120            .append(totalRows).append(' ')
121            .append(fetchResult.getMinDuration().millisToString()).append(' ')
122            .append(fetchResult.getMaxDuration().millisToString()).append(' ')
123            .append(fetchResult.getTotalDuration().millisToString());
124       }
125       buf.append(" ms / ").append(getCount());
126       return buf.toString();
127     }
128     return "<invalid>";
129   }
130 
131 }