Ejemplo n.º 1
0
        public Relation(ViewsManager manager, string name, string description, TableField masterField, TableField detailsField)
            : this(manager, name, description)
        {
            if (null == masterField)
            {
                throw new ArgumentNullException("masterField");
            }
            if (null == detailsField)
            {
                throw new ArgumentNullException("detailsField");
            }

            this.MasterField  = masterField;
            this.MasterTable  = masterField.Table;
            this.DetailsField = detailsField;
            this.DetailsTable = detailsField.Table;

            if (String.IsNullOrEmpty(name))
            {
                this.Name = String.Format("{0}.{1} TO {2}.{3}", this.MasterTable.Name, this.MasterField.Name, this.DetailsTable.Name, this.DetailsField.Name);
            }

            this.MasterTable.Relations.Add(this);
            this.DetailsTable.Relations.Add(this);
        }
Ejemplo n.º 2
0
        public Relation Add(string name, string description, TableField masterField, TableField detailsField)
        {
            Relation relation = new Relation(this.Manager, name, description, masterField, detailsField);

            Add(relation);
            return(relation);
        }
Ejemplo n.º 3
0
        public FieldSorting(TableField field)
        {
            if (null == field)
            {
                throw new ArgumentNullException("field");
            }

            this.Field = field;
        }
Ejemplo n.º 4
0
        public Relation(ViewsManager manager, string name, string description, TableField masterField, TableField detailsField)
            : this(manager, name, description)
        {
            if (null == masterField) throw new ArgumentNullException("masterField");
            if (null == detailsField) throw new ArgumentNullException("detailsField");

            this.MasterField = masterField;
            this.MasterTable = masterField.Table;
            this.DetailsField = detailsField;
            this.DetailsTable = detailsField.Table;

            if (String.IsNullOrEmpty(name))
                this.Name = String.Format("{0}.{1} TO {2}.{3}", this.MasterTable.Name, this.MasterField.Name, this.DetailsTable.Name, this.DetailsField.Name);

            this.MasterTable.Relations.Add(this);
            this.DetailsTable.Relations.Add(this);
        }
Ejemplo n.º 5
0
        public FieldSorting this[string fieldNameOrAlias]
        {
            get
            {
                if (String.IsNullOrEmpty(fieldNameOrAlias))
                {
                    throw new ArgumentNullException("fieldNameOrAlias");
                }
                TableField field = this.View.Fields.SingleOrDefault(f => f.Alias == fieldNameOrAlias || f.Name == fieldNameOrAlias);
                if (null == field)
                {
                    throw new ArgumentException(String.Format("View '{0}' doesn't have a field '{1}'", this.View.Name, fieldNameOrAlias));
                }

                FieldSorting sortedField = this.SortedFields.SingleOrDefault(sf => sf.Field == field);
                if (null == sortedField)
                {
                    sortedField = new FieldSorting(field);
                    this.SortedFields.Add(sortedField);
                }
                return(sortedField);
            }
        }
Ejemplo n.º 6
0
        public void ToUpdateSql(StringBuilder builder)
        {
            if (null == builder)
            {
                throw new ArgumentNullException("builder");
            }
            if (this.UpdateParameters.Count() == 0)
            {
                throw new InvalidOperationException("No update parameters defined");
            }

            SetUpTokens(true);

            // cmd
            builder.AppendLine("exec sp_executesql N'");
            builder.AppendFormat("update [{0}]", this.Source.Name);
            builder.AppendLine();
            builder.AppendLine("set");

            foreach (string updateParameter in this.UpdateParameters)
            {
                TableField field = this.Source.Fields[updateParameter];
                builder.AppendFormat("    [{0}].[{1}] = {2},", field.Table.Name, field.Name, CastToDbType(this.UpdateParameters[updateParameter], field.DBType, true));
                builder.AppendLine();
            }
            builder.Remove(builder.Length - 3, 3);

            builder.AppendLine();
            builder.AppendLine("from");
            builder.Append("    ");
            builder.AppendFormat("[{0}]", this.Source.Name);
            builder.AppendLine();
            if (this.Relationship.Count > 0)
            {
                this.Relationship.ToSql(builder, this.Source);
            }
            if (this.Filters.Count > 0)
            {
                builder.AppendLine();
                builder.AppendLine("where");
                this.Filters.ToSql(builder);
            }
            builder.Append("'");

            if (!this.OwnParameters.IsEmpty)
            {
                builder.AppendLine(",");

                // parameters declaration
                builder.Append("N'");
                bool firstParameter = true;
                foreach (string parameter in this.OwnParameters)
                {
                    string identifier = this.OwnParameters.SqlIdentifier(parameter);

                    if (!firstParameter)
                    {
                        builder.Append(", ");
                    }
                    else
                    {
                        firstParameter = false;
                    }

                    builder.AppendFormat("{0} varchar(max)", identifier);
                }

                builder.AppendLine("',");

                // parameters values
                firstParameter = true;
                foreach (string parameter in this.OwnParameters)
                {
                    string identifier = this.OwnParameters.SqlIdentifier(parameter);

                    if (!firstParameter)
                    {
                        builder.Append(", ");
                    }
                    else
                    {
                        firstParameter = false;
                    }

                    builder.AppendFormat("{0} = '{1}'", identifier, this.OwnParameters[parameter] ?? "");
                }
            }
        }
