Files
Umbraco-CMS/src/Umbraco.Core/Persistence/DatabaseModelDefinitions/DefinitionFactory.cs

171 lines
8.4 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Data;
using System.Linq;
using System.Reflection;
using NPoco;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.DatabaseModelDefinitions
{
internal static class DefinitionFactory
{
public static TableDefinition GetTableDefinition(Type modelType, ISqlSyntaxProvider sqlSyntax)
{
//Looks for NPoco's TableNameAtribute for the name of the table
//If no attribute is set we use the name of the Type as the default convention
var tableNameAttribute = modelType.FirstAttribute<TableNameAttribute>();
string tableName = tableNameAttribute == null ? modelType.Name : tableNameAttribute.Value;
var tableDefinition = new TableDefinition {Name = tableName};
var objProperties = modelType.GetProperties().ToList();
foreach (var propertyInfo in objProperties)
{
//If current property has an IgnoreAttribute then skip it
var ignoreAttribute = propertyInfo.FirstAttribute<IgnoreAttribute>();
if (ignoreAttribute != null) continue;
//If current property has a ResultColumnAttribute then skip it
var resultColumnAttribute = propertyInfo.FirstAttribute<ResultColumnAttribute>();
if (resultColumnAttribute != null) continue;
//Looks for ColumnAttribute with the name of the column, which would exist with ExplicitColumns
//Otherwise use the name of the property itself as the default convention
var columnAttribute = propertyInfo.FirstAttribute<ColumnAttribute>();
string columnName = columnAttribute != null ? columnAttribute.Name : propertyInfo.Name;
var columnDefinition = GetColumnDefinition(modelType, propertyInfo, columnName, tableName, sqlSyntax);
tableDefinition.Columns.Add(columnDefinition);
//Creates a foreignkey definition and adds it to the collection on the table definition
var foreignKeyAttributes = propertyInfo.MultipleAttribute<ForeignKeyAttribute>();
if (foreignKeyAttributes != null)
{
foreach (var foreignKeyAttribute in foreignKeyAttributes)
{
var foreignKeyDefinition = GetForeignKeyDefinition(modelType, propertyInfo, foreignKeyAttribute, columnName, tableName);
tableDefinition.ForeignKeys.Add(foreignKeyDefinition);
}
}
//Creates an index definition and adds it to the collection on the table definition
var indexAttribute = propertyInfo.FirstAttribute<IndexAttribute>();
if (indexAttribute != null)
{
var indexDefinition = GetIndexDefinition(modelType, propertyInfo, indexAttribute, columnName, tableName);
tableDefinition.Indexes.Add(indexDefinition);
}
}
return tableDefinition;
}
public static ColumnDefinition GetColumnDefinition(Type modelType, PropertyInfo propertyInfo, string columnName, string tableName, ISqlSyntaxProvider sqlSyntax)
{
var definition = new ColumnDefinition{ Name = columnName, TableName = tableName, ModificationType = ModificationType.Create };
//Look for specific Null setting attributed a column
var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
if (nullSettingAttribute != null)
{
definition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
}
//Look for specific DbType attributed a column
var databaseTypeAttribute = propertyInfo.FirstAttribute<SpecialDbTypeAttribute>();
if (databaseTypeAttribute != null)
{
definition.HasSpecialDbType = true;
definition.DbType = databaseTypeAttribute.DatabaseType;
}
else
{
definition.PropertyType = propertyInfo.PropertyType;
}
//Look for Primary Key for the current column
var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
if (primaryKeyColumnAttribute != null)
{
string primaryKeyName = string.IsNullOrEmpty(primaryKeyColumnAttribute.Name)
? string.Format("PK_{0}", tableName)
: primaryKeyColumnAttribute.Name;
definition.IsPrimaryKey = true;
definition.IsIdentity = primaryKeyColumnAttribute.AutoIncrement;
definition.IsIndexed = primaryKeyColumnAttribute.Clustered;
definition.PrimaryKeyName = primaryKeyName;
definition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
definition.Seeding = primaryKeyColumnAttribute.IdentitySeed;
}
//Look for Size/Length of DbType
var lengthAttribute = propertyInfo.FirstAttribute<LengthAttribute>();
if (lengthAttribute != null)
{
definition.Size = lengthAttribute.Length;
}
//Look for Constraint for the current column
var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
if (constraintAttribute != null)
{
definition.ConstraintName = constraintAttribute.Name ?? string.Empty;
definition.DefaultValue = constraintAttribute.Default ?? string.Empty;
}
return definition;
}
public static ForeignKeyDefinition GetForeignKeyDefinition(Type modelType, PropertyInfo propertyInfo,
ForeignKeyAttribute attribute, string columnName, string tableName)
{
var referencedTable = attribute.Type.FirstAttribute<TableNameAttribute>();
var referencedPrimaryKey = attribute.Type.FirstAttribute<PrimaryKeyAttribute>();
string referencedColumn = string.IsNullOrEmpty(attribute.Column)
? referencedPrimaryKey.Value
: attribute.Column;
string foreignKeyName = string.IsNullOrEmpty(attribute.Name)
? string.Format("FK_{0}_{1}_{2}", tableName, referencedTable.Value, referencedColumn)
: attribute.Name;
var definition = new ForeignKeyDefinition
{
Name = foreignKeyName,
ForeignTable = tableName,
PrimaryTable = referencedTable.Value
};
definition.ForeignColumns.Add(columnName);
definition.PrimaryColumns.Add(referencedColumn);
return definition;
}
public static IndexDefinition GetIndexDefinition(Type modelType, PropertyInfo propertyInfo, IndexAttribute attribute, string columnName, string tableName)
{
string indexName = string.IsNullOrEmpty(attribute.Name)
? string.Format("IX_{0}_{1}", tableName, columnName)
: attribute.Name;
var definition = new IndexDefinition
{
Name = indexName,
IndexType = attribute.IndexType,
ColumnName = columnName,
Merge remote-tracking branch 'origin/dev-v7' into temp8 # Conflicts: # .github/README.md # build.bat # src/Umbraco.Core/Migrations/Expressions/Alter/Table/AlterTableBuilder.cs # src/Umbraco.Core/Migrations/Expressions/Create/Column/CreateColumnBuilder.cs # src/Umbraco.Core/Migrations/Expressions/Create/Index/CreateIndexBuilder.cs # src/Umbraco.Core/Migrations/Expressions/Create/Table/CreateTableBuilder.cs # src/Umbraco.Core/Persistence/DatabaseModelDefinitions/DefinitionFactory.cs # src/Umbraco.Core/Persistence/DatabaseModelDefinitions/IndexDefinition.cs # src/Umbraco.Core/Persistence/Factories/MediaFactory.cs # src/Umbraco.Core/Persistence/Migrations/Syntax/Alter/Column/AlterColumnBuilder.cs # src/Umbraco.Core/Persistence/Repositories/EntityRepository.cs # src/Umbraco.Core/Persistence/Repositories/UserRepository.cs # src/Umbraco.Core/Persistence/Repositories/VersionableRepositoryBase.cs # src/Umbraco.Core/Persistence/SqlSyntax/MySqlSyntaxProvider.cs # src/Umbraco.Core/Services/EntityService.cs # src/Umbraco.Core/Services/IMediaService.cs # src/Umbraco.Core/Services/MediaService.cs # src/Umbraco.Core/Services/NotificationService.cs # src/Umbraco.Web.UI.Client/bower.json # src/Umbraco.Web.UI.Client/src/common/directives/components/imaging/umbimagegravity.directive.js # src/Umbraco.Web.UI.Client/src/common/directives/components/tree/umbtreeitem.directive.js # src/Umbraco.Web.UI.Client/src/common/directives/validation/valtab.directive.js # src/Umbraco.Web.UI.Client/src/common/resources/media.resource.js # src/Umbraco.Web.UI.Client/src/common/resources/mediatype.resource.js # src/Umbraco.Web.UI.Client/src/controllers/navigation.controller.js # src/Umbraco.Web.UI.Client/src/less/main.less # src/Umbraco.Web.UI.Client/src/less/panel.less # src/Umbraco.Web.UI.Client/src/less/property-editors.less # src/Umbraco.Web.UI.Client/src/views/common/infiniteeditors/mediapicker/mediapicker.controller.js # src/Umbraco.Web.UI.Client/src/views/common/overlays/mediaPicker/mediapicker.html # src/Umbraco.Web.UI.Client/src/views/components/imaging/umb-image-gravity.html # src/Umbraco.Web.UI.Client/src/views/content/copy.html # src/Umbraco.Web.UI.Client/src/views/content/emptyrecyclebin.html # src/Umbraco.Web.UI.Client/src/views/content/move.html # src/Umbraco.Web.UI.Client/src/views/dashboard/settings/settingsdashboardintro.html # src/Umbraco.Web.UI.Client/src/views/datatypes/move.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/copy.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/create.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/move.html # src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.controller.js # src/Umbraco.Web.UI.Client/src/views/documenttypes/views/templates/templates.html # src/Umbraco.Web.UI.Client/src/views/media/move.html # src/Umbraco.Web.UI.Client/src/views/mediatypes/copy.html # src/Umbraco.Web.UI.Client/src/views/mediatypes/create.html # src/Umbraco.Web.UI.Client/src/views/mediatypes/move.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/fileupload/fileupload.controller.js # src/Umbraco.Web.UI.Client/src/views/propertyeditors/imagecropper/imagecropper.prevalues.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/listview/listview.controller.js # src/Umbraco.Web.UI.Client/src/views/propertyeditors/listview/listview.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/mediapicker/mediapicker.controller.js # src/Umbraco.Web.UI.Client/src/views/propertyeditors/mediapicker/mediapicker.html # src/Umbraco.Web.UI.Client/src/views/propertyeditors/radiobuttons/radiobuttons.html # src/Umbraco.Web.UI.Client/src/views/users/views/user/details.html # src/Umbraco.Web.UI/Umbraco/config/lang/da.xml # src/Umbraco.Web.UI/Umbraco/config/lang/fr.xml # src/Umbraco.Web.UI/Umbraco/config/lang/nl.xml # src/Umbraco.Web.UI/Umbraco/config/lang/pl.xml # src/Umbraco.Web.UI/Umbraco/config/lang/ru.xml # src/Umbraco.Web.UI/Umbraco/config/lang/sv.xml # src/Umbraco.Web.UI/Umbraco/config/lang/zh.xml # src/Umbraco.Web.UI/Umbraco/developer/Macros/editMacro.aspx # src/Umbraco.Web.UI/Umbraco/dialogs/ChangeDocType.aspx # src/Umbraco.Web.UI/umbraco/config/lang/en.xml # src/Umbraco.Web.UI/umbraco/config/lang/en_us.xml # src/Umbraco.Web.UI/umbraco/config/lang/es.xml # src/Umbraco.Web.UI/umbraco/dialogs/editMacro.aspx # src/Umbraco.Web/Controllers/UmbLoginController.cs # src/Umbraco.Web/Editors/ContentControllerBase.cs # src/Umbraco.Web/Editors/ContentTypeController.cs # src/Umbraco.Web/Editors/MediaController.cs # src/Umbraco.Web/Editors/PasswordChanger.cs # src/Umbraco.Web/HttpRequestExtensions.cs # src/Umbraco.Web/Models/ContentEditing/Tab.cs # src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs # src/Umbraco.Web/Templates/TemplateRenderer.cs # src/Umbraco.Web/Trees/ApplicationTreeController.cs # src/Umbraco.Web/Trees/ContentTreeController.cs # src/Umbraco.Web/Trees/ContentTreeControllerBase.cs # src/Umbraco.Web/Trees/MediaTreeController.cs # src/Umbraco.Web/_Legacy/Controls/TabView.cs # src/Umbraco.Web/umbraco.presentation/umbraco/developer/RelationTypes/EditRelationType.aspx.cs # src/Umbraco.Web/umbraco.presentation/umbraco/developer/Xslt/editXslt.aspx.cs # src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/protectPage.aspx.cs # src/Umbraco.Web/umbraco.presentation/umbraco/settings/editLanguage.aspx.cs # src/umbraco.cms/Actions/Action.cs # src/umbraco.providers/members/UmbracoMembershipProvider.cs
2018-11-19 15:32:26 +11:00
TableName = tableName,
2018-06-29 19:52:40 +02:00
};
if (string.IsNullOrEmpty(attribute.ForColumns) == false)
{
var columns = attribute.ForColumns.Split(',').Select(p => p.Trim());
foreach (var column in columns)
{
definition.Columns.Add(new IndexColumnDefinition {Name = column, Direction = Direction.Ascending});
}
}
return definition;
}
}
}