Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.litongjava.db.activerecord;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.jfinal.kit.StrKit;
import com.litongjava.cache.ICache;
import com.litongjava.db.IPlugin;
import com.litongjava.db.activerecord.cache.DefaultEhCache;
import com.litongjava.db.activerecord.dialect.Dialect;
import com.litongjava.db.activerecord.dialect.MysqlDialect;
import com.litongjava.db.activerecord.sql.SqlKit;
/**
* ActiveRecord plugin.
* ActiveRecord plugin not support mysql type year, you can use int instead of
* year. Mysql error message for type year when insert a record: Data truncated
* for column 'xxx' at row 1
*/
public class ReplicaActiveRecordPlugin implements IPlugin {
protected TableBuilder tableBuilder = new TableBuilder();
protected List dataSourceProviders;
protected Boolean devMode = null;
protected List configs;
protected volatile boolean isStarted = false;
protected List
tableList = new ArrayList
();
public ReplicaActiveRecordPlugin(List datasources) {
this.configs = new ArrayList<>();
for (int i = 0; i < datasources.size(); i++) {
this.configs.add(new Config(DbKit.REPLICA_CONFIG_NAME + "_" + i, datasources.get(i), DbKit.DEFAULT_TRANSACTION_LEVEL));
}
}
public ReplicaActiveRecordPlugin(String configName, DataSource dataSource, int transactionLevel) {
this.configs = new ArrayList<>();
if (StrKit.isBlank(configName)) {
throw new IllegalArgumentException("configName can not be blank");
}
if (dataSource == null) {
throw new IllegalArgumentException("dataSource can not be null");
}
this.configs.add(new Config(configName, dataSource, transactionLevel));
}
public ReplicaActiveRecordPlugin(DataSource dataSource) {
this(DbKit.MAIN_CONFIG_NAME, dataSource);
}
public ReplicaActiveRecordPlugin(String configName, DataSource dataSource) {
this(configName, dataSource, DbKit.DEFAULT_TRANSACTION_LEVEL);
}
public ReplicaActiveRecordPlugin(DataSource dataSource, int transactionLevel) {
this(DbKit.MAIN_CONFIG_NAME, dataSource, transactionLevel);
}
public ReplicaActiveRecordPlugin(String configName, IDataSourceProvider dataSourceProvider, int transactionLevel) {
this.dataSourceProviders=new ArrayList<>();
if (StrKit.isBlank(configName)) {
throw new IllegalArgumentException("configName can not be blank");
}
if (dataSourceProvider == null) {
throw new IllegalArgumentException("dataSourceProvider can not be null");
}
this.dataSourceProviders.add(dataSourceProvider);
this.configs.add(new Config(configName, null, transactionLevel));
}
public ReplicaActiveRecordPlugin(String configName, List dataSourceProviders, int transactionLevel) {
this.dataSourceProviders=new ArrayList<>();
if (StrKit.isBlank(configName)) {
throw new IllegalArgumentException("configName can not be blank");
}
if (dataSourceProviders == null) {
throw new IllegalArgumentException("dataSourceProviders can not be null");
}
for (int i = 0; i < dataSourceProviders.size(); i++) {
this.dataSourceProviders.add(dataSourceProviders.get(i));
this.configs.add(new Config(configName + "_" + i, null, transactionLevel));
}
}
public ReplicaActiveRecordPlugin(IDataSourceProvider dataSourceProvider) {
this(DbKit.REPLICA_CONFIG_NAME, dataSourceProvider);
}
public ReplicaActiveRecordPlugin(String configName, IDataSourceProvider dataSourceProvider) {
this(configName, dataSourceProvider, DbKit.DEFAULT_TRANSACTION_LEVEL);
}
public ReplicaActiveRecordPlugin(IDataSourceProvider dataSourceProvider, int transactionLevel) {
this(DbKit.REPLICA_CONFIG_NAME, dataSourceProvider, transactionLevel);
}
public ReplicaActiveRecordPlugin(Config config) {
if (config == null) {
throw new IllegalArgumentException("Config can not be null");
}
this.configs.add(config);
}
public ReplicaActiveRecordPlugin addMapping(String tableName, String primaryKey, Class extends Model>> modelClass) {
tableList.add(new Table(tableName, primaryKey, modelClass));
return this;
}
public ReplicaActiveRecordPlugin addMapping(String tableName, Class extends Model>> modelClass) {
tableList.add(new Table(tableName, modelClass));
return this;
}
public ReplicaActiveRecordPlugin addSqlTemplate(String sqlTemplate) {
for (Config config : configs) {
config.sqlKit.addSqlTemplate(sqlTemplate);
}
return this;
}
public ReplicaActiveRecordPlugin addSqlTemplate(com.jfinal.template.source.ISource sqlTemplate) {
for (Config config : configs) {
config.sqlKit.addSqlTemplate(sqlTemplate);
}
return this;
}
public ReplicaActiveRecordPlugin setBaseSqlTemplatePath(String baseSqlTemplatePath) {
for (Config config : configs) {
config.sqlKit.setBaseSqlTemplatePath(baseSqlTemplatePath);
}
return this;
}
public SqlKit getSqlKit(int i) {
return configs.get(i).sqlKit;
}
public com.jfinal.template.Engine getEngine(int i) {
return getSqlKit(i).getEngine();
}
/**
* Set transaction level define in java.sql.Connection
*
* @param transactionLevel only be 0, 1, 2, 4, 8
*/
public ReplicaActiveRecordPlugin setTransactionLevel(int transactionLevel) {
for (Config config : configs) {
config.setTransactionLevel(transactionLevel);
}
return this;
}
public ReplicaActiveRecordPlugin setCache(ICache cache) {
if (cache == null) {
throw new IllegalArgumentException("cache can not be null");
}
for (Config config : configs) {
config.cache = cache;
}
return this;
}
public ReplicaActiveRecordPlugin setShowSql(boolean showSql) {
for (Config config : configs) {
config.showSql = showSql;
}
return this;
}
public ReplicaActiveRecordPlugin setDevMode(boolean devMode) {
this.devMode = devMode;
for (Config config : configs) {
config.setDevMode(devMode);
}
return this;
}
public Boolean getDevMode() {
return devMode;
}
public ReplicaActiveRecordPlugin setDialect(Dialect dialect) {
if (dialect == null) {
throw new IllegalArgumentException("dialect can not be null");
}
for (Config config : configs) {
config.dialect = dialect;
if (config.transactionLevel == Connection.TRANSACTION_REPEATABLE_READ && dialect.isOracle()) {
// Oracle 不支持 Connection.TRANSACTION_REPEATABLE_READ
config.transactionLevel = Connection.TRANSACTION_READ_COMMITTED;
}
}
return this;
}
public ReplicaActiveRecordPlugin setContainerFactory(IContainerFactory containerFactory) {
if (containerFactory == null) {
throw new IllegalArgumentException("containerFactory can not be null");
}
for (Config config : configs) {
config.containerFactory = containerFactory;
}
return this;
}
public ReplicaActiveRecordPlugin setDbProFactory(IDbProFactory dbProFactory) {
if (dbProFactory == null) {
throw new IllegalArgumentException("dbProFactory can not be null");
}
for (Config config : configs) {
config.dbProFactory = dbProFactory;
}
return this;
}
/**
* 当使用 create table 语句创建用于开发使用的数据表副本时,假如create table 中使用的
* 复合主键次序不同,那么MappingKitGeneretor 反射生成的复合主键次序也会不同。
*
* 而程序中类似于 model.deleteById(id1, id2) 方法中复合主键次序与需要与映射时的次序 保持一致,可以在MappingKit
* 映射完成以后通过调用此方法再次强制指定复合主键次序
*
*