18 using System.Collections.Generic;
29 namespace Deveel.Data.Sql.Triggers {
37 this.transaction = transaction;
38 triggerCache =
new List<Trigger>(24);
48 GC.SuppressFinalize(
this);
53 if (triggerCache != null)
68 InvalidateTriggerCache();
69 tableModified =
false;
70 }
else if ((commitEvent.
AddedRows != null &&
74 InvalidateTriggerCache();
80 InvalidateTriggerCache();
81 tableModified =
false;
82 }
else if ((commitInfo.
AddedRows != null &&
86 InvalidateTriggerCache();
94 var list =
new List<Trigger>();
95 foreach (var row
in table) {
96 var triggerInfo = FormTrigger(row);
97 list.Add(
new Trigger(triggerInfo));
100 triggerCache =
new List<Trigger>(list);
118 transaction.CreateTable(tableInfo);
123 var schemaColumn = table.GetResolvedColumnName(0);
124 var nameColumn = table.GetResolvedColumnName(1);
127 using (var context = session.CreateQuery()) {
130 return t.ExhaustiveSelect(context,
137 var fullTableName = tableName.
FullName;
138 var eventTypeCode = (int)eventType;
144 var tableColumn = table.GetResolvedColumnName(3);
145 var eventTypeColumn = table.GetResolvedColumnName(4);
149 using (var context = session.CreateQuery()) {
153 result = t.ExhaustiveSelect(context,
161 var list =
new List<TriggerInfo>();
163 foreach (var row
in result) {
164 var triggerInfo = FormTrigger(row);
169 list.Add(triggerInfo);
172 return list.AsEnumerable();
177 if (triggerInfo == null)
178 throw new ArgumentException();
180 CreateTrigger(triggerInfo);
184 return TriggerExists(objName);
188 return TriggerExists(objName);
192 return GetTrigger(objName);
197 if (triggerInfo == null)
198 throw new ArgumentException();
200 return AlterTrigger(triggerInfo);
204 return DropTrigger(objName);
208 throw new NotImplementedException();
224 data.
SetValue(
"Arguments", Arguments);
229 using (var stream =
new MemoryStream()) {
230 using (var writer =
new BinaryWriter(stream)) {
232 serializer.Serialize(writer, args);
236 return stream.ToArray();
242 CreateTrigger(triggerInfo);
251 var binArgs = SerializeArguments(args);
259 var action = (
int) triggerInfo.
EventType;
265 var row = table.NewRow();
275 InvalidateTriggerCache();
279 tableModified =
true;
280 }
catch (Exception) {
287 throw new NotImplementedException();
292 var result = FindTrigger(table, triggerName.
ParentName, triggerName.
Name);
293 if (result.RowCount == 0)
296 if (result.RowCount > 1)
297 throw new InvalidOperationException(
String.Format(
"More than one trigger found with name '{0}'.", triggerName));
304 var result = FindTrigger(table, triggerName.
ParentName, triggerName.
Name);
305 if (result.RowCount == 0)
308 if (result.RowCount > 1)
309 throw new InvalidOperationException(
String.Format(
"More than one trigger found with name '{0}'.", triggerName));
311 var triggerInfo = FormTrigger(result.First());
312 return new Trigger(triggerInfo);
324 throw new NotImplementedException();
328 return new TriggerInfo(triggerName, triggerType, eventType, tableName);
332 throw new NotImplementedException();
337 return triggers.Select(x =>
new Trigger(x));
346 foreach (var trigger
in triggerCache) {
347 if (trigger.CanInvoke(tableEvent))
348 trigger.Invoke(context, tableEvent);
356 #region TriggersTableContainer
364 var triggerName = GetTableName(offset);
365 return CreateTableInfo(triggerName.ParentName, triggerName.Name);
381 return tableInfo.AsReadOnly();
387 var enumerator = table.GetEnumerator();
391 while (enumerator.MoveNext()) {
392 i = enumerator.Current.RowId.RowNumber;
401 throw new ArgumentOutOfRangeException(
"offset");
403 var schema = table.GetValue(rowIndex, 0).Value.ToString();
404 var name = table.GetValue(rowIndex, 1).Value.ToString();
406 var tableInfo = CreateTableInfo(schema, name);
408 var type = table.GetValue(rowIndex, 2);
409 var tableName = table.GetValue(rowIndex, 3);
410 var routine = table.GetValue(rowIndex, 4);
411 var args = table.GetValue(rowIndex, 5);
412 var owner = table.GetValue(rowIndex, 6);
416 TableName = tableName,
430 this.tableInfo = tableInfo;
434 get {
return tableInfo; }
447 public override int RowCount {
453 throw new ArgumentOutOfRangeException(
"rowNumber");
455 switch (columnOffset) {
467 throw new ArgumentOutOfRangeException(
"columnOffset");
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
IEnumerable< int > AddedRows
static TableInfo CreateTableInfo(string schema, string name)
bool AlterObject(IObjectInfo objInfo)
Modifies an existing object managed, identified by IObjectInfo.FullName component of the given specif...
TriggerArgument(ObjectData data)
static DataObject Integer(int value)
bool DropObject(ObjectName objName)
Deletes a database object handled by this manager from the system.
void GetData(SerializeData data)
Defines the contract to access the data contained into a table of a database.
static DataObject Binary(SqlBinary binary)
Defines the information about a trigger on a table of the database, such as the event on which is fir...
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
A long string in the system.
The system implementation of a transaction model that handles isolated operations within a database c...
TriggerEventType
The different types of high layer trigger events.
IEnumerable< int > AddedRows
bool RealObjectExists(ObjectName objName)
Checks if an object really exists in the system.
DataObject GetValue(int columnOffset)
Gets or the value of a cell of the row at the given offset.
IEnumerable< TriggerInfo > FindTriggers(ObjectName tableName, TriggerEventType eventType)
TriggerInfo FormTrigger(Row row)
Trigger GetTrigger(ObjectName triggerName)
TriggerManager(ITransaction transaction)
void OnCommit(TableCommitInfo commitInfo)
ObjectName ProcedureName
Gets or sets the name of a stored procedure to be executed when the trigger is fired.
TriggersTableContainer(ITransaction transaction)
ObjectName ResolveName(ObjectName objName, bool ignoreCase)
Normalizes the input object name using the case sensitivity specified.
Represents a database object, such as a table, a trigger, a type or a column.
An event fired when a database object of the given type is created during the lifetime of a transacti...
static BinaryType Binary(int maxSize)
void InvalidateTriggerCache()
static byte[] SerializeArguments(TriggerArgument args)
static NumericType Integer()
void SetValue(string key, Type type, object value)
The default implementation of a database in a system.
void RegisterEvent(ITransactionEvent e)
static SqlBinaryExpression Equal(SqlExpression left, SqlExpression right)
ITable FindTrigger(ITable table, string schema, string name)
Describes the name of an object within a database.
ISqlObject Value
Gets the underlined value that is handled.
A single row in a table of a database.
static DataObject String(string s)
TriggerEventType EventType
Gets the type of event that happened on the table.
void Dispose(bool disposing)
ICollection< SqlExpression > Arguments
SqlExpressionType
All the possible type of SqlExpression supported
ObjectName TableName
Gets the fully qualified name of the table where the event happened.
static readonly ObjectName TriggerTableName
IEnumerable< int > RemovedRows
TriggerEventType EventType
Gets the modification event on the attached table at which to fire the trigger.
A routine (PROCEDURE or FUNCTION) defined in a database, that is a program with defined input paramet...
A user-defined TYPE that holds complex objects in a database column.
ITableContainer CreateTriggersTableContainer()
override string GetTableType(int offset)
Gets the type of the table at the given offset.
ObjectName TriggerName
Gets the fully qualified name of the trigger.
void OnTableCommit(TableCommitEvent commitEvent)
TriggerArgument(SqlExpression[] args)
IEnumerable< int > RemovedRows
static NumericType Numeric()
void CreateObject(IObjectInfo objInfo)
Create a new object of the ObjectType given the specifications given.
Provides the constant names of the types of tables in a database system.
TriggerType
Enumerates the types of triggers, that can be volatile (like the Callback) or stored in the database...
void RegisterOnCommit(Action< TableCommitInfo > action)
void CreateTrigger(TriggerInfo triggerInfo)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
A container for any system tables that are generated from information inside the database engine...
Provides utilities and properties for handling the SYSTEN schema of a database.
IEnumerable< Trigger > FindTriggers(TriggerEventInfo eventInfo)
string FullName
Gets the full reference name formatted.
int RowCount
Gets the total number of rows in the table.
DbObjectType ObjectType
Gets the type of objects managed by this instance.
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
Exposes the context of an event fired on a table.
bool TriggerExists(ObjectName triggerName)
void FireTriggers(IQuery context, TableEvent tableEvent)
ObjectName TableName
Gets the fully qualified name of the database table on which to attach the trigger.
override ITable GetTable(int offset)
Gets the table contained at the given offset within the context.
string Name
Gets the name of the object being referenced.
TriggerTable(ITransaction transaction, TableInfo tableInfo)
Represents an event fired at a given modification event (either INSERT, DELETE or UPDATE) at a given ...
static SqlReferenceExpression Reference(ObjectName objectName)
static StringType String()
TransactionRegistry Registry
List< Trigger > triggerCache
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.
object GetValue(string key)
Defines the base class for instances that represent SQL expression tree nodes.
Defines the contract for the business managers of database objects of a given type.
const string Name
The name of the system schema that contains tables referring to system information.
static SqlConstantExpression Constant(object value)
The simplest implementation of a transaction.
An object that defines the arguments of an event, used to find triggers associated.
void Create()
Initializes the manager into the underlying system.
DbObjectType
The kind of objects that can be handled by a database system and its managers
TriggerType TriggerType
Gets the type of trigger.
Defines the metadata properties of a table existing within a database.
bool AlterTrigger(TriggerInfo triggerInfo)
void RegisterTrigger(TriggerInfo triggerInfo)
bool DropTrigger(ObjectName triggerName)
override TableInfo GetTableInfo(int offset)
Gets the information of the table at the given offset in this container.