DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Package Functions | Properties | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Database Class Reference

The default implementation of a database in a system. More...

Inheritance diagram for Deveel.Data.Database:
Deveel.Data.IDatabase Deveel.Data.Diagnostics.IEventSource

Classes

class  DatabaseTransactionFactory
 

Public Member Functions

void Dispose ()
 
void Create (string adminName, string adminPassword)
 Creates the database in the context given, granting the administrative control to the user identified by the given name and password. More...
 
void Open ()
 Opens the database making it ready to be accessed. More...
 
void Close ()
 Closes the database making it not accessible to connections. More...
 

Package Functions

 Database (DatabaseSystem system, IDatabaseContext context)
 

Properties

string Name [get, private set]
 Gets the database name, as configured in the parent context. More...
 
DatabaseSystem System [get, private set]
 
ActiveSessionList Sessions [get, private set]
 
Locker Locker [get, private set]
 
ITransactionFactory TransactionFactory [get, private set]
 Gets an object that is used to create new transactions to this database More...
 
IEventSource IEventSource. ParentSource [get]
 
IContext IEventSource. Context [get]
 
IEnumerable< KeyValuePair< string, object > > IEventSource. Metadata [get]
 
IDatabaseContext Context [get, private set]
 Gets the context that contains this database. More...
 
Version Version [get, private set]
 Gets the version number of this database. More...
 
bool Exists [get]
 Gets a boolean value indicating if the database exists within the context given. More...
 
bool IsOpen [get, private set]
 Gets a boolean value that indicates if the database was open. More...
 
TableSourceComposite TableComposite [get, private set]
 
ITable SingleRowTable [get, private set]
 Gets a special table, unique for every database, that has a single row and a single cell. More...
 
- Properties inherited from Deveel.Data.IDatabase
string Name [get]
 Gets the name of the database. More...
 
new IDatabaseContext Context [get]
 Gets the context that contains this database. More...
 
ITransactionFactory TransactionFactory [get]
 Gets an object that is used to create new transactions to this database More...
 
ActiveSessionList Sessions [get]
 Gets a list of all the open sessions to the database. More...
 
Locker Locker [get]
 Gets the objects that is used to lock database objects between transactions. More...
 
Version Version [get]
 Gets the version number of this database. More...
 
bool Exists [get]
 Gets a boolean value indicating if the database exists within the context given. More...
 
bool IsOpen [get]
 Gets a boolean value that indicates if the database was open. More...
 
ITable SingleRowTable [get]
 Gets a special table, unique for every database, that has a single row and a single cell. More...
 
- Properties inherited from Deveel.Data.Diagnostics.IEventSource
IContext Context [get]
 
IEventSource ParentSource [get]
 Gets an optional parent source. More...
 
IEnumerable< KeyValuePair< string, object > > Metadata [get]
 Gets the list of metadata associated to the source. More...
 

Private Member Functions

 ~Database ()
 Finalizes an instance of the Database class. More...
 
void DiscoverDataVersion ()
 
void Dispose (bool disposing)
 
void OnDatabaseCreate (IQuery context)
 
void SetCurrentDataVersion (IQuery context)
 
void CreateSchemata (IQuery context)
 
void AssertDataVersion ()
 

Private Attributes

bool disposed
 

Detailed Description

The default implementation of a database in a system.

This class implements the IDatabase contract, that is backed by a IDatabaseContext for configurations and services, provides functionalities for the management of data in the relational model.

Definition at line 38 of file Database.cs.

Constructor & Destructor Documentation

Deveel.Data.Database.Database ( DatabaseSystem  system,
IDatabaseContext  context 
)
inlinepackage

Definition at line 39 of file Database.cs.

