001: /*
002: * Copyright (C) Jahia Ltd. All rights reserved.
003: *
004: * This software is published under the terms of the Jahia Open Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the LICENSE.txt file.
007: */
008: package org.jahia.sqlprofiler;
009:
010: import java.util.StringTokenizer;
011:
012: /**
013: * <p>Title: Contains a P6Spy Log entry information, which may be an SQL
014: * query, a result set log, etc... </p>
015: * <p>Description: </p>
016: * <p>Copyright: Copyright (c) 2003</p>
017: * <p>Company: Jahia Ltd</p>
018: * @author Serge Huber
019: * @version 1.0
020: */
021:
022: public class QueryEntry implements Comparable {
023: private long time = 0;
024: private long elapsedTime = 0;
025: private String preparedSQL;
026: private String sqlStatement;
027: private String category;
028: private String connectionID;
029: private int receptionRank = 0;
030:
031: public QueryEntry() {
032: }
033:
034: public QueryEntry(String p6EncodedText) {
035: parseP6Encoding(p6EncodedText);
036: }
037:
038: public long getTime() {
039: return time;
040: }
041:
042: public void setTime(long time) {
043: this .time = time;
044: }
045:
046: public long getElapsedTime() {
047: return elapsedTime;
048: }
049:
050: public void setElapsedTime(long elapsedTime) {
051: this .elapsedTime = elapsedTime;
052: }
053:
054: public String getPreparedSQL() {
055: return preparedSQL;
056: }
057:
058: public void setPreparedSQL(String preparedSQL) {
059: this .preparedSQL = preparedSQL;
060: }
061:
062: public String getSqlStatement() {
063: return sqlStatement;
064: }
065:
066: public void setSqlStatement(String sqlStatement) {
067: this .sqlStatement = sqlStatement;
068: }
069:
070: public String getCategory() {
071: return category;
072: }
073:
074: public void setCategory(String category) {
075: this .category = category;
076: }
077:
078: public String getConnectionID() {
079: return connectionID;
080: }
081:
082: public void setConnectionID(String connectionID) {
083: this .connectionID = connectionID;
084: }
085:
086: public int getReceptionRank() {
087: return receptionRank;
088: }
089:
090: public void setReceptionRank(int receptionRank) {
091: this .receptionRank = receptionRank;
092: }
093:
094: private void parseP6Encoding(String p6EncodedText) {
095: /*
096: * Format taken from com.p6spy.engine.logging.appender.FormattedLogger.java
097: * String logEntry = now + "|"+ elapsed + "|"+(connectionId==-1 ? "" : String.valueOf(connectionId))+"|"+category+"|"+prepared+"|"+sql;
098: */
099: StringTokenizer tokenizer = new StringTokenizer(p6EncodedText,
100: "|", true);
101: String now = tokenizer.nextToken();
102: if (now.equals("|")) {
103: now = null;
104: } else {
105: tokenizer.nextToken();
106: }
107: String elapsed = tokenizer.nextToken();
108: if (elapsed.equals("|")) {
109: elapsed = null;
110: } else {
111: tokenizer.nextToken();
112: }
113: String connectionID = tokenizer.nextToken();
114: if (connectionID.equals("|")) {
115: connectionID = null;
116: } else {
117: tokenizer.nextToken();
118: }
119: String category = tokenizer.nextToken();
120: if (category.equals("|")) {
121: category = null;
122: } else {
123: tokenizer.nextToken();
124: }
125: String prepared = tokenizer.nextToken();
126: if (prepared.equals("|")) {
127: prepared = null;
128: } else {
129: tokenizer.nextToken();
130: }
131: String sqlStatement = "";
132: if (tokenizer.hasMoreTokens()) {
133: sqlStatement = tokenizer.nextToken();
134: }
135:
136: this .time = Long.parseLong(now);
137: this .elapsedTime = Long.parseLong(elapsed);
138: this .connectionID = connectionID;
139: this .category = category;
140: this .preparedSQL = prepared;
141: this .sqlStatement = sqlStatement;
142:
143: }
144:
145: public int compareTo(Object o) {
146: QueryEntry right = (QueryEntry) o;
147: Long leftTime = new Long(getTime());
148: Long rightTime = new Long(right.getTime());
149: int timeCompare = leftTime.compareTo(rightTime);
150: if (timeCompare != 0) {
151: return timeCompare;
152: } else {
153: return new Integer(receptionRank).compareTo(new Integer(
154: right.getReceptionRank()));
155: }
156: }
157:
158: public boolean equals(Object o) {
159: return (compareTo(o) == 0);
160: }
161:
162: public int hashCode() {
163: return (Long.toString(time) + Integer.toString(receptionRank))
164: .hashCode();
165: }
166:
167: }
|