// TODO: add parameter to define view (e.g.: Id, OwnerId, DynProp1, DynProp4) public override async Task <Document> FindByIdAsync(long id, CancellationToken cancellationToken) { var options = new DocumentQueryOptions(); options.Ids.Add(id); return((await FindAllAsync(options, cancellationToken)).SingleOrDefault()); }
/// <summary> /// Gets the document with the specified options. /// </summary> /// <param name="options">The query options.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public override async Task <IList <Document> > FindAllAsync(DocumentQueryOptions options, CancellationToken cancellationToken) { await Task.Yield(); Trace.WriteLine("NullDocumentStore.FindAll"); return(DocumentList.ToList()); }
// TODO: add parameter to define view (e.g.: Id, OwnerId, DynProp1, DynProp4) public override async Task <IList <Document> > FindAllAsync(DocumentQueryOptions options, CancellationToken cancellationToken) { // TODO: cache var schema = (await FieldStore.FindAllAsync(cancellationToken)).Where(field => !options.FieldNames.Any() || options.FieldNames.Contains(field.Id)).ToDictionary(field => field.Id); var dynColNames = string.Join("", schema.Keys.Select(dynField => $",[{dynField}]")); var dynParamNames = string.Join("", schema.Keys.Select(dynField => $",@{dynField}")); var whereClauses = new List <string>(); var cmdParameters = new List <SqlParameter>(); if (options.Ids.Any()) { var idsString = string.Join(',', options.Ids); whereClauses.Add(" Id IN (SELECT convert(int, value) FROM string_split(@ids, ',')) "); cmdParameters.Add(Connection.CreateCommandParameter("@ids", SqlDbType.NVarChar, idsString)); } if (options.ParentIds.Any()) { var parentIdsString = string.Join(',', options.ParentIds); whereClauses.Add(" ParentId IN (SELECT value FROM string_split(@parentIds, ',')) "); cmdParameters.Add(Connection.CreateCommandParameter("@parentIds", SqlDbType.NVarChar, parentIdsString)); } var orderClause = $" ORDER BY {(string.IsNullOrEmpty(options.OrderBy) ? "Id" : options.OrderBy)} {(options.OrderAsc ? string.Empty : "DESC")}"; var skipClause = options.Offset > 0 || options.Limit > 0 ? $" OFFSET {options.Offset} ROWS " : string.Empty; var takeClause = options.Limit > 0 ? $" FETCH NEXT {options.Limit} ROWS ONLY " : string.Empty; var cmdText = $@"SELECT [Id], [CreatedDate], [ModifiedDate], [ParentId] {dynColNames} FROM [Document] {(whereClauses.Any() ? $" WHERE {string.Join(" AND ", whereClauses)}" : string.Empty)} {orderClause} {skipClause} {takeClause}"; var documents = await Connection !.ExecuteQueryAsync(Connection.CreateCommand(cmdText, cmdParameters), (sqlReader) => DocumentHelpers.ReadDocuments(sqlReader, schema.Values), cancellationToken); return(documents); }
/// <summary> /// Gets the document ids with the specified options. /// </summary> /// <param name="options">The query options.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public override async Task <Dictionary <long, string> > GetDocumentIdsByIdAsync(DocumentQueryOptions options, CancellationToken cancellationToken) { // TODO: cache var documentIdField = (await FieldStore.FindAllAsync(cancellationToken)).First(field => field.Name == Field.DocumentIdFieldName); var whereClauses = new List <string>(); var cmdParameters = new List <SqlParameter>(); if (options.Ids.Any()) { var idsString = string.Join(',', options.Ids); whereClauses.Add(" Id IN (SELECT convert(int, value) FROM string_split(@ids, ',')) "); cmdParameters.Add(Connection.CreateCommandParameter("@ids", SqlDbType.NVarChar, idsString)); } if (options.ParentIds.Any()) { var parentIdsString = string.Join(',', options.ParentIds); whereClauses.Add(" ParentId IN (SELECT value FROM string_split(@parentIds, ',')) "); cmdParameters.Add(Connection.CreateCommandParameter("@parentIds", SqlDbType.NVarChar, parentIdsString)); } var orderClause = $" ORDER BY {(string.IsNullOrEmpty(options.OrderBy) ? "Id" : options.OrderBy)} {(options.OrderAsc ? string.Empty : "DESC")}"; var skipClause = options.Offset > 0 || options.Limit > 0 ? $" OFFSET {options.Offset} ROWS " : string.Empty; var takeClause = options.Limit > 0 ? $" FETCH NEXT {options.Limit} ROWS ONLY " : string.Empty; var cmdText = $@"SELECT [Id], [{documentIdField.Id}] FROM [Document] {(whereClauses.Any() ? $" WHERE {string.Join(" AND ", whereClauses)}" : string.Empty)} {orderClause} {skipClause} {takeClause}"; var documentIdsById = await Connection !.ExecuteQueryAsync(Connection.CreateCommand(cmdText, cmdParameters), (sqlReader) => { var documentIdsById = new Dictionary <long, string>(); while (sqlReader.Read()) { documentIdsById.Add(sqlReader.GetInt64(0), sqlReader.GetString(1)); } return(documentIdsById); }, cancellationToken); return(documentIdsById); }
/// <summary> /// Gets the document ids with the specified options. /// </summary> /// <param name="options">The query options.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public override async Task <Dictionary <long, string> > GetDocumentIdsByIdAsync(DocumentQueryOptions options, CancellationToken cancellationToken) { await Task.Yield(); Trace.WriteLine("NullDocumentStore.GetDocumentIdsByIdAsync"); var result = new Dictionary <long, string>(); if (!DocumentList.Any()) { return(result); } var documentIdFieldKey = DocumentList.First().Fields.Keys.FirstOrDefault(f => f.Contains("DocumentId", StringComparison.InvariantCultureIgnoreCase)); if (string.IsNullOrWhiteSpace(documentIdFieldKey)) { return(result); } for (int d = 0; d < DocumentList.Count; d++) { result.Add(d, DocumentList.ToList()[d].Fields[documentIdFieldKey]?.ToString() ?? ""); } return(result); }