This commit is contained in:
sunkaixuan 2017-05-14 12:27:42 +08:00
parent 5ad323e993
commit 0260337376
4 changed files with 119 additions and 21 deletions

View File

@ -33,11 +33,44 @@ namespace SqlSugar
}
}
public string WhereInTemplate {
get {
public string WhereInTemplate
{
get
{
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
{
get
@ -62,7 +95,7 @@ namespace SqlSugar
{
var isFirst = i == 0;
whereString += isFirst ? "WHERE " : "AND ";
whereString +=(item + PubConst.Space);
whereString += (item + PubConst.Space);
++i;
}
return whereString;

View File

@ -22,24 +22,81 @@ namespace SqlSugar
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)
{
var expResult=DeleteBuilder.GetExpressionValue(expression, ResolveExpressType.WhereSingle);
var expResult = DeleteBuilder.GetExpressionValue(expression, ResolveExpressType.WhereSingle);
DeleteBuilder.WhereInfos.Add(expResult.GetResultString());
return this;
}
public IDeleteable<T> Where(T deleteObj)
{
string tableName = this.Context.GetTableName<T>();
Where(new List<T>() { deleteObj });
return this;
}
public IDeleteable<T> Where(string whereString, object whereObj=null)
public IDeleteable<T> Where(string whereString, object whereObj = null)
{
DeleteBuilder.WhereInfos.Add(whereString);
if (whereObj != null)
@ -51,7 +108,8 @@ namespace SqlSugar
public IDeleteable<T> In<PkType>(PkType[] primaryKeyValues)
{
if (primaryKeyValues == null || primaryKeyValues.Count() == 0) {
if (primaryKeyValues == null || primaryKeyValues.Count() == 0)
{
Where("1=2 ");
return this;
}
@ -63,7 +121,8 @@ namespace SqlSugar
Check.ArgumentNullException(primaryField, "Table " + tableName + " with no primarykey");
Where(string.Format(DeleteBuilder.WhereInTemplate, primaryField, primaryKeyValues.ToJoinSqlInVals()));
}
else {
else
{
}
return this;
@ -85,7 +144,7 @@ namespace SqlSugar
{
string sql = DeleteBuilder.ToSqlString();
var paramters = DeleteBuilder.Parameters.ToList();
return new KeyValuePair<string, List<SugarParameter>>(sql,paramters);
return new KeyValuePair<string, List<SugarParameter>>(sql, paramters);
}
}
}

View File

@ -25,6 +25,11 @@ namespace SqlSugar
if (checkObj == null)
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)
{

View File

@ -335,7 +335,7 @@ namespace SqlSugar
else
{
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
return mappingInfo.DbTableName;
return mappingInfo==null?typeName:mappingInfo.DbTableName;
}
}
internal string GetEntityName(string tableName)
@ -344,26 +344,27 @@ namespace SqlSugar
else
{
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;
if (this.MappingTables == null || this.MappingTables.Count == 0) return typeName;
if (this.MappingColumns == null || this.MappingColumns.Count == 0) return entityPropertyName;
else
{
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.EntityName == typeName);
return mappingInfo.DbTableName;
var mappingInfo = this.MappingColumns.SingleOrDefault(it =>it.EntityName==typeName &&it.EntityPropertyName == entityPropertyName);
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
{
var mappingInfo = this.MappingTables.SingleOrDefault(it => it.DbTableName == tableName);
return mappingInfo.EntityName;
var mappingInfo = this.MappingColumns.SingleOrDefault(it => it.EntityName == typeName && it.DbColumnName == dbColumnName);
return mappingInfo == null ? dbColumnName : mappingInfo.DbColumnName;
}
}
#endregion