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 PrimitiveLongType extends PrimitiveType { 45 private static final L10N L = new L10N(PrimitiveLongType.class); 46 47 private static final PrimitiveLongType LONG_TYPE = new PrimitiveLongType(); 48 49 private PrimitiveLongType() 50 { 51 } 52 53 56 public static PrimitiveLongType create() 57 { 58 return LONG_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "long"; 67 } 68 69 72 public boolean isNumeric() 73 { 74 return true; 75 } 76 77 80 public Type getForeignType() 81 { 82 return LongType.create(); 83 } 84 85 88 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 89 { 90 return manager.getCreateColumnSQL(Types.BIGINT, length, precision, scale); 91 } 92 93 96 public int generateLoad(JavaWriter out, String rs, 97 String indexVar, int index) 98 throws IOException 99 { 100 out.print(rs + ".getLong(" + indexVar + " + " + index + ")"); 101 102 return index + 1; 103 } 104 105 108 public int generateLoadForeign(JavaWriter out, String rs, 109 String indexVar, int index) 110 throws IOException 111 { 112 out.print("com.caucho.amber.type.PrimitiveLongType.toForeignLong(" + 113 rs + ".getLong(" + indexVar + " + " + index + "), " + 114 rs + ".wasNull())"); 115 116 return index + 1; 117 } 118 119 122 public void generateSet(JavaWriter out, String pstmt, 123 String index, String value) 124 throws IOException 125 { 126 out.println(pstmt + ".setLong(" + index + "++, " + value + ");"); 127 } 128 129 132 public void generateSetNull(JavaWriter out, String pstmt, String index) 133 throws IOException 134 { 135 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.BIGINT);"); 136 } 137 138 141 public void generateSetVersion(JavaWriter out, 142 String pstmt, 143 String index, 144 String value) 145 throws IOException 146 { 147 out.println(pstmt + ".setLong(" + index + "++, " + value + " + 1);"); 148 } 149 150 153 public String toObject(String value) 154 { 155 return "new Long(" + value + ")"; 156 } 157 158 161 public String generateCastFromObject(String value) 162 { 163 return "((Number) " + value + ").longValue()"; 164 } 165 166 169 public Object getObject(ResultSet rs, int index) 170 throws SQLException 171 { 172 long v = rs.getLong(index); 173 174 return rs.wasNull() ? null : new Long (v); 175 } 176 177 180 public static Long toForeignLong(long value, boolean wasNull) 181 { 182 if (wasNull || value == 0) 184 return null; 185 else 186 return new Long (value); 187 } 188 189 192 public void setParameter(PreparedStatement pstmt, int index, Object value) 193 throws SQLException 194 { 195 if (value == null) { 196 pstmt.setString(index, null); 198 } 199 else if (value instanceof Number ) 200 pstmt.setString(index, value.toString()); 201 else 202 throw new IllegalArgumentException ("Invalid argument for setParameter."); 203 } 204 } 205 | Popular Tags |