1 23 24 package org.xquark.jdbc.typing; 25 26 import java.math.BigDecimal ; 27 import java.sql.*; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 import java.sql.Types ; 31 32 import org.xquark.schema.SchemaException; 33 import org.xquark.schema.SimpleType; 34 import org.xquark.schema.datatypes.*; 35 import org.xquark.schema.datatypes.PrimitiveType; 36 import org.xquark.schema.datatypes.QName; 37 import org.xquark.schema.datatypes.URI; 38 39 48 public class MappingInfo extends JavaTypeInfo implements DBMSConstants 49 { 50 private static final String RCSRevision = "$Revision: 1.1 $"; 51 private static final String RCSName = "$Name: $"; 52 53 54 public static int ABSTRACT_SCHEMA_JDBC_MAPPING[] = new int[33]; 55 56 static 57 { 58 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.STRING] = Types.VARCHAR; 60 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.BOOLEAN] = Types.DECIMAL; 61 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DECIMAL] = Types.DECIMAL; 62 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.FLOAT] = Types.FLOAT; 63 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DOUBLE] = Types.DOUBLE; 64 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.QNAME] = Types.VARCHAR; 65 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.NOTATION] = Types.VARCHAR; 66 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DURATION] = Types.VARCHAR; 67 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DATE_TIME] = Types.TIMESTAMP; 68 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.TIME] = Types.TIMESTAMP; 69 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.DATE] = Types.TIMESTAMP; 70 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GYEAR_MONTH] = Types.TIMESTAMP; 71 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GYEAR] = Types.TIMESTAMP; 72 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GMONTH_DAY] = Types.TIMESTAMP; 73 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GDAY] = Types.TIMESTAMP; 74 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.GMONTH] = Types.TIMESTAMP; 75 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.HEX_BINARY] = Types.VARBINARY; 76 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.BASE64_BINARY] = Types.VARBINARY; 77 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.ANY_URI] = Types.VARCHAR; 78 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.UNION] = Types.VARCHAR; ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.LIST] = Types.VARCHAR; 80 ABSTRACT_SCHEMA_JDBC_MAPPING[PrimitiveType.ANYSIMPLETYPE] = Types.VARCHAR; 81 } 82 83 84 protected boolean isJDBCStringIOAllowed = true; 86 87 protected boolean useStreams = false; 89 93 protected static final short NONE = 0; 94 95 protected static final short IMPLICIT = 1; 96 100 protected static final short AUTOMATIC = 2; 101 102 103 protected static final short EXPLICIT = 3; 104 105 106 protected static final short IMPOSSIBLE = 4; 107 108 111 protected short conversion = IMPOSSIBLE; 113 protected DbType dbType = null; 114 protected boolean targetIsString; 115 116 protected boolean useStringDelimitor = false; 117 118 protected MappingInfo() {} 122 123 125 public MappingInfo(String XMLType) 126 { 127 super(XMLType); 128 } 129 130 131 public MappingInfo(SimpleType XMLType) 132 { 133 super(XMLType); 134 } 135 136 public MappingInfo(SimpleType XMLType, DbType cmeta) 137 { 138 super(XMLType); 139 setDBType(cmeta); 140 } 141 142 protected void setDBType(DbType cmeta) { 143 this.dbType = cmeta; 145 if (cmeta.isLongType()) 146 useStreams = true; 147 148 int targetJDBCType = cmeta.getJDBCType(); 149 targetIsString = (targetJDBCType == Types.CHAR) 150 || (targetJDBCType == Types.VARCHAR) 151 || (targetJDBCType == Types.LONGVARCHAR); 152 153 154 isJDBCStringIOAllowed = targetIsString || (javaType == JAVA_STRING); 156 } 157 158 public DbType getDBType() 159 { 160 return dbType; 161 } 162 163 169 public boolean useJDBCStringMethods() 170 { 171 return isJDBCStringIOAllowed; 172 } 173 174 178 public boolean storeCanonicalString() 179 { 180 return isJDBCStringIOAllowed && (javaType != JAVA_STRING); 181 } 182 183 public void setParameter(PreparedStatement pStmt, int i, String value) 184 throws SQLException , SchemaException 185 { 186 Object oValue = sType.convert(value); 187 switch(javaType) { 188 case JAVA_STRING : 189 pStmt.setString(i, (String )oValue); 190 break; 191 case SCHEMA_QNAME : 192 pStmt.setString(i, ((QName)oValue).toString()); 193 break; 194 case SCHEMA_URI : 195 pStmt.setString(i, ((URI)oValue).toString()); 196 break; 197 case SCHEMA_DATETIME : 198 pStmt.setTimestamp(i, new Timestamp(((DateTime)oValue).getTime())); break; 200 case SCHEMA_DURATION : 201 pStmt.setString(i, ((Duration)oValue).toString()); 202 break; 203 case JAVA_LONG : 204 pStmt.setLong(i, ((Long )oValue).longValue()); 205 break; 206 case JAVA_BIG_DECIMAL : 207 if (oValue instanceof Long ) 208 pStmt.setLong(i, ((Long )oValue).longValue()); 209 else 210 pStmt.setBigDecimal(i, (BigDecimal )oValue); 211 break; 212 case JAVA_FLOAT : 213 pStmt.setFloat(i, ((Float )oValue).floatValue()); 214 break; 215 case JAVA_DOUBLE : 216 pStmt.setDouble(i, ((Double )oValue).doubleValue()); 217 break; 218 case JAVA_BOOLEAN : 219 pStmt.setBoolean(i, ((Boolean )oValue).booleanValue()); 220 break; 221 case SCHEMA_BYTE_ARRAY : 222 pStmt.setBytes(i, ((ByteArray) oValue).getData()); 223 break; 224 default: 225 } 226 } 227 228 public String toString() 229 { 230 return "[javaType=" + javaType + ",string=" + isJDBCStringIOAllowed + ",targetJDBC=" + dbType.getJDBCType() + "]"; 231 } 232 } 233 | Popular Tags |