org.springframework.jdbc.support.lob.LobHandler Java Examples

The following examples show how to use org.springframework.jdbc.support.lob.LobHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileUploadRepository.java    From alf.io with GNU General Public License v3.0 6 votes vote down vote up
default void upload(UploadBase64FileModification file, String digest, Map<String, String> attributes) {
    LobHandler lobHandler = new DefaultLobHandler();

    NamedParameterJdbcTemplate jdbc = getNamedParameterJdbcTemplate();

    jdbc.getJdbcOperations().execute("insert into file_blob (id, name, content_size, content, content_type, attributes) values(?, ?, ?, ?, ?, ?)",
        new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
            @Override
            protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                ps.setString(1, digest);
                ps.setString(2, file.getName());
                ps.setLong(3, file.getFile().length);
                lobCreator.setBlobAsBytes(ps, 4, file.getFile());
                ps.setString(5, file.getType());
                ps.setString(6, Json.GSON.toJson(attributes));
            }
        });
}
 
Example #2
Source File: AbstractDbDialect.java    From DataLink with Apache License 2.0 6 votes vote down vote up
public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler) {
    this.jdbcTemplate = jdbcTemplate;
    this.lobHandler = lobHandler;
    // 初始化transction
    this.transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

    // 初始化一些数据
    jdbcTemplate.execute(new ConnectionCallback() {

        public Object doInConnection(Connection c) throws SQLException, DataAccessException {
            DatabaseMetaData meta = c.getMetaData();
            databaseName = meta.getDatabaseProductName();
            databaseMajorVersion = meta.getDatabaseMajorVersion();
            databaseMinorVersion = meta.getDatabaseMinorVersion();

            return null;
        }
    });

    initTables(jdbcTemplate);
}
 
Example #3
Source File: BlobSerializableType.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException, IOException, HibernateException {

	InputStream is = lobHandler.getBlobAsBinaryStream(rs, names[0]);
	if (is != null) {
		ObjectInputStream ois = new ObjectInputStream(is);
		try {
			return ois.readObject();
		}
		catch (ClassNotFoundException ex) {
			throw new HibernateException("Could not deserialize BLOB contents", ex);
		}
		finally {
			ois.close();
		}
	}
	else {
		return null;
	}
}
 
Example #4
Source File: BlobSerializableType.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException, IOException, HibernateException {

	InputStream is = lobHandler.getBlobAsBinaryStream(rs, names[0]);
	if (is != null) {
		ObjectInputStream ois = new ObjectInputStream(is);
		try {
			return ois.readObject();
		}
		catch (ClassNotFoundException ex) {
			throw new HibernateException("Could not deserialize BLOB contents", ex);
		}
		finally {
			ois.close();
		}
	}
	else {
		return null;
	}
}
 
Example #5
Source File: UploadedResourceRepository.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
default int upload(Integer organizationId, Integer eventId, UploadBase64FileModification file, Map<String, String> attributes) {

        LobHandler lobHandler = new DefaultLobHandler();

        String query = "insert into resource_global (name, content_size, content, content_type, attributes) values(?, ?, ?, ?, ?)";
        if (organizationId != null && eventId != null) {
            query = "insert into resource_event (name, content_size, content, content_type, attributes, organization_id_fk, event_id_fk) values(?, ?, ?, ?, ?, ?, ?)";
        } else if(organizationId != null) {
            query = "insert into resource_organizer (name, content_size, content, content_type, attributes, organization_id_fk) values(?, ?, ?, ?, ?, ?)";
        }

        return getNamedParameterJdbcTemplate().getJdbcOperations().execute(query,
            new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                @Override
                protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setString(1, file.getName());
                    ps.setLong(2, file.getFile().length);
                    lobCreator.setBlobAsBytes(ps, 3, file.getFile());
                    ps.setString(4, file.getType());
                    ps.setString(5, Json.GSON.toJson(attributes));
                    if (organizationId != null) {
                        ps.setInt(6, organizationId);
                    }
                    if (eventId != null) {
                        ps.setInt(7, eventId);
                    }
                }
            }
        );
    }
 
