private GradientNode(string name, Expression min, Expression max, OnConflict onConflict, StopNode[] stops) { Name = name; Min = min; Max = max; OnConflict = onConflict; Stops = stops; }
public virtual Task Upsert(TEntity entity, OnConflict onConflict) { var queryParams = new Dictionary <string, object>(); var query = QueryBuilder.Upsert(entity, onConflict, queryParams); Logger.LogInformation("Executing query: {@Query}", new { Method = nameof(Upsert), Query = query, Params = queryParams }); return(Connection .ExecuteAsync( query, queryParams, Transaction)); }
protected virtual string GetUpsertStatement(PropertyColumnMap mapping, OnConflict onConflict, QueryParameters queryParameters) { var insertColumnExpressions = GetInsertStatement(mapping, queryParameters); var updateColumnsMapping = ExcludeColumnsForUpdate(mapping); var updateColumnExpressions = GetColumnValueList( GetColumnParameterMap(updateColumnsMapping, queryParameters) .ToDictionary(x => x.Key, x => $"EXCLUDED.{x.Key}"), insert: false); var keyColumn = Schema.Columns.Mapping[Schema.Columns.KeyProperty].ColumnName; var sql = insertColumnExpressions; var onConflictExpression = $"ON CONFLICT({ keyColumn })"; switch (onConflict) { case OnConflict.Update: sql += $@" { onConflictExpression } DO UPDATE SET { updateColumnExpressions } "; break; case OnConflict.DoNothing: sql += $@" { onConflictExpression } DO NOTHING"; break; default: break; } return(FormatSqlStatement(sql)); }
/// <summary> /// Inserts multiple values into a table efficiently using a transaction /// </summary> /// <typeparam name="T">Value type</typeparam> /// <param name="connection">The connection to be used</param> /// <param name="items">Items to be inserted</param> /// <param name="resolution">Conflict resolution policy</param> /// <param name="tableName">The name of the table, NULL to use `T` type name as the name of the table</param> /// <returns>Returns the integer Primary Key or __ROWID__ of the inserted rows</returns> public static long[] BulkInsert <T>(this ISqliteConnection connection, IEnumerable <T> items, OnConflict resolution = OnConflict.Abort, string tableName = null) { List <long> ids = new List <long>(); string sql = HelperFunctions.buildInsertSql <T>(connection.typeCollection, resolution, tableName); using var trn = connection.connection.BeginTransaction(); using var cmd = new SqliteCommand(sql, connection.connection, trn); foreach (var item in items) { cmd.Parameters.Clear(); HelperFunctions.fillParameters(cmd, item, connection.typeCollection); var scalar = cmd.ExecuteScalar(); if (scalar is long sL) { ids.Add(sL); } } trn.Commit(); return(ids.ToArray()); }
/// <summary> /// Inserts a value into a table /// </summary> /// <typeparam name="T">Value type</typeparam> /// <param name="connection">The connection to be used</param> /// <param name="item">Item to be inserted</param> /// <param name="resolution">Conflict resolution policy</param> /// <param name="tableName">The name of the table, NULL to use `T` type name as the name of the table</param> /// <returns>Returns the integer Primary Key or __ROWID__ of the inserted row</returns> public static long Insert <T>(this ISqliteConnection connection, T item, OnConflict resolution = OnConflict.Abort, string tableName = null) => connection.ExecuteScalar <long>(HelperFunctions.buildInsertSql <T>(connection.typeCollection, resolution, tableName), item);
internal static string buildInsertSql <T>(ReaderCachedCollection typeCollection, OnConflict resolution, string tableName = null) { var info = typeCollection.GetInfo <T>(); if (tableName == null) { tableName = info.TypeName; } var names = getNames(info, !info.IsAnonymousType); var fields = string.Join(",", names); var values = string.Join(",", names.Select(n => $"@{n}")); if (resolution == OnConflict.Abort) { return($"INSERT INTO {tableName} ({fields}) VALUES ({values}); SELECT last_insert_rowid();"); } else { string txtConflict = resolution switch { OnConflict.RollBack => "ROLLBACK", OnConflict.Fail => "FAIL", OnConflict.Ignore => "IGNORE", OnConflict.Replace => "REPLACE", _ => throw new ArgumentException($"Invalid resolution: {resolution}"), }; return($"INSERT OR {txtConflict} INTO {tableName} ({fields}) VALUES ({values}); SELECT last_insert_rowid();"); } }
public virtual string Upsert(TEntity entity, OnConflict onConflict, QueryParameters queryParams) { ExtractValues(entity, queryParams); return(GetUpsertStatement(Schema.Columns.Mapping, onConflict)); }
public Insert OnConflict(OnConflict onConflict) { _onConflict = onConflict; _onConflict.Values(_valueClauses); return(this); }