1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import java.sql.PreparedStatement ; 15 import java.sql.ResultSet ; 16 import java.sql.SQLException ; 17 import java.sql.Types ; 18 import javax.jdo.JDODataStoreException; 19 20 21 public class BooleanMapping extends ColumnMapping 22 { 23 private boolean bitIsReallyBoolean; 24 25 public BooleanMapping(DatabaseAdapter dba, Class type) 26 { 27 super(dba, type); 28 29 initTypeInfo(); 30 } 31 32 public BooleanMapping(Column col) 33 { 34 super(col); 35 36 col.checkPrimitive(); 37 38 initTypeInfo(); 39 } 40 41 public BooleanMapping(ClassBaseTable table, int relativeFieldNumber) 42 { 43 this(table.newColumn(relativeFieldNumber)); 44 } 45 46 protected TypeInfo getTypeInfo() 47 { 48 return dba.getTypeInfo(new int[] { Types.BIT, Types.CHAR }); 49 } 50 51 protected void initTypeInfo() 52 { 53 super.initTypeInfo(); 54 55 if (col != null && typeInfo.dataType == Types.CHAR) 56 { 57 col.setFixedLength(1); 58 col.checkString(); 59 60 StringBuffer constraints = new StringBuffer ("CHECK (" + col.getName() + " IN ('Y','N')"); 61 62 if (col.isNullable()) 63 constraints.append(" OR " + col.getName() + " IS NULL"); 64 65 constraints.append(')'); 66 67 col.setConstraints(constraints.toString()); 68 } 69 else if (typeInfo.dataType == Types.BIT && typeInfo.typeName.toLowerCase().startsWith("bool")) 70 bitIsReallyBoolean = true; 71 } 72 73 public void setBoolean(PersistenceManager pm, PreparedStatement ps, int param, boolean value) 74 { 75 try 76 { 77 if (typeInfo.dataType == Types.CHAR) 78 ps.setString(param, value ? "Y" : "N"); 79 else 80 ps.setBoolean(param, value); 81 } 82 catch (SQLException e) 83 { 84 throw dba.newDataStoreException("Can't set boolean parameter: value = " + value, e); 85 } 86 } 87 88 public boolean getBoolean(PersistenceManager pm, ResultSet rs, int param) 89 { 90 boolean value; 91 92 try 93 { 94 if (typeInfo.dataType == Types.CHAR) 95 { 96 String s = rs.getString(param); 97 98 if (s == null) 99 throw new NullValueException("Illegal null value in column " + col); 100 101 if (s.equals("Y")) 102 value = true; 103 else if (s.equals("N")) 104 value = false; 105 else 106 throw new JDODataStoreException("Illegal value \"" + s + "\" in column " + col); 107 } 108 else 109 { 110 value = rs.getBoolean(param); 111 112 if (rs.wasNull()) 113 throw new NullValueException("Illegal null value in column " + col); 114 } 115 } 116 catch (SQLException e) 117 { 118 throw dba.newDataStoreException("Can't get boolean result: param = " + param, e); 119 } 120 121 return value; 122 } 123 124 public void setObject(PersistenceManager pm, PreparedStatement ps, int param, Object value) 125 { 126 try 127 { 128 if (value == null) 129 ps.setNull(param, typeInfo.dataType); 130 else if (typeInfo.dataType == Types.CHAR) 131 ps.setString(param, ((Boolean )value).booleanValue() ? "Y" : "N"); 132 else 133 ps.setBoolean(param, ((Boolean )value).booleanValue()); 134 } 135 catch (SQLException e) 136 { 137 throw dba.newDataStoreException("Can't set Boolean parameter: value = " + value, e); 138 } 139 } 140 141 public Object getObject(PersistenceManager pm, ResultSet rs, int param) 142 { 143 Object value; 144 145 try 146 { 147 if (typeInfo.dataType == Types.CHAR) 148 { 149 String s = rs.getString(param); 150 151 if (s == null) 152 value = null; 153 else if (s.equals("Y")) 154 value = Boolean.TRUE; 155 else if (s.equals("N")) 156 value = Boolean.FALSE; 157 else 158 throw new JDODataStoreException("Illegal value \"" + s + "\" in column " + col); 159 } 160 else 161 { 162 boolean b = rs.getBoolean(param); 163 value = rs.wasNull() ? null : new Boolean (b); 164 } 165 } 166 catch (SQLException e) 167 { 168 throw dba.newDataStoreException("Can't get Boolean result: param = " + param, e); 169 } 170 171 return value; 172 } 173 174 public SQLExpression newSQLLiteral(QueryStatement qs, Object value) 175 { 176 switch (typeInfo.dataType) 177 { 178 case Types.BIT: 179 if (bitIsReallyBoolean) 180 return new BooleanLiteral(qs, ((Boolean )value).booleanValue()); 181 else 182 return new BooleanBitColumnLiteral(qs, ((Boolean )value).booleanValue()); 183 case Types.CHAR: 184 return new BooleanCharColumnLiteral(qs, ((Boolean )value).booleanValue()); 185 default: 187 return new BooleanLiteral(qs, ((Boolean )value).booleanValue()); 188 } 189 } 190 191 public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String fieldName) 192 { 193 switch (typeInfo.dataType) 194 { 195 case Types.BIT: 196 if (bitIsReallyBoolean) 197 return new BooleanExpression(qs, qsc); 198 else 199 return new BooleanBitColumnExpression(qs, qsc); 200 case Types.CHAR: 201 return new BooleanCharColumnExpression(qs, qsc); 202 default: 204 return new BooleanExpression(qs, qsc); 205 } 206 } 207 } 208 | Popular Tags |