Example #1
0
        public Database Create(DatabaseSchema databaseSchema)
        {
            if (File.Exists(settings.MappingFile))
            {
                _database = Dbml.CopyWithFilledInDefaults(Dbml.FromFile(settings.MappingFile));
            }
            else
            {
                _database = new Database();
            }

            Database.Name = databaseSchema.Name;
            CreateContext(databaseSchema);

            _enumDatabase = new DbmlEnum.Database {
                Name = databaseSchema.Name
            };
            _existingEnumDatabase = DbmlEnum.Database.DeserializeFromFile(EnumXmlFileName) ?? new DbmlEnum.Database();

            foreach (TableSchema t in databaseSchema.Tables)
            {
                if (Settings.IsIgnored(t.FullName))
                {
                    Debug.WriteLine("Skipping Table: " + t.FullName);
                }
                else if (Settings.IsEnum(t))
                {
                    Debug.WriteLine("Getting Enum Table: " + t.FullName);
                    GetEnum(t);
                }
                else
                {
                    Debug.WriteLine("Getting Table Schema: " + t.FullName);
                    GetTable(t);
                }
                OnSchemaItemProcessed(t.FullName);
            }

            if (Settings.IncludeViews)
            {
                foreach (ViewSchema v in databaseSchema.Views)
                {
                    if (Settings.IsIgnored(v.FullName))
                    {
                        Debug.WriteLine("Skipping View: " + v.FullName);
                    }
                    else
                    {
                        Debug.WriteLine("Getting View Schema: " + v.FullName);
                        CreateView(v);
                    }
                    OnSchemaItemProcessed(v.FullName);
                }
            }

            if (Settings.IncludeFunctions)
            {
                foreach (CommandSchema c in databaseSchema.Commands)
                {
                    if (Settings.IsIgnored(c.FullName))
                    {
                        Debug.WriteLine("Skipping Function: " + c.FullName);
                    }
                    else
                    {
                        Debug.WriteLine("Getting Function Schema: " + c.FullName);
                        CreateFunction(c);
                    }
                    OnSchemaItemProcessed(c.FullName);
                }
            }

            //sync tables
            RemoveExtraMembers(_database);

            _database.Tables.Sort();
            Dbml.ToFile(Dbml.CopyWithNulledOutDefaults(_database),
                        settings.MappingFile);

            if (_enumDatabase.Enums.Count > 0 || File.Exists(EnumXmlFileName))
            {
                _enumDatabase.SerializeToFile(EnumXmlFileName);
            }

            return(_database);
        }
        /// <summary>
        ///Renders the code as defined in the source script file.
        ///</summary>
        ///<param name="function"></param>
        ///<param name="type"></param>
        public virtual void RenderFunctionResult(Function function, Dbml.Type type)
        {
            base.Output.Write("\t");
            base.Output.Write( GetAccessModifier(function.AccessModifier) );
            base.Output.Write(" partial class ");
            base.Output.Write( type.Name );
            base.Output.Write("\r\n\t{\r\n");
 RenderTypePrivateProperties(type); 
            base.Output.Write("\t\t\r\n\t\tpublic ");
            base.Output.Write( type.Name );
            base.Output.Write("()\r\n\t\t{\r\n\t\t}\r\n");
  RenderFunctionResultProperties(function); 
            base.Output.Write("\r\n\t}");
            base.Output.WriteLine();
        }
 /// <summary>
 ///Renders the code as defined in the source script file.
 ///</summary>
 ///<param name="type"></param>
 ///<param name="column"></param>
 public virtual void RenderFunctionResultProperty(Dbml.Type type, Column column)
 {
     base.Output.Write("\t\t[Column(");
     base.Output.Write( GetFunctionResultPropertyAttributes(type, column) );
     base.Output.Write(")]\r\n\t\tpublic ");
     base.Output.Write( ColumnType(column) );
     base.Output.Write(" ");
     base.Output.Write( column.Name );
     base.Output.Write("\r\n\t\t{\r\n\t\t\tget\r\n\t\t\t{\r\n\t\t\t\treturn this._");
     base.Output.Write( column.Name );
     base.Output.Write(";\r\n\t\t\t}\r\n\t\t\tset\r\n\t\t\t{\r\n\t\t\t\tif ((this._");
     base.Output.Write( column.Name );
     base.Output.Write(" != value))\r\n\t\t\t\t{\r\n\t\t\t\t\tthis._");
     base.Output.Write( column.Name );
     base.Output.Write(" = value;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}");
     base.Output.WriteLine();
 }
 /// <summary>
 /// Renders all the columns and relationships of a table as private properties.
 /// </summary>
 private void RenderTypePrivateProperties(Dbml.Type type)
 {
     foreach (object o in type.Items)
     {
         if (o is Column)
             RenderColumnAsPrivateProperty((Column)o);
         if (o is Association)
             RenderAssociationAsPrivateProperty((Association)o);
     }
 }
        /// <summary>
        /// Tests whether the <c>CanBeNull</c> attribute should be added to the property derived from a column.
        /// </summary>
        private bool ShouldRenderCanBeNull(Dbml.Type t, Column column)
        {
            if (!column.CanBeNullSpecified)
                return false;
            if (column.CanBeNull && column.DbType != "Image")
                return false;

            if (column.DbType.StartsWith("Bit NOT NULL") ||
                column.DbType.StartsWith("Decimal") ||
                column.DbType.StartsWith("Image") ||
                column.DbType.StartsWith("Money NOT NULL") ||
                column.DbType.StartsWith("SmallInt NOT NULL") ||
                column.DbType.StartsWith("Int NOT NULL") ||
                column.DbType.StartsWith("Real NOT NULL"))
            {
                // seems like a bug in MS's implementation. But since the purpose is to mimic perfectly... Change this if necessary.
                return false;
            }

            System.Type type = System.Type.GetType(column.Type, false);
            foreach (object o in t.Items)
            {
                Association association = o as Association;
                if (association == null)
                    continue;
                if (!association.IsForeignKey)
                    continue;

                if (!string.IsNullOrEmpty(association.OtherKey))
                {
                    if ((new List<string>(association.OtherKey.Split(',')).Contains(column.Name)))
                        return type == null || !type.IsValueType;
                }
                if (!string.IsNullOrEmpty(association.ThisKey))
                {
                    if ((new List<string>(association.ThisKey.Split(',')).Contains(column.Name)))
                        return type == null || !type.IsValueType;
                }
            }
            if (!column.IsPrimaryKey)
                return true;
            return type == null || !type.IsValueType;
        }
 /// <summary>
 /// Renderes the atributes associated with a property generated
 /// from the field returned by a stored procedure.
 /// </summary>
 private string GetFunctionResultPropertyAttributes(Dbml.Type type, Column column)
 {
     StringBuilder sb = new StringBuilder();
     string storage = string.IsNullOrEmpty(column.Storage) ? column.Name : column.Storage;
     sb.AppendFormat(@"Storage=""_{0}""", storage);
     sb.AppendFormat(@", DbType=""{0}""", column.DbType);
     if (ShouldRenderCanBeNull(type, column))
     {
         sb.AppendFormat(@", CanBeNull={0}", column.CanBeNull.ToString().ToLower());
     }
     return sb.ToString();
 }
Example #7
0
 public void SetThisKey(string[] columnNames)
 {
     ThisKey = Dbml.BuildKeyField(columnNames);
 }
Example #8
0
 public void SetOtherKey(string[] columnNames)
 {
     OtherKey = Dbml.BuildKeyField(columnNames);
 }
Example #9
0
 public string[] GetThisKey()
 {
     return(Dbml.ParseKeyField(ThisKey));
 }
Example #10
0
 public string[] GetOtherKey()
 {
     return(Dbml.ParseKeyField(OtherKey));
 }