Commit 1e43c512 authored by yanzg's avatar yanzg

下载视频

parent 803753fa
package com.yanzuoguang.dao; package com.yanzuoguang.dao;
import com.yanzuoguang.util.YzgError; import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
/** /**
...@@ -104,6 +103,9 @@ public class DaoConst { ...@@ -104,6 +103,9 @@ public class DaoConst {
*/ */
public static final String SQL_LOAD = "{SELECT_OUTER_BEGIN}SELECT {FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1" + public static final String SQL_LOAD = "{SELECT_OUTER_BEGIN}SELECT {FIELD} FROM {TABLE} AS a {INNER} WHERE 1=1" +
"{WHERE}{GROUP}{HAVING}{ORDER}{LIMIT}{SELECT_OUTER_END}{INNER_OUTER}{WHERE_OUTER}{GROUP_OUTER}{HAVING_OUTER}{ORDER_OUTER}{LIMIT_OUTER}"; "{WHERE}{GROUP}{HAVING}{ORDER}{LIMIT}{SELECT_OUTER_END}{INNER_OUTER}{WHERE_OUTER}{GROUP_OUTER}{HAVING_OUTER}{ORDER_OUTER}{LIMIT_OUTER}";
public static final String SQL_LIST_FROM = "^[\\s\\S]+?VALUES\\s*?\\(";
public static final String SQL_LIST_TO = ",(";
/** /**
* 表名代码片段 * 表名代码片段
*/ */
......
...@@ -65,6 +65,64 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao { ...@@ -65,6 +65,64 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
return retVal; return retVal;
} }
protected List<String> createReplaceList(String sqlName, Collection collection) {
this.checkTable();
int size = collection.size();
// 写入主键,用于排序修改,防止互相锁数据
for (Object item : collection) {
this.table.initKeyValue(item);
}
// 获取排序后的数据
List list = this.table.getKeySort(collection);
boolean isKeyString = this.table.getTable().getKeyType() == String.class;
boolean isEmpty = true;
// 循环创建
for (Object item : list) {
// 判断主键是字符串和需要生成主键
String keyString = this.table.initKeyValue(item);
// 检测数据合法性
this.check(DaoConst.OPERATOR_TYPE_CREATE, keyString, item);
// 判断是否为空
if (!StringHelper.isEmpty(keyString)) {
isEmpty = false;
}
}
// 获取创建语句
SqlData sqlData = this.getSql(sqlName);
// 最终sql
StringBuilder sqlList = new StringBuilder();
// 最终列表
List<Object> paraList = new ArrayList<>();
for (Object item : list) {
List<Object> paras = new ArrayList<>();
String sql = this.getPara(paras, sqlData, item);
if (sqlList.length() > 0) {
sql = sql.replaceAll(DaoConst.SQL_LIST_FROM, DaoConst.SQL_LIST_TO);
}
sqlList.append(sql);
paraList.addAll(paras);
}
// 最终执行的更新语句
int ret = this.getDb().update(this.getClass(), sqlData.getName(), sqlList.toString(), paraList.toArray());
// 判断是否需要获取自增编号(主键为整形)
if (isEmpty && !isKeyString) {
long identity = StringHelper.toInt(this.getIdentity());
int pos = 0;
// 回写主键
for (Object item : list) {
pos++;
String keyString = StringHelper.toString(identity - size + pos);
this.table.setKeyValue(item, keyString);
}
}
// 触发更新事件
for (Object item : list) {
this.onUpdateSql(item);
}
// 最后获取主键集合
return this.table.getCollectionRet(collection);
}
/** /**
* 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。 * 创建数据,当不传入了主键时,则会自动生成主键,传入时不会生成。
* *
...@@ -164,19 +222,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao { ...@@ -164,19 +222,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/ */
@Override @Override
public List<String> createList(Collection collection) { public List<String> createList(Collection collection) {
this.checkTable(); return this.createReplaceList(DaoConst.CREATE, collection);
// 写入主键,用于排序修改,防止互相锁数据
for (Object item : collection) {
this.table.initKeyValue(item);
}
// 获取排序后的数据
List list = this.table.getKeySort(collection);
// 循环创建
for (Object item : list) {
this.create(item);
}
// 最后获取主键集合
return this.table.getCollectionRet(collection);
} }
/** /**
...@@ -228,19 +274,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao { ...@@ -228,19 +274,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
*/ */
@Override @Override
public List<String> replaceList(Collection collection) { public List<String> replaceList(Collection collection) {
this.checkTable(); return this.createReplaceList(DaoConst.REPLACE, collection);
// 写入主键,用于排序修改,防止互相锁数据
for (Object item : collection) {
this.table.initKeyValue(item);
}
// 获取排序后的数据
List list = this.table.getKeySort(collection);
// 循环处理排序后的数据
for (Object item : list) {
this.replace(item);
}
// 最后获取主键集合
return this.table.getCollectionRet(collection);
} }
/** /**
......
...@@ -8,7 +8,6 @@ import com.yanzuoguang.db.impl.DbRow; ...@@ -8,7 +8,6 @@ import com.yanzuoguang.db.impl.DbRow;
import com.yanzuoguang.util.YzgError; import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper; import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.cache.MemoryCache; import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.ArrayHelper; import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringFormatHandle; import com.yanzuoguang.util.helper.StringFormatHandle;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
...@@ -188,7 +187,7 @@ public abstract class BaseDaoSql { ...@@ -188,7 +187,7 @@ public abstract class BaseDaoSql {
*/ */
protected int updateSql(SqlData sqlData, Object model) { protected int updateSql(SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<Object>(); List<Object> paras = new ArrayList<Object>();
String sql = this.getQueryPara(paras, sqlData, model); String sql = this.getPara(paras, sqlData, model);
int ret = this.getDb().update(this.getClass(), sqlData.getName(), sql, paras.toArray()); int ret = this.getDb().update(this.getClass(), sqlData.getName(), sql, paras.toArray());
this.onUpdateSql(model); this.onUpdateSql(model);
return ret; return ret;
...@@ -249,7 +248,7 @@ public abstract class BaseDaoSql { ...@@ -249,7 +248,7 @@ public abstract class BaseDaoSql {
*/ */
protected <T extends Object> List<T> queryData(Class<T> cls, SqlData sqlData, Object model) { protected <T extends Object> List<T> queryData(Class<T> cls, SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<Object>(); List<Object> paras = new ArrayList<Object>();
String sql = this.getQueryPara(paras, sqlData, model); String sql = this.getPara(paras, sqlData, model);
List<T> list = this.queryWithCache(cls, sqlData.getName(), sql, paras.toArray()); List<T> list = this.queryWithCache(cls, sqlData.getName(), sql, paras.toArray());
return list; return list;
} }
...@@ -264,7 +263,7 @@ public abstract class BaseDaoSql { ...@@ -264,7 +263,7 @@ public abstract class BaseDaoSql {
*/ */
protected <T extends Object> void queryData(Class<T> cls, DbRow<T> handle, SqlData sqlData, Object model) { protected <T extends Object> void queryData(Class<T> cls, DbRow<T> handle, SqlData sqlData, Object model) {
List<Object> paras = new ArrayList<>(); List<Object> paras = new ArrayList<>();
String sql = this.getQueryPara(paras, sqlData, model); String sql = this.getPara(paras, sqlData, model);
// 查询数据 // 查询数据
this.getDb().query(this.getClass(), cls, handle, sqlData.getName(), sql, paras.toArray()); this.getDb().query(this.getClass(), cls, handle, sqlData.getName(), sql, paras.toArray());
} }
...@@ -303,7 +302,7 @@ public abstract class BaseDaoSql { ...@@ -303,7 +302,7 @@ public abstract class BaseDaoSql {
sqlData = getSqlQueryPara(sqlData, queryPara, true); sqlData = getSqlQueryPara(sqlData, queryPara, true);
List<Object> paras = new ArrayList<>(); List<Object> paras = new ArrayList<>();
String sql = this.getQueryPara(paras, sqlData, model); String sql = this.getPara(paras, sqlData, model);
Object cell = this.queryCellWithCache(sqlData.getName(), sql, paras.toArray()); Object cell = this.queryCellWithCache(sqlData.getName(), sql, paras.toArray());
return cell; return cell;
} }
...@@ -525,7 +524,7 @@ public abstract class BaseDaoSql { ...@@ -525,7 +524,7 @@ public abstract class BaseDaoSql {
// 按照分页查询数据 // 按照分页查询数据
List<Object> baseParas = new ArrayList<Object>(); List<Object> baseParas = new ArrayList<Object>();
String sql = this.getQueryPara(baseParas, to, model); String sql = this.getPara(baseParas, to, model);
// 查询实体数据 // 查询实体数据
List<T> list = this.queryWithCache(cls, sqlName, sql, baseParas.toArray()); List<T> list = this.queryWithCache(cls, sqlName, sql, baseParas.toArray());
data.setList(list); data.setList(list);
...@@ -542,7 +541,7 @@ public abstract class BaseDaoSql { ...@@ -542,7 +541,7 @@ public abstract class BaseDaoSql {
// 按照分页查询数据 // 按照分页查询数据
List<Object> baseParas = new ArrayList<Object>(); List<Object> baseParas = new ArrayList<Object>();
String sql = this.getQueryPara(baseParas, fromPageSize, model); String sql = this.getPara(baseParas, fromPageSize, model);
sql = sql.trim(); sql = sql.trim();
// 查询总数据量 // 查询总数据量
String sqlSize = "SELECT COUNT(1) FROM (" + String sqlSize = "SELECT COUNT(1) FROM (" +
...@@ -567,8 +566,7 @@ public abstract class BaseDaoSql { ...@@ -567,8 +566,7 @@ public abstract class BaseDaoSql {
* @param model 参数的实体 * @param model 参数的实体
* @return SQL条件 * @return SQL条件
*/ */
protected String getQueryPara(List<Object> paras, SqlData sqlData, Object model) { protected String getPara(List<Object> paras, SqlData sqlData, Object model) {
// 将SQL语句进行代码片段追加 // 将SQL语句进行代码片段追加
StringBuilder sb = new StringBuilder(sqlData.getSql()); StringBuilder sb = new StringBuilder(sqlData.getSql());
for (String code : DaoConst.LAST_AUTO_ADD) { for (String code : DaoConst.LAST_AUTO_ADD) {
...@@ -581,10 +579,6 @@ public abstract class BaseDaoSql { ...@@ -581,10 +579,6 @@ public abstract class BaseDaoSql {
// 处理字段以及代码片段 // 处理字段以及代码片段
String sql = sb.toString(); String sql = sb.toString();
// 对查询条件进行初始化
if (model instanceof InitDaoQuery) {
((InitDaoQuery) model).initCond();
}
sql = handleCodeRelease(sql, sqlData, model, codeMap); sql = handleCodeRelease(sql, sqlData, model, codeMap);
sql = handleCodeReplace(sqlData, sql, codeMap); sql = handleCodeReplace(sqlData, sql, codeMap);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment