Пример #1
0
 static SqlExpr SqlFromTmpl(SqlExpr tmpl, SqlSectionExpr newSelect)
 {
     if (tmpl[SqlSectionExpr.Kind.Select] == newSelect)
     {
         return(tmpl);
     }
     return(SqlFromSections(
                newSelect,
                tmpl[SqlSectionExpr.Kind.From],
                tmpl[SqlSectionExpr.Kind.Where],
                tmpl[SqlSectionExpr.Kind.OrderBy],
                tmpl[SqlSectionExpr.Kind.GroupBy]
                ));
 }
Пример #2
0
            internal SqlExpr PostProc(SqlFuncPreprocessingCtx c, SqlExpr sql)
            {
                c.tblAttrs.TryGetValue(Attr.Tbl.AbstractTable, out var objAbstractTable);
                c.tblAttrs.TryGetValue(Attr.Tbl.LookupTableTemplate, out var objLookupTableTemplate);

                var modFunc = c.tblAttrs.TryGetValue(Attr.Tbl.Substance, out var objSubstance)
                    ? ModifyFieldExpr(c, objSubstance.ToString())
                    : (objAbstractTable == null && objLookupTableTemplate == null)
                        ? ModifyFieldExpr(c, null)
                        : (expr, attrs) => expr;

                SqlSectionExpr select = sql[SqlSectionExpr.Kind.Select];

                #region Postprocess SELECT expression: insert inherited fields if needed
                if (c.tblAttrs.TryGetValue(Attr.Tbl._columns_attrs, out var objInnerAttrs))
                {
                    var innerAttrs = (IList <Dictionary <Attr.Col, object> >)objInnerAttrs;
                    var args       = select.args;

                    //bool changed = false;
                    int n        = innerAttrs.Count;
                    var newInner = new List <Dictionary <Attr.Col, object> >(n);
                    var fields   = new List <Expr>(n);

                    for (int i = 0; i < n; i++)
                    {
                        var attrs = innerAttrs[i];
                        #region 'Inherits' attribute processing
                        if (attrs.TryGet(Attr.Col.Inherits, out var objInherits))
                        {   // inherit lot of fields from abstract tables
                            var lst = objInherits as IList;
                            if (lst == null)
                            {
                                lst = new object[] { objInherits }
                            }
                            ;
                            foreach (var aT in lst)
                            {
                                if (!abstracts.TryGetValue(aT.ToString(), out var abstr))
                                {
                                    throw new Generator.Exception($"No one AbstractTable='{aT}' found");
                                }

                                var inheritedFields = abstr.sql[SqlSectionExpr.Kind.Select].args;
                                // inherit fields
                                //changed = true;
                                if (abstr.attrs.TryGetValue(Attr.Tbl._columns_attrs, out var objInners))
                                {
                                    var inners = (Dictionary <Attr.Col, object>[])objInners;
                                    int k      = inheritedFields.Count;
                                    for (int j = 0; j < k; j++)
                                    {
                                        fields.Add(modFunc(inheritedFields[j], inners[j]));
                                    }
                                    // inherit fields attributes
                                    newInner.AddRange(inners);
                                }
                                else
                                {
                                    fields.AddRange(inheritedFields.Select(s => modFunc(s, null)));
                                    // no attributes to inherit
                                    for (int j = inheritedFields.Count - 1; j >= 0; j--)
                                    {
                                        newInner.Add(null);
                                    }
                                }
                            }
                        }
                        #endregion
                        if (i < args.Count)
                        {
                            // add field
                            fields.Add(modFunc(args[i], attrs));
                        }
                        newInner.Add(attrs);
                    }
                    //if (changed)
                    {
                        // inherited fields added, create updated SELECT expression
                        select = new SqlSectionExpr(SqlSectionExpr.Kind.Select, fields);
                        c.tblAttrs[Attr.Tbl._columns_attrs] = newInner.ToArray();
                    }
                }
                else if (objSubstance != null && objAbstractTable == null && objLookupTableTemplate == null)
                {
                    select = new SqlSectionExpr(SqlSectionExpr.Kind.Select, select.args.Select(s => modFunc(s, null)).ToList());
                }
                #endregion

                var newSql = SqlFromTmpl(sql, select);

                if (objAbstractTable != null)
                {   // It is "abstract table", add to abstracts dictionary
                    var abstractTable = objAbstractTable.ToString();
                    abstracts.Add(abstractTable, new SqlInfo()
                    {
                        sql = newSql, attrs = c.tblAttrs
                    });
                    return(null);
                }

                if (objLookupTableTemplate != null)
                {
                    var ltt = objLookupTableTemplate.ToString();
                    templates.Add(ltt, new SqlInfo()
                    {
                        sql = newSql, attrs = c.tblAttrs
                    });
                    return(null);
                }

                return(newSql);
            }
