public virtual AtTimeZoneExpression AtTimeZone( SqlExpression timestamp, SqlExpression timeZone, Type type, RelationalTypeMapping?typeMapping = null) { if (typeMapping is null) { // PostgreSQL AT TIME ZONE flips the given type from timestamptz to timestamp and vice versa // See https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT typeMapping = timestamp.TypeMapping ?? _typeMappingSource.FindMapping(timestamp.Type, Dependencies.Model) !; var storeType = typeMapping.StoreType; typeMapping = storeType.StartsWith("timestamp with time zone", StringComparison.Ordinal) || storeType.StartsWith("timestamptz", StringComparison.Ordinal) ? _typeMappingSource.FindMapping("timestamp without time zone") ! : storeType.StartsWith("timestamp without time zone", StringComparison.Ordinal) || storeType.StartsWith("timestamp", StringComparison.Ordinal) ? _typeMappingSource.FindMapping("timestamp with time zone") ! : throw new ArgumentException($"timestamp argument to AtTimeZone had unknown store type {storeType}", nameof(timestamp)); } return(new AtTimeZoneExpression( ApplyDefaultTypeMapping(timestamp), ApplyDefaultTypeMapping(timeZone), type, typeMapping)); }
public NpgsqlSqlExpressionFactory(SqlExpressionFactoryDependencies dependencies) : base(dependencies) { _typeMappingSource = (NpgsqlTypeMappingSource)dependencies.TypeMappingSource; _boolTypeMapping = (RelationalTypeMapping)_typeMappingSource.FindMapping(typeof(bool), dependencies.Model) !; _doubleTypeMapping = (RelationalTypeMapping)_typeMappingSource.FindMapping(typeof(double), dependencies.Model) !; }
public virtual PostgresBinaryExpression AtTimeZone( SqlExpression timestamp, SqlExpression timeZone, Type type, RelationalTypeMapping? typeMapping = null) { // PostgreSQL AT TIME ZONE flips the given type from timestamptz to timestamp and vice versa // See https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT typeMapping ??= FlipTimestampTypeMapping( timestamp.TypeMapping ?? (RelationalTypeMapping?)_typeMappingSource.FindMapping(timestamp.Type, Dependencies.Model)!); return new PostgresBinaryExpression( PostgresExpressionType.AtTimeZone, ApplyDefaultTypeMapping(timestamp), ApplyDefaultTypeMapping(timeZone), type, typeMapping); RelationalTypeMapping FlipTimestampTypeMapping(RelationalTypeMapping mapping) { var storeType = mapping.StoreType; if (storeType.StartsWith("timestamp with time zone", StringComparison.Ordinal) || storeType.StartsWith("timestamptz", StringComparison.Ordinal)) { return _typeMappingSource.FindMapping("timestamp without time zone")!; } if (storeType.StartsWith("timestamp without time zone", StringComparison.Ordinal) || storeType.StartsWith("timestamp", StringComparison.Ordinal)) { return _typeMappingSource.FindMapping("timestamp with time zone")!; } throw new ArgumentException($"timestamp argument to AtTimeZone had unknown store type {storeType}", nameof(timestamp)); } }
public NpgsqlStringMethodTranslator( [NotNull] NpgsqlTypeMappingSource typeMappingSource, [NotNull] ISqlExpressionFactory sqlExpressionFactory) { _sqlExpressionFactory = sqlExpressionFactory; _whitespace = _sqlExpressionFactory.Constant( @" \t\n\r", // TODO: Complete this typeMappingSource.EStringTypeMapping); _textTypeMapping = (RelationalTypeMapping)typeMappingSource.FindMapping(typeof(string)); }
public NpgsqlStringMethodTranslator( NpgsqlTypeMappingSource typeMappingSource, ISqlExpressionFactory sqlExpressionFactory, IModel model) { _sqlExpressionFactory = sqlExpressionFactory; _whitespace = _sqlExpressionFactory.Constant( @" \t\n\r", // TODO: Complete this typeMappingSource.EStringTypeMapping); _textTypeMapping = typeMappingSource.FindMapping(typeof(string), model) !; }
static RelationalTypeMapping GetMapping(string storeType) => Mapper.FindMapping(storeType);
/// <summary> /// Inserts multiple records /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="dbContext"></param> /// <param name="entities"></param> public static int Execute <TEntity>(DbContext dbContext, IEnumerable <TEntity> entities) { /* Check dbContext against null */ if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } /* Check entities against null */ if (entities == null) { throw new ArgumentNullException(nameof(entities)); } /* NoOp */ if (entities.Count() == 0) { return(0); } /* Get the underlying ADO.NET DbConnection for this DbContext */ var connection = dbContext.Database.GetDbConnection(); /* Open a database connection, if not done already */ if (connection.State != ConnectionState.Open) { connection.Open(); } /* Get the type of the entity */ var classType = typeof(TEntity); /* Gets the type of the entity that maps the given entity class */ var entityType = dbContext.Model.FindEntityType(classType); /* Gets the relational database specific metadata for the specified entity */ var tableMetadata = entityType.Relational(); /* Type mapper */ var typeMappingSource = new NpgsqlTypeMappingSource( new TypeMappingSourceDependencies(new ValueConverterSelector(new ValueConverterSelectorDependencies()), Array.Empty <ITypeMappingSourcePlugin>()), new RelationalTypeMappingSourceDependencies(Array.Empty <IRelationalTypeMappingSourcePlugin>()), new NpgsqlSqlGenerationHelper(new RelationalSqlGenerationHelperDependencies()), null ); /* Get column info */ var properties = entityType.GetProperties() .Where(x => !x.IsPrimaryKey()) .Select(x => { /* Gets the relational database specific metadata for the current property */ var propertyMetadata = x.Relational(); return(new { PropertyName = x.Name, PropertyType = x.ClrType, PropertyInfo = classType.GetProperty(x.Name), propertyMetadata.ColumnName, NpgsqlType = (typeMappingSource.FindMapping(x.ClrType) as NpgsqlTypeMapping)?.NpgsqlDbType, propertyMetadata.ColumnType, x.IsNullable }); }) .ToList(); /* Build the COPY command */ var command = string.Format( @"COPY {0}""{1}"" ({2}) FROM STDIN BINARY;", string.IsNullOrWhiteSpace(tableMetadata.Schema) ? string.Empty : $"{tableMetadata.Schema}.", tableMetadata.TableName, properties.Select(x => $@"""{x.ColumnName}""").Aggregate((c, n) => $"{c}, {n}") ); /* Begin a binary COPY FROM STDIN operation */ using (var writer = (connection as NpgsqlConnection).BeginBinaryImport(command)) { foreach (var entity in entities) { if (entity == null) { continue; } /* Start writing a single row */ writer.StartRow(); foreach (var property in properties) { /* Get property value */ var value = property.PropertyInfo.GetValue(entity, null); /* Write null */ if (value == null) { writer.WriteNull(); continue; } if (property.NpgsqlType != null) { writer.Write(value, property.NpgsqlType.Value); } else { writer.Write(value is Enum ? (int)value : value, property.ColumnType); } } } /* Complete the import operation */ writer.Complete(); } return(entities.Count()); }