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.JDOUserException; 19 20 21 public class ByteArrayMapping extends ColumnMapping 22 { 23 public ByteArrayMapping(DatabaseAdapter dba, Class type) 24 { 25 super(dba, type); 26 27 initTypeInfo(); 28 } 29 30 public ByteArrayMapping(Column col) 31 { 32 super(col); 33 34 col.checkString(); 35 36 initTypeInfo(); 37 } 38 39 public ByteArrayMapping(ClassBaseTable table, int relativeFieldNumber) 40 { 41 this(table.newColumn(relativeFieldNumber)); 42 } 43 44 protected TypeInfo getTypeInfo() 45 { 46 TypeInfo ti; 47 48 if (col == null) 49 ti = dba.getTypeInfo(new int[] { Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB }); 50 else 51 { 52 switch (col.getLengthType()) 53 { 54 case Column.FIXED_LENGTH: 55 ti = dba.getTypeInfo(new int[] { Types.BINARY, Types.LONGVARBINARY, Types.BLOB }); 56 57 if (ti.dataType != Types.BINARY) 58 col.setUnlimitedLength(); 59 60 break; 61 62 case Column.MAXIMUM_LENGTH: 63 ti = dba.getTypeInfo(new int[] { Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB }); 64 65 if (ti.dataType != Types.VARBINARY) 66 col.setUnlimitedLength(); 67 68 break; 69 70 case Column.UNLIMITED_LENGTH: 71 default: 72 ti = dba.getTypeInfo(new int[] { Types.LONGVARBINARY, Types.BLOB }); 73 break; 74 } 75 } 76 77 return ti; 78 } 79 80 public void setObject(PersistenceManager pm, PreparedStatement ps, int param, Object value) 81 { 82 try 83 { 84 if (value == null) 85 ps.setNull(param, typeInfo.dataType); 86 else 87 ps.setBytes(param, (byte[])value); 88 } 89 catch (SQLException e) 90 { 91 throw dba.newDataStoreException("Can't set byte[] parameter: value = " + value, e); 92 } 93 } 94 95 public Object getObject(PersistenceManager pm, ResultSet rs, int param) 96 { 97 Object value; 98 99 try 100 { 101 byte[] ba = rs.getBytes(param); 102 value = rs.wasNull() ? null : ba; 103 } 104 catch (SQLException e) 105 { 106 throw dba.newDataStoreException("Can't get byte[] result: param = " + param, e); 107 } 108 109 return value; 110 } 111 112 public SQLExpression newSQLLiteral(QueryStatement qs, Object value) 113 { 114 return new BinaryLiteral(qs, ((byte[])value)); 115 } 116 117 public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String fieldName) 118 { 119 return new BinaryExpression(qs, qsc); 120 } 121 } 122 | Popular Tags |