DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DatabaseExtensions.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 
19 using Deveel.Data.Security;
20 using Deveel.Data.Sql;
21 using Deveel.Data.Sql.Schemas;
23 
24 namespace Deveel.Data {
25  public static class DatabaseExtensions {
26  #region Transactions
27 
28  public static ITransaction CreateTransaction(this IDatabase database, IsolationLevel isolation) {
29  if (!database.IsOpen)
30  throw new InvalidOperationException(String.Format("Database '{0}' is not open.", database.Name));
31 
32  return database.CreateSafeTransaction(isolation);
33  }
34 
35  public static ITransaction FindTransactionById(this IDatabase database, int commidId) {
36  return database.TransactionFactory.OpenTransactions.FindById(commidId);
37  }
38 
39  internal static ITransaction CreateSafeTransaction(this IDatabase database, IsolationLevel isolation) {
40  return database.TransactionFactory.CreateTransaction(isolation);
41  }
42 
43  #endregion
44 
45  #region Sessions
46 
47  static ISession CreateUserSession(this IDatabase database, User user, IsolationLevel isolation) {
48  if (user == null)
49  throw new ArgumentNullException("user");
50 
51  // TODO: if the isolation is not specified, use a configured default one
52  if (isolation == IsolationLevel.Unspecified)
53  isolation = IsolationLevel.Serializable;
54 
55  var transaction = database.CreateTransaction(isolation);
56  return new Session(transaction, user);
57  }
58 
59  static ISession CreateSystemSession(this IDatabase database, IsolationLevel isolation) {
60  var transaction = database.CreateTransaction(isolation);
61  return new SystemSession(transaction, SystemSchema.Name);
62  }
63 
64  internal static ISession CreateInitialSystemSession(this IDatabase database) {
65  var transaction = database.CreateSafeTransaction(IsolationLevel.Serializable);
66  return new SystemSession(transaction, SystemSchema.Name);
67  }
68 
69  internal static ISession CreateSystemSession(this IDatabase database) {
70  return database.CreateSystemSession(IsolationLevel.Serializable);
71  }
72 
73  public static ISession CreateUserSession(this IDatabase database, User user) {
74  // TODO: get the pre-configured default transaction isolation
75  return database.CreateUserSession(user, IsolationLevel.Unspecified);
76  }
77 
78  public static ISession CreateUserSession(this IDatabase database, string userName, string password) {
79  var user = database.Authenticate(userName, password);
80  if (user == null)
81  throw new InvalidOperationException(String.Format("Unable to create a session for user '{0}': not authenticated.", userName));
82 
83  return database.CreateUserSession(user);
84  }
85 
86  static ISession OpenUserSession(this IDatabase database, int commitId, User user) {
87  if (commitId < 0)
88  throw new ArgumentException("Invalid commit reference specified.");
89 
90  var transaction = database.FindTransactionById(commitId);
91  if (transaction == null)
92  throw new InvalidOperationException(String.Format("The request transaction with ID '{0}' is not open.", commitId));
93 
94  return new Session(transaction, user);
95  }
96 
97  #endregion
98 
99  #region Security
100 
101  public static void CreateAdminUser(this IDatabase database, IQuery context, string adminName, string adminPassword) {
102  try {
103  var user = context.CreateUser(adminName, adminPassword);
104 
105  // This is the admin user so add to the 'secure access' table.
106  context.AddUserToGroup(adminName, SystemGroups.SecureGroup);
107 
108  context.GrantToUserOnSchema(database.Context.DefaultSchema(), user.Name, Privileges.SchemaAll, true);
109  context.GrantToUserOnSchema(SystemSchema.Name, user.Name, Privileges.SchemaRead);
110  context.GrantToUserOnSchema(InformationSchema.SchemaName, user.Name, Privileges.SchemaRead);
111 
112  SystemSchema.GrantToPublic(context);
113  } catch (DatabaseSystemException) {
114  throw;
115  } catch (Exception ex) {
116  throw new DatabaseSystemException("Could not create the database administrator.", ex);
117  }
118  }
119 
120  public static User Authenticate(this IDatabase database, string username, string password) {
121  // Create a temporary connection for authentication only...
122  using (var session = database.CreateSystemSession()) {
123  session.CurrentSchema(SystemSchema.Name);
124 
125  using (var queryContext = session.CreateQuery()) {
126  return queryContext.Authenticate(username, password);
127  }
128  }
129  }
130 
131  #endregion
132  }
133 }
ITransaction CreateTransaction(IsolationLevel isolation)
Creates a new the transaction with the isolation specified.
static User Authenticate(this IDatabase database, string username, string password)
bool IsOpen
Gets a boolean value that indicates if the database was open.
Definition: IDatabase.cs:97
static void CreateAdminUser(this IDatabase database, IQuery context, string adminName, string adminPassword)
Exception thrown where various problems occur within the database.
static ISession CreateUserSession(this IDatabase database, User user)
static ITransaction FindTransactionById(this IDatabase database, int commidId)
string Name
Gets the name of the database.
Definition: IDatabase.cs:44
static ITransaction CreateTransaction(this IDatabase database, IsolationLevel isolation)
The representation of a single database in the system.
Definition: IDatabase.cs:40
This is a session that is constructed around a given user and a transaction, to the given database...
Definition: Session.cs:32
new IDatabaseContext Context
Gets the context that contains this database.
Definition: IDatabase.cs:50
static ISession CreateInitialSystemSession(this IDatabase database)
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
static ISession CreateUserSession(this IDatabase database, User user, IsolationLevel isolation)
static void GrantToPublic(IQuery context)
Provides utilities and properties for handling the SYSTEN schema of a database.
Definition: SystemSchema.cs:37
static ISession CreateSystemSession(this IDatabase database, IsolationLevel isolation)
ITransactionFactory TransactionFactory
Gets an object that is used to create new transactions to this database
Definition: IDatabase.cs:56
static ITransaction CreateSafeTransaction(this IDatabase database, IsolationLevel isolation)
static ISession CreateUserSession(this IDatabase database, string userName, string password)
static ISession OpenUserSession(this IDatabase database, int commitId, User user)
const string SecureGroup
THe name of the secure access group.
Definition: SystemGroups.cs:46
const string Name
The name of the system schema that contains tables referring to system information.
The simplest implementation of a transaction.
Definition: ITransaction.cs:30
static ISession CreateSystemSession(this IDatabase database)
Provides the information for a user in a database system
Definition: User.cs:27
TransactionCollection OpenTransactions
Gets the collection of currently open transactions.