DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Trigger.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.Diagnostics;
20 
21 namespace Deveel.Data.Sql.Triggers {
38  public class Trigger : IDbObject {
47  public Trigger(TriggerInfo triggerInfo) {
48  if (triggerInfo == null)
49  throw new ArgumentNullException("triggerInfo");
50 
51  TriggerInfo = triggerInfo;
52  }
53 
57  public TriggerInfo TriggerInfo { get; private set; }
58 
64  public ObjectName TriggerName {
65  get { return TriggerInfo.TriggerName; }
66  }
67 
74  get { return TriggerInfo.TriggerType; }
75  }
76 
78  get { return DbObjectType.Trigger; }
79  }
80 
82  get { return TriggerInfo.TriggerName; }
83  }
84 
85  private void FireTrigger(IQuery context, TableEvent tableEvent) {
86  if (TriggerType == TriggerType.Callback) {
87  NotifyTriggerEvent(context, tableEvent);
88  } else {
89  ExecuteProcedure(context);
90  }
91  }
92 
93  private void ExecuteProcedure(IQuery context) {
94  throw new NotImplementedException();
95  }
96 
97  private void NotifyTriggerEvent(IQuery context, TableEvent tableEvent) {
98  var tableName = tableEvent.Table.FullName;
99  var eventType = tableEvent.EventType;
100 
101  var triggerEvent = new TriggerEvent(context.Session, TriggerName, tableName, eventType, tableEvent.OldRowId,
102  tableEvent.NewRow);
103  // TODO: context.RegisterEvent(triggerEvent);
104  }
105 
106  public bool CanInvoke(TableEvent context) {
107  if ((TriggerInfo.EventType & context.EventType) == 0)
108  return false;
109 
110  var tableName = context.Table.TableInfo.TableName;
111  return TriggerInfo.TableName == null ||
112  TriggerInfo.TableName.Equals(tableName, true);
113  }
114 
115  public void Invoke(IQuery context, TableEvent tableEvent) {
116  var isBefore = (tableEvent.EventType & TriggerEventType.Before) != 0;
117 
118  var transaction = context.Session.Transaction;
119  if (transaction is ITableStateHandler) {
120  var stateHandler = (ITableStateHandler) transaction;
121  var oldState = stateHandler.TableState;
122  var newState = new OldNewTableState(tableEvent.Table.FullName, tableEvent.OldRowId.RowNumber, tableEvent.NewRow,
123  isBefore);
124 
125  stateHandler.SetTableState(newState);
126 
127  try {
128  FireTrigger(context, tableEvent);
129  } finally {
130  stateHandler.SetTableState(oldState);
131  }
132  } else {
133  FireTrigger(context, tableEvent);
134  }
135  }
136  }
137 }
Defines the information about a trigger on a table of the database, such as the event on which is fir...
Definition: TriggerInfo.cs:29
Trigger(TriggerInfo triggerInfo)
Constructs a trigger with the given information.
Definition: Trigger.cs:47
TriggerEventType
The different types of high layer trigger events.
ITable Table
Gets the table on which the event occurred.
Definition: TableEvent.cs:40
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
ObjectName FullName
Gets the fully qualified name of the object used to resolve it uniquely within the database...
Definition: IDbObject.cs:30
void FireTrigger(IQuery context, TableEvent tableEvent)
Definition: Trigger.cs:85
Describes the name of an object within a database.
Definition: ObjectName.cs:44
ITransaction Transaction
Gets the instance of ITransaction that handles the transactional operations of this session...
Definition: ISession.cs:46
override bool Equals(object obj)
Definition: ObjectName.cs:241
bool CanInvoke(TableEvent context)
Definition: Trigger.cs:106
TriggerEventType EventType
Gets the modification event on the attached table at which to fire the trigger.
Definition: TriggerInfo.cs:84
ObjectName TriggerName
Gets the fully qualified name of the trigger.
Definition: TriggerInfo.cs:78
void NotifyTriggerEvent(IQuery context, TableEvent tableEvent)
Definition: Trigger.cs:97
TriggerType
Enumerates the types of triggers, that can be volatile (like the Callback) or stored in the database...
Definition: TriggerType.cs:22
ISession Session
Definition: IQuery.cs:23
RowId OldRowId
Gets an optional reference to a row removed or updated.
Definition: TableEvent.cs:50
TriggerEventType EventType
Gets the type of event that occurred on the table.
Definition: TableEvent.cs:45
DbObjectType ObjectType
Gets the type of database object that the implementation is for
Definition: IDbObject.cs:35
Exposes the context of an event fired on a table.
Definition: TableEvent.cs:26
ObjectName TableName
Gets the fully qualified name of the database table on which to attach the trigger.
Definition: TriggerInfo.cs:96
void Invoke(IQuery context, TableEvent tableEvent)
Definition: Trigger.cs:115
Represents an event fired at a given modification event (either INSERT, DELETE or UPDATE) at a given ...
Definition: Trigger.cs:38
Row NewRow
Gets the row object being added or updated.
Definition: TableEvent.cs:55
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
TriggerType TriggerType
Gets the type of trigger.
Definition: TriggerInfo.cs:90
void ExecuteProcedure(IQuery context)
Definition: Trigger.cs:93