01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.orm.ibatis.support;
18:
19: import java.sql.PreparedStatement;
20: import java.sql.ResultSet;
21: import java.sql.SQLException;
22:
23: import org.springframework.jdbc.support.lob.LobCreator;
24: import org.springframework.jdbc.support.lob.LobHandler;
25:
26: /**
27: * iBATIS TypeHandler implementation for byte arrays that get mapped to BLOBs.
28: * Retrieves the LobHandler to use from SqlMapClientFactoryBean at config time.
29: *
30: * <p>Can also be defined in generic iBATIS mappings, as DefaultLobCreator will
31: * work with most JDBC-compliant database drivers. In this case, the field type
32: * does not have to be BLOB: For databases like MySQL and MS SQL Server, any
33: * large enough binary type will work.
34: *
35: * @author Juergen Hoeller
36: * @since 1.1.5
37: * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#setLobHandler
38: */
39: public class BlobByteArrayTypeHandler extends AbstractLobTypeHandler {
40:
41: /**
42: * Constructor used by iBATIS: fetches config-time LobHandler from
43: * SqlMapClientFactoryBean.
44: * @see org.springframework.orm.ibatis.SqlMapClientFactoryBean#getConfigTimeLobHandler
45: */
46: public BlobByteArrayTypeHandler() {
47: super ();
48: }
49:
50: /**
51: * Constructor used for testing: takes an explicit LobHandler.
52: */
53: protected BlobByteArrayTypeHandler(LobHandler lobHandler) {
54: super (lobHandler);
55: }
56:
57: protected void setParameterInternal(PreparedStatement ps,
58: int index, Object value, String jdbcType,
59: LobCreator lobCreator) throws SQLException {
60: lobCreator.setBlobAsBytes(ps, index, (byte[]) value);
61: }
62:
63: protected Object getResultInternal(ResultSet rs, int index,
64: LobHandler lobHandler) throws SQLException {
65: return lobHandler.getBlobAsBytes(rs, index);
66: }
67:
68: public Object valueOf(String s) {
69: return s.getBytes();
70: }
71:
72: }
|