Exemple #1
0
        public static void CreateContext <TContext>(int dbVersion, string contextPath = "", string namespaceName = "")
        {
            string       contextName  = typeof(TContext).Name;
            DB_Operation dB_Operation = new DB_Operation(contextName);

            dB_Operation.ContextGenerateFromDB(dbVersion, contextPath, namespaceName);
            CreateEverythingForMSSQL <TContext>();
        }
Exemple #2
0
        private static void CreateEverythingForMSSQL <TContext>()
        {
            #region MSSQL için oluşturulacak sorgular oluşturulur
            CreateDatabase <TContext> createDatabase = new CreateDatabase <TContext>();
            Tuple <string, string>    tupleData      = createDatabase.EntityClassQueries();

            DB_Operation      dB_Operation = new DB_Operation(typeof(TContext).Name);
            Tuple <bool, int> tupleData2   = dB_Operation.CreateORAlterDatabaseAndTables(tupleData.Item2, tupleData.Item1);
            #endregion

            #region Veritabanı versiyon bilgisine ulaşılır ve WORM.config dosyasına yazılır
            string    contextName = typeof(TContext).Name;
            XDocument xDocument   = XDocument.Load(path);
            if (xDocument.Element("Databases").Element(contextName).Elements("Version") != null)
            {
                xDocument.Element("Databases").Element(contextName).Elements("Version").Remove();
            }
            xDocument.Element("Databases").Element(contextName).Add(new XElement("Version", new XAttribute("value", tupleData2.Item2)));
            xDocument.Save(path);
            #endregion
        }
