001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.meta.strats;
020:
021: import java.io.CharArrayReader;
022: import java.io.CharArrayWriter;
023: import java.io.IOException;
024: import java.io.Reader;
025: import java.lang.reflect.Array;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028:
029: import org.apache.openjpa.jdbc.kernel.JDBCStore;
030: import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
031: import org.apache.openjpa.jdbc.schema.Column;
032: import org.apache.openjpa.jdbc.sql.DBDictionary;
033: import org.apache.openjpa.jdbc.sql.Joins;
034: import org.apache.openjpa.jdbc.sql.Result;
035: import org.apache.openjpa.jdbc.sql.Row;
036: import org.apache.openjpa.kernel.OpenJPAStateManager;
037: import org.apache.openjpa.lib.util.Localizer;
038: import org.apache.openjpa.util.MetaDataException;
039:
040: /**
041: * Mapping for CLOB character array on a dictionary that has a maximum
042: * embedded CLOB size.
043: *
044: * @author Abe White
045: * @nojavadoc
046: * @since 0.4.0
047: */
048: public class MaxEmbeddedCharArrayFieldStrategy extends
049: MaxEmbeddedLobFieldStrategy {
050:
051: private static final Localizer _loc = Localizer
052: .forPackage(MaxEmbeddedCharArrayFieldStrategy.class);
053:
054: private int _maxSize = 0;
055:
056: protected int getExpectedJavaType() {
057: return JavaSQLTypes.CHAR_STREAM;
058: }
059:
060: protected void update(OpenJPAStateManager sm, Row row)
061: throws SQLException {
062: char[] c = PrimitiveWrapperArrays.toCharArray(sm
063: .fetchObject(field.getIndex()));
064: if (c == null || c.length > _maxSize)
065: row.setNull(field.getColumns()[0], true);
066: else
067: row.setCharacterStream(field.getColumns()[0],
068: new CharArrayReader(c), c.length);
069: }
070:
071: protected Boolean isCustom(OpenJPAStateManager sm, JDBCStore store) {
072: Object val = sm.fetchObject(field.getIndex());
073: return (val != null && Array.getLength(val) > _maxSize) ? null
074: : Boolean.FALSE;
075: }
076:
077: protected void putData(OpenJPAStateManager sm, ResultSet rs,
078: DBDictionary dict) throws SQLException {
079: Object clob = rs.getClob(1);
080: dict.putChars(clob, PrimitiveWrapperArrays.toCharArray(sm
081: .fetchObject(field.getIndex())));
082: }
083:
084: protected Object load(Column col, Result res, Joins joins)
085: throws SQLException {
086: Reader reader = res.getCharacterStream(col, joins);
087: if (reader == null)
088: return null;
089:
090: try {
091: CharArrayWriter writer = new CharArrayWriter();
092: for (int c; (c = reader.read()) != -1;)
093: writer.write(c);
094: return PrimitiveWrapperArrays.toObjectValue(field, writer
095: .toCharArray());
096: } catch (IOException ioe) {
097: throw new SQLException(ioe.toString());
098: }
099: }
100:
101: public void map(boolean adapt) {
102: if (field.getType() != char[].class
103: && field.getType() != Character[].class)
104: throw new MetaDataException(_loc.get("not-chars", field));
105: super .map(adapt);
106: }
107:
108: public void initialize() {
109: DBDictionary dict = field.getMappingRepository()
110: .getDBDictionary();
111: _maxSize = dict.maxEmbeddedClobSize;
112: }
113: }
|