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 PrimitiveCharType extends PrimitiveType { 45 private static final L10N L = new L10N(PrimitiveCharType.class); 46 47 private static final PrimitiveCharType CHAR_TYPE = new PrimitiveCharType(); 48 49 private PrimitiveCharType() 50 { 51 } 52 53 56 public static PrimitiveCharType create() 57 { 58 return CHAR_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "char"; 67 } 68 69 72 public Type getForeignType() 73 { 74 return CharacterType.create(); 75 } 76 77 80 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 81 { 82 return manager.getCreateColumnSQL(Types.CHAR, 0, 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.PrimitiveCharType.toChar(" + rs + ".getString(" + indexVar + " + " + index + "))"); 93 94 return index + 1; 95 } 96 97 100 public void generateSet(JavaWriter out, String pstmt, 101 String index, String value) 102 throws IOException 103 { 104 out.println(pstmt + ".setString(" + index + "++, String.valueOf(" + value + "));"); 105 } 106 107 110 public void generateSetNull(JavaWriter out, String pstmt, String index) 111 throws IOException 112 { 113 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.CHAR);"); 114 } 115 116 119 public String toObject(String value) 120 { 121 return "new Character(" + value + ")"; 122 } 123 124 127 public String generateCastFromObject(String value) 128 { 129 return "((Character) " + value + ").charValue()"; 130 } 131 132 135 public Object getObject(ResultSet rs, int index) 136 throws SQLException 137 { 138 String v = rs.getString(index); 139 140 return rs.wasNull() ? null : new Character (v.charAt(0)); 141 } 142 143 146 public static char toChar(String value) 147 { 148 if (value == null || value.length() == 0) 149 return 0; 150 else 151 return value.charAt(0); 152 } 153 154 157 public void setParameter(PreparedStatement pstmt, int index, Object value) 158 throws SQLException 159 { 160 if ((value instanceof Character ) || (value instanceof String )) 161 pstmt.setString(index, value.toString()); 162 else 163 throw new IllegalArgumentException ("Invalid argument for setParameter."); 164 } 165 } 166 | Popular Tags |