com.github.deansquirrel.tools.db.SQLiteLoadHelper Maven / Gradle / Ivy
The newest version!
package com.github.deansquirrel.tools.db;
import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.text.MessageFormat;
@Component
public class SQLiteLoadHelper {
private static final Logger logger = LoggerFactory.getLogger(SQLiteLoadHelper.class);
private final IToolsDbHelper iToolsDbHelper;
public SQLiteLoadHelper(IToolsDbHelper iToolsDbHelper) {
this.iToolsDbHelper = iToolsDbHelper;
}
public void addSQLiteConn(String connName, String dbPath) throws Exception {
iToolsDbHelper.addDataSource(connName, getDataSource(connName, dbPath));
}
public DruidDataSource getDataSource(String connName, String dbPath) throws Exception {
if(connName == null || connName.isEmpty() || dbPath == null || dbPath.isEmpty()) {
throw new Exception("连接地址或名称不允许为空");
}
if(iToolsDbHelper.isExistDataSource(connName)) {
throw new Exception(MessageFormat.format("连接名称[{0}]已存在", connName));
}
SQLiteConnHelper conn = SQLiteConnHelper.builder(connName).setPath(dbPath);
return conn.getDataSource();
}
}