DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Triggers.TriggerManager Class Reference
Inheritance diagram for Deveel.Data.Sql.Triggers.TriggerManager:
Deveel.Data.Sql.IObjectManager Deveel.Data.Sql.Triggers.ITriggerManager

Classes

class  TriggerArgument
 
class  TriggersTableContainer
 

Public Member Functions

 TriggerManager (ITransaction transaction)
 
void Dispose ()
 
void Create ()
 Initializes the manager into the underlying system. More...
 
ObjectName ResolveName (ObjectName objName, bool ignoreCase)
 Normalizes the input object name using the case sensitivity specified. More...
 
void CreateTrigger (TriggerInfo triggerInfo)
 
bool DropTrigger (ObjectName triggerName)
 
bool TriggerExists (ObjectName triggerName)
 
Trigger GetTrigger (ObjectName triggerName)
 
bool AlterTrigger (TriggerInfo triggerInfo)
 
IEnumerable< TriggerFindTriggers (TriggerEventInfo eventInfo)
 
void FireTriggers (IQuery context, TableEvent tableEvent)
 
ITableContainer CreateTriggersTableContainer ()
 

Properties

DbObjectType IObjectManager. ObjectType [get]
 
- Properties inherited from Deveel.Data.Sql.IObjectManager
DbObjectType ObjectType [get]
 Gets the type of objects managed by this instance. More...
 

Private Member Functions

 ~TriggerManager ()
 
void Dispose (bool disposing)
 
void OnTableCommit (TableCommitEvent commitEvent)
 
void OnCommit (TableCommitInfo commitInfo)
 
void BuildTriggerCache ()
 
void InvalidateTriggerCache ()
 
ITable FindTrigger (ITable table, string schema, string name)
 
IEnumerable< TriggerInfoFindTriggers (ObjectName tableName, TriggerEventType eventType)
 
void IObjectManager. CreateObject (IObjectInfo objInfo)
 Create a new object of the ObjectType given the specifications given. More...
 
bool IObjectManager. RealObjectExists (ObjectName objName)
 Checks if an object really exists in the system. More...
 
bool IObjectManager. ObjectExists (ObjectName objName)
 Checks if an object identified by the given name is managed by this instance. More...
 
IDbObject IObjectManager. GetObject (ObjectName objName)
 Gets a database object managed by this manager. More...
 
bool IObjectManager. AlterObject (IObjectInfo objInfo)
 Modifies an existing object managed, identified by IObjectInfo.FullName component of the given specification, with the format given. More...
 
bool IObjectManager. DropObject (ObjectName objName)
 Deletes a database object handled by this manager from the system. More...
 
void ITriggerManager. RegisterTrigger (TriggerInfo triggerInfo)
 
TriggerInfo FormTrigger (Row row)
 

Static Private Member Functions

static byte[] SerializeArguments (TriggerArgument args)
 

Private Attributes

ITransaction transaction
 
bool tableModified
 
bool cacheValid
 
List< TriggertriggerCache
 

Detailed Description

Definition at line 30 of file TriggerManager.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Triggers.TriggerManager.TriggerManager ( ITransaction  transaction)
inline

Definition at line 36 of file TriggerManager.cs.

36  {
37  this.transaction = transaction;
38  triggerCache = new List<Trigger>(24);
39  this.transaction.RegisterOnCommit(OnCommit);
40  }
void OnCommit(TableCommitInfo commitInfo)
void RegisterOnCommit(Action< TableCommitInfo > action)
Deveel.Data.Sql.Triggers.TriggerManager.~TriggerManager ( )
inlineprivate

Definition at line 42 of file TriggerManager.cs.

42  {
43  Dispose(false);
44  }

Member Function Documentation

bool IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.AlterObject ( IObjectInfo  objInfo)
inlineprivate

Modifies an existing object managed, identified by IObjectInfo.FullName component of the given specification, with the format given.

Parameters
objInfoThe object specification used to alter an existing object.
Returns
Returns true an object was identified and successfully altered, or false if none database object was found for the unique name given.
Exceptions
ArgumentNullExceptionIf the given objInfo object is null.
ArgumentExceptionIf the type of the object specified (IObjectInfo.ObjectType) is different than the type of objects handled by this manager.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 195 of file TriggerManager.cs.

