Coverage Report - com.jcabi.jdbc.Outcome
 
Classes in this File Line Coverage Branch Coverage Complexity
Outcome
N/A
N/A
1.4
Outcome$1
66%
2/3
N/A
1.4
Outcome$1$AjcClosure1
0%
0/1
N/A
1.4
Outcome$2
100%
3/3
N/A
1.4
Outcome$2$AjcClosure1
100%
1/1
N/A
1.4
Outcome$3
66%
2/3
N/A
1.4
Outcome$3$AjcClosure1
0%
0/1
N/A
1.4
Outcome$4
80%
4/5
50%
1/2
1.4
Outcome$4$AjcClosure1
100%
1/1
N/A
1.4
 
 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.Loggable;
 33  
 import java.sql.ResultSet;
 34  
 import java.sql.SQLException;
 35  
 import java.sql.Statement;
 36  
 
 37  
 /**
 38  
  * Outcome of ResultSet.
 39  
  *
 40  
  * <p>The following convenience implementations are provided:
 41  
  *
 42  
  * <ul>
 43  
  *  <li>{@link Outcome#NOT_EMPTY} to check that at least one result row is
 44  
  *      returned.
 45  
  *  <li>{@link Outcome#VOID} for when you wish to disregard the result.
 46  
  *  <li>{@link Outcome#UPDATE_COUNT} to check the number of updated rows.
 47  
  * </ul>
 48  
  *
 49  
  * @param <T> Type of expected result
 50  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 51  
  * @version $Id: efc0ec3f8aed56818f108bac1743cf5a49511ba5 $
 52  
  * @since 0.12
 53  
  */
 54  
 public interface Outcome<T> {
 55  
 
 56  
     /**
 57  
      * Returns {@code TRUE} if at least one SQL record found in
 58  
      * {@link ResultSet}.
 59  
      *
 60  
      * <p>The outcome returns the value of {@link ResultSet#next()} and throws
 61  
      * {@link SQLException} in case of a problem.
 62  
      *
 63  
      * @since 0.12
 64  
      */
 65  1
     Outcome<Boolean> NOT_EMPTY = new Outcome<Boolean>() {
 66  
         @Override
 67  
         @Loggable(Loggable.DEBUG)
 68  
         public Boolean handle(final ResultSet rset, final Statement stmt)
 69  
             throws SQLException {
 70  0
             return rset.next();
 71  
         }
 72  
     };
 73  
 
 74  
     /**
 75  
      * Outcome that does nothing (and always returns {@code null}).
 76  
      *
 77  
      * <p>Useful when you're not interested in the result:
 78  
      *
 79  
      * <pre> new JdbcSession(source)
 80  
      *   .sql("INSERT INTO foo (name) VALUES (?)")
 81  
      *   .set("Jeff Lebowski")
 82  
      *   .insert(Outcome.VOID);</pre>
 83  
      *
 84  
      * @since 0.12
 85  
      */
 86  34
     Outcome<Void> VOID = new Outcome<Void>() {
 87  
         @Override
 88  
         @Loggable(Loggable.DEBUG)
 89  
         public Void handle(final ResultSet rset, final Statement stmt) {
 90  65
             return null;
 91  
         }
 92  
     };
 93  
 
 94  
     /**
 95  
      * Outcome that returns the number of updated rows.
 96  
      *
 97  
      * <p>Use it when you need to determine the number of rows updated:
 98  
      *
 99  
      * <pre> Integer count = new JdbcSession(source)
 100  
      *   .sql("UPDATE employee SET salary = 35000 WHERE department = ?")
 101  
      *   .set("Finance")
 102  
      *   .update(Outcome.UPDATE_COUNT);</pre>
 103  
      *
 104  
      * @since 0.12
 105  
      */
 106  1
     Outcome<Integer> UPDATE_COUNT = new Outcome<Integer>() {
 107  
         @Override
 108  
         @Loggable(Loggable.DEBUG)
 109  
         public Integer handle(final ResultSet rset, final Statement stmt)
 110  
             throws SQLException {
 111  0
             return stmt.getUpdateCount();
 112  
         }
 113  
     };
 114  
 
 115  
     /**
 116  
      * Outcome that returns last insert ID.
 117  
      *
 118  
      * <p>Use it when you need to get last insert ID from INSERT:
 119  
      *
 120  
      * <pre> long id = new JdbcSession(source)
 121  
      *   .sql("INSERT INTO employee (name) VALUES (?)")
 122  
      *   .set("Jeffrey")
 123  
      *   .insert(Outcome.LAST_INSERT_ID);</pre>
 124  
      *
 125  
      * @since 0.13
 126  
      */
 127  2
     Outcome<Long> LAST_INSERT_ID = new Outcome<Long>() {
 128  
         @Override
 129  
         @Loggable(Loggable.DEBUG)
 130  
         public Long handle(final ResultSet rset, final Statement stmt)
 131  
             throws SQLException {
 132  2
             if (!rset.next()) {
 133  0
                 throw new SQLException("no last_insert_id() available");
 134  
             }
 135  1
             return rset.getLong(1);
 136  
         }
 137  
     };
 138  
 
 139  
     /**
 140  
      * Process the result set and return some value.
 141  
      * @param rset The result set to process
 142  
      * @param stmt The statement used in the run
 143  
      * @return The result
 144  
      * @throws SQLException If something goes wrong inside
 145  
      */
 146  
     T handle(ResultSet rset, Statement stmt) throws SQLException;
 147  
 
 148  
 }