mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 05:13:27 +08:00
-
This commit is contained in:
parent
5ad323e993
commit
0260337376
@ -33,11 +33,44 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string WhereInTemplate {
|
public string WhereInTemplate
|
||||||
get {
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
return "{0} IN ({1})";
|
return "{0} IN ({1})";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string WhereInOrTemplate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "OR";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public string WhereInAndTemplate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "AND";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public string WhereInEqualTemplate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "{0}=N'{1}'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string WhereInAreaTemplate
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "({0})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public virtual string GetTableNameString
|
public virtual string GetTableNameString
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -62,7 +95,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
var isFirst = i == 0;
|
var isFirst = i == 0;
|
||||||
whereString += isFirst ? "WHERE " : "AND ";
|
whereString += isFirst ? "WHERE " : "AND ";
|
||||||
whereString +=(item + PubConst.Space);
|
whereString += (item + PubConst.Space);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
return whereString;
|
return whereString;
|
||||||
|
@ -22,24 +22,81 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public IDeleteable<T> Where(List<T> deleteObjs)
|
public IDeleteable<T> Where(List<T> deleteObjs)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
if (deleteObjs == null || deleteObjs.Count() == 0)
|
||||||
|
{
|
||||||
|
Where("1=2 ");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
string tableName = this.Context.GetTableName<T>();
|
||||||
|
var entityInfo = this.Context.EntityProvider.GetEntityInfo<T>();
|
||||||
|
if (this.Context.IsSystemTablesConfig)
|
||||||
|
{
|
||||||
|
var primaryFields = this.Db.DbMaintenance.GetPrimaries(tableName).ToArray();
|
||||||
|
var isSinglePrimaryKey = primaryFields.Length == 1;
|
||||||
|
Check.ArgumentNullException(primaryFields, string.Format("Table {0} with no primarykey", tableName));
|
||||||
|
if (isSinglePrimaryKey)
|
||||||
|
{
|
||||||
|
List<object> primaryKeyValues = new List<object>();
|
||||||
|
var primaryField = primaryFields.Single();
|
||||||
|
foreach (var deleteObj in deleteObjs)
|
||||||
|
{
|
||||||
|
var entityPropertyName = this.Context.GetEntityPropertyName<T>(primaryField);
|
||||||
|
var columnInfo = entityInfo.Columns.Single(it => it.Name == entityPropertyName);
|
||||||
|
var value = columnInfo.PropertyInfo.GetValue(deleteObj, null);
|
||||||
|
primaryKeyValues.Add(value);
|
||||||
|
}
|
||||||
|
var inValueString = primaryKeyValues.ToArray().ToJoinSqlInVals();
|
||||||
|
Where(string.Format(DeleteBuilder.WhereInTemplate, primaryFields.Single(), inValueString));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StringBuilder whereInSql = new StringBuilder();
|
||||||
|
foreach (var deleteObj in deleteObjs)
|
||||||
|
{
|
||||||
|
StringBuilder orString = new StringBuilder();
|
||||||
|
var isFirst = deleteObjs.IndexOf(deleteObj)==0;
|
||||||
|
if (isFirst) {
|
||||||
|
orString.Append(DeleteBuilder.WhereInOrTemplate+PubConst.Space);
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
StringBuilder andString = new StringBuilder();
|
||||||
|
foreach (var primaryField in primaryFields)
|
||||||
|
{
|
||||||
|
if (i == 0)
|
||||||
|
andString.Append(DeleteBuilder.WhereInAndTemplate + PubConst.Space);
|
||||||
|
var entityPropertyName = this.Context.GetEntityPropertyName<T>(primaryField);
|
||||||
|
var columnInfo = entityInfo.Columns.Single(it => it.Name == entityPropertyName);
|
||||||
|
var entityValue = columnInfo.PropertyInfo.GetValue(deleteObj, null);
|
||||||
|
andString.AppendFormat(DeleteBuilder.WhereInEqualTemplate, primaryField, entityValue);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
orString.AppendFormat(DeleteBuilder.WhereInAreaTemplate,andString);
|
||||||
|
whereInSql.Append(orString);
|
||||||
|
}
|
||||||
|
Where(string.Format(DeleteBuilder.WhereInAreaTemplate,whereInSql.ToString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDeleteable<T> Where(Expression<Func<T, bool>> expression)
|
public IDeleteable<T> Where(Expression<Func<T, bool>> expression)
|
||||||
{
|
{
|
||||||
var expResult=DeleteBuilder.GetExpressionValue(expression, ResolveExpressType.WhereSingle);
|
var expResult = DeleteBuilder.GetExpressionValue(expression, ResolveExpressType.WhereSingle);
|
||||||
DeleteBuilder.WhereInfos.Add(expResult.GetResultString());
|
DeleteBuilder.WhereInfos.Add(expResult.GetResultString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDeleteable<T> Where(T deleteObj)
|
public IDeleteable<T> Where(T deleteObj)
|
||||||
{
|
{
|
||||||
string tableName = this.Context.GetTableName<T>();
|
Where(new List<T>() { deleteObj });
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDeleteable<T> Where(string whereString, object whereObj=null)
|
public IDeleteable<T> Where(string whereString, object whereObj = null)
|
||||||
{
|
{
|
||||||
DeleteBuilder.WhereInfos.Add(whereString);
|
DeleteBuilder.WhereInfos.Add(whereString);
|
||||||
if (whereObj != null)
|
if (whereObj != null)
|
||||||
@ -51,7 +108,8 @@ namespace SqlSugar
|
|||||||
|
|
||||||
public IDeleteable<T> In<PkType>(PkType[] primaryKeyValues)
|
public IDeleteable<T> In<PkType>(PkType[] primaryKeyValues)
|
||||||
{
|
{
|
||||||
if (primaryKeyValues == null || primaryKeyValues.Count() == 0) {
|
if (primaryKeyValues == null || primaryKeyValues.Count() == 0)
|
||||||
|
{
|
||||||
Where("1=2 ");
|
Where("1=2 ");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -63,7 +121,8 @@ namespace SqlSugar
|
|||||||
Check.ArgumentNullException(primaryField, "Table " + tableName + " with no primarykey");
|
Check.ArgumentNullException(primaryField, "Table " + tableName + " with no primarykey");
|
||||||
Where(string.Format(DeleteBuilder.WhereInTemplate, primaryField, primaryKeyValues.ToJoinSqlInVals()));
|
Where(string.Format(DeleteBuilder.WhereInTemplate, primaryField, primaryKeyValues.ToJoinSqlInVals()));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -85,7 +144,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
string sql = DeleteBuilder.ToSqlString();
|
string sql = DeleteBuilder.ToSqlString();
|
||||||
var paramters = DeleteBuilder.Parameters.ToList();
|
var paramters = DeleteBuilder.Parameters.ToList();
|
||||||
return new KeyValuePair<string, List<SugarParameter>>(sql,paramters);
|
return new KeyValuePair<string, List<SugarParameter>>(sql, paramters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,11 @@ namespace SqlSugar
|
|||||||
if (checkObj == null)
|
if (checkObj == null)
|
||||||
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
||||||
}
|
}
|
||||||
|
public static void ArgumentNullException(object [] checkObj, string message)
|
||||||
|
{
|
||||||
|
if (checkObj == null|| checkObj.Length==0)
|
||||||
|
throw new SqlSugarException("SqlSugarException.ArgumentNullException:" + message);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Exception(bool isException, string message, params string[] args)
|
public static void Exception(bool isException, string message, params string[] args)
|
||||||
{
|
{
|
||||||
|
@ -335,7 +335,7 @@ namespace SqlSugar
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
|
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
|
||||||
return mappingInfo.DbTableName;
|
return mappingInfo==null?typeName:mappingInfo.DbTableName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal string GetEntityName(string tableName)
|
internal string GetEntityName(string tableName)
|
||||||
@ -344,26 +344,27 @@ namespace SqlSugar
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.DbTableName == tableName);
|
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.DbTableName == tableName);
|
||||||
return mappingInfo.EntityName;
|
return mappingInfo==null?tableName:mappingInfo.EntityName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal string GetDbColumnName<T>(string entityNam)
|
internal string GetDbColumnName<T>(string entityPropertyName)
|
||||||
{
|
{
|
||||||
var typeName = typeof(T).Name;
|
var typeName = typeof(T).Name;
|
||||||
if (this.MappingTables == null || this.MappingTables.Count == 0) return typeName;
|
if (this.MappingColumns == null || this.MappingColumns.Count == 0) return entityPropertyName;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
|
var mappingInfo = this.MappingColumns.SingleOrDefault(it =>it.EntityName==typeName &&it.EntityPropertyName == entityPropertyName);
|
||||||
return mappingInfo.DbTableName;
|
return mappingInfo==null?entityPropertyName:mappingInfo.DbColumnName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal string GetEntityPropertyName(string bbColumnName)
|
internal string GetEntityPropertyName<T>(string dbColumnName)
|
||||||
{
|
{
|
||||||
if (this.MappingTables == null || this.MappingTables.Count == 0) return tableName;
|
var typeName = typeof(T).Name;
|
||||||
|
if (this.MappingColumns == null || this.MappingColumns.Count == 0) return dbColumnName;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.DbTableName == tableName);
|
var mappingInfo = this.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName == dbColumnName);
|
||||||
return mappingInfo.EntityName;
|
return mappingInfo == null ? dbColumnName : mappingInfo.DbColumnName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user