Example #6
Source File: AltibaseClobStringTypeHandler.java    From oslits with GNU General Public License v3.0 5 votes vote down vote up
protected Object getResultInternal(ResultSet rs, int index, LobHandler lobHandler)
		throws SQLException {

	StringBuffer read_data = new StringBuffer("");
    int read_length;

	char [] buf = new char[1024];

	Reader rd =  lobHandler.getClobAsCharacterStream(rs, index);
    try {
		while( (read_length=rd.read(buf))  != -1) {
			read_data.append(buf, 0, read_length);
		}
    } catch (IOException ie) {
    	SQLException sqle = new SQLException(ie.getMessage());
    	throw sqle;
   	// 2011.10.10 보안점검 후속조치
    } finally {
	    if (rd != null) {
		try {
		    rd.close();
		} catch (Exception ignore) {
			LOGGER.debug("IGNORE: {}", ignore.getMessage());
		}
	    }
	}

    return read_data.toString();

	//return lobHandler.getClobAsString(rs, index);
}
 
Example #7
Source File: SqlLobValueTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	MockitoAnnotations.initMocks(this);
	preparedStatement = mock(PreparedStatement.class);
	handler = mock(LobHandler.class);
	creator = mock(LobCreator.class);
	given(handler.getLobCreator()).willReturn(creator);
}
 
Example #8
Source File: SqlLobValueTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	MockitoAnnotations.initMocks(this);
	preparedStatement = mock(PreparedStatement.class);
	handler = mock(LobHandler.class);
	creator = mock(LobCreator.class);
	given(handler.getLobCreator()).willReturn(creator);
}
 
Example #9
Source File: AbstractDbDialect.java    From DataLink with Apache License 2.0 5 votes vote down vote up
public AbstractDbDialect(JdbcTemplate jdbcTemplate, LobHandler lobHandler, String name, int majorVersion,
                         int minorVersion) {
    this.jdbcTemplate = jdbcTemplate;
    this.lobHandler = lobHandler;
    // 初始化transction
    this.transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);

    this.databaseName = name;
    this.databaseMajorVersion = majorVersion;
    this.databaseMinorVersion = minorVersion;

    initTables(jdbcTemplate);
}
 
Example #10
Source File: BlobByteArrayType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException {

	return lobHandler.getBlobAsBytes(rs, names[0]);
}
 
Example #11
Source File: BlobByteArrayType.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException {

	return lobHandler.getBlobAsBytes(rs, names[0]);
}
 
Example #12
Source File: ClobStringType.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException {

	return lobHandler.getClobAsString(rs, names[0]);
}
 
Example #13
Source File: BlobStringType.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException, UnsupportedEncodingException {

	byte[] bytes = lobHandler.getBlobAsBytes(rs, names[0]);
	if (bytes != null) {
		String encoding = getCharacterEncoding();
		return (encoding != null ? new String(bytes, encoding) : new String(bytes));
	}
	else {
		return null;
	}
}
 
Example #14
Source File: SqlLobValueTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	MockitoAnnotations.initMocks(this);
	preparedStatement = mock(PreparedStatement.class);
	handler = mock(LobHandler.class);
	creator = mock(LobCreator.class);
	given(handler.getLobCreator()).willReturn(creator);
}
 
Example #15
Source File: JdbcHttpSessionConfigurationTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void customLobHandlerConfiguration() {
	registerAndRefresh(DataSourceConfiguration.class, CustomLobHandlerConfiguration.class);

	JdbcIndexedSessionRepository repository = this.context.getBean(JdbcIndexedSessionRepository.class);
	LobHandler lobHandler = this.context.getBean(LobHandler.class);
	assertThat(repository).isNotNull();
	assertThat(lobHandler).isNotNull();
	assertThat(ReflectionTestUtils.getField(repository, "lobHandler")).isEqualTo(lobHandler);
}
 
Example #16
Source File: SqlLobValueTests.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	MockitoAnnotations.initMocks(this);
	preparedStatement = mock(PreparedStatement.class);
	handler = mock(LobHandler.class);
	creator = mock(LobCreator.class);
	given(handler.getLobCreator()).willReturn(creator);
}
 
