1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.java.JavaWriter; 32 import com.caucho.util.L10N; 33 34 import java.io.IOException ; 35 import java.sql.PreparedStatement ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 39 42 public class ByteArrayType extends ArrayType { 43 private static final L10N L = new L10N(ByteArrayType.class); 44 45 private ByteArrayType() 46 { 47 } 48 49 52 public static ByteArrayType create() 53 { 54 return new ByteArrayType(); 55 } 56 57 60 public String getName() 61 { 62 return "java.lang.Byte[]"; 63 } 64 65 68 public String getJavaTypeName() 69 { 70 return "java.lang.Byte[]"; 71 } 72 73 76 public String getJavaObjectTypeName() 77 { 78 return "java.lang.Byte"; 79 } 80 81 85 public String getPrimitiveArrayTypeName() 86 { 87 return "byte[]"; 88 } 89 90 93 public int generateLoad(JavaWriter out, String rs, 94 String indexVar, int index) 95 throws IOException 96 { 97 out.print(rs + ".getBytes(" + indexVar + " + " + index + ")"); 98 99 return index + 1; 100 } 101 102 105 public void generateSet(JavaWriter out, String pstmt, 106 String index, String value) 107 throws IOException 108 { 109 out.println("if (" + value + " == null)"); 110 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.LONGVARBINARY);"); 111 out.println("else {"); 112 out.println(" java.lang.Byte[] super_temp_" + index + " = " + value + ";"); 113 out.println(" byte[] temp_" + index + " = new byte[super_temp_" + index + ".length];"); 114 out.println(" for (int i=0; i < temp_" + index + ".length; i++)"); 115 out.println(" temp_" + index + "[i] = super_temp_" + index + "[i].byteValue();"); 116 out.println(" " + pstmt + ".setBytes(" + index + "++, temp_" + index + ");"); 117 out.println("}"); 118 } 119 120 123 public void setParameter(PreparedStatement pstmt, int index, Object value) 124 throws SQLException 125 { 126 Byte [] wrapperByte = (Byte []) value; 127 128 byte[] primitiveByte = new byte[wrapperByte.length]; 129 for (int i=0; i<wrapperByte.length; i++) 130 primitiveByte[i] = wrapperByte[i].byteValue(); 131 132 pstmt.setBytes(index, primitiveByte); 133 } 134 135 138 public Object getObject(ResultSet rs, int index) 139 throws SQLException 140 { 141 byte[] primitiveByte = rs.getBytes(index); 142 143 if (rs.wasNull()) 144 return null; 145 146 Byte [] wrapperByte = new Byte [primitiveByte.length]; 147 for (int i=0; i < primitiveByte.length; i++) 148 wrapperByte[i] = new Byte (primitiveByte[i]); 149 150 return wrapperByte; 151 } 152 } 153 | Popular Tags |