1. 程式人生 > 實用技巧 >Dapper 封裝03-組裝SQL-多條件

Dapper 封裝03-組裝SQL-多條件

在上一篇中。我們可以獲得單條where 語句拼裝的 條件。但是我們往往 where 條件有很多。所以這篇組要來解決多條件問題。我們定義一個類 WhereClip ,其目的主要是講多個單條件的SQL給組裝起來。那麼這個類應該有那些元素呢?我覺得應該含有 完整的SQL和引數列表。

  protected StringBuilder sbWhereSql = new StringBuilder(); 
        protected bool toWrapBrackets = false;
        protected readonly List<DataParameter> parameters = new
List<DataParameter>(); public string WhereSql { get { return sbWhereSql.ToString(); } set { sbWhereSql = new StringBuilder(value); } } private bool fixWhere = true
; /// <summary> /// sql語句前是否新增where /// </summary> public bool FixWhere { get { return fixWhere; } set { fixWhere = value; } } public string
ToSql() { StringBuilder sbSql = new StringBuilder(); if (sbWhereSql.Length > 0) { if (fixWhere) { sbSql.Append(" WHERE "); } else { sbSql.Append(" AND "); } sbSql.Append(WhereSql); } return sbSql.ToString(); } public DynamicParameters GetDynamicParms() { DynamicParameters dynamicParms = null; if (this.parameters != null && this.parameters.Count > 0) { dynamicParms = new DynamicParameters(); List<DataParameter>.Enumerator en = this.parameters.GetEnumerator(); while (en.MoveNext()) { dynamicParms.Add(en.Current.Name, en.Current.Value == null ? DBNull.Value : en.Current.Value); } } return dynamicParms; } internal List<DataParameter> Parameters => this.parameters; public static bool IsNullOrEmpty(WhereClip where) { return ((object)where) == null || where.sbWhereSql.Length == 0; } public WhereClip And(WhereClip whereClip) { if(IsNullOrEmpty(whereClip)) { return this; } AddParameters(whereClip); if (sbWhereSql.Length >0) { sbWhereSql.Append(" AND "); if (whereClip.toWrapBrackets) { sbWhereSql.Append($"({whereClip.WhereSql})"); } else { sbWhereSql.Append($"{whereClip.WhereSql}"); } } else { sbWhereSql.Append(whereClip.WhereSql); } this.toWrapBrackets = true; return this; } public WhereClip Or(WhereClip whereClip) { if (IsNullOrEmpty(whereClip)) { return this; } AddParameters(whereClip); if (sbWhereSql.Length > 0) { sbWhereSql.Append(" OR "); if (whereClip.toWrapBrackets) { sbWhereSql.Append($"({whereClip.WhereSql})"); } else { sbWhereSql.Append($"{whereClip.WhereSql}"); } } else { sbWhereSql.Append(whereClip.WhereSql); } this.toWrapBrackets = true; return this; } public WhereClip Not() { if (IsNullOrEmpty(this)) { return this; } sbWhereSql.Insert(0, "NOT("); sbWhereSql.Append(") "); this.toWrapBrackets = false; return this; } public WhereClip ToWrap() { if (IsNullOrEmpty(this)) { return this; } sbWhereSql.Insert(0, "("); sbWhereSql.Append(") "); this.toWrapBrackets = false; return this; } public WhereClip ApendWhere(string strSubWhere) { sbWhereSql.Append(strSubWhere); return this; } public static WhereClip operator &(WhereClip left, WhereClip right) { WhereClip newWhere = new WhereClip(); newWhere.And(left); newWhere.And(right); return newWhere; } public static WhereClip operator |(WhereClip left, WhereClip right) { WhereClip newWhere = new WhereClip(); newWhere.Or(left); newWhere.Or(right); return newWhere; } public static WhereClip operator !(WhereClip right) { return right.Not(); } private void AddParameters(WhereClip whereClip) { if (whereClip == null || whereClip.Parameters == null) { return; } List<DataParameter> additional = whereClip.Parameters; foreach (DataParameter item in additional) { string propertyName = item.Name; int chkFix = item.Name.IndexOf("_pfix_"); if (chkFix > 0) { propertyName = item.Name.Substring(0, chkFix); } DataParameter targetItem = parameters.FirstOrDefault(m => m.Name == item.Name); if (targetItem == null) { parameters.Add(item); } else { if (item.Value == null || (item.Value != null && targetItem.Value != null && item.Value.Equals(targetItem.Value))) { continue; } string srcParmName = item.Name; int count = parameters.Count(m => m.Name.StartsWith($"{srcParmName}_pfix_")); string newParmName = $"{srcParmName}_pfix_{count + 1}"; item.Name = newParmName; parameters.Add(item); bool chk = additional.Any(m => m.Name == newParmName); string replace = chk ? $"@{newParmName}_pfixtmp " : $"@{newParmName} "; whereClip.WhereSql = whereClip.WhereSql.Replace($"@{srcParmName}", replace); } } whereClip.WhereSql = whereClip.WhereSql.Replace("_pfixtmp", ""); }

這裡面每個 操作 方法 都返回 this 本身。主要是方便連寫。AddParameters 主要解決的 引數列表中出現多個同名的引數。重新命名的問題。

那麼有了上面的方法。我們可以比較方便的呼叫了。