public void TestUpsert() { var toInsert = new List <StandardModel>(); toInsert.Add(new StandardModel { PK = 1, FK = 1000, Name = DateTime.Now.ToString("s") }); toInsert.Add(new StandardModel { PK = 2, FK = 1002, Name = DateTime.Now.ToString("s") }); toInsert.Add(new StandardModel { PK = 3, FK = 1003, Name = DateTime.Now.ToString("s") }); toInsert.Add(new StandardModel { PK = 103, FK = 1004, Name = DateTime.Now.ToString("s") }); var upsertOptions = new UpsertOptions <StandardModel>() .AddMatchColumn(p => p.PK) .AddMapColumn(p => p.FK) .AddMapColumn(p => p.Name) .SetTargetTableWithSchema("dbo", "StandardModels"); _fixture.Connection.Upsert(toInsert, upsertOptions); int actual; using (var verifyCmd = _fixture.Connection.CreateCommand()) { verifyCmd.CommandText = "SELECT COUNT(*) FROM dbo.StandardModels"; actual = (int)verifyCmd.ExecuteScalar(); } Assert.Equal(4, actual); }
public void AddTwoColumnToInternalMatchList() { // arrange var instance = new UpsertOptions <StandardModel>(); var expected = "PK,Name"; // act instance.AddMatchColumn(p => p.PK).AddMatchColumn(p => p.Name); var actual = string.Join(",", instance._matchColumns); // assert Assert.Equal(expected, actual); }
public void ShouldGetThreeNamedColumns() { // arrange var instance = new UpsertOptions <StandardModel>(); var expected = "PK,FK,Name"; // act instance.MapAllColumns(); var actual = string.Join(",", instance._mapColumns); // assert Assert.Equal(expected, actual); }
public void AddColumnToInternalMatchList() { // arrange var instance = new UpsertOptions <StandardModel>(); var expected = "PK"; // act instance.AddMatchColumn(p => p.PK); var actual = instance._matchColumns.First(); // assert Assert.Equal(expected, actual); }
public async Task WhereCasWorks() { // insert a new document var id = Guid.NewGuid().ToString(); var pizza = new Pizza { SizeInches = 14, Toppings = new List <string> { "Pepperoni", "Mushroom" }, ExtraCheese = true }; await Collection.InsertAsync(id, pizza); // get document with CAS IGetResult getResult = await Collection.GetAsync(id); Assert.That(getResult.Cas, Is.GreaterThan(0)); // replace with CAS pizza.ExtraCheese = false; var replaceOptions = new ReplaceOptions(); replaceOptions.Cas(getResult.Cas); // there IS a CAS option for replace IMutationResult replaceResult = await Collection.ReplaceAsync(id, pizza, replaceOptions); Assert.That(replaceResult.Cas, Is.Not.EqualTo(getResult.Cas)); Assert.That(replaceResult.Cas, Is.GreaterThan(0)); // upsert with CAS? pizza.ExtraCheese = true; var upsertOptions = new UpsertOptions(); // upsertOptions.Cas(. . . ) there is no Cas option here! IMutationResult upsertResult = await Collection.UpsertAsync(id, pizza, upsertOptions); Assert.That(upsertResult.Cas, Is.GreaterThan(0)); Assert.That(upsertResult.Cas, Is.Not.EqualTo(getResult.Cas)); Assert.That(upsertResult.Cas, Is.Not.EqualTo(replaceResult.Cas)); // if you have a CAS value, use replace instead }
public async Task UpsertAsync(Entity entity, UpsertOptions upsertOptions = UpsertOptions.None) { string fullUrl = ApiUrl + RequestEntityParser.GetEntityApiUrl(entity, WebApiMetadata); JObject jObject = RequestEntityParser.EntityToJObject(entity, WebApiMetadata); var request = new HttpRequestMessage(new HttpMethod("PATCH"), fullUrl) { Content = new StringContent(JsonConvert.SerializeObject(jObject), Encoding.UTF8, "application/json") }; if (upsertOptions == UpsertOptions.OnlyUpdate) { request.Headers.Add("If-Match", "*"); } if (upsertOptions == UpsertOptions.OnlyCreate) { request.Headers.Add("If-None-Match", "*"); } HttpResponseMessage response = await Authorization.GetHttpClient().SendAsync(request); ResponseValidator.EnsureSuccessStatusCode(response); }
ObjectDbCommandBuilder <AbstractCommand, AbstractParameter, TArgument> IUpsertHelper <AbstractCommand, AbstractParameter, AbstractObjectName, AbstractDbType> .OnInsertOrUpdateObject <TArgument>(AbstractObjectName tableName, TArgument argumentValue, UpsertOptions options) { return(new SQLiteInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }
ObjectDbCommandBuilder <SQLiteCommand, SQLiteParameter, TArgument> OnInsertOrUpdateObject <TArgument>(SQLiteObjectName tableName, TArgument argumentValue, UpsertOptions options) where TArgument : class { return(new SQLiteInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }
/// <summary> /// Performs an insert or update operation as appropriate. /// </summary> /// <typeparam name="TArgument"></typeparam> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options for how the insert/update occurs.</param> /// <returns></returns> public ObjectDbCommandBuilder <SqlCommand, SqlParameter, TArgument> Upsert <TArgument>(TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(Upsert(DatabaseMetadata.GetTableOrViewFromClass <TArgument>().Name, argumentValue, options)); }
/// <summary> /// Performs an insert or update operation as appropriate. /// </summary> /// <param name="tableName">Name of the table.</param> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options for how the insert/update occurs.</param> /// <returns>SqlServerUpdate.</returns> public ObjectDbCommandBuilder <SqlCommand, SqlParameter, TArgument> Upsert <TArgument>(SqlServerObjectName tableName, TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(new SqlServerInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }
/// <summary> /// Perform an insert or update operation as appropriate. /// </summary> /// <typeparam name="TArgument"></typeparam> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options for how the insert/update occurs.</param> /// <returns></returns> public ObjectDbCommandBuilder <AbstractCommand, AbstractParameter, TArgument> Upsert <TArgument>(TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(OnInsertOrUpdateObject <TArgument>(DatabaseMetadata.GetTableOrViewFromClass <TArgument>().Name, argumentValue, options)); }
/// <summary> /// Creates a operation used to perform an "upsert" operation. /// </summary> /// <param name="tableName"></param> /// <param name="argumentValue"></param> /// <param name="options"></param> /// <returns></returns> public ObjectDbCommandBuilder <AbstractCommand, AbstractParameter, TArgument> Upsert <TArgument>(AbstractObjectName tableName, TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(OnInsertOrUpdateObject <TArgument>(tableName, argumentValue, options)); }
IObjectDbCommandBuilder <TArgument> ISupportsUpsert.Upsert <TArgument>(TArgument argumentValue, UpsertOptions options) { return(DataSource.OnInsertOrUpdateObject(DataSource.DatabaseMetadata.GetTableOrViewFromClass <TArgument>().Name, argumentValue, options)); }
IObjectDbCommandBuilder <TArgument> ISupportsUpsert.Upsert <TArgument>(string tableName, TArgument argumentValue, UpsertOptions options) { return(DataSource.OnInsertOrUpdateObject(DataSource.DatabaseMetadata.ParseObjectName(tableName), argumentValue, options)); }
IObjectDbCommandBuilder <TArgument> IClass1DataSource.Upsert <TArgument>(TArgument argumentValue, UpsertOptions options) { return(Upsert(argumentValue, options)); }
IObjectDbCommandBuilder <TArgument> IClass1DataSource.Upsert <TArgument>(TArgument argumentValue, UpsertOptions options) { throw new NotImplementedException("See issue #122"); //return Upsert(argumentValue, options); }
IObjectDbCommandBuilder <TArgument> IClass1DataSource.Upsert <TArgument>(TArgument argumentValue, UpsertOptions options) { #if ACCESS throw new NotImplementedException("See issue #122"); #else return(Upsert(argumentValue, options)); #endif }
public void Upsert(Entity entity, UpsertOptions upsertOptions = UpsertOptions.None) { UpsertAsync(entity, upsertOptions).GetAwaiter().GetResult(); }
/// <summary> /// Initializes a new instance of the <see cref="MySqlInsertOrUpdateObject{TArgument}"/> class. /// </summary> /// <param name="dataSource">The data source.</param> /// <param name="tableName">Name of the table.</param> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options.</param> public MySqlInsertOrUpdateObject(MySqlDataSourceBase dataSource, MySqlObjectName tableName, TArgument argumentValue, UpsertOptions options) : base(dataSource, tableName, argumentValue) { m_Options = options; }
/// <summary> /// Initializes a new instance of the <see cref="OleDbSqlServerInsertOrUpdateObject{TArgument}"/> class. /// </summary> /// <param name="dataSource">The data source.</param> /// <param name="tableName">Name of the table.</param> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options.</param> public OleDbSqlServerInsertOrUpdateObject(OleDbSqlServerDataSourceBase dataSource, SqlServerObjectName tableName, TArgument argumentValue, UpsertOptions options) : base(dataSource, tableName, argumentValue) { m_Options = options; }
/// <summary> /// Upserts the specified table name. /// </summary> /// <typeparam name="TArgument">The type of the t argument.</typeparam> /// <param name="tableName">Name of the table.</param> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options.</param> /// <returns>ObjectDbCommandBuilder<NpgsqlCommand, NpgsqlParameter, TArgument>.</returns> public ObjectDbCommandBuilder <NpgsqlCommand, NpgsqlParameter, TArgument> Upsert <TArgument>(PostgreSqlObjectName tableName, TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(new PostgreSqlInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }
ObjectDbCommandBuilder <MySqlCommand, MySqlParameter, TArgument> OnInsertOrUpdateObject <TArgument>(MySqlObjectName tableName, TArgument argumentValue, UpsertOptions options) where TArgument : class { return(new MySqlInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }
public ObjectDbCommandBuilder <TCommand, TParameter, TArgument> Upsert <TArgument>(TObjectName tableName, TArgument argumentValue, UpsertOptions options = UpsertOptions.None) where TArgument : class { return(DataSource.OnInsertOrUpdateObject(tableName, argumentValue, options)); }
IObjectDbCommandBuilder <TArgument> IClass1DataSource.Upsert <TArgument>(string tableName, TArgument argumentValue, UpsertOptions options) { return(Upsert(tableName, argumentValue, options)); }
/// <summary> /// Perform an insert or update operation as appropriate. /// </summary> /// <param name="tableName">Name of the table.</param> /// <param name="argumentValue">The argument value.</param> /// <param name="options">The options for how the insert/update occurs.</param> /// <returns>SqlServerUpdate.</returns> public ObjectDbCommandBuilder <AbstractCommand, AbstractParameter, TArgument> Upsert <TArgument>(string tableName, TArgument argumentValue, UpsertOptions options) where TArgument : class { return(Upsert <TArgument>(new AbstractObjectName(tableName), argumentValue, options)); }
/// <summary> /// Initializes a new instance of the <see cref="SQLiteInsertOrUpdateObject{TArgument}"/> class. /// </summary> /// <param name="dataSource"></param> /// <param name="tableName"></param> /// <param name="argumentValue"></param> /// <param name="options"></param> public SQLiteInsertOrUpdateObject(SQLiteDataSourceBase dataSource, SQLiteObjectName tableName, TArgument argumentValue, UpsertOptions options) : base(dataSource, tableName, argumentValue) { m_Options = options; }
ObjectDbCommandBuilder <OleDbCommand, OleDbParameter, TArgument> OnInsertOrUpdateObject <TArgument>(SqlServerObjectName tableName, TArgument argumentValue, UpsertOptions options) where TArgument : class { return(new OleDbSqlServerInsertOrUpdateObject <TArgument>(this, tableName, argumentValue, options)); }