diff options
author | afinkbein | 2009-06-30 16:20:27 +0000 |
---|---|---|
committer | afinkbein | 2009-06-30 16:20:27 +0000 |
commit | 1e7356a39e26588ed5e7f58e013ddf96fe13446f (patch) | |
tree | ab72a98e773718fba697ecbba96a561baab8232f /org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java | |
parent | f84afa8a636edbb008037cef318b404fddcaf276 (diff) | |
download | org.eclipse.osee-1e7356a39e26588ed5e7f58e013ddf96fe13446f.tar.gz org.eclipse.osee-1e7356a39e26588ed5e7f58e013ddf96fe13446f.tar.xz org.eclipse.osee-1e7356a39e26588ed5e7f58e013ddf96fe13446f.zip |
Diffstat (limited to 'org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java')
-rw-r--r-- | org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java b/org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java new file mode 100644 index 00000000000..e89cf0cf6cc --- /dev/null +++ b/org.eclipse.osee.framework.db.connection/src/org/eclipse/osee/framework/db/connection/core/query/QueryRecord.java @@ -0,0 +1,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; + } + +} |