Example #1
0
 public void AddRequiredAttribute(IAttributeModel attributeModel)
 {
     if (attributeModel.Owner.Model.Id != this.Model.Id)
     {
         throw new Exception("Try to add unknown attribute to required");
     }
     // HACK: Это ужасно. Добавление в необходимые для зачитки с NULL
     this.RequiredAttributes.Add(new AStep(attributeModel.Name));
 }
Example #2
0
 public static void BuildStatString(IAttributeModel attribute, StringBuilder builder, StatDef def)
 {
     try
     {
         if (def.Worker?.IsDisabledFor(attribute.Model.Base) ?? true)
         {
             return;
         }
         builder.AppendLine($"{def.LabelCap}: {def.ValueToString(attribute.Model.Base.GetStatValue(def))}");
     }
     catch (Exception exception) { Mod.HandleWarning(exception); }
 }
Example #3
0
        public override SqlComputedString ToSql()
        {
            if (this.attributeModel == null)
            {
                if (this.attributePath != null)
                {
                    this.attributeModel = this.attributePath.ProcessElement(this.Owner);
                }
                else
                {
                    this.attributeModel = this.Owner.GetAttribute(attributeName);
                }
            }

            string qr = string.Format("{0} {1} @{2}", attributeModel.Mapping.ColumnName, OperatorProcessor.OperatorToString(this.operatorType), attributeModel.Mapping.AttributeName);
            Dictionary <string, object> sqlParams = new Dictionary <string, object>()
            {
                { "@" + attributeModel.Name, this.value }
            };


            return(new SqlComputedString(qr, sqlParams));
        }
Example #4
0
        private static IAttributeModel ProcessPath(List <IAttributeStep> steps, IObjectInstance ownerInstance)
        {
            foreach (IAttributeStep step in steps)
            {
                step.Attribute = ownerInstance.GetAttribute(step.Name);
                IAttributeModel attribute = step.Attribute;

                if (steps.Count > 1 && attribute is IReferenceAttribute)
                {
                    //TODO: rewrite this
                    List <IAttributeStep> tempSteps = new List <IAttributeStep>();
                    for (int i = steps.IndexOf(step) + 1; i < steps.Count; i++)
                    {
                        tempSteps.Add(steps[i]);
                    }
                    return(PathProcessor.ProcessPath(tempSteps, ((IReferenceAttribute)attribute).Field.Reference));
                }
                else
                {
                    return(attribute);
                }
            }
            return(null);
        }
Example #5
0
        protected virtual void ParseQuery()
        {
            if (this.ReturnedAttributes.Count > 0)
            {
                foreach (IPathElement path in this.ReturnedAttributes)
                {
                    this.ownerInstance.AddRequiredAttribute(path.ProcessElement(this.ownerInstance));
                }
            }

            #region WHERE
            StringBuilder whereStmnt = new StringBuilder();

            // First, parse where statment
            if (this._criteria != null)
            {
                SqlComputedString computedString = this._criteria.ToSql();

                if (!string.IsNullOrEmpty(computedString.Query))
                {
                    this.QueryParameters = computedString.SqlParameters;

                    whereStmnt.AppendFormat("WHERE {0}", computedString.Query);
                }
            }

            #endregion

            StringBuilder showedColumns = new StringBuilder();

            #region Joins
            StringBuilder joinStmnt = new StringBuilder();

            int tableNum = 0;

            foreach (IPathElement pathEl in this.ownerInstance.RequiredAttributes)
            {
                if (pathEl is IAttributePath)
                {
                    foreach (IAttributeStep step in ((IAttributePath)pathEl).Steps)
                    {
                        if (step.Attribute is IReferenceAttribute)
                        {
                            IReferenceAttribute refAttr = (IReferenceAttribute)step.Attribute;

                            tableNum++;

                            string joinTableName = "t" + tableNum;

                            joinStmnt.AppendFormat("LEFT JOIN {0} AS {1} ON {2}={3}",
                                                   refAttr.Field.Reference.Table.FullName,
                                                   joinTableName,
                                                   joinTableName + "." + step.Attribute.Mapping.ColumnName,
                                                   refAttr.Field.Reference.PrimaryKey.Mapping.ColumnName
                                                   );
                            showedColumns.AppendFormat("[{0}].[{1}],", joinTableName, step.Attribute.Owner.PrimaryKey.Mapping.ColumnName);
                        }
                    }
                }
                else
                {
                    showedColumns.AppendFormat("[{0}],", pathEl.ProcessElement(this.ownerInstance).Mapping.ColumnName);
                }
            }

            #endregion

            #region Order by

            StringBuilder orderStmnt = new StringBuilder();

            if (this.OrderBy != null)
            {
                this.orderByAttribute = this.OrderBy.ProcessElement(this.ownerInstance);

                string orderType = string.Empty;

                switch (this.OrderType)
                {
                case OrderType.DESC:
                    orderType = "DESC";
                    break;

                case OrderType.ASC:
                    orderType = "ASC";
                    break;
                }

                orderStmnt.AppendFormat("ORDER BY {0}.{1} {2}", this.orderByAttribute.Owner.Table.FullName, this.orderByAttribute.Mapping.ColumnName, orderType);
            }

            #endregion

            #region Top

            string topStmnt = this.Top == 0 ? string.Empty : string.Format("TOP {0}", this.Top);

            #endregion

            #region SELECT

            StringBuilder selectStmnt = new StringBuilder();
            selectStmnt.AppendFormat("SELECT {0} {1} FROM {2} {3} {4} {5}",
                                     topStmnt,
                                     showedColumns.ToString().TrimEnd(','),
                                     this.ownerInstance.Table.FullName,
                                     joinStmnt.ToString(),
                                     whereStmnt.ToString(),
                                     orderStmnt.ToString()
                                     );

            this.QueryString = selectStmnt.ToString();

            #endregion
        }
