GetDatabaseCollation() static private method

static private GetDatabaseCollation ( string connectionString ) : string
connectionString string
return string
Ejemplo n.º 1
0
        public Function GetFunction(string connectionString, string name, IEnumerable <SpecialField> auditFields)
        {
            var database = new Database();

            database.Collate = DatabaseHelper.GetDatabaseCollation(connectionString);

            LoadFunctions(database, connectionString);

            var l = database.FunctionList.Where(x => x.Name != name).ToList();

            database.FunctionList.RemoveAll(x => l.Contains(x));

            return(database.FunctionList.FirstOrDefault());
        }
Ejemplo n.º 2
0
        public StoredProc GetStoredProcedure(string connectionString, string procName, IEnumerable <SpecialField> auditFields)
        {
            var database = new Database();

            database.Collate = DatabaseHelper.GetDatabaseCollation(connectionString);

            LoadStoredProcedures(database, procName, connectionString);

            var l = database.StoredProcList.Where(x => x.Name != procName).ToList();

            database.StoredProcList.RemoveAll(x => l.Contains(x));

            return(database.StoredProcList.FirstOrDefault());
        }
Ejemplo n.º 3
0
        public Entity GetEntity(string connectionString, string name, IEnumerable <SpecialField> auditFields)
        {
            var database = new Database();

            database.Collate = DatabaseHelper.GetDatabaseCollation(connectionString);

            #region Load Entities
            using (var tableReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlDatabaseTables()))
            {
                while (tableReader.Read())
                {
                    var newEntity = new Entity();
                    newEntity.Name = tableReader["name"].ToString();
                    if (string.Compare(newEntity.Name, name, true) == 0) //Only the specified item
                    {
                        database.EntityList.Add(newEntity);
                    }
                    newEntity.Schema = tableReader["schema"].ToString();
                }
            }
            #endregion

            #region Load Entity Fields
            using (var columnReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlColumnsForTable(name)))
            {
                while (columnReader.Read())
                {
                    var columnName = columnReader["columnName"].ToString();
                    var tableName  = columnReader["tableName"].ToString();

                    var entity = database.EntityList.FirstOrDefault(x => x.Name == tableName);
                    //Ensure the field name is not an Audit field
                    if (entity != null && !auditFields.Any(x => x.Name.ToLower() == columnName.ToLower()))
                    {
                        var maxSortOrder = 0;
                        if (entity.FieldList.Count > 0)
                        {
                            maxSortOrder = entity.FieldList.Max(x => x.SortOrder);
                        }
                        var newColumn = new Field()
                        {
                            Name = columnName, SortOrder = ++maxSortOrder
                        };
                        entity.FieldList.Add(newColumn);

                        newColumn.Nullable = bool.Parse(columnReader["allowNull"].ToString());
                        if (bool.Parse(columnReader["isIdentity"].ToString()))
                        {
                            newColumn.Identity = true;
                        }

                        if (columnReader["isPrimaryKey"] != System.DBNull.Value)
                        {
                            newColumn.PrimaryKey = true;
                        }

                        try
                        {
                            newColumn.DataType = DatabaseHelper.GetSQLDataType(columnReader["xtype"].ToString(), database.UserDefinedTypes);
                        }
                        catch { }

                        var defaultvalue = columnReader["defaultValue"].ToString();
                        SetupDefault(newColumn, defaultvalue);

                        newColumn.Length = (int)columnReader["length"];

                        //Decimals are a little different
                        if (newColumn.DataType == SqlDbType.Decimal)
                        {
                            newColumn.Length = (byte)columnReader["precision"];
                            newColumn.Scale  = (int)columnReader["scale"];
                        }

                        if (columnReader["collation"] != System.DBNull.Value)
                        {
                            if (database.Collate != (string)columnReader["collation"])
                            {
                                newColumn.Collate = (string)columnReader["collation"];
                            }
                        }
                    }
                    else if (entity != null)
                    {
                        if (auditFields.Any(x => (x.Type == SpecialFieldTypeConstants.CreatedDate ||
                                                  x.Type == SpecialFieldTypeConstants.CreatedBy) &&
                                            x.Name.ToLower() == columnName.ToLower()))
                        {
                            entity.AllowCreateAudit = true;
                        }

                        if (auditFields.Any(x => (x.Type == SpecialFieldTypeConstants.ModifedDate ||
                                                  x.Type == SpecialFieldTypeConstants.ModifiedBy) &&
                                            x.Name.ToLower() == columnName.ToLower()))
                        {
                            entity.AllowModifyAudit = true;
                        }

                        if (auditFields.Any(x => x.Type == SpecialFieldTypeConstants.Timestamp &&
                                            x.Name.ToLower() == columnName.ToLower()))
                        {
                            entity.AllowTimestamp = true;
                        }

                        if (auditFields.Any(x => x.Type == SpecialFieldTypeConstants.Tenant &&
                                            x.Name.ToLower() == columnName.ToLower()))
                        {
                            entity.IsTenant = true;
                        }
                    }
                }
            }
            #endregion

            #region Load Entity Fields Extra
            using (var indexReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlIndexesForTable()))
            {
                while (indexReader.Read())
                {
                    var indexName  = indexReader["indexname"].ToString();
                    var columnName = indexReader["columnname"].ToString();
                    var tableName  = indexReader["tableName"].ToString();
                    var entity     = database.EntityList.FirstOrDefault(x => x.Name == tableName);
                    if (entity != null)
                    {
                        var pk     = bool.Parse(indexReader["is_primary_key"].ToString());
                        var column = entity.FieldList.FirstOrDefault(x => x.Name == columnName);
                        if (column != null && !pk)
                        {
                            column.IsIndexed = true;
                        }
                    }
                }
            }
            #endregion

            LoadIndexes(database, connectionString);
            LoadUniqueFields(database, connectionString);

            return(database.EntityList.FirstOrDefault());
        }
