DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DefaultLocalBootable.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 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 using System;
17 using System.Data;
18 
20 using Deveel.Data.Control;
21 using Deveel.Data.DbSystem;
22 
23 namespace Deveel.Data.Protocol {
31  public class DefaultLocalBootable : ILocalBootable, IDatabaseHandler {
32  public DefaultLocalBootable(DbController controller, string databaseName) {
33  this.controller = controller;
34  this.databaseName = databaseName;
35  }
36 
37  private readonly DbController controller;
38  private readonly string databaseName;
39 
43  private bool booted;
44 
48  private Control.DbSystem dbsys;
49 
57  private int connectId;
58 
62  private int openConnections;
63 
65  public IDatabaseInterface Create(string username, string password, IDbConfig config) {
66  if (String.IsNullOrEmpty(username) ||
67  String.IsNullOrEmpty(password))
68  throw new DataException("Username and Password must both be set.");
69 
70  if (booted)
71  throw new DataException("Database is already created.");
72 
73  // Local connections are formatted as;
74  // 'Local/[type]/[connect_id]'
75  string hostString = String.Format("{0}/Create/", KnownConnectionProtocols.Local);
76 
77  // Create the DbSystem and bind it to a IDatabaseInterface.
78  dbsys = controller.CreateDatabase(config, databaseName, username, password);
79  IDatabaseInterface dbInterface = new LocalDatabaseInterface(this, hostString);
80 
81  booted = true;
82  ++openConnections;
83 
84  return dbInterface;
85  }
86 
87 
89  return (dbsys == null ? null : dbsys.Database);
90  }
91 
93  public IDatabaseInterface Boot(IDbConfig config) {
94  if (booted)
95  throw new DataException("Database was booted more than once.");
96 
97  // Local connections are formatted as;
98  // 'Local/[type]/[connect_id]'
99  string hostString = String.Format("{0}/Boot/", KnownConnectionProtocols.Local);
100 
101  // Start the DbSystem and bind it to a IDatabaseInterface.
102  if (controller.IsInitialized(databaseName))
103  dbsys = controller.ConnectToDatabase(config, databaseName);
104  else
105  dbsys = controller.StartDatabase(config, databaseName);
106 
107  IDatabaseInterface dbInterface = new LocalDatabaseInterface(this, hostString);
108 
109  booted = true;
110  ++openConnections;
111 
112  return dbInterface;
113  }
114 
116  public bool CheckExists(IDbConfig config) {
117  if (booted)
118  throw new DataException("The database is already booted.");
119 
120  return controller.DatabaseExists(config, databaseName);
121  }
122 
124  public bool IsBooted {
125  get { return booted; }
126  }
127 
129  public IDatabaseInterface Connect() {
130  if (!booted)
131  throw new DataException("The database is not started.");
132 
133  // Local connections are formatted as;
134  // 'Local/[type]/[connect_id]'
135  string hostString = String.Format("{0}/Connection/{1}", KnownConnectionProtocols.Local, connectId);
136 
137  // Create a IDatabaseInterface,
138  IDatabaseInterface dbInterface = new LocalDatabaseInterface(this, hostString);
139 
140  ++connectId;
141  ++openConnections;
142 
143  return dbInterface;
144  }
145 
146  // ---------- Inner classes ----------
147 
153  private class LocalDatabaseInterface : DatabaseInterface {
155  private bool closed;
156 
157  public LocalDatabaseInterface(DefaultLocalBootable localBootable, string hostString)
158  : base(localBootable, localBootable.databaseName, hostString) {
159  this.localBootable = localBootable;
160  }
161 
162  // ---------- Overwritten from DatabaseInterface ----------
163 
164  protected override void Dispose(bool disposing) {
165  if (disposing) {
166  if (!closed) {
167  base.Dispose(true);
168 
169  --localBootable.openConnections;
170 
171  // When all connections are closed, shut down...
172  if (localBootable.openConnections <= 0) {
173  // When the local database interface is disposed, we must shut down
174  // the database system.
175  localBootable.dbsys.Close();
176  localBootable.booted = false;
177  localBootable.dbsys = null;
178  }
179  closed = true;
180  }
181  }
182  }
183  }
184  }
185 }
LocalDatabaseInterface(DefaultLocalBootable localBootable, string hostString)
IDatabaseInterface Boot(IDbConfig config)
bool booted
Set to true if the database is booted.
The representation of a single database in the system.
Definition: IDatabase.cs:40
Control.DbSystem dbsys
The local DbSystem database object.
int openConnections
The number of connections that are current open.
IDatabase GetDatabase(string databaseName)
DefaultLocalBootable(DbController controller, string databaseName)
A local implementation of DatabaseInterface that will dispose the parent ILocalBootable object when t...
IDatabaseInterface Create(string username, string password, IDbConfig config)
A bootable object that filters through to a DatabaseInterface but is thread-safe and multi-threaded...