KickJava   Java API By Example, From Geeks To Geeks.

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


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: StringMapping.java,v 1.6 2004/01/31 19:45:55 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.JDODataStoreException;
19 import javax.jdo.JDOUserException;
20
21
22 public class StringMapping extends ColumnMapping
23 {
24     public StringMapping(DatabaseAdapter dba, Class JavaDoc type)
25     {
26         super(dba, type);
27
28         initTypeInfo();
29     }
30
31     public StringMapping(Column col)
32     {
33         super(col);
34
35         col.checkString();
36
37         if (col.getLengthType() == Column.MAXIMUM_LENGTH)
38         {
39             int maxlength = dba.getTypeInfo(Types.VARCHAR).precision;
40
41             if (col.getPrecision() <= 0 || col.getPrecision() > maxlength)
42             {
43                 throw new JDOUserException("String max length of " + col.getPrecision() +
44                     " is outside the acceptable range [0, " + maxlength + "]");
45             }
46         }
47
48         initTypeInfo();
49     }
50
51     public StringMapping(ClassBaseTable table, int relativeFieldNumber)
52     {
53         this(table.newColumn(relativeFieldNumber));
54     }
55
56     protected TypeInfo getTypeInfo()
57     {
58         TypeInfo ti;
59
60         if (col == null)
61             ti = dba.getTypeInfo(Types.VARCHAR);
62         else
63         {
64             switch (col.getLengthType())
65             {
66                 case Column.FIXED_LENGTH:
67                     ti = dba.getTypeInfo(Types.CHAR);
68                     break;
69
70                 case Column.MAXIMUM_LENGTH:
71                     ti = dba.getTypeInfo(Types.VARCHAR);
72                     break;
73
74                 case Column.UNLIMITED_LENGTH:
75                 default:
76                     ti = dba.getTypeInfo(new int[] { Types.LONGVARCHAR, Types.CLOB });
77                     break;
78             }
79         }
80
81         return ti;
82     }
83
84     public void setString(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, String JavaDoc value)
85     {
86         try
87         {
88             if (value == null)
89                 ps.setNull(param, typeInfo.dataType);
90             else
91                 ps.setString(param, value);
92         }
93         catch (SQLException JavaDoc e)
94         {
95             throw dba.newDataStoreException("Can't set String parameter: value = " + value, e);
96         }
97     }
98
99     public String JavaDoc getString(PersistenceManager pm, ResultSet JavaDoc rs, int param)
100     {
101         String JavaDoc value;
102
103         try
104         {
105             value = rs.getString(param);
106         }
107         catch (SQLException JavaDoc e)
108         {
109             throw dba.newDataStoreException("Can't get String result: param = " + param, e);
110         }
111
112         return value;
113     }
114
115     public void setObject(PersistenceManager pm, PreparedStatement JavaDoc ps, int param, Object JavaDoc value)
116     {
117         try
118         {
119             if (value == null)
120                 ps.setNull(param, typeInfo.dataType);
121             else
122                 ps.setString(param, (String JavaDoc)value);
123         }
124         catch (SQLException JavaDoc e)
125         {
126             throw dba.newDataStoreException("Can't set String parameter: value = " + value, e);
127         }
128     }
129
130     public Object JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
131     {
132         Object JavaDoc value;
133
134         try
135         {
136             String JavaDoc s = rs.getString(param);
137             value = rs.wasNull() ? null : s;
138         }
139         catch (SQLException JavaDoc e)
140         {
141             throw dba.newDataStoreException("Can't get String result: param = " + param, e);
142         }
143
144         return value;
145     }
146
147     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
148     {
149         return new CharacterLiteral(qs, ((String JavaDoc)value));
150     }
151
152     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
153     {
154         return new CharacterExpression(qs, qsc);
155     }
156 }
157
Popular Tags