39  {
40  System = system;
41  Context = context;
42 
43  Name = Context.DatabaseName();
44 
46 
47  TableComposite = new TableSourceComposite(this);
48 
49  Context.RegisterInstance(this);
50  Context.RegisterInstance<ITableSourceComposite>(TableComposite);
51 
52  Locker = new Locker(this);
53 
54  Sessions = new ActiveSessionList(this);
55 
56  // Create the single row table
57  var t = new TemporaryTable(context, "SINGLE_ROW_TABLE", new ColumnInfo[0]);
58  t.NewRow();
59  SingleRowTable = t;
60 
61  TransactionFactory = new DatabaseTransactionFactory(this);
62  }
Defines the metadata properties of a column within a table of a database.
Definition: ColumnInfo.cs:36
IContext IEventSource. Context
Definition: Database.cs:95
ITransactionFactory TransactionFactory
Gets an object that is used to create new transactions to this database
Definition: Database.cs:89
ITable SingleRowTable
Gets a special table, unique for every database, that has a single row and a single cell...
Definition: Database.cs:198
TableSourceComposite TableComposite
Definition: Database.cs:192
ActiveSessionList Sessions
Definition: Database.cs:81
string Name
Gets the database name, as configured in the parent context.
Definition: Database.cs:77
void DiscoverDataVersion()
Definition: Database.cs:108
Deveel.Data.Database.~Database ( )
inlineprivate

Finalizes an instance of the Database class.

Definition at line 67 of file Database.cs.

67  {
68  Dispose(false);
69  }

Member Function Documentation

void Deveel.Data.Database.AssertDataVersion ( )
inlineprivate

Definition at line 362 of file Database.cs.

362  {
363  // TODO:
364  }
void Deveel.Data.Database.Close ( )
inline

Closes the database making it not accessible to connections.

Exceptions
DatabaseSystemExceptionThe database is not initialized. or An error occurred during database shutdown.

