DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Query.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 using Deveel.Data;
21 using Deveel.Data.Diagnostics;
22 using Deveel.Data.Services;
23 using Deveel.Data.Sql;
24 
25 namespace Deveel.Data {
26  public sealed class Query : IQuery {
27  private Dictionary<string, object> metadata;
28 
29  internal Query(ISession session)
30  : this(session, null) {
31  }
32 
33  internal Query(ISession session, SqlQuery sourceQuery) {
34  Session = session;
35  SourceQuery = sourceQuery;
36 
38  Context.RegisterInstance<IQuery>(this);
39 
40  StartedOn = DateTimeOffset.UtcNow;
41 
42  metadata = GetMetadata();
43  }
44 
45  private Dictionary<string, object> GetMetadata() {
46  return new Dictionary<string, object> {
47  { "query.startTime", StartedOn },
48  { "query.source", SourceQuery }
49  };
50  }
51 
52  ~Query() {
53  Dispose(false);
54  }
55 
57  return Context.CreateBlockContext();
58  }
59 
60  public IBlock CreateBlock() {
61  return new Block(this);
62  }
63 
65  get { return this; }
66  }
67 
68  public IQueryContext Context { get; private set; }
69 
70  public ISession Session { get; private set; }
71 
72  public DateTimeOffset StartedOn { get; private set; }
73 
74  public SqlQuery SourceQuery { get; private set; }
75 
76  public bool HasSourceQuery {
77  get { return SourceQuery != null; }
78  }
79 
80  public void Dispose() {
81  Dispose(true);
82  GC.SuppressFinalize(this);
83  }
84 
85  private void Dispose(bool disposing) {
86  if (disposing) {
87  if (Context != null)
88  Context.Dispose();
89  }
90 
91  Context = null;
92  Session = null;
93  }
94 
96  get { return Context; }
97  }
98 
100  get { return Session; }
101  }
102 
103  IEnumerable<KeyValuePair<string, object>> IEventSource.Metadata {
104  get { return metadata; }
105  }
106  }
107 }
IBlockContext CreateBlockContext()
IQueryContext CreateQueryContext()
virtual void Dispose(bool disposing)
Definition: Context.cs:63
Query(ISession session)
Definition: Query.cs:29
Provides a context for executing queries, accessing the system resources and evaluation context...
This is a session that is constructed around a given user and a transaction, to the given database...
Definition: Session.cs:32
IEventSource ParentSource
Gets an optional parent source.
Definition: IEventSource.cs:49
IEnumerable< KeyValuePair< string, object > > Metadata
Gets the list of metadata associated to the source.
Definition: IEventSource.cs:57
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
IBlock CreateBlock()
Definition: Query.cs:60
void Dispose()
Definition: Query.cs:80
new ISessionContext Context
Definition: ISession.cs:48
Dictionary< string, object > GetMetadata()
Definition: Query.cs:45
Query(ISession session, SqlQuery sourceQuery)
Definition: Query.cs:33
Dictionary< string, object > metadata
Definition: Query.cs:27
void Dispose(bool disposing)
Definition: Query.cs:85
Represents the origin of system events, providing a mechanism to fill the metadata before dispatching...
Definition: IEventSource.cs:40