Example #17
Source File: BlobStringType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException, UnsupportedEncodingException {

	byte[] bytes = lobHandler.getBlobAsBytes(rs, names[0]);
	if (bytes != null) {
		String encoding = getCharacterEncoding();
		return (encoding != null ? new String(bytes, encoding) : new String(bytes));
	}
	else {
		return null;
	}
}
 
Example #18
Source File: ClobStringType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object nullSafeGetInternal(
		ResultSet rs, String[] names, Object owner, LobHandler lobHandler)
		throws SQLException {

	return lobHandler.getClobAsString(rs, names[0]);
}
 
Example #19
Source File: JdbcIndexedSessionRepository.java    From spring-session with Apache License 2.0 4 votes vote down vote up
private LobHandler getLobHandler() {
	return this.lobHandler;
}
 
Example #20
Source File: JdbcHttpSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
LobHandler springSessionLobHandler() {
	return mock(LobHandler.class);
}
 
Example #21
Source File: JdbcHttpSessionConfiguration.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Autowired(required = false)
@Qualifier("springSessionLobHandler")
public void setLobHandler(LobHandler lobHandler) {
	this.lobHandler = lobHandler;
}
 
Example #22
Source File: SqlLobValue.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new BLOB value with the given byte array.
 * @param bytes the byte array containing the BLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(byte[] bytes, LobHandler lobHandler) {
	this.content = bytes;
	this.length = (bytes != null ? bytes.length : 0);
	this.lobCreator = lobHandler.getLobCreator();
}
 
Example #23
Source File: SqlLobValue.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new BLOB value with the given byte array.
 * @param bytes the byte array containing the BLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(byte[] bytes, LobHandler lobHandler) {
	this.content = bytes;
	this.length = (bytes != null ? bytes.length : 0);
	this.lobCreator = lobHandler.getLobCreator();
}
 
Example #24
Source File: AbstractLobType.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor used for testing: takes an explicit LobHandler
 * and an explicit JTA TransactionManager (can be {@code null}).
 */
protected AbstractLobType(LobHandler lobHandler, TransactionManager jtaTransactionManager) {
	this.lobHandler = lobHandler;
	this.jtaTransactionManager = jtaTransactionManager;
}
 
Example #25
Source File: StartApplication.java    From spring-boot with MIT License 4 votes vote down vote up
@Bean
public LobHandler lobHandler() {
    return new DefaultLobHandler();
}
 
Example #26
Source File: SqlLobValue.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new BLOB value with the given byte array.
 * @param bytes the byte array containing the BLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(@Nullable byte[] bytes, LobHandler lobHandler) {
	this.content = bytes;
	this.length = (bytes != null ? bytes.length : 0);
	this.lobCreator = lobHandler.getLobCreator();
}
 
Example #27
Source File: SqlLobValue.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new CLOB value with the given content string.
 * @param content the String containing the CLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(@Nullable String content, LobHandler lobHandler) {
	this.content = content;
	this.length = (content != null ? content.length() : 0);
	this.lobCreator = lobHandler.getLobCreator();
}
 
Example #28
Source File: AbstractLobType.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor used for testing: takes an explicit LobHandler
 * and an explicit JTA TransactionManager (can be {@code null}).
 */
protected AbstractLobType(LobHandler lobHandler, TransactionManager jtaTransactionManager) {
	this.lobHandler = lobHandler;
	this.jtaTransactionManager = jtaTransactionManager;
}
 
Example #29
Source File: SqlLobValue.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new BLOB value with the given byte array.
 * @param bytes the byte array containing the BLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(@Nullable byte[] bytes, LobHandler lobHandler) {
	this.content = bytes;
	this.length = (bytes != null ? bytes.length : 0);
	this.lobCreator = lobHandler.getLobCreator();
}
 
Example #30
Source File: SqlLobValue.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new CLOB value with the given content string.
 * @param content the String containing the CLOB value
 * @param lobHandler the LobHandler to be used
 */
public SqlLobValue(String content, LobHandler lobHandler) {
	this.content = content;
	this.length = (content != null ? content.length() : 0);
	this.lobCreator = lobHandler.getLobCreator();
}