1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.util.L10N; 35 36 import java.io.IOException ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Types ; 40 41 44 public class BooleanType extends Type { 45 private static final L10N L = new L10N(BooleanType.class); 46 47 private static final BooleanType BOOLEAN_TYPE = new BooleanType(); 48 49 private BooleanType() 50 { 51 } 52 53 56 public static BooleanType create() 57 { 58 return BOOLEAN_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "java.lang.Boolean"; 67 } 68 69 72 public boolean isBoolean() 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.BOOLEAN, 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.BooleanType.toBoolean(" + 93 rs + ".getBoolean(" + 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.BIT);"); 108 out.println("else"); 109 out.println(" " + pstmt + ".setBoolean(" + index + "++, " + 110 value + ".booleanValue());"); 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.BIT);"); 121 } 122 123 126 public static Boolean toBoolean(boolean value, boolean wasNull) 127 { 128 if (wasNull) 129 return null; 130 else 131 return new Boolean (value); 132 } 133 134 137 public Object getObject(ResultSet rs, int index) 138 throws SQLException 139 { 140 boolean value = rs.getBoolean(index); 141 142 return rs.wasNull() ? null : (value ? Boolean.TRUE : Boolean.FALSE); 143 } 144 } 145 | Popular Tags |