001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.sql.columns; 005 006 import java.sql.PreparedStatement; 007 import java.sql.ResultSet; 008 import java.sql.ResultSetMetaData; 009 import java.sql.SQLException; 010 011 import biz.hammurapi.config.Context; 012 import biz.hammurapi.convert.CompositeConverter; 013 014 015 /** 016 * @author Pavel Vlasov 017 * 018 * @version $Revision: 1.11 $ 019 */ 020 public class CharColumn extends Column { 021 private char value; 022 023 // Original stuff 024 private char originalValue; 025 private boolean isOriginalValueSet; 026 027 public char getOriginalValue() { 028 return isOriginalValueSet ? originalValue : value; 029 } 030 031 public void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException { 032 ps.setInt(idx, getOriginalValue()); 033 } 034 035 public void setOriginal() { 036 originalValue=value; 037 isOriginalValueSet=true; 038 } 039 // End of original stuff 040 041 public char getValue() { 042 return value; 043 } 044 045 public void setValue(char value) { 046 if (force || this.value!=value) { 047 this.value = value; 048 onChange(); 049 } 050 } 051 052 public CharColumn(String name, boolean isPrimaryKey) { 053 super(name, isPrimaryKey); 054 } 055 056 public CharColumn(String name, boolean isPrimaryKey, char value) { 057 super(name, isPrimaryKey); 058 this.value=value; 059 } 060 061 public CharColumn(String name, boolean isPrimaryKey, ResultSet rs) throws SQLException { 062 super(name, isPrimaryKey); 063 ResultSetMetaData metaData = rs.getMetaData(); 064 for (int i=1, c=metaData.getColumnCount(); i<=c; i++) { 065 if (name.equals(metaData.getColumnName(i))) { 066 String strVal = rs.getString(i); 067 if (strVal!=null && strVal.length()>=1) { 068 this.value=strVal.charAt(0); 069 } 070 break; 071 } 072 } 073 } 074 075 protected void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException { 076 ps.setInt(idx, value); 077 } 078 079 public Object getObjectValue(boolean ov) { 080 if (ov) { 081 return isOriginalValueSet ? new Character(originalValue) : null; 082 } 083 return new Character(value); 084 } 085 086 public String toString() { 087 return getName()+(isModified() ? "*" : "")+"='"+value+"'"; 088 } 089 090 public boolean equals(Object otherColumn) { 091 if (otherColumn instanceof CharColumn) { 092 return value==((CharColumn) otherColumn).value; 093 } 094 095 return false; 096 } 097 098 public int hashCode() { 099 return getName().hashCode() ^ value; 100 } 101 102 public void load(String textValue) { 103 setValue(textValue.length()==0 ? 0 : textValue.charAt(0)); 104 } 105 106 public void clear() { 107 setValue((char) 0); 108 clearModified(); 109 } 110 111 public void configure(Context context, CompositeConverter converter) { 112 Object o=context.get(getName()); 113 if (o!=null) { 114 setValue(((Character) converter.convert(o, char.class, false)).charValue()); 115 } 116 } 117 118 protected String getType() { 119 return "char"; 120 } 121 122 public void set(Column source) { 123 setValue(((CharColumn) source).getValue()); 124 } 125 126 /** 127 * Clears modified flag and sets original value to current value. 128 */ 129 public void clearModified() { 130 super.clearModified(); 131 originalValue=value; 132 } 133 }