private static void SetupAuditTables(IDatabase Key, Schema.Default.Database.Database TempDatabase)
        {
            Contract.Requires <ArgumentNullException>(Key != null, "Key");
            Contract.Requires <ArgumentNullException>(TempDatabase != null, "TempDatabase");
            Contract.Requires <ArgumentNullException>(TempDatabase.Tables != null, "TempDatabase.Tables");
            if (!Key.Audit)
            {
                return;
            }
            var TempTables = new List <ITable>();

            foreach (ITable Table in TempDatabase.Tables)
            {
                TempTables.Add(SetupAuditTables(Table));
                SetupInsertUpdateTrigger(Table);
                SetupDeleteTrigger(Table);
            }
            TempDatabase.Tables.Add(TempTables);
        }
        /// <summary>
        /// Sets up the specified database schema
        /// </summary>
        /// <param name="Mappings">The mappings.</param>
        /// <param name="Database">The database.</param>
        /// <param name="QueryProvider">The query provider.</param>
        public void Setup(ListMapping <IDatabase, IMapping> Mappings, IDatabase Database, QueryProvider.Manager QueryProvider)
        {
            ISourceInfo TempSource   = SourceProvider.GetSource(Database.Name);
            var         TempDatabase = new Schema.Default.Database.Database(Regex.Match(TempSource.Connection, "Initial Catalog=(.*?;)").Value.Replace("Initial Catalog=", "").Replace(";", ""));

            SetupTables(Mappings, Database, TempDatabase);
            SetupJoiningTables(Mappings, Database, TempDatabase);
            SetupAuditTables(Database, TempDatabase);

            foreach (ITable Table in TempDatabase.Tables)
            {
                Table.SetupForeignKeys();
            }
            List <string> Commands = GenerateSchema(TempDatabase, SourceProvider.GetSource(Database.Name)).ToList();
            IBatch        Batch    = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));

            for (int x = 0; x < Commands.Count; ++x)
            {
                if (Commands[x].ToUpperInvariant().Contains("CREATE DATABASE"))
                {
                    QueryProvider.Batch(SourceProvider.GetSource(Regex.Replace(SourceProvider.GetSource(Database.Name).Connection, "Initial Catalog=(.*?;)", ""))).AddCommand(null, null, CommandType.Text, Commands[x]).Execute();
                }
                else if (Commands[x].Contains("CREATE TRIGGER") || Commands[x].Contains("CREATE FUNCTION"))
                {
                    if (Batch.CommandCount > 0)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                    if (x < Commands.Count - 1)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                }
                else
                {
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                }
            }
            Batch.Execute();
        }
        /// <summary>
        /// Sets up the specified database schema
        /// </summary>
        /// <param name="Mappings">The mappings.</param>
        /// <param name="Database">The database.</param>
        /// <param name="QueryProvider">The query provider.</param>
        public void Setup(ListMapping<IDatabase, IMapping> Mappings, IDatabase Database, QueryProvider.Manager QueryProvider)
        {
            ISourceInfo TempSource = SourceProvider.GetSource(Database.Name);
            var TempDatabase = new Schema.Default.Database.Database(Regex.Match(TempSource.Connection, "Initial Catalog=(.*?;)").Value.Replace("Initial Catalog=", "").Replace(";", ""));
            SetupTables(Mappings, Database, TempDatabase);
            SetupJoiningTables(Mappings, Database, TempDatabase);
            SetupAuditTables(Database, TempDatabase);

            foreach (ITable Table in TempDatabase.Tables)
            {
                Table.SetupForeignKeys();
            }
            List<string> Commands = GenerateSchema(TempDatabase, SourceProvider.GetSource(Database.Name)).ToList();
            IBatch Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
            for (int x = 0; x < Commands.Count; ++x)
            {
                if (Commands[x].ToUpperInvariant().Contains("CREATE DATABASE"))
                {
                    QueryProvider.Batch(SourceProvider.GetSource(Regex.Replace(SourceProvider.GetSource(Database.Name).Connection, "Initial Catalog=(.*?;)", ""))).AddCommand(null, null, CommandType.Text, Commands[x]).Execute();
                }
                else if (Commands[x].Contains("CREATE TRIGGER") || Commands[x].Contains("CREATE FUNCTION"))
                {
                    if (Batch.CommandCount > 0)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                    if (x < Commands.Count - 1)
                    {
                        Batch.Execute();
                        Batch = QueryProvider.Batch(SourceProvider.GetSource(Database.Name));
                    }
                }
                else
                {
                    Batch.AddCommand(null, null, CommandType.Text, Commands[x]);
                }
            }
            Batch.Execute();
        }
 private static void SetupTables(ListMapping <IDatabase, IMapping> Mappings, IDatabase Key, Schema.Default.Database.Database TempDatabase)
 {
     Contract.Requires <NullReferenceException>(Mappings != null, "Mappings");
     foreach (IMapping Mapping in Mappings[Key])
     {
         TempDatabase.AddTable(Mapping.TableName);
         SetupProperties(TempDatabase[Mapping.TableName], Mapping);
     }
 }
        private static void SetupJoiningTablesEnumerable(ListMapping <IDatabase, IMapping> Mappings, IMapping Mapping, IProperty Property, IDatabase Key, Schema.Default.Database.Database TempDatabase)
        {
            Contract.Requires <ArgumentNullException>(TempDatabase != null, "TempDatabase");
            Contract.Requires <ArgumentNullException>(TempDatabase.Tables != null, "TempDatabase.Tables");
            if (TempDatabase.Tables.FirstOrDefault(x => x.Name == Property.TableName) != null)
            {
                return;
            }
            IMapping MapMapping = Mappings[Key].FirstOrDefault(x => x.ObjectType == Property.Type);

            if (MapMapping == null)
            {
                return;
            }
            if (MapMapping == Mapping)
            {
                TempDatabase.AddTable(Property.TableName);
                TempDatabase[Property.TableName].AddColumn("ID_", DbType.Int32, 0, false, true, true, true, false, "", "", "");
                TempDatabase[Property.TableName].AddColumn(Mapping.TableName + Mapping.IDProperties.First().FieldName,
                                                           Mapping.IDProperties.First().Type.To(DbType.Int32),
                                                           Mapping.IDProperties.First().MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           Mapping.TableName,
                                                           Mapping.IDProperties.First().FieldName,
                                                           "",
                                                           false,
                                                           false,
                                                           false);
                TempDatabase[Property.TableName].AddColumn(MapMapping.TableName + MapMapping.IDProperties.First().FieldName + "2",
                                                           MapMapping.IDProperties.First().Type.To(DbType.Int32),
                                                           MapMapping.IDProperties.First().MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           MapMapping.TableName,
                                                           MapMapping.IDProperties.First().FieldName,
                                                           "",
                                                           false,
                                                           false,
                                                           false);
            }
            else
            {
                TempDatabase.AddTable(Property.TableName);
                TempDatabase[Property.TableName].AddColumn("ID_", DbType.Int32, 0, false, true, true, true, false, "", "", "");
                TempDatabase[Property.TableName].AddColumn(Mapping.TableName + Mapping.IDProperties.First().FieldName,
                                                           Mapping.IDProperties.First().Type.To(DbType.Int32),
                                                           Mapping.IDProperties.First().MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           Mapping.TableName,
                                                           Mapping.IDProperties.First().FieldName,
                                                           "",
                                                           true,
                                                           false,
                                                           false);
                TempDatabase[Property.TableName].AddColumn(MapMapping.TableName + MapMapping.IDProperties.First().FieldName,
                                                           MapMapping.IDProperties.First().Type.To(DbType.Int32),
                                                           MapMapping.IDProperties.First().MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           MapMapping.TableName,
                                                           MapMapping.IDProperties.First().FieldName,
                                                           "",
                                                           true,
                                                           false,
                                                           false);
            }
        }
 private static void SetupJoiningTables(ListMapping <IDatabase, IMapping> Mappings, IDatabase Key, Schema.Default.Database.Database TempDatabase)
 {
     Contract.Requires <NullReferenceException>(Mappings != null, "Mappings");
     foreach (IMapping Mapping in Mappings[Key])
     {
         foreach (IProperty Property in Mapping.Properties)
         {
             if (Property is IMap)
             {
                 IMapping MapMapping = Mappings[Key].FirstOrDefault(x => x.ObjectType == Property.Type);
                 foreach (IProperty IDProperty in MapMapping.IDProperties)
                 {
                     TempDatabase[Mapping.TableName].AddColumn(Property.FieldName,
                                                               IDProperty.Type.To(DbType.Int32),
                                                               IDProperty.MaxLength,
                                                               !Property.NotNull,
                                                               false,
                                                               Property.Index,
                                                               false,
                                                               false,
                                                               MapMapping.TableName,
                                                               IDProperty.FieldName,
                                                               "",
                                                               false,
                                                               false,
                                                               Mapping.Properties.Count(x => x.Type == Property.Type) == 1 && Mapping.ObjectType != Property.Type);
                 }
             }
             else if (Property is IMultiMapping || Property is ISingleMapping)
             {
                 SetupJoiningTablesEnumerable(Mappings, Mapping, Property, Key, TempDatabase);
             }
         }
     }
 }