Refactoring database creation, adding sql syntax provider to account for differences in syntax between sql, ce and mysql.
Adding MySql unit test.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions
|
||||
{
|
||||
public class ColumnDefinition
|
||||
{
|
||||
public string ColumnName { get; set; }
|
||||
|
||||
public Type PropertyType { get; set; }
|
||||
public string DbType { get; set; }
|
||||
public int? DbTypeLength { get; set; }
|
||||
|
||||
public bool IsNullable { get; set; }
|
||||
|
||||
public bool IsPrimaryKey { get; set; }
|
||||
public bool IsPrimaryKeyIdentityColumn { get; set; }
|
||||
public bool IsPrimaryKeyClustered { get; set; }
|
||||
public string PrimaryKeyName { get; set; }
|
||||
public string PrimaryKeyColumns { get; set; }
|
||||
|
||||
public string ConstraintName { get; set; }
|
||||
public string ConstraintDefaultValue { get; set; }
|
||||
public bool HasConstraint
|
||||
{
|
||||
get { return !string.IsNullOrEmpty(ConstraintName) || !string.IsNullOrEmpty(ConstraintDefaultValue); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Persistence.DatabaseAnnotations;
|
||||
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions
|
||||
{
|
||||
public static class DefinitionFactory
|
||||
{
|
||||
public static TableDefinition GetTableDefinition(Type modelType)
|
||||
{
|
||||
var tableNameAttribute = modelType.FirstAttribute<TableNameAttribute>();
|
||||
string tableName = tableNameAttribute.Value;
|
||||
|
||||
var tableDefinition = new TableDefinition { TableName = tableName };
|
||||
var objProperties = modelType.GetProperties().ToList();
|
||||
foreach (var propertyInfo in objProperties)
|
||||
{
|
||||
//If current property is a ResultColumn then skip it
|
||||
var resultColumnAttribute = propertyInfo.FirstAttribute<ResultColumnAttribute>();
|
||||
if (resultColumnAttribute != null) continue;
|
||||
|
||||
//Assumes ExplicitColumn attribute and thus having a ColumnAttribute with the name of the column
|
||||
var columnAttribute = propertyInfo.FirstAttribute<ColumnAttribute>();
|
||||
if (columnAttribute == null) continue;
|
||||
|
||||
//Creates a column definition and adds it to the collection on the table definition
|
||||
var columnDefinition = new ColumnDefinition
|
||||
{
|
||||
ColumnName = columnAttribute.Name,
|
||||
PropertyType = propertyInfo.PropertyType
|
||||
};
|
||||
|
||||
//Look for specific DbType attributed a column
|
||||
var databaseTypeAttribute = propertyInfo.FirstAttribute<DatabaseTypeAttribute>();
|
||||
if (databaseTypeAttribute != null)
|
||||
{
|
||||
columnDefinition.DbType = databaseTypeAttribute.DatabaseType.ToString();
|
||||
if (databaseTypeAttribute.Length > 0)
|
||||
columnDefinition.DbTypeLength = databaseTypeAttribute.Length;
|
||||
}
|
||||
|
||||
//Look for specific Null setting attributed a column
|
||||
var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
|
||||
if (nullSettingAttribute != null)
|
||||
{
|
||||
columnDefinition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
|
||||
}
|
||||
|
||||
//Look for Primary Key for the current column
|
||||
var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
|
||||
if (primaryKeyColumnAttribute != null)
|
||||
{
|
||||
columnDefinition.IsPrimaryKey = true;
|
||||
columnDefinition.IsPrimaryKeyIdentityColumn = primaryKeyColumnAttribute.AutoIncrement;
|
||||
columnDefinition.IsPrimaryKeyClustered = primaryKeyColumnAttribute.Clustered;
|
||||
columnDefinition.PrimaryKeyName = primaryKeyColumnAttribute.Name ?? string.Empty;
|
||||
columnDefinition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
|
||||
}
|
||||
|
||||
//Look for Constraint for the current column
|
||||
var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
|
||||
if (constraintAttribute != null)
|
||||
{
|
||||
columnDefinition.ConstraintName = constraintAttribute.Name ?? string.Empty;
|
||||
columnDefinition.ConstraintDefaultValue = constraintAttribute.Default ?? string.Empty;
|
||||
}
|
||||
|
||||
tableDefinition.ColumnDefinitions.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 referencedTable = foreignKeyAttribute.Type.FirstAttribute<TableNameAttribute>();
|
||||
var referencedPrimaryKey = foreignKeyAttribute.Type.FirstAttribute<PrimaryKeyAttribute>();
|
||||
|
||||
string referencedColumn = string.IsNullOrEmpty(foreignKeyAttribute.Column)
|
||||
? referencedPrimaryKey.Value
|
||||
: foreignKeyAttribute.Column;
|
||||
|
||||
var foreignKeyDefinition = new ForeignKeyDefinition
|
||||
{
|
||||
ColumnName = columnAttribute.Name,
|
||||
ConstraintName = foreignKeyAttribute.Name,
|
||||
ReferencedColumnName = referencedColumn,
|
||||
ReferencedTableName = referencedTable.Value
|
||||
};
|
||||
tableDefinition.ForeignKeyDefinitions.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 = new IndexDefinition
|
||||
{
|
||||
ColumnNames = indexAttribute.ForColumns,
|
||||
IndexName = indexAttribute.Name,
|
||||
IndexType = indexAttribute.IndexType,
|
||||
IndexForColumn = columnAttribute.Name
|
||||
};
|
||||
tableDefinition.IndexDefinitions.Add(indexDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
return tableDefinition;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions
|
||||
{
|
||||
public class ForeignKeyDefinition
|
||||
{
|
||||
public string ConstraintName { get; set; }
|
||||
public string ColumnName { get; set; }
|
||||
public string ReferencedTableName { get; set; }
|
||||
public string ReferencedColumnName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Umbraco.Core.Persistence.DatabaseAnnotations;
|
||||
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions
|
||||
{
|
||||
public class IndexDefinition
|
||||
{
|
||||
public string IndexName { get; set; }
|
||||
public IndexTypes IndexType { get; set; }
|
||||
public string ColumnNames { get; set; }
|
||||
public string IndexForColumn { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions
|
||||
{
|
||||
public class TableDefinition
|
||||
{
|
||||
public TableDefinition()
|
||||
{
|
||||
ColumnDefinitions = new List<ColumnDefinition>();
|
||||
ForeignKeyDefinitions = new List<ForeignKeyDefinition>();
|
||||
IndexDefinitions = new List<IndexDefinition>();
|
||||
}
|
||||
|
||||
public string TableName { get; set; }
|
||||
public bool IsPrimaryKeyClustered
|
||||
{
|
||||
get { return ColumnDefinitions.Any(x => x.IsPrimaryKeyClustered); }
|
||||
}
|
||||
public string PrimaryKeyName
|
||||
{
|
||||
get { return ColumnDefinitions.First(x => x.IsPrimaryKey).PrimaryKeyName ?? string.Empty; }
|
||||
}
|
||||
public string PrimaryKeyColumns
|
||||
{
|
||||
get { return ColumnDefinitions.First(x => x.IsPrimaryKey).PrimaryKeyColumns ?? string.Empty; }
|
||||
}
|
||||
|
||||
public List<ColumnDefinition> ColumnDefinitions { get; set; }
|
||||
public List<ForeignKeyDefinition> ForeignKeyDefinitions { get; set; }
|
||||
public List<IndexDefinition> IndexDefinitions { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user