
com.arakelian.jdbc.store.JdbcStoreConfig 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;
import org.immutables.value.Value;
import com.arakelian.jackson.utils.JacksonUtils;
import com.arakelian.jdbc.conn.ConnectionFactory;
import com.arakelian.jdbc.store.strategy.HasTimestampJdbcStrategy;
import com.arakelian.jdbc.store.strategy.JdbcStrategy;
import com.arakelian.jdbc.store.strategy.SimpleJdbcStrategy;
import com.arakelian.store.StoreConfig;
import com.arakelian.store.feature.HasId;
import com.arakelian.store.json.JacksonStoreObjectMapper;
import com.arakelian.store.json.StoreObjectMapper;
@Value.Immutable(copy = false)
@Value.Style(get = { "is*", "get*" })
public interface JdbcStoreConfig extends StoreConfig {
public ConnectionFactory getConnectionFactory();
/**
* Returns SQL snippet that looks like 'delete from {table} where '
*
* @return a SQL snippet for delete
*/
@Value.Derived
public default String getDeleteSql() {
return "delete from " + getTable();
}
/**
* Return SQL snippet that looks like 'replace into {table}(id,document,created,updated)
* values(?,?,?,?) on duplicate key update document=?,updated=?'
*
* @return a SQL snippet for insert
**/
@Value.Derived
public default String getInsertSql() {
return getStrategy().getInsertSql(getTable());
}
/**
* Return SQL snippet that looks like 'select document from {table} '
*
* @return a SQL snippet for select
**/
@Value.Derived
public default String getSelectSql() {
return getStrategy().getSelectSql(getTable());
}
@Value.Default
public default JdbcStrategy getStrategy() {
final Class clazz = getClazz();
final StoreObjectMapper serializer = new JacksonStoreObjectMapper<>(clazz,
JacksonUtils.getObjectMapper());
final JdbcStrategy strategy;
if (HasTimestampJdbcStrategy.supports(clazz)) {
strategy = new HasTimestampJdbcStrategy<>(serializer);
} else {
strategy = new SimpleJdbcStrategy<>(serializer);
}
return strategy;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy