KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004 (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: ByteArrayMapping.java,v 1.1 2004/03/22 04:58:13 jackknifebarber 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.JDOUserException;
19
20
21 public class ByteArrayMapping extends ColumnMapping
22 {
23     public ByteArrayMapping(DatabaseAdapter dba, Class JavaDoc type)
24     {
25         super(dba, type);
26
27         initTypeInfo();
28     }
29
30     public ByteArrayMapping(Column col)
31     {
32         super(col);
33
34         col.checkString();
35
36         initTypeInfo();
37     }
38
39     public ByteArrayMapping(ClassBaseTable table, int relativeFieldNumber)
40     {
41         this(table.newColumn(relativeFieldNumber));
42     }
43
44     protected TypeInfo getTypeInfo()
45     {
46         TypeInfo ti;
47
48         if (col == null)
49             ti = dba.getTypeInfo(new int[] { Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB });
50         else
51         {
52             switch (col.getLengthType())
53             {
54                 case Column.FIXED_LENGTH:
55                     ti = dba.getTypeInfo(new int[] { Types.BINARY, Types.LONGVARBINARY, Types.BLOB });
56
57                     if (ti.dataType != Types.BINARY)
58                         col.setUnlimitedLength();
59
60                     break;
61
62                 case Column.MAXIMUM_LENGTH:
63                     ti = dba.getTypeInfo(new int[] { Types.VARBINARY, Types.LONGVARBINARY, Types.BLOB });
64
65                     if (ti.dataType != Types.VARBINARY)
66                         col.setUnlimitedLength();
67
68                     break;
69
70                 case Column.UNLIMITED_LENGTH:
71                 default:
72                     ti = dba.getTypeInfo(new int[] { Types.LONGVARBINARY, Types.BLOB });
73                     break;
74             }
75         }
76
77         return ti;
78     }
79
80     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
81     {
82         try
83         {
84             if (value == null)
85                 ps.setNull(param, typeInfo.dataType);
86             else
87                 ps.setBytes(param, (byte[])value);
88         }
89         catch (SQLException JavaDoc e)
90         {
91             throw dba.newDataStoreException("Can't set byte[] parameter: value = " + value, e);
92         }
93     }
94
95     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
96     {
97         Object JavaDoc value;
98
99         try
100         {
101             byte[] ba = rs.getBytes(param);
102             value = rs.wasNull() ? null : ba;
103         }
104         catch (SQLException JavaDoc e)
105         {
106             throw dba.newDataStoreException("Can't get byte[] result: param = " + param, e);
107         }
108
109         return value;
110     }
111
112     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
113     {
114         return new BinaryLiteral(qs, ((byte[])value));
115     }
116
117     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
118     {
119         return new BinaryExpression(qs, qsc);
120     }
121 }
122
Popular Tags