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 import javax.jdo.JDOUserException; 20 21 22 public class StringMapping extends ColumnMapping 23 { 24 public StringMapping(DatabaseAdapter dba, Class type) 25 { 26 super(dba, type); 27 28 initTypeInfo(); 29 } 30 31 public StringMapping(Column col) 32 { 33 super(col); 34 35 col.checkString(); 36 37 if (col.getLengthType() == Column.MAXIMUM_LENGTH) 38 { 39 int maxlength = dba.getTypeInfo(Types.VARCHAR).precision; 40 41 if (col.getPrecision() <= 0 || col.getPrecision() > maxlength) 42 { 43 throw new JDOUserException("String max length of " + col.getPrecision() + 44 " is outside the acceptable range [0, " + maxlength + "]"); 45 } 46 } 47 48 initTypeInfo(); 49 } 50 51 public StringMapping(ClassBaseTable table, int relativeFieldNumber) 52 { 53 this(table.newColumn(relativeFieldNumber)); 54 } 55 56 protected TypeInfo getTypeInfo() 57 { 58 TypeInfo ti; 59 60 if (col == null) 61 ti = dba.getTypeInfo(Types.VARCHAR); 62 else 63 { 64 switch (col.getLengthType()) 65 { 66 case Column.FIXED_LENGTH: 67 ti = dba.getTypeInfo(Types.CHAR); 68 break; 69 70 case Column.MAXIMUM_LENGTH: 71 ti = dba.getTypeInfo(Types.VARCHAR); 72 break; 73 74 case Column.UNLIMITED_LENGTH: 75 default: 76 ti = dba.getTypeInfo(new int[] { Types.LONGVARCHAR, Types.CLOB }); 77 break; 78 } 79 } 80 81 return ti; 82 } 83 84 public void setString(PersistenceManager pm, PreparedStatement ps, int param, String value) 85 { 86 try 87 { 88 if (value == null) 89 ps.setNull(param, typeInfo.dataType); 90 else 91 ps.setString(param, value); 92 } 93 catch (SQLException e) 94 { 95 throw dba.newDataStoreException("Can't set String parameter: value = " + value, e); 96 } 97 } 98 99 public String getString(PersistenceManager pm, ResultSet rs, int param) 100 { 101 String value; 102 103 try 104 { 105 value = rs.getString(param); 106 } 107 catch (SQLException e) 108 { 109 throw dba.newDataStoreException("Can't get String result: param = " + param, e); 110 } 111 112 return value; 113 } 114 115 public void setObject(PersistenceManager pm, PreparedStatement ps, int param, Object value) 116 { 117 try 118 { 119 if (value == null) 120 ps.setNull(param, typeInfo.dataType); 121 else 122 ps.setString(param, (String )value); 123 } 124 catch (SQLException e) 125 { 126 throw dba.newDataStoreException("Can't set String parameter: value = " + value, e); 127 } 128 } 129 130 public Object getObject(PersistenceManager pm, ResultSet rs, int param) 131 { 132 Object value; 133 134 try 135 { 136 String s = rs.getString(param); 137 value = rs.wasNull() ? null : s; 138 } 139 catch (SQLException e) 140 { 141 throw dba.newDataStoreException("Can't get String result: param = " + param, e); 142 } 143 144 return value; 145 } 146 147 public SQLExpression newSQLLiteral(QueryStatement qs, Object value) 148 { 149 return new CharacterLiteral(qs, ((String )value)); 150 } 151 152 public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String fieldName) 153 { 154 return new CharacterExpression(qs, qsc); 155 } 156 } 157 | Popular Tags |