195  {
196  var triggerInfo = objInfo as TriggerInfo;
197  if (triggerInfo == null)
198  throw new ArgumentException();
199 
200  return AlterTrigger(triggerInfo);
201  }
bool AlterTrigger(TriggerInfo triggerInfo)
bool Deveel.Data.Sql.Triggers.TriggerManager.AlterTrigger ( TriggerInfo  triggerInfo)
inline

Definition at line 331 of file TriggerManager.cs.

331  {
332  throw new NotImplementedException();
333  }
void Deveel.Data.Sql.Triggers.TriggerManager.BuildTriggerCache ( )
inlineprivate

Definition at line 90 of file TriggerManager.cs.

90  {
91  if (!cacheValid) {
92  var table = transaction.GetTable(SystemSchema.TriggerTableName);
93 
94  var list = new List<Trigger>();
95  foreach (var row in table) {
96  var triggerInfo = FormTrigger(row);
97  list.Add(new Trigger(triggerInfo));
98  }
99 
100  triggerCache = new List<Trigger>(list);
101  cacheValid = true;
102  }
103  }
A TRIGGER fired at provided write events (INSERT, UPDATE or DELETE) over a table at a given moments (...
void Deveel.Data.Sql.Triggers.TriggerManager.Create ( )
inline

Initializes the manager into the underlying system.

Typically this method generates the tables required to manage the features relative to the objects.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 109 of file TriggerManager.cs.

109  {
110  var tableInfo = new TableInfo(SystemSchema.TriggerTableName);
111  tableInfo.AddColumn("schema", PrimitiveTypes.String());
112  tableInfo.AddColumn("name", PrimitiveTypes.String());
113  tableInfo.AddColumn("type", PrimitiveTypes.Integer());
114  tableInfo.AddColumn("on_object", PrimitiveTypes.String());
115  tableInfo.AddColumn("action", PrimitiveTypes.Integer());
116  tableInfo.AddColumn("procedure_name", PrimitiveTypes.String());
117  tableInfo.AddColumn("args", PrimitiveTypes.Binary());
118  transaction.CreateTable(tableInfo);
119  }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static BinaryType Binary(int maxSize)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
void IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.CreateObject ( IObjectInfo  objInfo)
inlineprivate

Create a new object of the ObjectType given the specifications given.

Parameters
objInfoThe object specifications used to create a new object.
Exceptions
ArgumentNullExceptionIf the given objInfo is null.
ArgumentExceptionIf the object type of the specification (IObjectInfo.ObjectType) is different than the ObjectType of this manager.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 175 of file TriggerManager.cs.

175  {
176  var triggerInfo = objInfo as TriggerInfo;
177  if (triggerInfo == null)
178  throw new ArgumentException();
179 
180  CreateTrigger(triggerInfo);
181  }
void CreateTrigger(TriggerInfo triggerInfo)
void Deveel.Data.Sql.Triggers.TriggerManager.CreateTrigger ( TriggerInfo  triggerInfo)
inline

Definition at line 245 of file TriggerManager.cs.

245  {
246  if (!transaction.TableExists(SystemSchema.TriggerTableName))
247  return;
248 
249  try {
250  var args = new TriggerArgument(triggerInfo.Arguments.ToArray());
251  var binArgs = SerializeArguments(args);
252 
253  var schema = triggerInfo.TriggerName.ParentName;
254  var name = triggerInfo.TriggerName.Name;
255  var type = (int) triggerInfo.TriggerType;
256  var onTable = triggerInfo.TableName == null ? null : triggerInfo.TableName.FullName;
257  var procedureName = triggerInfo.ProcedureName != null ? triggerInfo.ProcedureName.FullName : null;
258 
259  var action = (int) triggerInfo.EventType;
260 
261  // TODO: if the trigger has a body, create a special procedure and set the name
262 
263  // Insert the entry into the trigger table,
264  var table = transaction.GetMutableTable(SystemSchema.TriggerTableName);
265  var row = table.NewRow();
266  row.SetValue(0, DataObject.String(schema));
267  row.SetValue(1, DataObject.String(name));
268  row.SetValue(2, DataObject.Integer(type));
269  row.SetValue(3, DataObject.String(onTable));
270  row.SetValue(4, DataObject.Integer(action));
271  row.SetValue(5, DataObject.String(procedureName));
272  row.SetValue(6, DataObject.Binary(binArgs));
273  table.AddRow(row);
274 
276 
277  transaction.Registry.RegisterEvent(new ObjectCreatedEvent(triggerInfo.TriggerName, DbObjectType.Trigger));
278 
279  tableModified = true;
280  } catch (Exception) {
281  // TODO: use a specialized exception
282  throw;
283  }
284  }
An event fired when a database object of the given type is created during the lifetime of a transacti...
static byte[] SerializeArguments(TriggerArgument args)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
ITableContainer Deveel.Data.Sql.Triggers.TriggerManager.CreateTriggersTableContainer ( )
inline

Definition at line 352 of file TriggerManager.cs.

352  {
353  return new TriggersTableContainer(transaction);
354  }
void Deveel.Data.Sql.Triggers.TriggerManager.Dispose ( )
inline

Definition at line 46 of file TriggerManager.cs.

46  {
47  Dispose(true);
48  GC.SuppressFinalize(this);
49  }
void Deveel.Data.Sql.Triggers.TriggerManager.Dispose ( bool  disposing)
inlineprivate

Definition at line 51 of file TriggerManager.cs.

51  {
52  if (disposing) {
53  if (triggerCache != null)
54  triggerCache.Clear();
55  }
56 
57  triggerCache = null;
58  transaction = null;
59  cacheValid = false;
60  }
bool IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.DropObject ( ObjectName  objName)
inlineprivate

Deletes a database object handled by this manager from the system.

Parameters
objNameThe unique name of the object to be deleted.
Returns
Returns true if a database object was found with the given unique name and successfully deleted from the system, or false if none object was found.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 203 of file TriggerManager.cs.

203  {
204  return DropTrigger(objName);
205  }
bool DropTrigger(ObjectName triggerName)
bool Deveel.Data.Sql.Triggers.TriggerManager.DropTrigger ( ObjectName  triggerName)
inline

Definition at line 286 of file TriggerManager.cs.

286  {
287  throw new NotImplementedException();
288  }
ITable Deveel.Data.Sql.Triggers.TriggerManager.FindTrigger ( ITable  table,
string  schema,
string  name 
)
inlineprivate

Definition at line 121 of file TriggerManager.cs.

121  {
122  // Find all the trigger entries with this name
123  var schemaColumn = table.GetResolvedColumnName(0);
124  var nameColumn = table.GetResolvedColumnName(1);
125 
126  using (var session = new SystemSession(transaction, SystemSchema.Name)) {
127  using (var context = session.CreateQuery()) {
128  var t = table.SimpleSelect(context, nameColumn, SqlExpressionType.Equal,
129  SqlExpression.Constant(DataObject.String(name)));
130  return t.ExhaustiveSelect(context,
131  SqlExpression.Equal(SqlExpression.Reference(schemaColumn), SqlExpression.Constant(DataObject.String(schema))));
132  }
133  }
134  }
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
IEnumerable<TriggerInfo> Deveel.Data.Sql.Triggers.TriggerManager.FindTriggers ( ObjectName  tableName,
TriggerEventType  eventType 
)
inlineprivate

Definition at line 136 of file TriggerManager.cs.

136  {
137  var fullTableName = tableName.FullName;
138  var eventTypeCode = (int)eventType;
139 
140  var table = transaction.GetTable(SystemSchema.TriggerTableName);
141  if (table == null)
142  return new TriggerInfo[0];
143 
144  var tableColumn = table.GetResolvedColumnName(3);
145  var eventTypeColumn = table.GetResolvedColumnName(4);
146 
147  ITable result;
148  using (var session = new SystemSession(transaction, SystemSchema.Name)) {
149  using (var context = session.CreateQuery()) {
150  var t = table.SimpleSelect(context, tableColumn, SqlExpressionType.Equal,
151  SqlExpression.Constant(DataObject.String(fullTableName)));
152 
153  result = t.ExhaustiveSelect(context,
154  SqlExpression.Equal(SqlExpression.Reference(eventTypeColumn), SqlExpression.Constant(eventTypeCode)));
155  }
156  }
157 
158  if (result.RowCount == 0)
159  return new TriggerInfo[0];
160 
161  var list = new List<TriggerInfo>();
162 
163  foreach (var row in result) {
164  var triggerInfo = FormTrigger(row);
165 
166  //TODO: get the other information such has the body, the external method or the procedure
167  // if this is a non-callback
168 
169  list.Add(triggerInfo);
170  }
171 
172  return list.AsEnumerable();
173  }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
int RowCount
Gets the total number of rows in the table.
Definition: ITable.cs:52
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
IEnumerable<Trigger> Deveel.Data.Sql.Triggers.TriggerManager.FindTriggers ( TriggerEventInfo  eventInfo)
inline

Definition at line 335 of file TriggerManager.cs.

335  {
336  var triggers = FindTriggers(eventInfo.TableName, eventInfo.EventType);
337  return triggers.Select(x => new Trigger(x));
338  }
IEnumerable< TriggerInfo > FindTriggers(ObjectName tableName, TriggerEventType eventType)
A TRIGGER fired at provided write events (INSERT, UPDATE or DELETE) over a table at a given moments (...
void Deveel.Data.Sql.Triggers.TriggerManager.FireTriggers ( IQuery  context,
TableEvent  tableEvent 
)
inline

Implements Deveel.Data.Sql.Triggers.ITriggerManager.

Definition at line 340 of file TriggerManager.cs.

340  {
341  if (!transaction.TableExists(SystemSchema.TriggerTableName))
342  return;
343 
345 
346  foreach (var trigger in triggerCache) {
347  if (trigger.CanInvoke(tableEvent))
348  trigger.Invoke(context, tableEvent);
349  }
350  }
TriggerInfo Deveel.Data.Sql.Triggers.TriggerManager.FormTrigger ( Row  row)
inlineprivate

Definition at line 315 of file TriggerManager.cs.

315  {
316  var schema = row.GetValue(0).Value.ToString();
317  var name = row.GetValue(1).Value.ToString();
318  var triggerName = new ObjectName(new ObjectName(schema), name);
319 
320  var triggerType = (TriggerType)((SqlNumber)row.GetValue(2).Value).ToInt32();
321 
322  // TODO: In case it's a procedural trigger, take the reference to the procedure
323  if (triggerType == TriggerType.Procedure)
324  throw new NotImplementedException();
325 
326  var tableName = ObjectName.Parse(((SqlString) row.GetValue(3).Value).ToString());
327  var eventType = (TriggerEventType) ((SqlNumber) row.GetValue(4).Value).ToInt32();
328  return new TriggerInfo(triggerName, triggerType, eventType, tableName);
329  }
TriggerEventType
The different types of high layer trigger events.
DataObject GetValue(int columnOffset)
Gets or the value of a cell of the row at the given offset.
Definition: Row.cs:203
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
TriggerType
Enumerates the types of triggers, that can be volatile (like the Callback) or stored in the database...
Definition: TriggerType.cs:22
IDbObject IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.GetObject ( ObjectName  objName)
inlineprivate

Gets a database object managed by this manager.

Parameters
objNameThe name that uniquely identifies the object to get.
Returns
Returns a IDbObject instance that is identified by the given unique name, or null if this manager was not able to map any object to the name specified.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 191 of file TriggerManager.cs.

191  {
192  return GetTrigger(objName);
193  }
Trigger GetTrigger(ObjectName triggerName)
Trigger Deveel.Data.Sql.Triggers.TriggerManager.GetTrigger ( ObjectName  triggerName)
inline

Definition at line 302 of file TriggerManager.cs.

302  {
303  var table = transaction.GetTable(SystemSchema.TriggerTableName);
304  var result = FindTrigger(table, triggerName.ParentName, triggerName.Name);
305  if (result.RowCount == 0)
306  return null;
307 
308  if (result.RowCount > 1)
309  throw new InvalidOperationException(String.Format("More than one trigger found with name '{0}'.", triggerName));
310 
311  var triggerInfo = FormTrigger(result.First());
312  return new Trigger(triggerInfo);
313  }
A long string in the system.
ITable FindTrigger(ITable table, string schema, string name)
A TRIGGER fired at provided write events (INSERT, UPDATE or DELETE) over a table at a given moments (...
void Deveel.Data.Sql.Triggers.TriggerManager.InvalidateTriggerCache ( )
inlineprivate

Definition at line 105 of file TriggerManager.cs.

105  {
106  cacheValid = false;
107  }
bool IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.ObjectExists ( ObjectName  objName)
inlineprivate

Checks if an object identified by the given name is managed by this instance.

Parameters
objNameThe name that uniquely identifies the object.
Returns

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 187 of file TriggerManager.cs.

187  {
188  return TriggerExists(objName);
189  }
bool TriggerExists(ObjectName triggerName)
void Deveel.Data.Sql.Triggers.TriggerManager.OnCommit ( TableCommitInfo  commitInfo)
inlineprivate

Definition at line 78 of file TriggerManager.cs.

78  {
79  if (tableModified) {
81  tableModified = false;
82  } else if ((commitInfo.AddedRows != null &&
83  commitInfo.AddedRows.Any()) ||
84  (commitInfo.RemovedRows != null &&
85  commitInfo.RemovedRows.Any())) {
87  }
88  }
void Deveel.Data.Sql.Triggers.TriggerManager.OnTableCommit ( TableCommitEvent  commitEvent)
inlineprivate

Definition at line 66 of file TriggerManager.cs.

66  {
67  if (tableModified) {
69  tableModified = false;
70  } else if ((commitEvent.AddedRows != null &&
71  commitEvent.AddedRows.Any()) ||
72  (commitEvent.RemovedRows != null &&
73  commitEvent.RemovedRows.Any())) {
75  }
76  }
bool IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.RealObjectExists ( ObjectName  objName)
inlineprivate

Checks if an object really exists in the system.

Parameters
objNameThe unique name of the object to check.
Returns
Returns true if an object with the given name concretely exists in the system, or false otherwise.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 183 of file TriggerManager.cs.

183  {
184  return TriggerExists(objName);
185  }
bool TriggerExists(ObjectName triggerName)
void ITriggerManager. Deveel.Data.Sql.Triggers.TriggerManager.RegisterTrigger ( TriggerInfo  triggerInfo)
inlineprivate

Implements Deveel.Data.Sql.Triggers.ITriggerManager.

Definition at line 241 of file TriggerManager.cs.

241  {
242  CreateTrigger(triggerInfo);
243  }
void CreateTrigger(TriggerInfo triggerInfo)
ObjectName Deveel.Data.Sql.Triggers.TriggerManager.ResolveName ( ObjectName  objName,
bool  ignoreCase 
)
inline

Normalizes the input object name using the case sensitivity specified.

Parameters
objNameThe input object name, that can be partial or complete, to be normalized to the real name of an object.
ignoreCaseThe case sensitivity specification used to compare the input name with the names of the existing objects handled by this manager.
Returns
Returns the fully normalized ObjectName that is the real name of an object matching the input name, or null if the input name was not possible to be resolved.

Implements Deveel.Data.Sql.IObjectManager.

Definition at line 207 of file TriggerManager.cs.

207  {
208  throw new NotImplementedException();
209  }
static byte [] Deveel.Data.Sql.Triggers.TriggerManager.SerializeArguments ( TriggerArgument  args)
inlinestaticprivate

Definition at line 228 of file TriggerManager.cs.

228  {
229  using (var stream = new MemoryStream()) {
230  using (var writer = new BinaryWriter(stream)) {
231  var serializer = new BinarySerializer();
232  serializer.Serialize(writer, args);
233 
234  writer.Flush();
235 
236  return stream.ToArray();
237  }
238  }
239  }
bool Deveel.Data.Sql.Triggers.TriggerManager.TriggerExists ( ObjectName  triggerName)
inline

Definition at line 290 of file TriggerManager.cs.

290  {
291  var table = transaction.GetTable(SystemSchema.TriggerTableName);
292  var result = FindTrigger(table, triggerName.ParentName, triggerName.Name);
293  if (result.RowCount == 0)
294  return false;
295 
296  if (result.RowCount > 1)
297  throw new InvalidOperationException(String.Format("More than one trigger found with name '{0}'.", triggerName));
298 
299  return true;
300  }
A long string in the system.
ITable FindTrigger(ITable table, string schema, string name)

Member Data Documentation

bool Deveel.Data.Sql.Triggers.TriggerManager.cacheValid
private

Definition at line 33 of file TriggerManager.cs.

bool Deveel.Data.Sql.Triggers.TriggerManager.tableModified
private

Definition at line 32 of file TriggerManager.cs.

ITransaction Deveel.Data.Sql.Triggers.TriggerManager.transaction
private

Definition at line 31 of file TriggerManager.cs.

List<Trigger> Deveel.Data.Sql.Triggers.TriggerManager.triggerCache
private

Definition at line 34 of file TriggerManager.cs.

Property Documentation

DbObjectType IObjectManager. Deveel.Data.Sql.Triggers.TriggerManager.ObjectType
getprivate

Definition at line 62 of file TriggerManager.cs.


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