DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
LocalDatabaseClient.cs
Go to the documentation of this file.
1 using System;
2 
4 using Deveel.Data.DbSystem;
5 
6 namespace Deveel.Data.Protocol {
8  private bool disposed;
9  private int openConnections;
10 
11  internal LocalDatabaseClient(LocalClient client, IDatabase database) {
12  Database = database;
13  Client = client;
14  }
15 
16  public void Dispose() {
17  if (openConnections == 0) {
18  Database = null;
19  Client = null;
20  disposed = true;
21  }
22  }
23 
24  private IDatabase Database { get; set; }
25 
27  get { return Client; }
28  }
29 
30  private LocalClient Client { get; set; }
31 
33  get {
34  AssertNotDisposed();
35  return Database.DatabaseContext.Configuration;
36  }
37  }
38 
39  private IDatabaseHandler DatabaseHandler {
40  get { return Client.SystemContext as IDatabaseHandler; }
41  }
42 
43  public bool IsBooted {
44  get {
45  AssertNotDisposed();
46  return Database.IsOpen;
47  }
48  }
49 
50  public bool Exist {
51  get {
52  AssertNotDisposed();
53  return Database.Exists;
54  }
55  }
56 
57  private void AssertNotDisposed() {
58  if (disposed)
59  throw new ObjectDisposedException(GetType().FullName);
60  }
61 
62  public IServerConnector Create(string adminUser, string adminPassword) {
63  AssertNotDisposed();
64 
65  if (String.IsNullOrEmpty(adminUser))
66  throw new ArgumentNullException("adminUser");
67  if (String.IsNullOrEmpty(adminPassword))
68  throw new ArgumentNullException("adminPassword");
69 
70  Database.Create(adminUser, adminPassword);
71  return new ServerConnector(this, DatabaseHandler);
72  }
73 
75  if (IsBooted)
76  throw new InvalidOperationException("The local database is already booted.");
77 
78  Database.Open();
79  return new ServerConnector(this, DatabaseHandler);
80  }
81 
83  if (!IsBooted)
84  throw new InvalidOperationException("The database is not booted.");
85 
86  return new ServerConnector(this, DatabaseHandler);
87  }
88 
89  #region ServerConnector
90 
92  public LocalDatabaseClient Client { get; set; }
93 
95  : base(handler) {
96  Client = client;
97  client.openConnections++;
98  }
99 
100  protected override void Dispose(bool disposing) {
101  if (disposing) {
102  Client.openConnections--;
103  if (Client.openConnections <= 0) {
104  Client.Dispose();
105  }
106  }
107 
108  Client = null;
109 
110  base.Dispose(disposing);
111  }
112  }
113 
114  #endregion
115  }
116 }
bool Exists
Gets a boolean value indicating if the database exists within the context given.
Definition: Database.cs:171
IServerConnector Create(string adminUser, string adminPassword)
bool IsOpen
Gets a boolean value that indicates if the database was open.
Definition: Database.cs:190
The default implementation of a database in a system.
Definition: Database.cs:38
The representation of a single database in the system.
Definition: IDatabase.cs:40
void Create(string adminName, string adminPassword)
Creates the database in the context given, granting the administrative control to the user identified...
Definition: Database.cs:240
void Open()
Opens the database making it ready to be accessed.
Definition: Database.cs:337
Defines the contract for the configuration node of a component within the system or of the system its...
ServerConnector(LocalDatabaseClient client, IDatabaseHandler handler)
LocalDatabaseClient(LocalClient client, IDatabase database)