| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ColumnOutcome |
|
| 7.333333333333333;7.333 | ||||
| ColumnOutcome$AjcClosure1 |
|
| 7.333333333333333;7.333 |
| 1 | 2 | /** |
| 2 | * Copyright (c) 2012-2015, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.jdbc; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Immutable; | |
| 33 | import com.jcabi.aspects.Loggable; | |
| 34 | import java.sql.ResultSet; | |
| 35 | import java.sql.SQLException; | |
| 36 | import java.sql.Statement; | |
| 37 | import java.util.Collection; | |
| 38 | import java.util.Date; | |
| 39 | import java.util.LinkedList; | |
| 40 | import lombok.EqualsAndHashCode; | |
| 41 | import lombok.ToString; | |
| 42 | ||
| 43 | /** | |
| 44 | * Outcome that returns first column. | |
| 45 | * | |
| 46 | * <p>Use it when you need the first column: | |
| 47 | * | |
| 48 | * <pre> Collection&lgt;Long> salaries = new JdbcSession(source) | |
| 49 | * .sql("SELECT salary FROM user") | |
| 50 | * .select(new ColumnOutcome<Long>(Long.class));</pre> | |
| 51 | * | |
| 52 | * <p>Supported types are: {@link String}, {@link Long}, {@link Boolean}, | |
| 53 | * {@link Byte}, {@link Date}, and {@link Utc}. | |
| 54 | * | |
| 55 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 56 | * @version $Id: 0d63c01ecb4e7b988bcdb2a4f452462d4b6e37f0 $ | |
| 57 | * @since 0.13 | |
| 58 | * @param <T> Type of items | |
| 59 | */ | |
| 60 | 1 | @Immutable |
| 61 | 1 | @ToString |
| 62 | 0 | @EqualsAndHashCode(of = "type") |
| 63 | public final class ColumnOutcome<T> implements Outcome<Collection<T>> { | |
| 64 | ||
| 65 | /** | |
| 66 | * The type name. | |
| 67 | */ | |
| 68 | private final transient String type; | |
| 69 | ||
| 70 | /** | |
| 71 | * Public ctor. | |
| 72 | * @param tpe The type to convert to | |
| 73 | */ | |
| 74 | 1 | public ColumnOutcome(final Class<T> tpe) { |
| 75 | //@checkstyle BooleanExpressionComplexity (3 lines) | |
| 76 | 1 | if (tpe.equals(String.class) || tpe.equals(Long.class) |
| 77 | || tpe.equals(Boolean.class) || tpe.equals(Byte.class) | |
| 78 | || tpe.equals(Date.class) || tpe.equals(Utc.class) | |
| 79 | || byte[].class.equals(tpe) | |
| 80 | ) { | |
| 81 | 1 | this.type = tpe.getName(); |
| 82 | } else { | |
| 83 | 0 | throw new IllegalArgumentException( |
| 84 | String.format("type %s is not supported", tpe.getName()) | |
| 85 | ); | |
| 86 | } | |
| 87 | 1 | } |
| 88 | ||
| 89 | @Override | |
| 90 | @Loggable(Loggable.DEBUG) | |
| 91 | public Collection<T> handle(final ResultSet rset, final Statement stmt) | |
| 92 | throws SQLException { | |
| 93 | 2 | final Collection<T> result = new LinkedList<T>(); |
| 94 | 3 | while (rset.next()) { |
| 95 | 2 | result.add(this.fetch(rset)); |
| 96 | } | |
| 97 | 1 | return result; |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Fetch the value from result set. | |
| 102 | * @param rset Result set | |
| 103 | * @return The result | |
| 104 | * @throws SQLException If some error inside | |
| 105 | */ | |
| 106 | @SuppressWarnings("unchecked") | |
| 107 | private T fetch(final ResultSet rset) throws SQLException { | |
| 108 | final Object result; | |
| 109 | Class<T> tpe; | |
| 110 | try { | |
| 111 | 2 | tpe = (Class<T>) Class.forName(this.type); |
| 112 | 2 | if (tpe.equals(String.class)) { |
| 113 | 2 | result = rset.getString(1); |
| 114 | 0 | } else if (tpe.equals(Long.class)) { |
| 115 | 0 | result = rset.getLong(1); |
| 116 | 0 | } else if (tpe.equals(Boolean.class)) { |
| 117 | 0 | result = rset.getBoolean(1); |
| 118 | 0 | } else if (tpe.equals(Byte.class)) { |
| 119 | 0 | result = rset.getByte(1); |
| 120 | 0 | } else if (tpe.equals(Date.class)) { |
| 121 | 0 | result = rset.getDate(1); |
| 122 | 0 | } else if (tpe.equals(Utc.class)) { |
| 123 | 0 | result = new Utc(Utc.getTimestamp(rset, 1)); |
| 124 | 0 | } else if (byte[].class.equals(tpe)) { |
| 125 | 0 | result = rset.getBytes(1); |
| 126 | } else { | |
| 127 | 0 | throw new IllegalStateException( |
| 128 | String.format("type %s is not allowed", tpe.getName()) | |
| 129 | ); | |
| 130 | } | |
| 131 | 0 | } catch (final ClassNotFoundException ex) { |
| 132 | 0 | throw new IllegalArgumentException( |
| 133 | String.format("Unknown type: %s", this.type), ex | |
| 134 | ); | |
| 135 | 2 | } |
| 136 | 2 | return tpe.cast(result); |
| 137 | } | |
| 138 | ||
| 139 | } |