KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > CharacterMapping


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: CharacterMapping.java,v 1.6 2003/10/10 22:22:14 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import java.sql.PreparedStatement JavaDoc;
15 import java.sql.ResultSet JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import java.sql.Types JavaDoc;
18 import javax.jdo.JDODataStoreException;
19
20
21 public class CharacterMapping extends ColumnMapping
22 {
23     public CharacterMapping(DatabaseAdapter dba, Class JavaDoc type)
24     {
25         super(dba, type);
26
27         initTypeInfo();
28     }
29
30     public CharacterMapping(Column col)
31     {
32         super(col);
33
34         col.setFixedLength(1);
35         col.checkString();
36
37         initTypeInfo();
38     }
39
40     public CharacterMapping(ClassBaseTable table, int relativeFieldNumber)
41     {
42         this(table.newColumn(relativeFieldNumber));
43     }
44
45     protected TypeInfo getTypeInfo()
46     {
47         return dba.getTypeInfo(Types.CHAR);
48     }
49
50     public void setChar(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, char value)
51     {
52         try
53         {
54             ps.setString(param, new Character JavaDoc(value).toString());
55         }
56         catch (SQLException JavaDoc e)
57         {
58             throw dba.newDataStoreException("Can't set char parameter: value = " + value, e);
59         }
60     }
61
62     public char getChar(PersistenceManager pm, ResultSet JavaDoc rs, int param)
63     {
64         char value;
65
66         try
67         {
68             String JavaDoc s = rs.getString(param);
69
70             if (rs.wasNull())
71                 throw new NullValueException("Illegal null value in column " + col);
72
73             value = s.length() == 0 ? '\000' : s.charAt(0);
74         }
75         catch (SQLException JavaDoc e)
76         {
77             throw dba.newDataStoreException("Can't get char result: param = " + param, e);
78         }
79
80         return value;
81     }
82
83     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
84     {
85         try
86         {
87             if (value == null)
88                 ps.setNull(param, typeInfo.dataType);
89             else
90                 ps.setString(param, ((Character JavaDoc)value).toString());
91         }
92         catch (SQLException JavaDoc e)
93         {
94             throw dba.newDataStoreException("Can't set Character parameter: value = " + value, e);
95         }
96     }
97
98     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
99     {
100         Object JavaDoc value;
101
102         try
103         {
104             String JavaDoc s = rs.getString(param);
105             value = rs.wasNull() ? null : new Character JavaDoc(s.length() == 0 ? '\000' : s.charAt(0));
106         }
107         catch (SQLException JavaDoc e)
108         {
109             throw dba.newDataStoreException("Can't get Character result: param = " + param, e);
110         }
111
112         return value;
113     }
114
115     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
116     {
117         return new CharacterLiteral(qs, ((Character JavaDoc)value).toString());
118     }
119
120     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
121     {
122         return new CharacterExpression(qs, qsc);
123     }
124 }
125
Popular Tags