package com.example;

import org.apache.ignite.cache.store.CacheStore;
import org.apache.ignite.lang.IgniteBiInClosure;
import org.jetbrains.annotations.Nullable;

import javax.cache.Cache;
import javax.cache.integration.CacheLoaderException;
import javax.cache.integration.CacheWriterException;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Map;

public class TraderCacheStore  implements CacheStore, Serializable {

    private transient DataSource dataSource;

    public void loadCache(IgniteBiInClosure clo, @Nullable Object... args) throws CacheLoaderException {

        try {
            Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from common..trader");
            while(resultSet.next()){
                System.out.println(resultSet.getString("name"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public void sessionEnd(boolean commit) throws CacheWriterException {

    }

    public Object load(Object o) throws CacheLoaderException {
        return null;
    }

    public Map loadAll(Iterable iterable) throws CacheLoaderException {
        return null;
    }

    public void write(Cache.Entry entry) throws CacheWriterException {

    }

    public void delete(Object o) throws CacheWriterException {

    }

    public void deleteAll(Collection collection) throws CacheWriterException {

    }

    public void writeAll(Collection collection) throws CacheWriterException {

    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
