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.

ProfilingPreparedStatementHandler.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.profiling;
  21. import java.lang.reflect.InvocationHandler;
  22. import java.lang.reflect.Method;
  23. import java.sql.PreparedStatement;
  24. import org.sonar.api.utils.log.Profiler;
  25. class ProfilingPreparedStatementHandler implements InvocationHandler {
  26. private final PreparedStatement statement;
  27. private final String sql;
  28. private final Object[] sqlParams;
  29. ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) {
  30. this.statement = statement;
  31. this.sql = sql;
  32. sqlParams = new Object[SqlLogFormatter.countArguments(sql)];
  33. }
  34. @Override
  35. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  36. if (method.getName().startsWith("execute")) {
  37. Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start();
  38. Object result;
  39. try {
  40. result = InvocationUtils.invokeQuietly(statement, method, args);
  41. } finally {
  42. profiler.addContext("sql", SqlLogFormatter.reformatSql(sql));
  43. if (sqlParams.length > 0) {
  44. profiler.addContext("params", SqlLogFormatter.reformatParams(sqlParams));
  45. }
  46. profiler.stopTrace("");
  47. }
  48. return result;
  49. } else if (method.getName().startsWith("set") && args.length > 1) {
  50. sqlParams[(int) args[0] - 1] = args[1];
  51. return InvocationUtils.invokeQuietly(statement, method, args);
  52. } else {
  53. return InvocationUtils.invokeQuietly(statement, method, args);
  54. }
  55. }
  56. }