1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.java.JavaWriter; 32 import com.caucho.util.L10N; 33 34 import java.io.IOException ; 35 import java.sql.ResultSet ; 36 import java.sql.SQLException ; 37 38 41 public class YesNoType extends Type { 42 private static final L10N L = new L10N(YesNoType.class); 43 44 private static final YesNoType YES_NO_TYPE = new YesNoType(); 45 46 private YesNoType() 47 { 48 } 49 50 53 public static YesNoType create() 54 { 55 return YES_NO_TYPE; 56 } 57 58 61 public String getName() 62 { 63 return "java.lang.Boolean"; 64 } 65 66 69 public int generateLoad(JavaWriter out, String rs, 70 String indexVar, int index) 71 throws IOException 72 { 73 out.print("com.caucho.amber.type.YesNoType.toBoolean(" + 74 rs + ".getString(" + indexVar + " + " + index + "), " + 75 rs + ".wasNull())"); 76 77 return index + 1; 78 } 79 80 83 public void generateSet(JavaWriter out, String pstmt, 84 String index, String value) 85 throws IOException 86 { 87 out.println("if (" + value + " == null)"); 88 out.println(" " + pstmt + ".setNull(" + index + "++, 0);"); 89 out.println("else"); 90 out.println(" " + pstmt + ".setString(" + index + "++, " + 91 value + ".booleanValue() ? \"y\" : \"n\");"); 92 } 93 94 97 public static Boolean toBoolean(String value, boolean wasNull) 98 { 99 if (wasNull) 100 return null; 101 else if ("y".equalsIgnoreCase(value)) 102 return Boolean.TRUE; 103 else if ("n".equalsIgnoreCase(value)) 104 return Boolean.FALSE; 105 else 106 return Boolean.FALSE; 107 } 108 109 112 public Object getObject(ResultSet rs, int index) 113 throws SQLException 114 { 115 String value = rs.getString(index); 116 117 if (rs.wasNull()) 118 return null; 119 else if ("y".equalsIgnoreCase(value)) 120 return Boolean.TRUE; 121 else if ("n".equalsIgnoreCase(value)) 122 return Boolean.FALSE; 123 else 124 return Boolean.FALSE; 125 } 126 } 127 | Popular Tags |