public async Task <SqlResponse <T> > DeleteBatch([NotNull] T[] batch, IResolveFieldContext context) { var selections = context.GetQuerySelections().ToArray(); var columns = context.GetArgument <IDictionary <string, object> >(nameof(batch)).Keys.ToArray(); var request = new BatchRequest { Delete = true, Table = this.Table, Input = batch.MapRowSet(columns) }; selections .If(selection => selection.Left(nameof(SqlResponse <T> .Data))) .To(selection => selection.TrimStart($"{nameof(SqlResponse<T>.Data)}.") !) .Do(selection => request.Output[selection] = "DELETED"); var sqlResponse = new SqlResponse <T>(); if (selections.Has(nameof(SqlResponse <T> .Table))) { sqlResponse.Table = this.Table; } if (selections.Any(selection => selection.Left(nameof(SqlResponse <T> .SQL)))) { sqlResponse.SQL = await this._Mediator.ApplyRulesAsync <BatchRequest, string>(request); } if (request.Output.Any()) { var data = await this._Mediator.ApplyRulesAsync <(ISqlApi, BatchRequest), RowSet>((this._SqlApi, request)); sqlResponse.Data = data?.Rows is not null?data.MapModels <T>() : Array <T> .Empty; } return(sqlResponse); }
public async Task <SqlResponse <T> > Delete(string where, [AllowNull] Parameter[] parameters, IResolveFieldContext context) { var selections = context.GetQuerySelections().ToArray(); var request = new DeleteRequest { From = this.Table, Where = where }; parameters.Do(parameter => request.Parameters[parameter.Name] = parameter.Value); selections .If(selection => selection.Left(nameof(SqlResponse <T> .Data))) .To(selection => selection.TrimStart($"{nameof(SqlResponse<T>.Data)}.") !) .Do(selection => request.Output[selection] = "DELETED"); var sqlResponse = new SqlResponse <T>(); if (selections.Has(nameof(SqlResponse <T> .Table))) { sqlResponse.Table = this.Table; } if (selections.Any(selection => selection.Left(nameof(SqlResponse <T> .SQL)))) { sqlResponse.SQL = await this._Mediator.ApplyRulesAsync <DeleteRequest, string>(request); } if (request.Output.Any()) { var data = await this._Mediator.ApplyRulesAsync <(ISqlApi, DeleteRequest), RowSet>((this._SqlApi, request)); sqlResponse.Data = data?.Rows is not null?data.MapModels <T>() : Array <T> .Empty; } return(sqlResponse); }
public async Task <SqlResponse <T> > Update([AllowNull] Parameter[] parameters, T set, string where, IResolveFieldContext context) { var selections = context.GetQuerySelections().ToArray(); var columns = context.GetArgument <IDictionary <string, object> >(nameof(set)).Keys.ToArray(); var request = new UpdateRequest { Table = this.Table, Where = where }; parameters.Do(parameter => request.Parameters[parameter.Name] = parameter.Value); selections .If(selection => selection.Left(nameof(SqlResponse <T> .Data))) .To(selection => selection.TrimStart($"{nameof(SqlResponse<T>.Data)}.") !) .Do(selection => request.Output[selection] = "INSERTED"); columns.Do(column => { var property = TypeOf <T> .Properties.Values.First(property => property.Attributes.First <GraphNameAttribute>()?.Name.Is(column) is true) ?? TypeOf <T> .Properties.Get(column) !; request.Set[property.Name] = property.GetValue(set); }); var sqlResponse = new SqlResponse <T>(); if (selections.Has(nameof(SqlResponse <T> .Table))) { sqlResponse.Table = this.Table; } if (selections.Any(selection => selection.Left(nameof(SqlResponse <T> .SQL)))) { sqlResponse.SQL = await this._Mediator.ApplyRulesAsync <UpdateRequest, string>(request); } if (request.Output.Any()) { var data = await this._Mediator.ApplyRulesAsync <(ISqlApi, UpdateRequest), RowSet>((this._SqlApi, request)); sqlResponse.Data = data?.Rows is not null?data.MapModels <T>() : Array <T> .Empty; } return(sqlResponse); }
public async Task <SqlResponse <T> > Select( [AllowNull] string where, [AllowNull] string having, [AllowNull] OrderBy <T>[] orderBy, [AllowNull] Parameter[] parameters, IResolveFieldContext context) { var selections = context.GetQuerySelections().ToArray(); var request = new SelectRequest { From = this.Table, Having = having, Where = where }; parameters.Do(parameter => request.Parameters[parameter.Name] = parameter.Value); selections .If(selection => selection.Left(nameof(SqlResponse <T> .Data))) .To(selection => selection.TrimStart($"{nameof(SqlResponse<T>.Data)}.") !) .Do(selection => request.Select[selection] = selection); orderBy.Do(_ => request.OrderBy[_.Expression] = _.Sort); var sqlResponse = new SqlResponse <T>(); if (selections.Has(nameof(SqlResponse <T> .Table))) { sqlResponse.Table = this.Table; } if (selections.Any(selection => selection.Left(nameof(SqlResponse <T> .SQL)))) { sqlResponse.SQL = await this._Mediator.ApplyRulesAsync <SelectRequest, string>(request); } if (request.Select.Any()) { var data = await this._Mediator.ApplyRulesAsync <(ISqlApi, SelectRequest), RowSet>((this._SqlApi, request)); sqlResponse.Data = data?.Rows is not null?data.MapModels <T>() : Array <T> .Empty; } return(sqlResponse); }