/// <summary> /// 生成条件语句 /// </summary> /// <param name="savePar"></param> /// <param name="CondiIndex"></param> /// <param name="parameters"></param> /// <param name="tryCodeStatementCollection"></param> /// <param name="paramterQuery"></param> /// <param name="sqls"></param> /// <param name="sQLsCount"></param> /// <param name="stringBuilderBlueprint"></param> /// <param name="cONDITION_TYPE"></param> /// <param name="customName"></param> private void CreateCondition( Dictionary <string, string> savePar , int CondiIndex , Dictionary <int, List <Dictionary <PARAMETER, string> > > parameters , CodeStatementCollection tryCodeStatementCollection , IParamterQuery paramterQuery , List <SqlFormat> sqls , int sQLsCount , StringBuilderBlueprint stringBuilderBlueprint , CONDITION_TYPE cONDITION_TYPE , string customName = "") { if (!savePar.ContainsKey($"{CondiIndex}Par{customName}")) { StringBuilderBlueprint stringBuilderBlueprintTmp = new StringBuilderBlueprint($"par{CondiIndex}{customName}"); tryCodeStatementCollection.Add(stringBuilderBlueprintTmp.Create()); for (int parametersChildCount = 0; parametersChildCount < parameters[CondiIndex].Count; parametersChildCount++) { tryCodeStatementCollection.AddRange(paramterQuery.Create( stringBuilderBlueprintTmp , parameters[CondiIndex][parametersChildCount] , cONDITION_TYPE)); } savePar.Add($"{CondiIndex}Par{customName}", stringBuilderBlueprintTmp.Field); } if (sqls[sQLsCount].CondiIndex != -1) { tryCodeStatementCollection.Add(stringBuilderBlueprint.AppendField($"par{CondiIndex}{customName}")); } }
protected override CodeStatementCollection Build(Dictionary <int, List <Dictionary <PARAMETER, string> > > parameters , List <SqlFormat> sQLs , string connection , bool isNotCondition , List <string> ReferencedAssemblies , Type type , string query) { ReferencedAssemblies.Add("LazySQL.dll"); ReferencedAssemblies.Add("LazySQL.SQLite.dll"); ReferencedAssemblies.Add("System.Data.SQLite.dll"); CodeStatementCollection codeStatementCollection = new CodeStatementCollection(); #region 添加所需零件 StringBuilderBlueprint stringBuilderBlueprint = new StringBuilderBlueprint(); SQLLiteTemplateBlueprint sQLiteTemplateBlueprint = new SQLLiteTemplateBlueprint(); #endregion codeStatementCollection.Add(stringBuilderBlueprint.Create()); codeStatementCollection.Add(sQLiteTemplateBlueprint.Create()); TryCatchFinallyBlueprint tryCatchFinallyBlueprint = new TryCatchFinallyBlueprint(); codeStatementCollection.Add(tryCatchFinallyBlueprint.Create(() => { CodeStatementCollection tryCodeStatementCollection = new CodeStatementCollection(); ListBlueprint listBlueprint = new ListBlueprint(); tryCodeStatementCollection.Add(listBlueprint.Create <SQLiteParameter>()); Building(isNotCondition , parameters , tryCodeStatementCollection , new SQLiteParamterQuery(listBlueprint) , type , sQLs , stringBuilderBlueprint , query , connection , sQLiteTemplateBlueprint , listBlueprint); return(tryCodeStatementCollection); })); return(codeStatementCollection); }
/// <summary> /// 代码构成模板 /// </summary> /// <param name="isNotCondition"></param> /// <param name="parameters"></param> /// <param name="tryCodeStatementCollection"></param> /// <param name="paramterQuery"></param> /// <param name="type"></param> /// <param name="sqls"></param> /// <param name="stringBuilderBlueprint"></param> protected void Building( bool isNotCondition , Dictionary <int, List <Dictionary <PARAMETER, string> > > parameters , CodeStatementCollection tryCodeStatementCollection , IParamterQuery paramterQuery , Type type , List <SqlFormat> sqls , StringBuilderBlueprint stringBuilderBlueprint , string query , string connection , ITemplateBlueprint templateBlueprint , ListBlueprint listBlueprint) { Dictionary <string, string> SavePar = new Dictionary <string, string>(); //按顺序添加SQL语句以及条件语句 for (int sQLsCount = 0; sQLsCount < sqls.Count; sQLsCount++) { tryCodeStatementCollection.Add(stringBuilderBlueprint.Append(sqls[sQLsCount].SQLText)); if (!isNotCondition) { int CondiIndex = sqls[sQLsCount].CondiIndex; if (CondiIndex != -1) { CreateCondition(SavePar, CondiIndex, parameters, tryCodeStatementCollection, paramterQuery, sqls, sQLsCount, stringBuilderBlueprint, sqls[sQLsCount].oNDITION_TYPE); } } } //返回值 ReturnBlueprint returnBlueprint = new ReturnBlueprint(); switch (query) { case "select": SelectReturn(connection , tryCodeStatementCollection , templateBlueprint , returnBlueprint , stringBuilderBlueprint , listBlueprint); break; default: DefaultReturn(connection , tryCodeStatementCollection , templateBlueprint , returnBlueprint , stringBuilderBlueprint , listBlueprint); break; } }
protected override void SelectReturn(string connection, CodeStatementCollection codeStatementCollection, ITemplateBlueprint templateBlueprint, ReturnBlueprint returnBlueprint, StringBuilderBlueprint stringBuilderBlueprint, ListBlueprint listBlueprint) { codeStatementCollection.Add(returnBlueprint.ReturnExpress(templateBlueprint.ExecuteDataTable(connection , stringBuilderBlueprint.Field , listBlueprint.Field))); }
public CodeStatementCollection Create(StringBuilderBlueprint sqlStrBlueprint , Dictionary <PARAMETER, string> paramter , CONDITION_TYPE cONDITION_TYPE) { stringBuilderBlueprint = sqlStrBlueprint; #region 属性赋值及其格式化处理 CodeStatementCollection codeStatementCollection = new CodeStatementCollection(); bool needSplit = false; string name; if (paramter.ContainsKey(PARAMETER.NAME)) { name = paramter[PARAMETER.NAME]; } else { throw new Exception("不存在Name属性"); } string fieldName = name.Replace(".", string.Empty); string target; if (paramter.ContainsKey(PARAMETER.TARGET)) { target = paramter[PARAMETER.TARGET]; } else { target = name; } string symbol; if (paramter.ContainsKey(PARAMETER.SYMBOL)) { symbol = paramter[PARAMETER.SYMBOL]; foreach (var c in ConditionSplit) { if (symbol.Equals(c, StringComparison.InvariantCultureIgnoreCase)) { needSplit = true; } } } else { symbol = "="; } string template = string.Empty; List <string> templates = new List <string>(); if (paramter.ContainsKey(PARAMETER.TEMPLATE)) { template = paramter[PARAMETER.TEMPLATE]; if (template.IndexOf("*") == -1) { throw new Exception("在模板中,没有作为标记的'*'符号"); } if (needSplit) { templates = new List <string>(template.Split('*')); } } else { if (needSplit) { throw new Exception("你使用了in,not in这类多参数的传值没有写模板"); } } #endregion //FieldName赋值,方便给子类使用 this.fieldName = fieldName; switch (cONDITION_TYPE) { case CONDITION_TYPE.SET: codeStatementCollection.Add(ToolManager.Instance.ConditionTool.CreateConditionCode($"!string.IsNullOrWhiteSpace({fieldName})", () => { CodeStatementCollection codeStatementCollectionIF = new CodeStatementCollection(); codeStatementCollectionIF.Add(sqlStrBlueprint.Append($"{fieldName} = @{fieldName}ParSET,")); SetTrue(codeStatementCollectionIF); return(codeStatementCollectionIF); })); break; case CONDITION_TYPE.VALUE: codeStatementCollection.Add(sqlStrBlueprint.Append($"@{fieldName}VALUE")); codeStatementCollection.Add(sqlStrBlueprint.Append(" , ")); codeStatementCollection.Add(ToolManager.Instance.ConditionTool.CreateConditionCode($"!string.IsNullOrWhiteSpace({fieldName})", () => { CodeStatementCollection codeStatementCollectionIF = new CodeStatementCollection(); ValueTrue(codeStatementCollectionIF); return(codeStatementCollectionIF); }, () => { CodeStatementCollection codeStatementCollectionElse = new CodeStatementCollection(); ValueFalse(codeStatementCollectionElse); return(codeStatementCollectionElse); })); break; case CONDITION_TYPE.WHERE: codeStatementCollection.Add(ToolManager.Instance.ConditionTool.CreateConditionCode($"!string.IsNullOrWhiteSpace({fieldName})", () => { CodeStatementCollection codeStatementCollectionIF = new CodeStatementCollection(); codeStatementCollectionIF.Add(stringBuilderBlueprint.Append(" AND ")); if (!needSplit) { codeStatementCollectionIF.Add(sqlStrBlueprint.Append($"{target} {symbol} {(string.IsNullOrWhiteSpace(template) ? $"@{fieldName}" : template)}")); ExecuteDataTableNormalBuild(codeStatementCollectionIF); } else { ListBlueprint listBlueprint = new ListBlueprint($"{fieldName}List"); codeStatementCollectionIF.Add(listBlueprint.Create <string>($"{fieldName}.Split(',')")); codeStatementCollectionIF.Add(sqlStrBlueprint.Append($"{target} {symbol} {templates[0]}")); codeStatementCollectionIF.Add(ToolManager.Instance.CircleTool.CreateCircle("int i = 0", $"i < {fieldName}List.Count", "i++", () => { CodeStatementCollection codeStatementCollectionFor = new CodeStatementCollection(); ExecuteDataTableCircleBuild(codeStatementCollectionFor); return(codeStatementCollectionFor); })); codeStatementCollectionIF.Add(sqlStrBlueprint.Append(templates[1])); } return(codeStatementCollectionIF); })); break; } return(codeStatementCollection); }
protected abstract void DefaultReturn(string connection , CodeStatementCollection codeStatementCollection , ITemplateBlueprint templateBlueprint , ReturnBlueprint returnBlueprint , StringBuilderBlueprint stringBuilderBlueprint , ListBlueprint listBlueprint);