
com.arakelian.jdbc.store.strategy.SimpleJdbcStrategy Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arakelian.jdbc.store.strategy;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.lang3.StringUtils;
import com.arakelian.jdbc.conn.ConnectionFactory;
import com.arakelian.jdbc.utils.DatabaseUtils;
import com.arakelian.store.feature.HasId;
import com.arakelian.store.json.StoreObjectMapper;
import com.google.common.base.Preconditions;
public class SimpleJdbcStrategy extends AbstractJdbcStrategy {
public SimpleJdbcStrategy(final StoreObjectMapper serializer) {
super(serializer);
}
@Override
public T get(final ResultSet rs) throws SQLException {
final String json = StringUtils.defaultIfEmpty(rs.getString(1), "{}");
try {
final T bean = mapper.readValue(json);
return bean;
} catch (final IOException e) {
throw new SQLException("Unable to deserialize record");
}
}
@Override
public String getInsertSql(final String table) {
Preconditions.checkArgument(!StringUtils.isEmpty(table), "table must be non-empty");
return "insert into " + table + "(id,document) values(?,?) on duplicate key update document=?";
}
@Override
public String getSelectSql(final String table) {
Preconditions.checkArgument(!StringUtils.isEmpty(table), "table must be non-empty");
return "select document from " + table;
}
@Override
public void put(final ConnectionFactory connectionFactory, final String insertSql, final T value)
throws SQLException {
String json;
try {
json = toString(value);
} catch (final IOException e) {
throw new SQLException("Unable to serialize value", e);
}
DatabaseUtils.executeUpdate(connectionFactory, insertSql, value.getId(), json);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy