Coverage Report - us.daveread.basicquery.queries.Query
 
Classes in this File Line Coverage Branch Coverage Complexity
Query
100%
33/33
100%
5/5
0
 
 1  
 package us.daveread.basicquery.queries;
 2  
 
 3  
 /**
 4  
  * Title: Query
 5  
  * <p>
 6  
  * Description: Represents an individual SQL statement. For BasicQuery this is
 7  
  * both the SQL statement and the mode (select or update) that is used when
 8  
  * executing the statement.
 9  
  * </p>
 10  
  * <p>
 11  
  * Copyright: Copyright (c) 2004-2014, David Read
 12  
  * </p>
 13  
  * <p>
 14  
  * This program is free software; you can redistribute it and/or modify it under
 15  
  * the terms of the GNU General Public License as published by the Free Software
 16  
  * Foundation; either version 2 of the License, or (at your option) any later
 17  
  * version.
 18  
  * </p>
 19  
  * <p>
 20  
  * This program is distributed in the hope that it will be useful, but WITHOUT
 21  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 22  
  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 23  
  * details.
 24  
  * </p>
 25  
  * <p>
 26  
  * You should have received a copy of the GNU General Public License along with
 27  
  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 28  
  * Place, Suite 330, Boston, MA 02111-1307 USA
 29  
  * </p>
 30  
  * <p>
 31  
  * </p>
 32  
  * 
 33  
  * @author David Read
 34  
  */
 35  
 
 36  
 public class Query {
 37  
   /**
 38  
    * Represents a select-type statement (e.g. produces a result set)
 39  
    */
 40  
   public static final int MODE_QUERY = 0;
 41  
 
 42  
   /**
 43  
    * Represents and update-type statement (e.g. changes the data with no result
 44  
    * set)
 45  
    */
 46  
   public static final int MODE_UPDATE = 1;
 47  
 
 48  
   /**
 49  
    * Represents a select-type statement (e.g. produces a result set) but reports
 50  
    * the metadata from the result set instead of the data
 51  
    */
 52  
   public static final int MODE_DESCRIBE = 2;
 53  
 
 54  
   /**
 55  
    * The SQL statement
 56  
    */
 57  
   private String sqlStatement;
 58  
 
 59  
   /**
 60  
    * The mode for this statement
 61  
    */
 62  
   private int mode;
 63  
 
 64  
   /**
 65  
    * The raw statement (e.g. as entered by the user - potentially with carriage
 66  
    * returns embedded)
 67  
    */
 68  
   private String rawSqlStatement;
 69  
 
 70  
   /**
 71  
    * Constructs a Query
 72  
    * 
 73  
    * Assumes the mode is a select statement (returns result set)
 74  
    * 
 75  
    * @param aRawSql
 76  
    *          The String to be queried
 77  
    */
 78  7
   public Query(String aRawSql) {
 79  7
     setSql(aRawSql);
 80  7
     mode = MODE_QUERY;
 81  7
   }
 82  
 
 83  
   /**
 84  
    * Constructs a Query to perform the operation specified by the mode
 85  
    * 
 86  
    * @param aRawSql
 87  
    *          The SQL statement
 88  
    * @param aMode
 89  
    *          The mode value
 90  
    */
 91  28
   public Query(String aRawSql, int aMode) {
 92  28
     setSql(aRawSql);
 93  28
     mode = aMode;
 94  28
   }
 95  
 
 96  
   /**
 97  
    * Set the mode value to aMode depending on whether the user
 98  
    * wants to update,describe or query
 99  
    * 
 100  
    * @param aMode
 101  
    *          The mode value that is set
 102  
    */
 103  
   public void setMode(int aMode) {
 104  4
     mode = aMode;
 105  4
   }
 106  
 
 107  
   /**
 108  
    * Set the SQL statement for this query to wrap
 109  
    * 
 110  
    * @param aRawSql The SQL statement
 111  
    */
 112  
   private void setSql(String aRawSql) {
 113  35
     rawSqlStatement = aRawSql.trim();
 114  35
     sqlStatement = rawSqlStatement.replace('\n', ' ');
 115  35
   }
 116  
 
 117  
   /**
 118  
    * Returns the SQL string
 119  
    * 
 120  
    * @return String
 121  
    */
 122  
   public String getSql() {
 123  8
     return sqlStatement;
 124  
   }
 125  
   
 126  
   /**
 127  
    * Is the statement commented out
 128  
    * 
 129  
    * @return True if the statement is commented out
 130  
    */
 131  
   public boolean isCommented() {
 132  18
     return sqlStatement.startsWith("//");
 133  
   }
 134  
 
 135  
   /**
 136  
    * The Raw SQL statement as entered, potentially contains carriage returns
 137  
    * 
 138  
    * @return The raw SQL statement
 139  
    */
 140  
   public String getRawSql() {
 141  23
     return rawSqlStatement;
 142  
   }
 143  
 
 144  
   @Override
 145  
   public boolean equals(Object object) {
 146  2
     if (object instanceof Query) {
 147  1
       return equals((Query) object);
 148  
     } 
 149  
     
 150  1
     return false;
 151  
   }
 152  
 
 153  
   /**
 154  
    * Check whether this Query instance is equavalent to another. Equivalence is
 155  
    * true if the SQL statements are the same
 156  
    * 
 157  
    * @param query A query to compare to this query
 158  
    * 
 159  
    * @return True if the queries are equivalent
 160  
    */
 161  
   public boolean equals(Query query) {
 162  
     String thisQuery, otherQuery;
 163  
 
 164  6
     if (query == null) {
 165  1
       return false;
 166  
     }
 167  
 
 168  5
     thisQuery = getRawSql();
 169  
     
 170  5
     if (isCommented()) {
 171  1
       thisQuery = thisQuery.substring(2).trim();
 172  
     }
 173  
 
 174  5
     otherQuery = query.getRawSql();
 175  
     
 176  5
     if (query.isCommented()) {
 177  1
       otherQuery = otherQuery.substring(2).trim();
 178  
     }
 179  
 
 180  5
     return thisQuery.equals(otherQuery);
 181  
   }
 182  
   
 183  
   @Override
 184  
   public int hashCode() {
 185  6
     if (isCommented()) {
 186  1
       return getRawSql().substring(2).trim().hashCode();
 187  
     } else {
 188  5
       return getRawSql().hashCode();
 189  
     }
 190  
   }
 191  
 
 192  
   /**
 193  
    * Returns the Query Mode
 194  
    * 
 195  
    * @return The query mode
 196  
    */
 197  
   public int getMode() {
 198  15
     return mode;
 199  
   }
 200  
 
 201  
   /**
 202  
    * Returns the SQL statement as a string
 203  
    * 
 204  
    * @return The SQL statement
 205  
    */
 206  
   public String toString() {
 207  4
     return getSql();
 208  
   }
 209  
 }