You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatementLogger.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2011 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.util;
  17. import java.text.DecimalFormat;
  18. import java.util.Set;
  19. import java.util.concurrent.atomic.AtomicLong;
  20. /**
  21. * Utility class to optionally log generated statements to StatementListeners.<br>
  22. * Statement logging is disabled by default.
  23. * <p>
  24. * This class also tracks the counts for generated statements by major type.
  25. *
  26. */
  27. public class StatementLogger {
  28. /**
  29. * Enumeration of the different statement types that are logged.
  30. */
  31. public enum StatementType {
  32. STAT, TOTAL, CREATE, INSERT, UPDATE, MERGE, DELETE, SELECT;
  33. }
  34. /**
  35. * Interface that defines a statement listener.
  36. */
  37. public interface StatementListener {
  38. void logStatement(StatementType type, String statement);
  39. }
  40. private static final Set<StatementListener> LISTENERS = Utils.newHashSet();
  41. private static final StatementListener CONSOLE = new StatementListener() {
  42. @Override
  43. public void logStatement(StatementType type, String message) {
  44. System.out.println(message);
  45. }
  46. };
  47. private static final AtomicLong SELECT_COUNT = new AtomicLong();
  48. private static final AtomicLong CREATE_COUNT = new AtomicLong();
  49. private static final AtomicLong INSERT_COUNT = new AtomicLong();
  50. private static final AtomicLong UPDATE_COUNT = new AtomicLong();
  51. private static final AtomicLong MERGE_COUNT = new AtomicLong();
  52. private static final AtomicLong DELETE_COUNT = new AtomicLong();
  53. /**
  54. * Activates the Console Logger.
  55. */
  56. public static void activateConsoleLogger() {
  57. LISTENERS.add(CONSOLE);
  58. }
  59. /**
  60. * Deactivates the Console Logger.
  61. */
  62. public static void deactivateConsoleLogger() {
  63. LISTENERS.remove(CONSOLE);
  64. }
  65. /**
  66. * Registers a listener with the relay.
  67. *
  68. * @param listener
  69. */
  70. public static void registerListener(StatementListener listener) {
  71. LISTENERS.add(listener);
  72. }
  73. /**
  74. * Unregisters a listener with the relay.
  75. *
  76. * @param listener
  77. */
  78. public static void unregisterListener(StatementListener listener) {
  79. LISTENERS.remove(listener);
  80. }
  81. public static void create(String statement) {
  82. CREATE_COUNT.incrementAndGet();
  83. logStatement(StatementType.CREATE, statement);
  84. }
  85. public static void insert(String statement) {
  86. INSERT_COUNT.incrementAndGet();
  87. logStatement(StatementType.INSERT, statement);
  88. }
  89. public static void update(String statement) {
  90. UPDATE_COUNT.incrementAndGet();
  91. logStatement(StatementType.UPDATE, statement);
  92. }
  93. public static void merge(String statement) {
  94. MERGE_COUNT.incrementAndGet();
  95. logStatement(StatementType.MERGE, statement);
  96. }
  97. public static void delete(String statement) {
  98. DELETE_COUNT.incrementAndGet();
  99. logStatement(StatementType.DELETE, statement);
  100. }
  101. public static void select(String statement) {
  102. SELECT_COUNT.incrementAndGet();
  103. logStatement(StatementType.SELECT, statement);
  104. }
  105. private static void logStatement(StatementType type, String statement) {
  106. for (StatementListener listener : LISTENERS) {
  107. try {
  108. listener.logStatement(type, statement);
  109. } catch (Throwable t) {
  110. }
  111. }
  112. }
  113. public static long getCreateCount() {
  114. return CREATE_COUNT.longValue();
  115. }
  116. public static long getInsertCount() {
  117. return INSERT_COUNT.longValue();
  118. }
  119. public static long getUpdateCount() {
  120. return UPDATE_COUNT.longValue();
  121. }
  122. public static long getMergeCount() {
  123. return MERGE_COUNT.longValue();
  124. }
  125. public static long getDeleteCount() {
  126. return DELETE_COUNT.longValue();
  127. }
  128. public static long getSelectCount() {
  129. return SELECT_COUNT.longValue();
  130. }
  131. public static long getTotalCount() {
  132. return getCreateCount() + getInsertCount() + getUpdateCount() + getDeleteCount() + getMergeCount()
  133. + getSelectCount();
  134. }
  135. public static void logStats() {
  136. logStatement(StatementType.STAT, "iciql Runtime Statistics");
  137. logStatement(StatementType.STAT, "========================");
  138. logStat(StatementType.CREATE, getCreateCount());
  139. logStat(StatementType.INSERT, getInsertCount());
  140. logStat(StatementType.UPDATE, getUpdateCount());
  141. logStat(StatementType.MERGE, getMergeCount());
  142. logStat(StatementType.DELETE, getDeleteCount());
  143. logStat(StatementType.SELECT, getSelectCount());
  144. logStatement(StatementType.STAT, "========================");
  145. logStat(StatementType.TOTAL, getTotalCount());
  146. }
  147. private static void logStat(StatementType type, long value) {
  148. if (value > 0) {
  149. DecimalFormat df = new DecimalFormat("###,###,###,###");
  150. logStatement(StatementType.STAT,
  151. StringUtils.pad(type.name(), 6, " ", true) + " = " + df.format(value));
  152. }
  153. }
  154. }