Skip to main content
summaryrefslogtreecommitdiffstats
blob: e89cf0cf6cc857e367b70847fed9b16518b734fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.db.connection.core.query;

import java.sql.SQLException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Interesting information from a query. These are automatically added to the QueryLog if it is not full.
 * 
 * @author Robert A. Fisher
 */
public class QueryRecord {
   private static final QueryLog log = QueryLog.getInstance();
   private final Date date;
   private final String sql;
   private Object[] bindVariables;
   private SQLException sqlException;
   private Long runDurationMs;
   private long startTime;

   /**
    * @param sql The sql text
    */
   public QueryRecord(String sql) {
      this(sql, (Object[]) null);
   }

   /**
    * Replaces all of the '?' characters with ':#' values, where # is in incrementing integer value starting at 1.
    * 
    * @param sql The sql string to perform the replacement on.
    */
   private String replaceBindValues(String sql) {
      int count = 1;

      Matcher matcher = Pattern.compile("\\?").matcher(sql);
      while (matcher.find()) {
         sql = matcher.replaceFirst(":" + count++);
         matcher.reset(sql);
      }
      return sql;
   }

   /**
    * @param sql The sql text
    * @param bindVariables The bind variables, if any
    */
   public QueryRecord(String sql, Object... bindVariablesLocal) {
      if (sql == null) throw new IllegalArgumentException("sql can not be null");
      this.date = new Date();
      this.sql = replaceBindValues(sql);
      this.bindVariables = new Object[bindVariablesLocal.length];
      System.arraycopy(bindVariablesLocal, 0, bindVariables, 0, bindVariables.length);

      for (int i = 0; i < bindVariables.length; i++) {
         Object obj = bindVariables[i];
         if (obj != null) {
            if (obj instanceof String) {
               String str = ((String) obj);
               if (str.length() > 80) {
                  bindVariables[i] = str.substring(0, 80);
               }
            } else if (!(obj instanceof Date || obj instanceof Integer || obj instanceof Long || obj instanceof Double)) {
               bindVariables[i] = "binary type";
            } else {
               bindVariables[i] = obj.toString();
            }
         }
      }
      log.add(this);
   }

   /**
    * @return the runDurationMs
    */
   public Long getRunDurationMs() {
      return runDurationMs;
   }

   /**
    * Mark the start of the query being run
    */
   public void markStart() {
      startTime = System.currentTimeMillis();
   }

   /**
    * Mark the end of the query being run
    */
   public void markEnd() {
      runDurationMs = System.currentTimeMillis() - startTime;
   }

   /**
    * @return the sqlException
    */
   public SQLException getSqlException() {
      return sqlException;
   }

   /**
    * @param sqlException the sqlException to set
    */
   public void setSqlException(SQLException sqlException) {
      this.sqlException = sqlException;
   }

   /**
    * @return the bindVariables
    */
   public Object[] getBindVariables() {
      return bindVariables;
   }

   /**
    * @return the sql
    */
   public String getSql() {
      return sql;
   }

   /**
    * @return the date
    */
   public Date getDate() {
      return date;
   }

}

Back to the top