01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.jdbc.meta.strats;
20:
21: import java.sql.ResultSet;
22: import java.sql.SQLException;
23:
24: import org.apache.openjpa.jdbc.kernel.JDBCStore;
25: import org.apache.openjpa.jdbc.sql.DBDictionary;
26: import org.apache.openjpa.jdbc.sql.Row;
27: import org.apache.openjpa.kernel.OpenJPAStateManager;
28: import org.apache.openjpa.lib.util.Localizer;
29: import org.apache.openjpa.meta.JavaTypes;
30: import org.apache.openjpa.util.MetaDataException;
31:
32: /**
33: * Mapping for CLOB string on a dictionary that has a maximum embedded
34: * CLOB size.
35: *
36: * @author Abe White
37: * @nojavadoc
38: * @since 0.4.0
39: */
40: public class MaxEmbeddedClobFieldStrategy extends
41: MaxEmbeddedLobFieldStrategy {
42:
43: private static final Localizer _loc = Localizer
44: .forPackage(MaxEmbeddedClobFieldStrategy.class);
45:
46: private int _maxSize = 0;
47:
48: protected int getExpectedJavaType() {
49: return JavaTypes.STRING;
50: }
51:
52: protected void update(OpenJPAStateManager sm, Row row)
53: throws SQLException {
54: String s = sm.fetchString(field.getIndex());
55: if (s == null || s.length() > _maxSize)
56: row.setNull(field.getColumns()[0], true);
57: else
58: row.setString(field.getColumns()[0], s);
59: }
60:
61: protected Boolean isCustom(OpenJPAStateManager sm, JDBCStore store) {
62: String s = sm.fetchString(field.getIndex());
63: return (s != null && s.length() > _maxSize) ? null
64: : Boolean.FALSE;
65: }
66:
67: protected void putData(OpenJPAStateManager sm, ResultSet rs,
68: DBDictionary dict) throws SQLException {
69: Object clob = rs.getClob(1);
70: dict.putString(clob, sm.fetchString(field.getIndex()));
71: }
72:
73: public void map(boolean adapt) {
74: if (field.getTypeCode() != JavaTypes.STRING)
75: throw new MetaDataException(_loc.get("not-clobstring",
76: field));
77: super .map(adapt);
78: }
79:
80: public void initialize() {
81: DBDictionary dict = field.getMappingRepository()
82: .getDBDictionary();
83: _maxSize = dict.maxEmbeddedClobSize;
84: }
85: }
|