DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
LocalClient.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 
5 using Deveel.Data.DbSystem;
6 
7 namespace Deveel.Data.Protocol {
8  public sealed class LocalClient : IClient {
9  private Dictionary<string, IDatabaseClient> localClients;
10  private bool disposed;
11 
12  public LocalClient(ISystemContext systemContext) {
13  if (systemContext == null)
14  throw new ArgumentNullException("systemContext");
15 
16  if (!(systemContext is IDatabaseHandler))
17  throw new ArgumentException("The system context does not handle databases");
18 
19  SystemContext = systemContext;
20  localClients = new Dictionary<string, IDatabaseClient>();
21  }
22 
24  Dispose(false);
25  }
26 
27  public void Dispose() {
28  Dispose(true);
29  GC.SuppressFinalize(this);
30  }
31 
32  private void Dispose(bool disposing) {
33  if (!disposed) {
34  if (disposing) {
35  if (localClients != null) {
36  foreach (var client in localClients.Values) {
37  client.Dispose();
38  }
39 
40  localClients.Clear();
41  }
42  }
43 
44  disposed = true;
45  }
46 
47  localClients = null;
48  }
49 
51  get { return SystemContext.Configuration; }
52  }
53 
54  public ISystemContext SystemContext { get; private set; }
55 
57  var dbConfig = Configuration.Merge(config);
58  var databaseName = config.DatabaseName();
59  if (String.IsNullOrEmpty(databaseName))
60  throw new ArgumentException("The given configuration does not provide any database name.");
61 
62  IDatabaseClient client;
63  if (!localClients.TryGetValue(databaseName, out client)) {
64  var dbHandler = SystemContext as IDatabaseHandler;
65  if (dbHandler == null)
66  throw new InvalidOperationException("The system context does not handle databases");
67 
68  var database = dbHandler.GetDatabase(databaseName);
69  if (database == null)
70  throw new InvalidOperationException(String.Format("The database '{0}' could not be found in the current context.", databaseName));
71 
72  client = new LocalDatabaseClient(this, database);
73  localClients[databaseName] = client;
74  }
75 
76  return client;
77  }
78  }
79 }
The execution context of a database system, that is defining the configurations and the components us...
This is the context of a database system, that handles the configurations and services used by all th...
IDatabaseClient ConnectToDatabase(IConfiguration config)
Definition: LocalClient.cs:56
void Dispose(bool disposing)
Definition: LocalClient.cs:32
IConfiguration Configuration
Gets the system configuration object
Dictionary< string, IDatabaseClient > localClients
Definition: LocalClient.cs:9
LocalClient(ISystemContext systemContext)
Definition: LocalClient.cs:12
Defines the contract for the configuration node of a component within the system or of the system its...