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.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Types ; 40 41 44 public class ShortType extends Type { 45 private static final L10N L = new L10N(ShortType.class); 46 47 private static final ShortType SHORT_TYPE = new ShortType(); 48 49 private ShortType() 50 { 51 } 52 53 56 public static ShortType create() 57 { 58 return SHORT_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "java.lang.Short"; 67 } 68 69 72 public boolean isNumeric() 73 { 74 return true; 75 } 76 77 80 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 81 { 82 return manager.getCreateColumnSQL(Types.SMALLINT, length, precision, scale); 83 } 84 85 88 public int generateLoad(JavaWriter out, String rs, 89 String indexVar, int index) 90 throws IOException 91 { 92 out.print("com.caucho.amber.type.ShortType.toShort(" + 93 rs + ".getShort(" + indexVar + " + " + index + "), " + 94 rs + ".wasNull())"); 95 96 return index + 1; 97 } 98 99 102 public void generateSet(JavaWriter out, String pstmt, 103 String index, String value) 104 throws IOException 105 { 106 out.println("if (" + value + " == null)"); 107 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.SMALLINT);"); 108 out.println("else"); 109 out.println(" " + pstmt + ".setShort(" + index + "++, " + 110 value + ".shortValue());"); 111 } 112 113 116 public void generateSetNull(JavaWriter out, String pstmt, 117 String index) 118 throws IOException 119 { 120 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.SMALLINT);"); 121 } 122 123 126 public void generateSetVersion(JavaWriter out, 127 String pstmt, 128 String index, 129 String value) 130 throws IOException 131 { 132 out.println("if (" + value + " == null)"); 133 out.println(" " + pstmt + ".setShort(" + index + "++, 1);"); 134 out.println("else"); 135 out.println(" " + pstmt + ".setShort(" + index + "++, " + 136 value + ".shortValue() + 1);"); 137 } 138 139 142 public String generateIncrementVersion(String value) 143 throws IOException 144 { 145 return value + ".shortValue() + 1"; 146 } 147 148 151 public Object toObject(long value) 152 { 153 return new Short ((short) value); 154 } 155 156 159 public static Short toShort(int value, boolean wasNull) 160 { 161 if (wasNull) 162 return null; 163 else 164 return new Short ((short) value); 165 } 166 167 170 public void setParameter(PreparedStatement pstmt, int index, Object value) 171 throws SQLException 172 { 173 if (value == null) 174 pstmt.setNull(index, 0); 175 else 176 pstmt.setShort(index, ((Number ) value).shortValue()); 177 } 178 179 182 public Object getObject(ResultSet rs, int index) 183 throws SQLException 184 { 185 short value = rs.getShort(index); 186 187 return rs.wasNull() ? null : new Short (value); 188 } 189 } 190 | Popular Tags |