Пример #3
0
        internal static SqlFuncPreprocessingCtx NewCodeLookupDict(SqlFuncPreprocessingCtx src, SqlInfo tmpl,
                                                                  string descriptor, Dictionary <Attr.Col, object> descrFieldAttrs)
        {
            var select = tmpl.sql[SqlSectionExpr.Kind.Select];

            var innerAttrs = new List <Dictionary <Attr.Col, object> >(select.args.Count + 1);

            var sb = new StringBuilder();

            foreach (var kind in SqlSectionExpr.EnumKinds())
            {
                var section = tmpl.sql[kind];

                switch (kind)
                {
                case SqlSectionExpr.Kind.Select:
                {
                    sb.AppendLine("SELECT");

                    var modFunc = src.ldr.ModifyFieldExpr(src, descriptor);
                    var unmFunc = src.ldr.ModifyFieldExpr(src, null);

                    var tmplInnerAttrs = (Dictionary <Attr.Col, object>[])tmpl.attrs[Attr.Tbl._columns_attrs];

                    for (int i = 0; i < select.args.Count; i++)
                    {
                        var attrs = tmplInnerAttrs[i];
                        if (i > 0)
                        {
                            sb.AppendLine(",");
                        }
                        object v;
                        if (Attr.GetBool(attrs, Attr.Col.FixedAlias))
                        {
                            v = unmFunc(select.args[i], attrs);
                        }
                        else
                        {
                            v = modFunc(select.args[i], attrs);
                        }
                        sb.Append($"\t{v}");
                        innerAttrs.Add(attrs);
                    }
                    sb.AppendLine();
                }
                break;

                case SqlSectionExpr.Kind.From:
                    if (section != null)
                    {
                        sb.AppendLine(section.ToString());
                    }
                    else
                    {
                        sb.AppendLine($"FROM {descriptor}");
                    }
                    break;

                default:
                    if (section != null)
                    {
                        sb.AppendLine(section.ToString());
                    }
                    break;
                }
            }


            var tblAttrs = new Dictionary <Attr.Tbl, object>(tmpl.attrs);

            var c = new SqlFuncPreprocessingCtx()
            {
                ldr             = src.ldr,
                actualityInDays = Attr.defaultActualityDays * 10,
                arrayResults    = true,
                funcNamesPrefix = descriptor + "_DictData",
                tblAttrs        = tblAttrs,
                queryText       = sb.ToString(),
            };

            tblAttrs.Remove(Attr.Tbl.LookupTableTemplate);

            string tmplDescr;

            {
                string srcTableComment  = Attr.OneLineText(src.tblAttrs.Get(Attr.Tbl.Description));
                string tmplDescrComment = Attr.OneLineText(descrFieldAttrs.Get(Attr.Col.Description));

                if (tblAttrs.TryGetValue(Attr.Tbl.TemplateDescription, out var objTmplDescr))
                {
                    tmplDescr = string.Format(Convert.ToString(objTmplDescr), descriptor, tmplDescrComment, srcTableComment);
                }
                else
                {
                    tmplDescr = $"Instantiated lookup table for\t{descriptor}\t{tmplDescrComment}\t{srcTableComment}";
                }
            }

            tblAttrs.Remove(Attr.Tbl.TemplateDescription);
            tblAttrs[Attr.Tbl.FuncPrefix] = c.funcNamesPrefix;
            Attr.Add(tblAttrs, Attr.Tbl.Description, tmplDescr, true);
            if (!src.isTimed)
            {
                tblAttrs.Remove(Attr.Tbl.ActualityDays);
            }
            else if (!tblAttrs.ContainsKey(Attr.Tbl.ActualityDays))
            {
                tblAttrs.Add(Attr.Tbl.ActualityDays, c.actualityInDays);
            }
            tblAttrs[Attr.Tbl.ArrayResults]   = true;
            tblAttrs[Attr.Tbl._columns_attrs] = innerAttrs;

            return(c);
        }