1 16 17 package org.springframework.jdbc.core.support; 18 19 import java.io.InputStream ; 20 import java.io.Reader ; 21 import java.sql.PreparedStatement ; 22 import java.sql.SQLException ; 23 import java.sql.Types ; 24 25 import org.springframework.jdbc.core.DisposableSqlTypeValue; 26 import org.springframework.jdbc.support.lob.DefaultLobHandler; 27 import org.springframework.jdbc.support.lob.LobCreator; 28 import org.springframework.jdbc.support.lob.LobHandler; 29 30 66 public class SqlLobValue implements DisposableSqlTypeValue { 67 68 private final Object content; 69 70 private final int length; 71 72 76 private final LobCreator lobCreator; 77 78 79 85 public SqlLobValue(byte[] bytes) { 86 this(bytes, new DefaultLobHandler()); 87 } 88 89 94 public SqlLobValue(byte[] bytes, LobHandler lobHandler) { 95 this.content = bytes; 96 this.length = (bytes != null ? bytes.length : 0); 97 this.lobCreator = lobHandler.getLobCreator(); 98 } 99 100 106 public SqlLobValue(String content) { 107 this(content, new DefaultLobHandler()); 108 } 109 110 115 public SqlLobValue(String content, LobHandler lobHandler) { 116 this.content = content; 117 this.length = (content != null ? content.length() : 0); 118 this.lobCreator = lobHandler.getLobCreator(); 119 } 120 121 128 public SqlLobValue(InputStream stream, int length) { 129 this(stream, length, new DefaultLobHandler()); 130 } 131 132 138 public SqlLobValue(InputStream stream, int length, LobHandler lobHandler) { 139 this.content = stream; 140 this.length = length; 141 this.lobCreator = lobHandler.getLobCreator(); 142 } 143 144 151 public SqlLobValue(Reader reader, int length) { 152 this(reader, length, new DefaultLobHandler()); 153 } 154 155 161 public SqlLobValue(Reader reader, int length, LobHandler lobHandler) { 162 this.content = reader; 163 this.length = length; 164 this.lobCreator = lobHandler.getLobCreator(); 165 } 166 167 168 171 public void setTypeValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName) 172 throws SQLException { 173 if (sqlType == Types.BLOB) { 174 if (this.content instanceof byte[] || this.content == null) { 175 this.lobCreator.setBlobAsBytes(ps, paramIndex, (byte[]) this.content); 176 } 177 else if (this.content instanceof String ) { 178 this.lobCreator.setBlobAsBytes(ps, paramIndex, ((String ) this.content).getBytes()); 179 } 180 else if (this.content instanceof InputStream ) { 181 this.lobCreator.setBlobAsBinaryStream(ps, paramIndex, (InputStream ) this.content, this.length); 182 } 183 else { 184 throw new IllegalArgumentException ( 185 "Content type [" + this.content.getClass().getName() + "] not supported for BLOB columns"); 186 } 187 } 188 else if (sqlType == Types.CLOB) { 189 if (this.content instanceof String || this.content == null) { 190 this.lobCreator.setClobAsString(ps, paramIndex, (String ) this.content); 191 } 192 else if (this.content instanceof InputStream ) { 193 this.lobCreator.setClobAsAsciiStream(ps, paramIndex, (InputStream ) this.content, this.length); 194 } 195 else if (this.content instanceof Reader ) { 196 this.lobCreator.setClobAsCharacterStream(ps, paramIndex, (Reader ) this.content, this.length); 197 } 198 else { 199 throw new IllegalArgumentException ( 200 "Content type [" + this.content.getClass().getName() + "] not supported for CLOB columns"); 201 } 202 } 203 else { 204 throw new IllegalArgumentException ("SqlLobValue only supports SQL types BLOB and CLOB"); 205 } 206 } 207 208 211 public void cleanup() { 212 this.lobCreator.close(); 213 } 214 215 } 216 | Popular Tags |