All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sagacity.sqltoy.solon.integration.SqlToyDbInjector Maven / Gradle / Ivy

There is a newer version: 5.6.32.jre8
Show newest version
package org.sagacity.sqltoy.solon.integration;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

import org.noear.solon.core.BeanInjector;
import org.noear.solon.core.VarHolder;
import org.sagacity.sqltoy.SqlToyContext;
import org.sagacity.sqltoy.dao.LightDao;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.service.SqlToyCRUDService;
import org.sagacity.sqltoy.solon.DbManager;
import org.sagacity.sqltoy.solon.annotation.Db;

/**
 * @author 夜の孤城
 * @since 1.5
 * */
public class SqlToyDbInjector implements BeanInjector {
    private static Map daoMap = new ConcurrentHashMap();
    private static Map serviceMap = new ConcurrentHashMap();

    @Override
    public void doInject(VarHolder varH, Db anno) {
        String v = anno.value();
        if (v.equals("")) {
            varH.context().getWrapAsync(DataSource.class, bw -> {
                inject(bw.get(), varH);
            });
        } else {
            varH.context().getWrapAsync(v, bw -> {
                inject(bw.get(), varH);
            });
        }
    }

    private void inject(DataSource dataSource, VarHolder varH) {
        Class type = varH.getType();

        if (type.equals(DataSource.class)) {
            varH.setValue(dataSource);
            return;
        }

        varH.context().getWrapAsync(SqlToyContext.class, bw -> {
            if (type.equals(SqlToyLazyDao.class)) {
                varH.setValue(DbManager.getDao(dataSource));
                return;
            }
            if(type.equals(LightDao.class)){
                varH.setValue(DbManager.getLightDao(dataSource));
                return;
            }
            if (type.equals(SqlToyCRUDService.class)) {
                varH.setValue(DbManager.getService(bw.context(), dataSource));
                return;
            }

//            if (type.isInterface()) {
//                varH.setValue(DbManager.getMapper(dataSource, type));
//                return;
//            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy