/
IQuery.cs
126 lines (107 loc) · 4.48 KB
/
IQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// IQuery.cs
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Threading.Tasks;
using Couchbase.Lite.Internal.Query;
using JetBrains.Annotations;
namespace Couchbase.Lite.Query
{
/// <summary>
/// Arguments for the <see cref="IQuery.AddChangeListener(TaskScheduler, EventHandler{QueryChangedEventArgs})" /> event
/// </summary>
public sealed class QueryChangedEventArgs : EventArgs
{
#region Properties
/// <summary>
/// Gets the error that occurred, if any
/// </summary>
[CanBeNull]
public Exception Error { get; }
/// <summary>
/// Gets the updated rows of the query
/// </summary>
[NotNull]
[ItemNotNull]
public IResultSet Results { get; }
#endregion
#region Constructors
internal QueryChangedEventArgs(IResultSet rows, Exception e = null)
{
Results = rows ?? new NullResultSet();
Error = e;
}
#endregion
}
/// <summary>
/// An interface representing a runnable query over a data source
/// </summary>
public interface IQuery : IDisposable
{
#region Properties
/// <summary>
/// Gets or sets the parameter collection for this query so that parameters may be
/// added for substitution into the query API (via <see cref="Expression.Parameter(string)"/>)
/// </summary>
/// <remarks>
/// The returned collection is a copy, and must be reset onto the query instance.
/// Doing so will trigger a re-run and update any listeners.
/// </remarks>
[NotNull]
Parameters Parameters { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Adds a change listener to track when this query instance has a change in
/// its results. Adding the first change listener will begin the live semantics.
/// </summary>
/// <param name="scheduler">The scheduler to use when firing events</param>
/// <param name="handler">The handler to call when the query result set changes</param>
/// <returns>A token that can be used to remove the listener later</returns>
ListenerToken AddChangeListener([CanBeNull]TaskScheduler scheduler, [NotNull]EventHandler<QueryChangedEventArgs> handler);
/// <summary>
/// Adds a change listener to track when this query instance has a change in
/// its results. Adding the first change listener will begin the live semantics.
/// </summary>
/// <param name="handler">The handler to call when the query result set changes</param>
/// <returns>A token that can be used to remove the listener later</returns>
ListenerToken AddChangeListener([NotNull]EventHandler<QueryChangedEventArgs> handler);
/// <summary>
/// Removes a changes listener based on the token that was received from
/// <see cref="AddChangeListener(TaskScheduler, EventHandler{QueryChangedEventArgs})"/>
/// </summary>
/// <param name="token">The received token from adding the change listener</param>
void RemoveChangeListener([NotNull]ListenerToken token);
/// <summary>
/// Runs the query
/// </summary>
/// <returns>The results of running the query</returns>
/// <exception cref="InvalidOperationException">Thrown if this query has
/// no database to operate on, or if it is missing SELECT or FROM statements (unusual)</exception>
[NotNull]
IResultSet Execute();
/// <summary>
/// Gets an explanation of what the query will do
/// </summary>
/// <returns>The explanation of the query</returns>
/// <exception cref="ObjectDisposedException">Thrown if this method is
/// called after disposal</exception>
[NotNull]
string Explain();
#endregion
}
}