Typical implementations of this interface will automatically invoke the closure of the database on disposal (IDisposable.Dispose.

Implements Deveel.Data.IDatabase.

Definition at line 378 of file Database.cs.

378  {
379  if (!IsOpen)
380  throw new DatabaseSystemException("The database is not initialized.");
381 
382  try {
383  if (Context.DeleteOnClose()) {
384  // Delete the tables if the database is set to delete on
385  // shutdown.
387  } else {
388  // Otherwise close the conglomerate.
390  }
391  } catch (DatabaseSystemException) {
392  throw;
393  } catch (Exception e) {
394  throw new DatabaseSystemException("An error occurred during database shutdown.", e);
395  } finally {
396  IsOpen = false;
397  }
398  }
bool IsOpen
Gets a boolean value that indicates if the database was open.
Definition: Database.cs:190
IContext IEventSource. Context
Definition: Database.cs:95
TableSourceComposite TableComposite
Definition: Database.cs:192
void Deveel.Data.Database.Create ( string  adminName,
string  adminPassword 
)
inline

Creates the database in the context given, granting the administrative control to the user identified by the given name and password.

Parameters
adminNameThe name of the administrator.
adminPasswordThe password used to identify the administrator.
Exceptions
DatabaseSystemExceptionIf the database context is configured to be in read-only model, if it was not possible to commit the initial information or if another unhanded error occurred while creating the database.
ArgumentNullExceptionIf either one of adminName or adminPassword are null or empty.

The properties used to create the database are extracted from the underlying context (DatabaseContext).

This method does not automatically open the database: to make it accessible a call to Open is required.

See also
IDatabaseContext.Configuration

Implements Deveel.Data.IDatabase.

Definition at line 240 of file Database.cs.

240  {
241  if (Context.ReadOnly())
242  throw new DatabaseSystemException("Cannot create database in read-only mode.");
243 
244  if (String.IsNullOrEmpty(adminName))
245  throw new ArgumentNullException("adminName");
246  if (String.IsNullOrEmpty(adminPassword))
247  throw new ArgumentNullException("adminPassword");
248 
249  try {
250  // Create the conglomerate
252 
253  using (var session = this.CreateInitialSystemSession()) {
254  using (var context = session.CreateQuery()) {
255  try {
256  session.CurrentSchema(SystemSchema.Name);
257 
258  // Create the schema information tables
259  CreateSchemata(context);
260 
261  // The system tables that are present in every conglomerate.
262  SystemSchema.CreateTables(context);
263  SystemGroups.Create(context);
264 
265  context.CreatePublicUser();
266 
267  // Create the system views
270 
271  this.CreateAdminUser(context, adminName, adminPassword);
272 
273  SetCurrentDataVersion(context);
274 
275  // Set all default system procedures.
276  // TODO: SystemSchema.SetupSystemFunctions(session, username);
277 
278  OnDatabaseCreate(context);
279 
280  try {
281  // Close and commit this transaction.
282  session.Commit();
283  } catch (TransactionException e) {
284  throw new DatabaseSystemException("Could not commit the initial information", e);
285  }
286  } catch (DatabaseSystemException) {
287  throw;
288  } catch (Exception ex) {
289  throw new DatabaseSystemException("An error occurred while creating the database.", ex);
290  }
291  }
292  }
293 
294  // Close the conglomerate.
296  } catch (DatabaseSystemException) {
297  throw;
298  } catch (Exception e) {
299  throw new DatabaseSystemException("An error occurred while creating the database.", e);
300  }
301  }
A long string in the system.
void OnDatabaseCreate(IQuery context)
Definition: Database.cs:200
void SetCurrentDataVersion(IQuery context)
Definition: Database.cs:303
IContext IEventSource. Context
Definition: Database.cs:95
void CreateSchemata(IQuery context)
Definition: Database.cs:307
TableSourceComposite TableComposite
Definition: Database.cs:192
static void Create(IQuery context)
Definition: SystemGroups.cs:57
void Deveel.Data.Database.CreateSchemata ( IQuery  context)
inlineprivate

Definition at line 307 of file Database.cs.

307  {
308  try {
309  context.CreateSchema(InformationSchema.SchemaName, SchemaTypes.System);
310  context.CreateSchema(Context.DefaultSchema(), SchemaTypes.Default);
311  } catch (DatabaseSystemException) {
312  throw;
313  } catch (Exception ex) {
314  throw new DatabaseSystemException("Unable to create the default schema for the database.", ex);
315  }
316  }
IContext IEventSource. Context
Definition: Database.cs:95
void Deveel.Data.Database.DiscoverDataVersion ( )
inlineprivate

Definition at line 108 of file Database.cs.

108  {
109  var dataVerion = Attribute.GetCustomAttribute(typeof (Database).Assembly, typeof (DataVersionAttribute))
110  as DataVersionAttribute;
111  if (dataVerion != null)
112  Version = dataVerion.Version;
113  }
Version Version
Gets the version number of this database.
Definition: Database.cs:163
Database(DatabaseSystem system, IDatabaseContext context)
Definition: Database.cs:39
void Deveel.Data.Database.Dispose ( )
inline

Definition at line 115 of file Database.cs.

115  {
116  Dispose(true);
117  GC.SuppressFinalize(this);
118  }
void Deveel.Data.Database.Dispose ( bool  disposing)
inlineprivate

Definition at line 122 of file Database.cs.

122  {
123  if (!disposed) {
124  if (disposing) {
125  if (IsOpen) {
126  // TODO: Report the error
127  }
128 
129  if (Locker != null)
130  Locker.Reset();
131 
132  if (TableComposite != null)
134 
135  if (Context != null)
136  Context.Dispose();
137 
138  if (System != null)
139  System.RemoveDatabase(this);
140  }
141 
142  Locker = null;
143  System = null;
144  TableComposite = null;
145  Context = null;
146  disposed = true;
147  }
148  }
bool IsOpen
Gets a boolean value that indicates if the database was open.
Definition: Database.cs:190
IContext IEventSource. Context
Definition: Database.cs:95
TableSourceComposite TableComposite
Definition: Database.cs:192
void Deveel.Data.Database.OnDatabaseCreate ( IQuery  context)
inlineprivate

Definition at line 200 of file Database.cs.

200  {
201  var callbacks = Context.ResolveAllServices<IDatabaseCreateCallback>();
202  if (callbacks != null) {
203  foreach (var callback in callbacks) {
204  try {
205  if (callback != null)
206  callback.OnDatabaseCreate(context);
207  } catch (Exception) {
208  //TODO: Route an error event to the listeners
209  }
210  }
211  }
212  }
IContext IEventSource. Context
Definition: Database.cs:95
void Deveel.Data.Database.Open ( )
inline

Opens the database making it ready to be accessed.

Exceptions
DatabaseSystemExceptionThe database was already initialized. or or An error occurred when initializing the database.

This method ensures the system components and the data are ready to allow any connection to be established.

After this method successfully exists, the state of IsOpen is changed to true.

Implements Deveel.Data.IDatabase.

Definition at line 337 of file Database.cs.

337  {
338  if (IsOpen)
339  throw new DatabaseSystemException("The database was already initialized.");
340 
341  try {
342  // Check if the state file exists. If it doesn't, we need to report
343  // incorrect version.
344  if (!TableComposite.Exists())
345  // If neither store or state file exist, assume database doesn't
346  // exist.
347  throw new DatabaseSystemException(String.Format("The database {0} does not exist.", Name));
348 
349  // Open the conglomerate
351 
353  } catch (DatabaseSystemException) {
354  throw;
355  } catch (Exception e) {
356  throw new DatabaseSystemException("An error occurred when initializing the database.", e);
357  }
358 
359  IsOpen = true;
360  }
void AssertDataVersion()
Definition: Database.cs:362
A long string in the system.
bool IsOpen
Gets a boolean value that indicates if the database was open.
Definition: Database.cs:190
TableSourceComposite TableComposite
Definition: Database.cs:192
string Name
Gets the database name, as configured in the parent context.
Definition: Database.cs:77
void Deveel.Data.Database.SetCurrentDataVersion ( IQuery  context)
inlineprivate

Definition at line 303 of file Database.cs.

303  {
304  // TODO: Get the data version and then set it to the database table 'vars'
305  }

Member Data Documentation

bool Deveel.Data.Database.disposed
private

Definition at line 120 of file Database.cs.

Property Documentation

IContext IEventSource. Deveel.Data.Database.Context
getprivate

Definition at line 95 of file Database.cs.

IDatabaseContext Deveel.Data.Database.Context
getprivate set

Gets the context that contains this database.

See also
IDatabaseContext

Definition at line 154 of file Database.cs.

bool Deveel.Data.Database.Exists
get

Gets a boolean value indicating if the database exists within the context given.

Exceptions
ExceptionAn error occurred while testing database existence.
See also
Create

Definition at line 171 of file Database.cs.

bool Deveel.Data.Database.IsOpen
getprivate set

Gets a boolean value that indicates if the database was open.

See also
Open, Close

Definition at line 190 of file Database.cs.

Locker Deveel.Data.Database.Locker
getprivate set

Definition at line 83 of file Database.cs.

IEnumerable<KeyValuePair<string, object> > IEventSource. Deveel.Data.Database.Metadata
getprivate

Definition at line 99 of file Database.cs.

string Deveel.Data.Database.Name
getprivate set

Gets the database name, as configured in the parent context.

The database name.

Definition at line 77 of file Database.cs.

IEventSource IEventSource. Deveel.Data.Database.ParentSource
getprivate

Definition at line 91 of file Database.cs.

ActiveSessionList Deveel.Data.Database.Sessions
getprivate set

Definition at line 81 of file Database.cs.

ITable Deveel.Data.Database.SingleRowTable
getprivate set

Gets a special table, unique for every database, that has a single row and a single cell.

Definition at line 198 of file Database.cs.

DatabaseSystem Deveel.Data.Database.System
getprivate set

Definition at line 79 of file Database.cs.

TableSourceComposite Deveel.Data.Database.TableComposite
getprivate setpackage

Definition at line 192 of file Database.cs.

ITransactionFactory Deveel.Data.Database.TransactionFactory
getprivate set

Gets an object that is used to create new transactions to this database

See also
ITransactionFactory

Definition at line 89 of file Database.cs.

Version Deveel.Data.Database.Version
getprivate set

Gets the version number of this database.

This value is useful for data compatibility between versions of the system.

Definition at line 163 of file Database.cs.


The documentation for this class was generated from the following file: