public static Table CreateSchema(CodeTypeDeclaration ctd, CodeCompileUnit cu, List <CodeMemberField> lFieldMembers)
        {
            Table t = new Table();

            t.Name      = Format.CamelCaseId(ctd.Name);
            t.Namespace = cu.Namespaces[0].Name;

            List <CodeMemberField> publicFields = lFieldMembers.Where(fm => fm.Attributes == MemberAttributes.Public).ToList();

            foreach (CodeMemberField cmf in publicFields)
            {
                string           fName = ConvertTypes.GetNameFromCodeMemberField(cmf);
                Components.Field f     = new Components.Field();

                //if (fName == "Metadata" && t.Name == "TGlobalElementWithMetadata")
                //{ }

                if (fName == "Id")
                {
                    fName = t.Name + "Id";

                    f.Name                  = fName;
                    f.IsPrimary             = true;
                    f.IsClustered           = true;
                    f.IdentitySpecification = new IdentitySpecification();
                    f.AllowNull             = false;
                }
                else if (fName.ToUpper() == "ID")
                {
                    if (t.Fields.Count(fi => fi.IsPrimary == true) == 0)
                    {
                        f.IsPrimary             = true;
                        f.IsClustered           = true;
                        f.IdentitySpecification = new IdentitySpecification();
                        f.AllowNull             = false;
                    }
                    fName = Format.CamelCaseId(fName);
                }
                else if (fName.Length > 1 && fName.Substring(fName.Length - 2, 2).ToUpper() == "ID")
                {
                    if (t.Fields.Count(fi => fi.IsPrimary == true) == 0)
                    {
                        f.IsPrimary             = true;
                        f.IsClustered           = true;
                        f.IdentitySpecification = new IdentitySpecification();
                        f.AllowNull             = false;
                    }
                    fName = Format.CamelCaseId(fName);
                }
                else
                {
                    f.Name = fName;
                }

                Table tTemp = ConvertTypes.SQLToBase(t, f, cmf.Type, cmf, ctd);
                if (tTemp != null)
                {
                    t = tTemp;
                    //Catch un-named fields, code error only. Not possible to complie XSD with empty field names
                    if (t.Fields.Count(ft => ft.Name == null) > 0)
                    {
                    }
                }
            }
            return(t);
        }
Beispiel #2
0
        public static Table CreateSchema(CodeTypeDeclaration ctd, CodeCompileUnit cu, List <CodeMemberField> lFieldMembers)
        {
            Table t = new Table();

            t.Name      = Format.CamelCaseId(ctd.Name);
            t.Namespace = cu.Namespaces[0].Name;

            Components.Field pkF = new Components.Field()
            {
                Name                  = t.Name + "Id",
                IsPrimary             = true,
                IsClustered           = true,
                IdentitySpecification = new IdentitySpecification(),
                AllowNull             = false,
            };

            if (lFieldMembers.Count < 255)
            {
                pkF.DataType = new DataType()
                {
                    IsNullable = false,
                    Name       = SQLCondensedDataType.TINYINT.ToString(),
                    IsBaseType = true,
                    IsList     = false
                };
            }
            else if (lFieldMembers.Count < 32767)
            {
                pkF.DataType = new DataType()
                {
                    IsNullable = false,
                    Name       = SQLCondensedDataType.SMALLINT.ToString(),
                    IsBaseType = true,
                    IsList     = false
                };
            }
            else if (lFieldMembers.Count < 2147483647)
            {
                pkF.DataType = new DataType()
                {
                    IsNullable = false,
                    Name       = SQLCondensedDataType.INT.ToString(),
                    IsBaseType = true,
                    IsList     = false
                };
            }
            //else if (lFieldMembers.Count < 9223372036854775807)
            //{
            //    pkF.DataType = new DataType()
            //    {
            //        IsNullable = false,
            //        Name = SQLCondensedDataType.BIGINT.ToString(),
            //        IsBaseType = true,
            //        IsList = false
            //    };
            //}
            else
            {
                pkF.DataType = new DataType()
                {
                    IsNullable = false,
                    Name       = SQLCondensedDataType.UNIQUEIDENTIFIER.ToString(),
                    IsBaseType = true,
                    IsList     = false
                };
            }
            t.Fields.Add(pkF);

            //Get data values
            Components.Field f = new Components.Field()
            {
                Name      = "Value",
                IsPrimary = false,
                AllowNull = false,
            };

            //Determine data type
            if (lFieldMembers.Count(m => Utils.IsNumber(m.Name)) == lFieldMembers.Count)
            {
                //All enum values are numbers
            }
            else
            {
                int longest = lFieldMembers.Select(m => m.Name)
                              .Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur).Length;
                if (longest < 4000)
                {
                    //NVARCHAR(longest)
                    f.DataType = new DataType()
                    {
                        Name       = SQLCondensedDataType.NVARCHAR.ToString(),
                        IsBaseType = true,
                        IsNullable = false,
                        IsList     = false,
                        Para1      = longest
                    };
                }
                else
                {
                    //NVARCHAR(MAX)
                    f.DataType = new DataType()
                    {
                        Name       = SQLCondensedDataType.NVARCHAR.ToString(),
                        IsBaseType = true,
                        IsNullable = false,
                        IsList     = false,
                        Para1      = 5000
                    };
                }
            }


            t.Fields.Add(f);

            return(t);
        }