Exemple #3
0
        /// <summary>
        /// TR : SQL Server için oluşturulacak tabloların, sütunların sorguları generate edilir
        /// EN :
        /// </summary>
        /// <returns>
        /// TupleData Item1 : TR : SQL Server üzerinde çalıştırılacak sorgular EN :
        /// TupleData Item2 : TR : __WORM_Configuration tablosunda tutulacak Veritabanı XML formu EN :
        /// </returns>
        public Tuple <string, string> EntityClassQueries()
        {
            #region Field Defination
            Type    entityType;
            dynamic entityInformation;
            string  entityColumnsMSSQL           = string.Empty,
                    createTableMSSQLQuery        = string.Empty,
                    alterTableMSSQLQuery         = string.Empty,
                    dropTableMSSQLQuery          = string.Empty,
                    dropConstraintListMSSQLQuery = string.Empty,
                    entityColumnsXML             = string.Empty,
                    entityColumnXML          = string.Empty,
                    createXMLObjectQuery     = string.Empty;
            List <string> dropConstraintList = new List <string>();
            #endregion

            #region Entity Class(Tablolar)larının property(sütun) olarak tanımlanmış olduğu Context(Database) sınıfından Entity Class(tablo)lar listelenir
            List <dynamic> implementedTableEntities = (from property in typeof(TDBEntity).GetProperties()
                                                       from genericArguments in property.PropertyType.GetGenericArguments()
                                                       where genericArguments.CustomAttributes.FirstOrDefault() != null &&
                                                       genericArguments.CustomAttributes.FirstOrDefault().AttributeType.Equals(typeof(TableAttribute))
                                                       orderby genericArguments.CustomAttributes.FirstOrDefault().NamedArguments.FirstOrDefault(x => x.MemberName == "OrdinalPosition").TypedValue.Value
                                                       select Activator.CreateInstance(genericArguments)).ToList();
            #endregion

            #region Creating SQL Server Queries

            #region Veritabanı versiyonu için XML verisi ve Create&Alter Tabloları ve Sütunları sorguları oluşturulur
            createXMLObjectQuery = "<Classes>";              // Veritabanında versiyonlama için kullanılacak XML bilgisinin başlangıcı
            foreach (var entity in implementedTableEntities) // Entity Class yani Veritabaındaki Tabloları oluşturmak için döngü başlatılır
            {
                entityType        = entity.GetType();
                entityInformation = entityType.GetCustomAttributes(typeof(TableAttribute), false).FirstOrDefault(); // Entity Class üzerindeki Table Attribute üzerinden bilgiler okunur

                columnList = new DB_Operation(contextName).ColumnListOnTable(entityType.Name);                      // Veritabanından ilgili tablonun tüm sütunları çekilir

                // Veritabanında Tablonun var olup olmadığı kontrol edilir Tablo Varsa ? ALTER çalışır CREATE çalışmaz : ALTER çalışmaz CREATE çalışır
                bool tableExistOnDb = tableList.FirstOrDefault(x => x.SchemaName == entityInformation.SchemaName && x.TableName == entityInformation.TableName) != null ? true : false;

                foreach (var entityColumn in entityType.GetProperties()) // Entity Class içerisindeki property ler yani Sütunları oluşturmak için döngü başlatılır
                {
                    // Property inin Custom Attributelerine bakarak MSSQL Query generate edilir
                    var columnInformation = new CSHARP_To_MSSQL().GetSQLQueryFormat(entityColumn);

                    #region Entity Class içerisinde oluşturulmuş yeni bir property varsa yani sütun ve Entity Class yani Tablo Veritabanında varsa
                    if (columnList.Where(x => x == entityColumn.Name).Count() == 0 && tableExistOnDb)
                    {
                        alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] ADD {columnInformation} ";
                    }
                    #endregion

                    #region Entity Class içerisinde var olup değişikliğe uğramış bir property

                    if (columnList.Where(x => x == entityColumn.Name).Count() > 0)
                    {
                        columnList.Remove(entityColumn.Name); // Tablo içerisindeki sütun listesinden işlem yapılan sütun çıkartılır (DROP edilmeyecektir)
                        Tuple <string, string> constraintTupleData = new DB_Operation(contextName).ConstraintNameByTableAndColumnName(entityInformation.SchemaName, entityInformation.TableName, entityColumn.Name);
                        if (!string.IsNullOrEmpty(constraintTupleData.Item1))
                        {
                            dropConstraintList.Add($"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] DROP CONSTRAINT {constraintTupleData.Item1} ");
                        }

                        dynamic isEntityColumnAutoIncrement = entityColumn.GetCustomAttributes(typeof(AUTO_INCREMENT), false).FirstOrDefault();
                        if (isEntityColumnAutoIncrement != null)
                        {
                            alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] DROP COLUMN {entityColumn.Name} ";
                            columnInformation     = columnInformation.Replace("PRIMARY KEY", "");
                            alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] ADD {columnInformation} ";
                            columnInformation     = columnInformation.Replace($"IDENTITY({isEntityColumnAutoIncrement.StartNumber},{isEntityColumnAutoIncrement.Increase})", "");
                        }

                        var isEntityColumnPrimaryKey = entityColumn.GetCustomAttributes(typeof(PRIMARY_KEY), false).FirstOrDefault();
                        if (isEntityColumnPrimaryKey != null)
                        {
                            columnInformation     = columnInformation.Replace("PRIMARY KEY", "");
                            alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] ADD PRIMARY KEY ({entityColumn.Name}) ";
                        }

                        dynamic isEntityColumnForeignKey = entityColumn.GetCustomAttributes(typeof(FOREIGN_KEY), false).FirstOrDefault();
                        if (isEntityColumnForeignKey != null)
                        {
                            columnInformation     = columnInformation.Replace($"FOREIGN KEY REFERENCES {isEntityColumnForeignKey.ClassName}({isEntityColumnForeignKey.PropertyName})", "");
                            alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] " +
                                                    $"ADD FOREIGN KEY({ entityColumn.Name}) REFERENCES {isEntityColumnForeignKey.ClassName}({isEntityColumnForeignKey.PropertyName}) ";
                        }

                        alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] ALTER COLUMN {columnInformation} ";
                    }
                    #endregion

                    #region Entity Class(tablo) Veritabanında yoksa oluşturulacak olan sütunların MSSQL Query generate edilir
                    if (tableList.Where(x => x.SchemaName == entityInformation.SchemaName && x.TableName == entityInformation.TableName).Count() == 0)
                    {
                        entityColumnsMSSQL += columnInformation + ", ";
                    }
                    #endregion

                    #region XMLQuery Sütunları generate edilir
                    entityColumnsXML += new CSHARP_To_MSSQL().GetXMLDataFormat(entityColumn);
                    #endregion
                }

                if (!string.IsNullOrEmpty(entityColumnsMSSQL))
                {
                    entityColumnsMSSQL     = entityColumnsMSSQL.Remove(entityColumnsMSSQL.Length - 2);
                    createTableMSSQLQuery += $"CREATE TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] ({entityColumnsMSSQL}) ";
                }

                // Veritabanında versiyonlama için kullanılacak XML bilgisinin gövdesi
                createXMLObjectQuery += $"<{entityType.Name} SchemaName=\"{entityInformation.SchemaName}\" TableName=\"{entityInformation.TableName}\" OrdinalPosition=\"{entityInformation.OrdinalPosition}\">" +
                                        $"{entityColumnsXML}" +
                                        $"</{entityType.Name}>";

                #region Tablo tableList listesinden çıkartılır
                DBTableModel tableInformation = tableList.FirstOrDefault(x => x.SchemaName == entityInformation.SchemaName && x.TableName == entityInformation.TableName);
                if (tableInformation != null) // Veritabanında Tablo zaten var ise güncelleme yapılıyordur (DROP edilmeyecektir)
                {
                    tableList.Remove(tableInformation);
                }
                #endregion

                #region DROP edilecek sütunlar
                if (columnList.Count() > 0)
                {
                    string columnNames = string.Empty;
                    foreach (string columnName in columnList)
                    {
                        Tuple <string, string> constraintTupleData = new DB_Operation(contextName).ConstraintNameByTableAndColumnName(entityInformation.SchemaName, entityInformation.TableName, columnName);
                        if (!string.IsNullOrEmpty(constraintTupleData.Item1))
                        {
                            dropConstraintList.Add($"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] DROP CONSTRAINT {constraintTupleData.Item1} ");
                        }
                        columnNames += $"{columnName}, ";
                    }
                    columnNames           = columnNames.Remove(columnNames.Length - 2);
                    alterTableMSSQLQuery += $"ALTER TABLE [{entityInformation.SchemaName}].[{entityInformation.TableName}] DROP COLUMN {columnNames} ";
                }
                #endregion

                entityColumnsMSSQL = string.Empty;
                entityColumnsXML   = string.Empty;
            } // Entity Class yani Veritabaındaki Tabloları oluşturmak için döngü sonlandırılır
            createXMLObjectQuery += "</Classes>"; // Veritabanında versiyonlama için kullanılacak XML bilgisinin bitişi
            #endregion

            #region DROP edilecek tablolar
            if (tableList.Count() > 0)
            {
                #region __WORM__Configuration dosyası silenecek tablolar listesinden kaldırılır
                DBTableModel wormTable = tableList.FirstOrDefault(x => x.SchemaName == "dbo" && x.TableName == "__WORM__Configuration");
                if (wormTable != null)
                {
                    tableList.Remove(wormTable);
                }
                #endregion

                // Tablo listesinde kalan herhangi bir kayıt Veritabanından silinecek tabloyu işaret eder.
                // Yani Kod tarafında bu tabloya ait Entity Class silinmiştir.
                foreach (DBTableModel table in tableList)
                {
                    dropTableMSSQLQuery += $"DROP TABLE [{table.SchemaName}].[{table.TableName}] ";
                }
            }
            #endregion

            #region Constraintler kaldırılır
            dropConstraintList.Reverse();
            foreach (string item in dropConstraintList)
            {
                dropConstraintListMSSQLQuery += item;
            }
            #endregion

            #endregion

            //                                                    DROP TABLES          CREATE TABLES             ALTER TABLES           XML QUERY
            return(Tuple.Create(dropConstraintListMSSQLQuery + dropTableMSSQLQuery + createTableMSSQLQuery + alterTableMSSQLQuery, createXMLObjectQuery));
        }