/// <summary> /// Creates a <see cref="DataMergeDefinition" /> from the specified <paramref name="importContext" />. /// </summary> /// <param name="importContext">The import context.</param> /// <returns> /// An instance of <see cref="DataMergeDefinition" /> /// </returns> /// <exception cref="InvalidOperationException">Could not find matching field definition for data column</exception> protected virtual DataMergeDefinition CreateMergeDefinition(ImportProcessContext importContext) { var importDefinition = importContext.Definition; var mergeDefinition = new DataMergeDefinition(); mergeDefinition.TargetTable = importDefinition.TargetTable; mergeDefinition.IncludeInsert = importDefinition.CanInsert; mergeDefinition.IncludeUpdate = importDefinition.CanUpdate; // fluent builder var mergeMapping = new DataMergeMapping(mergeDefinition); // map included columns foreach (var fieldMapping in importContext.MappedFields) { var fieldDefinition = fieldMapping.Definition; var nativeType = SqlTypeMapping.NativeType(fieldDefinition.DataType); mergeMapping .Column(fieldDefinition.Name) .Insert(fieldDefinition.CanInsert) .Update(fieldDefinition.CanUpdate) .Key(fieldDefinition.IsKey) .NativeType(nativeType); } return(mergeDefinition); }
public static string BuildJsonSelectSql <T>() { string sql = "select * from openjson(@json) with ("; var properties = typeof(T).GetProperties(); int count = 1; foreach (var property in properties) { // If is not a collection or another class // this is just a regular primitive property if (!(property.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))) { if (!(property.PropertyType != typeof(string) && property.PropertyType.IsClass)) { if (count != properties.Count() || count != 1) { sql += ","; } string match = SqlTypeMapping.MatchType(property); sql += $" [{property.Name}] {match}"; } } count++; } sql += ")"; return(sql); }
/// <summary> /// Validates the entites against the database schema /// </summary> private void CheckSchemaAgainstEntityModels() { if (!this.SqlConnectionConfiguration.ValidateSchema) { return; } if (!this.SqlConnectionConfiguration.SupportsMetadata) { return; } if (schemaAlreadyChecked) { return; } using var databaseMetadata = new DatabaseMetadata(this.SqlConnectionConfiguration, this.Logger); var dataSets = this.GetType() .GetProperties() .Where(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>)) .ToList(); foreach (var dataSet in dataSets) { var entityType = dataSet.PropertyType.GetGenericArguments().Single(); var tableSchema = "dbo"; var tableName = entityType.Name; if (dataSet.PropertyType.BaseType == typeof(object)) { this.Logger.Debug("Skipping DBSet:{0} type:{1} - inhertince validation not supported", dataSet.Name, dataSet.PropertyType.FullName); continue; } // Get Schema / Table var tableAttribute = ReflectionHelper.SingleOrDefaultAttribute <TableAttribute>(entityType); if (tableAttribute != null) { if (tableAttribute.Schema.IsNotBlank()) { tableSchema = tableAttribute.Schema; } tableName = tableAttribute.Name; } var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); var tableMeta = databaseMetadata.Table(tableSchema, tableName); if (tableMeta == null) { var message = FormattableString.Invariant($"Schema.Table : {tableSchema}.{tableName} not found in sql meta data for dbset:{dataSet.Name}"); throw new DataException(message); } foreach (var property in properties) { if (!SqlTypeMapping.SupportedCSharpType(property)) { this.Logger.Debug("Skipping Property:{0} type:{1}", property.Name, property.PropertyType.FullName); continue; } var columnName = property.Name; var columnAttribute = ReflectionHelper.SingleOrDefaultAttribute <ColumnAttribute>(property); if (columnAttribute != null && columnAttribute.Name.IsNotBlank()) { columnName = columnAttribute !.Name; } var columnMeta = tableMeta.Column(columnName); if (columnMeta == null) { var message = FormattableString.Invariant($"Table.Column={tableName}.{columnName} not found for Class.Property={entityType.Name}.{property.Name} type:{property.PropertyType.Name}"); throw new DataException(message); } // check if Type matches / compatable if (!SqlTypeMapping.AreTypesCompatible(columnMeta, property)) { var message = FormattableString.Invariant($"incompatable data type Class.Property={entityType.Name}.{property.Name} type:{property.PropertyType.Name} Table.Column={tableName}.{columnName} type:{columnMeta.SqlDbType} length:{columnMeta.MaxLength}"); throw new DataException(message); } } } schemaAlreadyChecked = true; }
/// <summary> /// Registers Types with dapper. /// </summary> public static void RegisterTypes() { SqlTypeMapping.RegisterTypes(EthereumTypeHandlerRegistry.Register, CommonTypeHandlerRegistry.Register, RegisterServerSpecificTypes); }
/// <summary> /// Automatics the map the properties of type <typeparamref name="TEntity"/> to the specified <see cref="DataMergeDefinition"/> . /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <param name="mergeDefinition">The merge definition up auto map to.</param> public static void AutoMap <TEntity>(DataMergeDefinition mergeDefinition) { var entityType = typeof(TEntity); var properties = TypeDescriptor.GetProperties(entityType); var tableAttribute = Attribute.GetCustomAttribute(entityType, typeof(TableAttribute)) as TableAttribute; if (tableAttribute != null) { string targetTable = tableAttribute.Name; if (!string.IsNullOrEmpty(tableAttribute.Schema)) { targetTable = tableAttribute.Schema + "." + targetTable; } mergeDefinition.TargetTable = targetTable; } if (string.IsNullOrEmpty(mergeDefinition.TargetTable)) { mergeDefinition.TargetTable = entityType.Name; } foreach (PropertyDescriptor p in properties) { string sourceColumn = p.Name; string targetColumn = sourceColumn; string nativeType = SqlTypeMapping.NativeType(p.PropertyType); var columnAttribute = p.Attributes .OfType <ColumnAttribute>() .FirstOrDefault(); if (columnAttribute != null) { if (columnAttribute.Name.HasValue()) { targetColumn = columnAttribute.Name; } if (columnAttribute.TypeName.HasValue()) { nativeType = columnAttribute.TypeName; } } var mergeColumn = mergeDefinition.Columns.FirstOrAdd( m => m.SourceColumn == sourceColumn, () => new DataMergeColumn { SourceColumn = sourceColumn }); mergeColumn.TargetColumn = targetColumn; mergeColumn.NativeType = nativeType; var keyAttribute = p.Attributes .OfType <KeyAttribute>() .FirstOrDefault(); if (keyAttribute != null) { mergeColumn.IsKey = true; mergeColumn.CanUpdate = false; } var ignoreAttribute = p.Attributes .OfType <NotMappedAttribute>() .FirstOrDefault(); if (ignoreAttribute != null) { mergeColumn.IsIgnored = true; } } }