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 LongType extends Type { 45 private static final L10N L = new L10N(LongType.class); 46 47 private static final LongType LONG_TYPE = new LongType(); 48 49 private LongType() 50 { 51 } 52 53 56 public static LongType create() 57 { 58 return LONG_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "java.lang.Long"; 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.BIGINT, 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.LongType.toLong(" + 93 rs + ".getLong(" + 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.BIGINT);"); 108 out.println("else"); 109 out.println(" " + pstmt + ".setLong(" + index + "++, " + 110 value + ".longValue());"); 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.BIGINT);"); 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 + ".setLong(" + index + "++, 1);"); 134 out.println("else"); 135 out.println(" " + pstmt + ".setLong(" + index + "++, " + 136 value + ".longValue() + 1);"); 137 } 138 139 142 public String generateIncrementVersion(String value) 143 throws IOException 144 { 145 return value + ".longValue() + 1"; 146 } 147 148 151 public static Long toLong(long value, boolean wasNull) 152 { 153 if (wasNull) 154 return null; 155 else 156 return new Long (value); 157 } 158 159 162 public void setParameter(PreparedStatement pstmt, int index, Object value) 163 throws SQLException 164 { 165 if (value == null) 166 pstmt.setNull(index, 0); 167 else 168 pstmt.setLong(index, ((Long ) value).longValue()); 169 } 170 171 174 public Object getObject(ResultSet rs, int index) 175 throws SQLException 176 { 177 long value = rs.getLong(index); 178 179 return rs.wasNull() ? null : new Long (value); 180 } 181 } 182 | Popular Tags |