1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.amber.manager.AmberPersistenceUnit; 32 import com.caucho.java.JavaWriter; 33 import com.caucho.util.L10N; 34 35 import java.io.IOException ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 import java.sql.Types ; 39 40 43 public class ByteType extends Type { 44 private static final L10N L = new L10N(ByteType.class); 45 46 private static final ByteType BYTE_TYPE = new ByteType(); 47 48 private ByteType() 49 { 50 } 51 52 55 public static ByteType create() 56 { 57 return BYTE_TYPE; 58 } 59 60 63 public String getName() 64 { 65 return "java.lang.Byte"; 66 } 67 68 71 public boolean isNumeric() 72 { 73 return true; 74 } 75 76 79 public String generateCreateColumnSQL(AmberPersistenceUnit manager, 80 int length, 81 int precision, 82 int scale) 83 { 84 return manager.getCreateColumnSQL(Types.TINYINT, length, precision, scale); 85 } 86 87 90 public int generateLoad(JavaWriter out, String rs, 91 String indexVar, int index) 92 throws IOException 93 { 94 out.print("com.caucho.amber.type.ByteType.toByte(" + 95 rs + ".getByte(" + indexVar + " + " + index + "), " + 96 rs + ".wasNull())"); 97 98 return index + 1; 99 } 100 101 104 public void generateSet(JavaWriter out, String pstmt, 105 String index, String value) 106 throws IOException 107 { 108 out.println("if (" + value + " == null)"); 109 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TINYINT);"); 110 out.println("else"); 111 out.println(" " + pstmt + ".setByte(" + index + "++, " + 112 value + ".byteValue());"); 113 } 114 115 118 public void generateSetNull(JavaWriter out, String pstmt, 119 String index) 120 throws IOException 121 { 122 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.TINYINT);"); 123 } 124 125 128 public static Byte toByte(int value, boolean wasNull) 129 { 130 if (wasNull) 131 return null; 132 else 133 return new Byte ((byte) value); 134 } 135 136 139 public Object getObject(ResultSet rs, int index) 140 throws SQLException 141 { 142 int value = rs.getByte(index); 143 144 return rs.wasNull() ? null : new Byte ((byte) value); 145 } 146 } 147 | Popular Tags |