KickJava   Java API By Example, From Geeks To Geeks.

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


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: ByteMapping.java,v 1.5 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.math.BigInteger JavaDoc;
15 import java.sql.PreparedStatement JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Types JavaDoc;
19 import javax.jdo.JDODataStoreException;
20
21
22 public class ByteMapping extends ColumnMapping
23 {
24     public ByteMapping(DatabaseAdapter dba, Class JavaDoc type)
25     {
26         super(dba, type);
27
28         initTypeInfo();
29     }
30
31     public ByteMapping(Column col)
32     {
33         super(col);
34
35         col.checkPrimitive();
36
37         initTypeInfo();
38     }
39
40     public ByteMapping(ClassBaseTable table, int relativeFieldNumber)
41     {
42         this(table.newColumn(relativeFieldNumber));
43     }
44
45     protected TypeInfo getTypeInfo()
46     {
47         return dba.getTypeInfo(new int[] { Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.DECIMAL });
48     }
49
50     public void setByte(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, byte value)
51     {
52         try
53         {
54             ps.setByte(param, value);
55         }
56         catch (SQLException JavaDoc e)
57         {
58             throw dba.newDataStoreException("Can't set byte parameter: value = " + value, e);
59         }
60     }
61
62     public byte getByte(PersistenceManager pm, ResultSet JavaDoc rs, int param)
63     {
64         byte value;
65
66         try
67         {
68             value = rs.getByte(param);
69
70             if (rs.wasNull())
71                 throw new NullValueException("Illegal null value in column " + col);
72         }
73         catch (SQLException JavaDoc e)
74         {
75             throw dba.newDataStoreException("Can't get byte result: param = " + param, e);
76         }
77
78         return value;
79     }
80
81     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
82     {
83         try
84         {
85             if (value == null)
86                 ps.setNull(param, typeInfo.dataType);
87             else
88                 ps.setByte(param, ((Byte JavaDoc)value).byteValue());
89         }
90         catch (SQLException JavaDoc e)
91         {
92             throw dba.newDataStoreException("Can't set Byte parameter: value = " + value, e);
93         }
94     }
95
96     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
97     {
98         Object JavaDoc value;
99
100         try
101         {
102             byte b = rs.getByte(param);
103             value = rs.wasNull() ? null : new Byte JavaDoc(b);
104         }
105         catch (SQLException JavaDoc e)
106         {
107             throw dba.newDataStoreException("Can't get Byte result: param = " + param, e);
108         }
109
110         return value;
111     }
112
113     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
114     {
115         return new IntegerLiteral(qs, BigInteger.valueOf(((Byte JavaDoc)value).longValue()));
116     }
117
118     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
119     {
120         return new NumericExpression(qs, qsc);
121     }
122 }
123
Popular Tags