PageRenderTime 142ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/jongo/handler/JongoResultSetHandler.java

https://code.google.com/p/jongo/
Java | 125 lines | 74 code | 12 blank | 39 comment | 13 complexity | 223215e60a454da61fad8fa847ed0cdd MD5 | raw file
  1. /**
  2. * Copyright (C) 2011, 2012 Alejandro Ayuso
  3. *
  4. * This file is part of Jongo.
  5. * Jongo is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * any later version.
  9. *
  10. * Jongo is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jongo. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package org.jongo.handler;
  19. import java.sql.ResultSet;
  20. import java.sql.ResultSetMetaData;
  21. import java.sql.SQLException;
  22. import java.sql.Types;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.apache.commons.dbutils.ResultSetHandler;
  28. import org.joda.time.DateTime;
  29. import org.joda.time.format.DateTimeFormatter;
  30. import org.joda.time.format.ISODateTimeFormat;
  31. import org.jongo.rest.xstream.Row;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. /**
  35. * Handles a ResultSet and converts it to a List of {@link org.jongo.rest.xstream.Row}
  36. * @author Alejandro Ayuso
  37. */
  38. public class JongoResultSetHandler implements ResultSetHandler<List<Row>> {
  39. private final boolean all;
  40. private static final Logger l = LoggerFactory.getLogger(JongoResultSetHandler.class);
  41. private static final DateTimeFormatter dateTimeFTR = ISODateTimeFormat.dateTime();
  42. private static final DateTimeFormatter dateFTR = ISODateTimeFormat.date();
  43. private static final DateTimeFormatter timeFTR = ISODateTimeFormat.time();
  44. /**
  45. * Constructor of the handler.
  46. * @param all if true, the handler will process all results in the {@linkplain java.sql.ResultSet}
  47. * if false, it will only process the first result.
  48. */
  49. public JongoResultSetHandler(final boolean all){
  50. super();
  51. this.all = all;
  52. }
  53. /**
  54. * Method in charge of the conversion. Depending on the argument given to the contructor, it
  55. * will process all results or only the first one.
  56. * @param rs the {@linkplain java.sql.ResultSet}
  57. * @return a List of {@link org.jongo.rest.xstream.Row}
  58. * @throws SQLException if we fail to handle the {@linkplain java.sql.ResultSet}
  59. */
  60. @Override
  61. public List<Row> handle(ResultSet rs) throws SQLException {
  62. List<Row> results = new ArrayList<Row>();
  63. int rowId = 0;
  64. if(all){
  65. while (rs.next()) {
  66. Map<String, String> map = resultSetToMap(rs);
  67. if(map != null) results.add(new Row(rowId++, map));
  68. }
  69. }else{
  70. rs.next();
  71. Map<String, String> map = resultSetToMap(rs);
  72. if(map != null) results.add(new Row(rowId++, map));
  73. }
  74. return results;
  75. }
  76. /**
  77. * Converts a ResultSet to a Map. Important to note that DATE, TIMESTAMP & TIME objects generate
  78. * a {@linkplain org.joda.time.DateTime} object using {@linkplain org.joda.time.format.ISODateTimeFormat}.
  79. * @param resultSet a {@linkplain java.sql.ResultSet}
  80. * @return a Map with the column names as keys and the values. null if something goes wrong.
  81. */
  82. public static Map<String, String> resultSetToMap(ResultSet resultSet) {
  83. Map<String, String> map = new HashMap<String, String>();
  84. try{
  85. int columnCount = resultSet.getMetaData().getColumnCount();
  86. l.trace("Mapping a result set with " + columnCount + " columns to a Map");
  87. ResultSetMetaData meta = resultSet.getMetaData();
  88. for(int i = 1; i < columnCount + 1; i++){
  89. String colName = meta.getColumnName(i).toLowerCase();
  90. int colType = meta.getColumnType(i);
  91. String v = resultSet.getString(i);
  92. if(colType == Types.DATE){
  93. v = new DateTime(resultSet.getDate(i)).toString(dateFTR);
  94. l.trace("Mapped DATE column " + colName + " with value : " + v);
  95. }else if(colType == Types.TIMESTAMP){
  96. v = new DateTime(resultSet.getTimestamp(i)).toString(dateTimeFTR);
  97. l.trace("Mapped TIMESTAMP column " + colName + " with value : " + v);
  98. }else if(colType == Types.TIME){
  99. v = new DateTime(resultSet.getTimestamp(i)).toString(timeFTR);
  100. l.trace("Mapped TIME column " + colName + " with value : " + v);
  101. }else{
  102. l.trace("Mapped " + meta.getColumnTypeName(i) + " column " + colName + " with value : " + v);
  103. }
  104. map.put(colName, v);
  105. }
  106. }catch(SQLException e){
  107. l.error("Failed to map ResultSet");
  108. l.error(e.getMessage());
  109. return null;
  110. }
  111. return map;
  112. }
  113. }