Example #6
0
 public ValueCondition(IAttributeModel attributeModel, object value, OperatorType operatorType = OperatorType.Equal)
 {
     this.attributeModel = attributeModel;
     this.value          = value;
     this.operatorType   = operatorType;
 }
Example #7
0
        public override void Parse()
        {
            try
            {
                XDocument modelDoc = XDocument.Parse(this.Model.Description);

                string pkName = modelDoc.Root.Attribute("PrimaryKey").Value;

                List <XElement> fields  = modelDoc.XPathSelectElements("ObjectModel/Fields/*").ToList();
                List <XElement> mapping = modelDoc.XPathSelectElements("ObjectModel/Mapping/*").ToList();

                #region Attributes creation. Section "FIELD"
                foreach (XElement singleField in fields)
                {
                    string attributeName = singleField.Attribute("Name").Value as string;

                    if (string.IsNullOrEmpty(attributeName))
                    {
                        throw new Exception(string.Format("В описании объекта {0} есть неназванный атрибут", this.ModelName));
                    }

                    if (this.Attributes.ContainsKey(attributeName))
                    {
                        throw new Exception(string.Format("Атрибут с именем {0} уже находится в коллекции аттрибутов объекта {1}", attributeName, this.ModelName));
                    }

                    string attributeType = singleField.Name.LocalName;

                    IAttributeModel newAttributeInstance = this.CreateAttribute(attributeName, attributeType, singleField.ToString());
                    if (newAttributeInstance == null)
                    {
                        throw new Exception();
                    }

                    this.Attributes.Add(attributeName, newAttributeInstance);

                    if (newAttributeInstance.Name == pkName)
                    {
                        this.PrimaryKey = this.Attributes[pkName];
                    }
                }
                #endregion

                #region Attribute mappings creation. Section "MAPPING"

                XElement mappingSection = modelDoc.XPathSelectElement("ObjectModel/Mapping");

                this.Table = new DbTableInfo(mappingSection.Attribute("Table").Value, mappingSection.Attribute("Prefix") == null ? string.Empty : mappingSection.Attribute("Prefix").Value);

                foreach (XElement singleMapping in mapping)
                {
                    XAttribute src      = singleMapping.Attribute("Source");
                    XAttribute attrName = singleMapping.Attribute("Attribute");
                    if (src == null || attrName == null)
                    {
                        IXmlLineInfo mappingInfo = singleMapping;
                        throw new Exception(string.Format("Маппирование объекта {0} неверно. Строка №{1}", this.ModelName, mappingInfo.LineNumber));
                    }

                    string attributeName = attrName.Value as string;
                    string sourceName    = src.Value as string;

                    if (string.IsNullOrEmpty(attributeName) || string.IsNullOrEmpty(sourceName))
                    {
                        continue;
                    }

                    this.Attributes[attributeName].Mapping = this.CreateAttributeMapping(attributeName, sourceName);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }