DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SchemaManager.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.Sql.Tables;
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Schemas {
24  public sealed class SchemaManager : IObjectManager {
25  public SchemaManager(ITransaction transaction) {
26  if (transaction == null)
27  throw new ArgumentNullException("transaction");
28 
29  Transaction = transaction;
30  }
31 
33  Dispose(false);
34  }
35 
36  public ITransaction Transaction { get; private set; }
37 
38  private void Dispose(bool disposing) {
39  if (disposing) {
40  // TODO: Additional disposals ...
41  }
42 
43  Transaction = null;
44  }
45 
46  public void Dispose() {
47  Dispose(true);
48  GC.SuppressFinalize(this);
49  }
50 
52  get { return DbObjectType.Schema; }
53  }
54 
55  public void Create() {
56  // SYSTEM.SCHEMA_INFO
57  var tableInfo = new TableInfo(SystemSchema.SchemaInfoTableName);
58  tableInfo.AddColumn("id", PrimitiveTypes.Numeric());
59  tableInfo.AddColumn("name", PrimitiveTypes.String());
60  tableInfo.AddColumn("type", PrimitiveTypes.String());
61  tableInfo.AddColumn("culture", PrimitiveTypes.String());
62  tableInfo.AddColumn("other", PrimitiveTypes.String());
63  tableInfo = tableInfo.AsReadOnly();
64  Transaction.CreateTable(tableInfo);
65  }
66 
68  var schemaInfo = objInfo as SchemaInfo;
69  if (schemaInfo == null)
70  throw new ArgumentException();
71 
72  CreateSchema(schemaInfo);
73  }
74 
76  return SchemaExists(objName.Name);
77  }
78 
80  return SchemaExists(objName.Name);
81  }
82 
84  return GetSchema(objName.Name);
85  }
86 
88  throw new NotImplementedException();
89  }
90 
92  return DropSchema(objName.Name);
93  }
94 
95  ObjectName IObjectManager.ResolveName(ObjectName objName, bool ignoreCase) {
96  return ResolveSchemaName(objName.Name, ignoreCase);
97  }
98 
99  public ObjectName ResolveSchemaName(string name, bool ignoreCase) {
100  var table = Transaction.GetTable(SystemSchema.SchemaInfoTableName);
101 
102  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
103 
104  foreach (var row in table) {
105  var objSchemaName = row.GetValue(1);
106  if (!(objSchemaName.Type is StringType))
107  throw new InvalidOperationException("Invalid column type for SCHEMA name table.");
108  if (objSchemaName.IsNull)
109  throw new InvalidOperationException();
110 
111  var schemaName = objSchemaName.Value.ToString();
112  if (String.Equals(schemaName, name, comparison))
113  return new ObjectName(schemaName);
114  }
115 
116  return null;
117  }
118 
119  public void CreateSchema(SchemaInfo schemaInfo) {
120  if (schemaInfo == null)
121  throw new ArgumentNullException("schemaInfo");
122 
123  var tableName = SystemSchema.SchemaInfoTableName;
124  var t = Transaction.GetMutableTable(tableName);
125 
126  var nameObj = DataObject.String(schemaInfo.Name);
127 
128  if (t.Exists(1, nameObj))
129  throw new DatabaseSystemException(String.Format("Schema '{0}' already defined in the database.", schemaInfo.Name));
130 
131  var row = t.NewRow();
132  var uniqueId = Transaction.NextTableId(tableName);
133  row.SetValue(0, DataObject.Number(uniqueId));
134  row.SetValue(1, DataObject.String(schemaInfo.Name));
135  row.SetValue(2, DataObject.String(schemaInfo.Type));
136  row.SetValue(3, DataObject.String(schemaInfo.Culture));
137 
138  t.AddRow(row);
139  }
140 
141  public bool SchemaExists(string name) {
142  var tableName = SystemSchema.SchemaInfoTableName;
143  var t = Transaction.GetMutableTable(tableName);
144 
145  var nameObj = DataObject.String(name);
146 
147  return t.Exists(1, nameObj);
148  }
149 
150  public bool DropSchema(string name) {
151  var tableName = SystemSchema.SchemaInfoTableName;
152  var t = Transaction.GetMutableTable(tableName);
153 
154  // Drop a single entry from dt where column 1 = name
155  var nameObj = DataObject.String(name);
156  return t.Delete(1, nameObj);
157  }
158 
159  public Schema GetSchema(string name) {
160  if (String.IsNullOrEmpty(name))
161  throw new ArgumentNullException("name");
162 
163  throw new NotImplementedException();
164  }
165  }
166 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
void CreateSchema(SchemaInfo schemaInfo)
bool AlterObject(IObjectInfo objInfo)
Modifies an existing object managed, identified by IObjectInfo.FullName component of the given specif...
bool DropObject(ObjectName objName)
Deletes a database object handled by this manager from the system.
A long string in the system.
The system implementation of a transaction model that handles isolated operations within a database c...
Definition: Transaction.cs:35
bool RealObjectExists(ObjectName objName)
Checks if an object really exists in the system.
static DataObject Number(SqlNumber value)
Definition: DataObject.cs:552
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
Describes the name of an object within a database.
Definition: ObjectName.cs:44
Exception thrown where various problems occur within the database.
static DataObject String(string s)
Definition: DataObject.cs:592
static readonly ObjectName SchemaInfoTableName
string Type
Gets the type of the schema that defines the kind of objects it can contains.
Definition: SchemaInfo.cs:73
string Culture
Gets the culture that will be applied to string comparisons, when not explicitly defined by types...
Definition: SchemaInfo.cs:79
static NumericType Numeric()
void CreateObject(IObjectInfo objInfo)
Create a new object of the ObjectType given the specifications given.
string Name
Gets the name of the schema.
Definition: SchemaInfo.cs:67
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Provides utilities and properties for handling the SYSTEN schema of a database.
Definition: SystemSchema.cs:37
DbObjectType ObjectType
Gets the type of objects managed by this instance.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
IDbObject GetObject(ObjectName objName)
Gets a database object managed by this manager.
bool ObjectExists(ObjectName objName)
Checks if an object identified by the given name is managed by this instance.
Defines the contract for the business managers of database objects of a given type.
The simplest implementation of a transaction.
Definition: ITransaction.cs:30
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
Describes the properties of a schema in a database system.
Definition: SchemaInfo.cs:39
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
void Create()
Initializes the manager into the underlying system.
SchemaManager(ITransaction transaction)
ObjectName ResolveSchemaName(string name, bool ignoreCase)
ObjectName ResolveName(ObjectName objName, bool ignoreCase)
Normalizes the input object name using the case sensitivity specified.