示例#1
0
        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);
        }
示例#2
0
        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);
        }