private static InsertProperties GetInsertProperties(Type type) { var properties = type.GetProperties(); var insertProperties = new InsertProperties(); foreach (var property in properties) { var propertyAttributes = property.GetCustomAttributes(typeof(IgnoreOnInsertAttribute), false); if (type.IsSubclassOf(typeof(DbIdObject)) && property.Name == "Id") { continue; } if (propertyAttributes.Length == 0) { insertProperties.Insert.Add(property); } else { insertProperties.Ignore.Add(property); } } return(insertProperties); }
public async Task AddDbContextFactory_ContextFactoryCreate_SaveChangesAsync() { //Arrange var serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext <IKSqlDBContext, KSqlDBContext>(options => options.UseKSqlDb(KSqlDbRestApiProvider.KsqlDbUrl), ServiceLifetime.Transient); serviceCollection.AddDbContextFactory <IKSqlDBContext>(factoryLifetime: ServiceLifetime.Scoped); var contextFactory = serviceCollection.BuildServiceProvider().GetRequiredService <IKSqlDBContextFactory <IKSqlDBContext> >(); var config = new InsertProperties { EntityName = EntityName, ShouldPluralizeEntityName = false }; var entity1 = new Movie { Id = 3, Title = "T3" }; var entity2 = new Movie { Id = 4, Title = "T4" }; //Act await using var context = contextFactory.Create(); context.Add(entity1, config); context.Add(entity2, config); var response = await context.SaveChangesAsync(); var c = await response.Content.ReadAsStringAsync(); //Assert response.StatusCode.Should().Be(HttpStatusCode.OK); }
/// <summary> /// Produce a row into an existing stream or table and its underlying topic based on explicitly specified entity properties. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity">Entity for insertion.</param> /// <param name="insertProperties">Overrides conventions.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns>Http response object.</returns> public Task <HttpResponseMessage> InsertIntoAsync <T>(T entity, InsertProperties insertProperties = null, CancellationToken cancellationToken = default) { var insertStatement = ToInsertStatement(entity, insertProperties); var httpResponseMessage = ExecuteStatementAsync(insertStatement, cancellationToken); return(httpResponseMessage); }
public void AddWithInsertProperties() { //Arrange var context = new TestableDbProvider <string>(TestParameters.KsqlDBUrl); var entity = new Tweet(); var insertProperties = new InsertProperties(); //Act context.Add(entity, insertProperties); //Assert context.KSqlDbRestApiClientMock.Verify(c => c.ToInsertStatement(entity, insertProperties), Times.Once); }
public void Generate_ShouldNotPluralizeEntityName() { //Arrange var movie = new Movie { Id = 1, Release_Year = 1988, Title = "Title" }; var insertProperties = new InsertProperties { ShouldPluralizeEntityName = false }; //Act string statement = new CreateInsert().Generate(movie, insertProperties); //Assert statement.Should().Be($"INSERT INTO {nameof(Movie)} (Title, Id, Release_Year) VALUES ('Title', 1, 1988);"); }
public void Generate_OverrideEntityName() { //Arrange var movie = new Movie { Id = 1, Release_Year = 1988, Title = "Title" }; var insertProperties = new InsertProperties { EntityName = "TestName" }; //Act string statement = new CreateInsert().Generate(movie, insertProperties); //Assert statement.Should().Be($"INSERT INTO {insertProperties.EntityName}s (Title, Id, Release_Year) VALUES ('Title', 1, 1988);"); }
public void Generate_ImmutableRecordType() { //Arrange var book = new Book("Title", "Author"); var insertProperties = new InsertProperties { ShouldPluralizeEntityName = false }; //Act string statement = new CreateInsert().Generate(book, insertProperties); //Assert statement.Should().Be($"INSERT INTO {nameof(Book)} (Title, Author) VALUES ('Title', 'Author');"); }
private static string GetOutputString(InsertProperties properties, string idColumnName = null) { var propertyStrings = new List <string>(); if (idColumnName != null) { propertyStrings.Add($"INSERTED.[{idColumnName}] AS [Id]"); } foreach (var property in properties.Ignore) { propertyStrings.Add($"INSERTED.[{property.Name}]"); } if (propertyStrings.Count > 0) { return($"OUTPUT {string.Join(", ", propertyStrings)} "); } return(string.Empty); }
private static void UpdateObjectProperties <T>(T obj, T insertedObj, InsertProperties properties, bool idColumn) { if (insertedObj == null) { return; } var type = obj.GetType(); if (idColumn) { var newId = type.GetProperty("Id").GetValue(insertedObj, null); type.GetProperty("Id").SetValue(obj, newId); } foreach (var property in properties.Ignore) { var newValue = type.GetProperty(property.Name).GetValue(insertedObj, null); type.GetProperty(property.Name).SetValue(obj, newValue); } }
internal string Generate <T>(T entity, InsertProperties insertProperties = null) { insertProperties ??= new InsertProperties(); var entityName = GetEntityName <T>(insertProperties); bool isFirst = true; var columnsStringBuilder = new StringBuilder(); var valuesStringBuilder = new StringBuilder(); foreach (var memberInfo in Members <T>(insertProperties?.IncludeReadOnlyProperties)) { if (isFirst) { isFirst = false; } else { columnsStringBuilder.Append(", "); valuesStringBuilder.Append(", "); } columnsStringBuilder.Append(memberInfo.Name); var type = GetMemberType(memberInfo); var value = new CreateKSqlValue().ExtractValue(entity, insertProperties, memberInfo, type); valuesStringBuilder.Append(value); } string insert = $"INSERT INTO {entityName} ({columnsStringBuilder}) VALUES ({valuesStringBuilder});"; return(insert); }
public async Task AddAndSaveChangesAsync() { //Arrange var config = new InsertProperties { EntityName = EntityName, ShouldPluralizeEntityName = false }; var entity1 = new Movie { Id = 1, Title = "T1" }; var entity2 = new Movie { Id = 2, Title = "T2" }; //Act Context.Add(entity1, config); Context.Add(entity2, config); var response = await Context.SaveChangesAsync(); var c = await response.Content.ReadAsStringAsync(); //Assert response.StatusCode.Should().Be(HttpStatusCode.OK); }
/// <summary> /// Constructs an enumeration of all the parameters denoting properties that are bound to columns available for insert. /// (e.g. @HouseNo, @AptNo) /// </summary> protected virtual string ConstructParamEnumerationForInsertInternal() { return(string.Join(",", InsertProperties.Select(propInfo => $"{ParameterPrefix + propInfo.PropertyName}"))); }
/// <summary> /// Constructs an enumeration of all the columns available for insert. /// (e.g. HouseNo, AptNo) /// </summary> protected virtual string ConstructColumnEnumerationForInsertInternal() { return(string.Join(",", InsertProperties.Select(propInfo => GetColumnName(propInfo, null, false)))); }
/// <summary> /// Generates raw string Insert Into, but does not execute it. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity">Entity for insertion.</param> /// <param name="insertProperties">Overrides conventions.</param> /// <returns></returns> public KSqlDbStatement ToInsertStatement <T>(T entity, InsertProperties insertProperties = null) { var insert = new CreateInsert().Generate(entity, insertProperties); return(new KSqlDbStatement(insert)); }