1 45 46 package org.exolab.jms.tools.db; 47 48 import org.exolab.jms.persistence.PersistenceException; 49 50 51 58 public class Type { 59 60 63 private final Descriptor _descriptor; 64 65 68 private final String _name; 69 70 73 private final long _precision; 74 75 78 private final boolean _parameters; 79 80 89 public Type(int type, long precision, boolean parameters) { 90 _descriptor = Descriptor.getDescriptor(type); 91 if (_descriptor == null) { 92 throw new IllegalArgumentException ("Type id=" + type + 93 " is not a valid type"); 94 } 95 _name = _descriptor.getName(); 96 _precision = precision; 97 _parameters = parameters; 98 } 99 100 110 public Type(int type, String name, long precision, boolean parameters) { 111 _descriptor = Descriptor.getDescriptor(type); 112 if (_descriptor == null) { 113 throw new IllegalArgumentException ("Type id=" + type + 114 " is not a valid type"); 115 } 116 _name = name; 117 _precision = precision; 118 _parameters = parameters; 119 } 120 121 127 public int getType() { 128 return _descriptor.getType(); 129 } 130 131 136 public String getName() { 137 return _name; 138 } 139 140 145 public long getPrecision() { 146 return _precision; 147 } 148 149 154 public boolean getParameters() { 155 return _parameters; 156 } 157 158 163 public String getSymbolicType() { 164 String result = _descriptor.getName(); 165 if (_parameters && _precision > 0) { 166 result += "(" + _precision + ")"; 167 } 168 return result; 169 } 170 171 176 public String getSQL() { 177 String result = _name; 178 if (_parameters && _precision > 0) { 179 result += "(" + _precision + ")"; 180 } 181 return result; 182 } 183 184 187 public String toString() { 188 return "type=" + _descriptor.getName() + ", name=" + _name + 189 ", precision=" + _precision + ", parameters=" + 190 _parameters; 191 } 192 193 200 public static Type getType(String type) throws PersistenceException { 201 int start = type.indexOf('('); 202 String name = type; 203 long precision = -1; 204 boolean parameters = false; 205 if (start != -1) { 206 name = type.substring(0, start); 207 int end = type.indexOf(')', start); 208 if (end == -1) { 209 throw new PersistenceException("Illegal type: " + type); 210 } 211 precision = Long.parseLong(type.substring(start + 1, end)); 212 parameters = true; 213 } 214 215 Descriptor descriptor = Descriptor.getDescriptor(name.trim()); 216 if (descriptor == null) { 217 throw new PersistenceException("Type name=" + type + 218 " is not a valid type"); 219 } 220 return new Type(descriptor.getType(), precision, parameters); 221 } 222 223 } | Popular Tags |