Ejemplo n.º 4
0
        public Database Import(string connectionString, IEnumerable <SpecialField> auditFields)
        {
            try
            {
                var database = new Database();
                database.Collate = DatabaseHelper.GetDatabaseCollation(connectionString);

                #region Load user defined types
                LoadUdts(database, connectionString);
                #endregion

                #region Load Entities
                this.ProgressText = "Loading Entities...";
                using (var tableReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlDatabaseTables()))
                {
                    while (tableReader.Read())
                    {
                        var newEntity = new Entity();
                        newEntity.Name = tableReader["name"].ToString();
                        database.EntityList.Add(newEntity);
                        newEntity.Schema = tableReader["schema"].ToString();
                    }
                }
                #endregion

                #region Load Entity Fields
                using (var columnReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlColumnsForTable()))
                {
                    while (columnReader.Read())
                    {
                        var columnName = columnReader["columnName"].ToString();
                        var tableName  = columnReader["tableName"].ToString();

                        var entity = database.EntityList.FirstOrDefault(x => x.Name == tableName);
                        //Ensure the field name is not an Audit field
                        if (entity != null && !auditFields.Any(x => x.Name.ToLower() == columnName.ToLower()))
                        {
                            var maxSortOrder = 0;
                            if (entity.FieldList.Count > 0)
                            {
                                maxSortOrder = entity.FieldList.Max(x => x.SortOrder);
                            }
                            var newColumn = new Field()
                            {
                                Name = columnName, SortOrder = ++maxSortOrder
                            };
                            entity.FieldList.Add(newColumn);

                            newColumn.Nullable = bool.Parse(columnReader["allowNull"].ToString());
                            if (bool.Parse(columnReader["isIdentity"].ToString()))
                            {
                                newColumn.Identity = true;
                            }

                            if (columnReader["isPrimaryKey"] != System.DBNull.Value)
                            {
                                newColumn.PrimaryKey = true;
                            }

                            try
                            {
                                newColumn.DataType = DatabaseHelper.GetSQLDataType(columnReader["xtype"].ToString(), database.UserDefinedTypes);
                            }
                            catch { }

                            var defaultvalue = columnReader["defaultValue"].ToString();
                            SetupDefault(newColumn, defaultvalue);
                            //newColumn.ImportedDefaultName = "";

                            newColumn.Length = (int)columnReader["length"];

                            //Decimals are a little different
                            if (newColumn.DataType == SqlDbType.Decimal)
                            {
                                newColumn.Length = (byte)columnReader["precision"];
                                newColumn.Scale  = (int)columnReader["scale"];
                            }

                            if (columnReader["collation"] != System.DBNull.Value)
                            {
                                if (database.Collate != (string)columnReader["collation"])
                                {
                                    newColumn.Collate = (string)columnReader["collation"];
                                }
                            }
                        }
                        else if (entity != null)
                        {
                            if (auditFields.Any(x => (x.Type == SpecialFieldTypeConstants.CreatedDate ||
                                                      x.Type == SpecialFieldTypeConstants.CreatedBy) &&
                                                x.Name.ToLower() == columnName.ToLower()))
                            {
                                entity.AllowCreateAudit = true;
                            }

                            if (auditFields.Any(x => (x.Type == SpecialFieldTypeConstants.ModifedDate ||
                                                      x.Type == SpecialFieldTypeConstants.ModifiedBy) &&
                                                x.Name.ToLower() == columnName.ToLower()))
                            {
                                entity.AllowModifyAudit = true;
                            }

                            if (auditFields.Any(x => x.Type == SpecialFieldTypeConstants.Timestamp &&
                                                x.Name.ToLower() == columnName.ToLower()))
                            {
                                entity.AllowTimestamp = true;
                            }

                            if (auditFields.Any(x => x.Type == SpecialFieldTypeConstants.Tenant &&
                                                x.Name.ToLower() == columnName.ToLower()))
                            {
                                entity.IsTenant = true;
                            }
                        }
                    }
                }

                using (var columnReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlColumnsForComputed()))
                {
                    while (columnReader.Read())
                    {
                        var tableName  = columnReader["tableName"].ToString();
                        var columnName = columnReader["columnName"].ToString();
                        var entity     = database.EntityList.FirstOrDefault(x => x.Name == tableName);
                        if (entity != null)
                        {
                            var column = entity.FieldList.FirstOrDefault(x => x.Name.ToLower() == columnName.ToLower());
                            if (column != null)
                            {
                                column.IsComputed = true;
                                column.Formula    = columnReader["definition"].ToString();
                            }
                        }
                    }
                }

                #endregion

                #region Load Entity Indexes
                using (var indexReader = DatabaseHelper.ExecuteReader(connectionString, CommandType.Text, SchemaModelHelper.GetSqlIndexesForTable()))
                {
                    while (indexReader.Read())
                    {
                        var indexName  = indexReader["indexname"].ToString();
                        var columnName = indexReader["columnname"].ToString();
                        var tableName  = indexReader["tableName"].ToString();
                        var entity     = database.EntityList.FirstOrDefault(x => x.Name == tableName);
                        if (entity != null)
                        {
                            var pk     = bool.Parse(indexReader["is_primary_key"].ToString());
                            var column = entity.FieldList.FirstOrDefault(x => x.Name == columnName);
                            if (column != null && !pk)
                            {
                                column.IsIndexed = true;
                            }
                        }
                    }
                }
                #endregion

                #region Load Relations

                var dsRelationship = DatabaseHelper.ExecuteDataset(connectionString, SchemaModelHelper.GetSqlForRelationships());
                foreach (DataRow rowRelationship in dsRelationship.Tables[0].Rows)
                {
                    var constraintName  = rowRelationship["FK_CONSTRAINT_NAME"].ToString();
                    var parentTableName = (string)rowRelationship["UQ_TABLE_NAME"];
                    var childTableName  = (string)rowRelationship["FK_TABLE_NAME"];
                    var parentTable     = database.EntityList.FirstOrDefault(x => x.Name == parentTableName);
                    var childTable      = database.EntityList.FirstOrDefault(x => x.Name == childTableName);
                    if (parentTable != null && childTable != null)
                    {
                        Relationship newRelation = null;
                        var          isAdd       = false;
                        if (database.RelationshipList.Count(x => x.ConstraintName == constraintName) == 0)
                        {
                            newRelation = new Relationship();
                            if (rowRelationship["id"] != System.DBNull.Value)
                            {
                                newRelation.ImportData = rowRelationship["id"].ToString();
                            }
                            newRelation.SourceEntity   = parentTable;
                            newRelation.TargetEntity   = childTable;
                            newRelation.ConstraintName = constraintName;
                            var search   = ("_" + childTable.Name + "_" + parentTable.Name).ToLower();
                            var roleName = constraintName.ToLower().Replace(search, string.Empty);
                            if (roleName.Length >= 3)
                            {
                                roleName = roleName.Remove(0, 3);
                            }
                            var v = roleName.ToLower();
                            if (v != "fk")
                            {
                                newRelation.RoleName = v;
                            }
                            isAdd = true;
                        }
                        else
                        {
                            newRelation = database.RelationshipList.First(x => x.ConstraintName == constraintName);
                        }

                        //add the column relationship to the relation
                        var columnRelationship = new RelationshipDetail();
                        var parentColumnName   = (string)rowRelationship["UQ_COLUMN_NAME"];
                        var childColumnName    = (string)rowRelationship["FK_COLUMN_NAME"];
                        if (parentTable.FieldList.Count(x => x.Name == parentColumnName) == 1 && (childTable.FieldList.Count(x => x.Name == childColumnName) == 1))
                        {
                            columnRelationship.ParentField = parentTable.FieldList.First(x => x.Name == parentColumnName);
                            columnRelationship.ChildField  = childTable.FieldList.First(x => x.Name == childColumnName);
                            newRelation.RelationshipColumnList.Add(columnRelationship);

                            //ONLY ADD THIS RELATION IF ALL WENT WELL
                            if (isAdd)
                            {
                                parentTable.RelationshipList.Add(newRelation);
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.Write(string.Empty);
                        }
                    }
                }
                #endregion

                #region Load StoredProcs

                LoadStoredProcedures(database, connectionString);

                #endregion

                #region Load Views
                this.ProgressText = "Loading Views...";
                LoadViews(database, connectionString);

                #endregion

                #region Load Functions

                this.ProgressText = "Loading Functions...";
                LoadFunctions(database, connectionString);

                #endregion

                #region Load Indexes

                this.ProgressText = "Loading Indexes...";
                LoadIndexes(database, connectionString);

                #endregion

                LoadUniqueFields(database, connectionString);

                return(database);
            }
            catch (Exception /*ignored*/)
            {
                throw;
            }
            finally
            {
                this.ProgressText  = string.Empty;
                this.ProgressValue = 0;
            }
        }