Ejemplo n.º 7
0
        public void FromXml(XElement element)
        {
            if (null == element)
            {
                throw new ArgumentNullException("element");
            }
            this.Name        = element.Attribute("name").Value;
            this.Description = element.Attribute("description").Value;
            if (null != element.Attribute("distinct"))
            {
                this.Distinct = bool.Parse(element.Attribute("distinct").Value);
            }

            foreach (XElement fieldElement in element.Element(TableField.XML_KEY).Elements("field"))
            {
                string tableName = fieldElement.Attribute("table").Value;
                string fieldName = fieldElement.Attribute("name").Value;
                if (!this.Manager.Tables.ContainsKey(tableName))
                {
                    throw new ApplicationException(String.Format("Can't find table '{0}' to add it's field '{1}' to the view '{2}'", tableName, fieldName, this.Name));
                }
                Table      table = this.Manager.Tables[tableName];
                TableField field = table.Fields[fieldName];
                if (null == field)
                {
                    throw new ApplicationException(String.Format("Table '{0}' does not contain field '{1}' to add it to the view '{2}'", tableName, fieldName, this.Name));
                }
                if (null != fieldElement.Attribute("alias"))
                {
                    this.Aliases.Add(field, fieldElement.Attribute("alias").Value);
                }
                this.Fields.Add(field);
            }

            XElement computedFieldsElement = element.Element(ComputedField.XML_KEY);

            if (null != computedFieldsElement)
            {
                this.ComputedFields.FromXml(computedFieldsElement);
            }

            if (null != element.Element("source") && null != element.Element("source").Attribute("name"))
            {
                string sourceName = element.Element("source").Attribute("name").Value;
                this.Source = this.Manager.Tables[sourceName];
            }

            foreach (XElement relationElement in element.Element("relationship").Elements("relation"))
            {
                string   relationName = relationElement.Attribute("name").Value;
                Relation relation     = this.Manager.Relations[relationName];
                if (null == relation)
                {
                    throw new ApplicationException(String.Format("Cannot find the relation '{0}' to add it to view '{1}'", relationName, this.Name));
                }
                this.Relationship.Add(relation);
            }

            this.Filters.FromXml(element.Element("filters"));

            if (null != element.Element("parameters"))
            {
                this.OwnParameters.FromXml(element.Element("parameters"));
            }

            XElement subviewsElement = element.Element("subviews");

            if (null != subviewsElement)
            {
                foreach (XElement subviewElement in subviewsElement.Elements("subview"))
                {
                    if (!this.Manager.Views.ContainsKey(subviewElement.Attribute("name").Value))
                    {
                        throw new InvalidOperationException(String.Format("Subview not found. Failed to load view '{0}' at subview '{1}'", this.Name, subviewElement.Attribute("name").Value));
                    }
                    View subview = this.Manager.Views[subviewElement.Attribute("name").Value];
                    Dictionary <string, Field> subviewparams = new Dictionary <string, Field>();
                    foreach (XElement subviewparamElement in subviewElement.Elements("parameter"))
                    {
                        string p = subviewparamElement.Attribute("name").Value;
                        Field  f = null;
                        if (null != subviewparamElement.Attribute("field"))
                        {
                            string fname = subviewparamElement.Attribute("field").Value;
                            if (!String.IsNullOrEmpty(fname))
                            {
                                f = this.AllFields.FirstOrDefault(fld => fld.Name == fname);
                                if (null == f)
                                {
                                    f = this.Aliases.FirstOrDefault(kvp => kvp.Value == fname).Key;
                                }
                            }
                        }
                        subviewparams.Add(p, f);
                    }
                    this.Subviews.Add(subview, subviewparams);
                }
            }
        }
Ejemplo n.º 8
0
        public FieldSorting(TableField field)
        {
            if (null == field) throw new ArgumentNullException("field");

            this.Field = field;
        }
Ejemplo n.º 9
0
 public Relation Add(TableField masterField, TableField detailsField)
 {
     return(Add(null, null, masterField, detailsField));
 }