Coverage Report - us.daveread.basicquery.SQLTypes
 
Classes in this File Line Coverage Branch Coverage Complexity
SQLTypes
100%
29/29
100%
3/3
0
 
 1  
 package us.daveread.basicquery;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 
 7  
 /**
 8  
  * <p>Title: SQL Types</p>
 9  
  * <p>Description: Associates string representation of SQL data types to the java.sql.Types value</p>
 10  
  * <p>Copyright: Copyright (c) 2004-2014, David Read</p>
 11  
  * <p>This program is free software; you can redistribute it and/or modify
 12  
  * it under the terms of the GNU General Public License as published by
 13  
  * the Free Software Foundation; either version 2 of the License, or
 14  
  * (at your option) any later version.</p>
 15  
  * <p>This program is distributed in the hope that it will be useful,
 16  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  
  * GNU General Public License for more details.</p>
 19  
  * <p>You should have received a copy of the GNU General Public License
 20  
  * along with this program; if not, write to the Free Software
 21  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
 22  
  * <p></p>
 23  
  * @author David Read
 24  
  * 
 25  
  * TODO Convert to Enumeration
 26  
  */
 27  
 
 28  
 public class SQLTypes {
 29  
   /**
 30  
    * The type name
 31  
    */
 32  
   private String typeName;
 33  
   
 34  
   /**
 35  
    * The type id
 36  
    */
 37  
   private int typeId;
 38  
   
 39  
   /**
 40  
    * The set of known types
 41  
    */
 42  
   private static SQLTypes[] knownTypes;
 43  
 
 44  
   /**
 45  
    * The collection of known SQL data types
 46  
    */
 47  
   static {
 48  
     List<SQLTypes> allTypes;
 49  1
     allTypes = new ArrayList<SQLTypes>();
 50  1
     allTypes.add(new SQLTypes("BOOLEAN", java.sql.Types.BOOLEAN));
 51  1
     allTypes.add(new SQLTypes("CHAR", java.sql.Types.CHAR));
 52  1
     allTypes.add(new SQLTypes("DATE", java.sql.Types.DATE));
 53  1
     allTypes.add(new SQLTypes("DECIMAL", java.sql.Types.DECIMAL));
 54  1
     allTypes.add(new SQLTypes("DOUBLE", java.sql.Types.DOUBLE));
 55  1
     allTypes.add(new SQLTypes("FLOAT", java.sql.Types.FLOAT));
 56  1
     allTypes.add(new SQLTypes("INTEGER", java.sql.Types.INTEGER));
 57  1
     allTypes.add(new SQLTypes("NULL", java.sql.Types.NULL));
 58  1
     allTypes.add(new SQLTypes("STRING", java.sql.Types.VARCHAR));
 59  
 
 60  1
     knownTypes = new SQLTypes[allTypes.size()];
 61  1
     knownTypes = allTypes.toArray(new SQLTypes[0]);
 62  1
   };
 63  
 
 64  
   /**
 65  
    * Create a SQLTypes instance
 66  
    *
 67  
    * @param pTypeName The string representation of the type
 68  
    * @param pTypeId The java.sql.Types value for the type
 69  
    */
 70  9
   private SQLTypes(String pTypeName, int pTypeId) {
 71  9
     typeName = pTypeName;
 72  9
     typeId = pTypeId;
 73  9
   }
 74  
 
 75  
   /**
 76  
    * Get the java.sql.Types value for the supplied type string representation
 77  
    *
 78  
    * @param pTypeName The string representation of the type
 79  
    * 
 80  
    * @return The java.sql.Types value associated with the supplied string representation
 81  
    */
 82  
   public static int getSQLTypeId(String pTypeName) {
 83  
     String typeName;
 84  
     int returnTypeId;
 85  
     int loop;
 86  
 
 87  12
     typeName = pTypeName.toUpperCase();
 88  
 
 89  
     // Default type if we don't match a defined string
 90  12
     returnTypeId = java.sql.Types.OTHER;
 91  
 
 92  12
     for (loop = 0;
 93  78
         loop < knownTypes.length && returnTypeId == java.sql.Types.OTHER;
 94  66
         ++loop) {
 95  66
       if (typeName.startsWith(knownTypes[loop].typeName)) {
 96  10
         returnTypeId = knownTypes[loop].typeId;
 97  
       }
 98  
     }
 99  
 
 100  12
     return returnTypeId;
 101  
   }
 102  
 
 103  
   /**
 104  
    * Return a String array of the known java.sql.Types that are defined
 105  
    *
 106  
    * @return The String array of known SQL data types
 107  
    */
 108  
   public static String[] getKnownTypeNames() {
 109  
     String[] knownTypeNames;
 110  
 
 111  1
     knownTypeNames = new String[knownTypes.length];
 112  
 
 113  10
     for (int loop = 0; loop < knownTypes.length; ++loop) {
 114  9
       knownTypeNames[loop] = knownTypes[loop].typeName;
 115  
     }
 116  
 
 117  1
     return knownTypeNames;
 118  
   }
 119  
 }