001: /**
002: * com.mckoi.database.TableModificationEvent 07 Mar 2003
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: /**
026: * The event information of when a table is modified inside a transaction.
027: *
028: * @author Tobias Downer
029: */
030:
031: public class TableModificationEvent {
032:
033: // ----- Statics -----
034:
035: /**
036: * Event that occurs before the action
037: */
038: public static final int BEFORE = 0x010;
039:
040: /**
041: * Event that occurs after the action
042: */
043: public static final int AFTER = 0x020;
044:
045: // ---
046:
047: /**
048: * Event type for insert action.
049: */
050: public static final int INSERT = 0x001;
051:
052: /**
053: * Event type for update action.
054: */
055: public static final int UPDATE = 0x002;
056:
057: /**
058: * Event type for delete action.
059: */
060: public static final int DELETE = 0x004;
061:
062: // ---
063:
064: /**
065: * Event for before an insert.
066: */
067: public static final int BEFORE_INSERT = BEFORE | INSERT;
068:
069: /**
070: * Event for after an insert.
071: */
072: public static final int AFTER_INSERT = AFTER | INSERT;
073:
074: /**
075: * Event for before an update.
076: */
077: public static final int BEFORE_UPDATE = BEFORE | UPDATE;
078:
079: /**
080: * Event for after an update.
081: */
082: public static final int AFTER_UPDATE = AFTER | UPDATE;
083:
084: /**
085: * Event for before a delete.
086: */
087: public static final int BEFORE_DELETE = BEFORE | DELETE;
088:
089: /**
090: * Event for after a delete.
091: */
092: public static final int AFTER_DELETE = AFTER | DELETE;
093:
094: // ----- Members -----
095:
096: /**
097: * The DatabaseConnection of the table that the modification occurred in.
098: */
099: private DatabaseConnection connection;
100:
101: /**
102: * The name of the table that was modified.
103: */
104: private TableName table_name;
105:
106: /**
107: * The type of event that occurred.
108: */
109: private int event_type;
110:
111: /**
112: * A RowData object representing the row that is being inserted by this
113: * modification. This is set for INSERT and UPDATE events. If the event
114: * type is BEFORE then this data represents the new data in the table and
115: * can be modified. This represents the NEW information.
116: */
117: private RowData row_data;
118:
119: /**
120: * The row index of the table that is before removed by this modification.
121: * This is set for UPDATE and DELETE events. This represents the OLD
122: * information.
123: */
124: private int row_index = -1;
125:
126: /**
127: * General Constructor.
128: */
129: private TableModificationEvent(DatabaseConnection connection,
130: TableName table_name, int row_index, RowData row_data,
131: int type, boolean before) {
132: this .connection = connection;
133: this .table_name = table_name;
134: this .row_index = row_index;
135: this .row_data = row_data;
136: this .event_type = type | (before ? BEFORE : AFTER);
137: }
138:
139: /**
140: * Constructs an insert event.
141: */
142: TableModificationEvent(DatabaseConnection connection,
143: TableName table_name, RowData row_data, boolean before) {
144: this (connection, table_name, -1, row_data, INSERT, before);
145: }
146:
147: /**
148: * Constructs an update event.
149: */
150: TableModificationEvent(DatabaseConnection connection,
151: TableName table_name, int row_index, RowData row_data,
152: boolean before) {
153: this (connection, table_name, row_index, row_data, UPDATE,
154: before);
155: }
156:
157: /**
158: * Constructs a delete event.
159: */
160: TableModificationEvent(DatabaseConnection connection,
161: TableName table_name, int row_index, boolean before) {
162: this (connection, table_name, row_index, null, DELETE, before);
163: }
164:
165: /**
166: * Returns the DatabaseConnection that this event fired in.
167: */
168: public DatabaseConnection getDatabaseConnection() {
169: return connection;
170: }
171:
172: /**
173: * Returns the event type.
174: */
175: public int getType() {
176: return event_type;
177: }
178:
179: /**
180: * Returns true if this is a BEFORE event.
181: */
182: public boolean isBefore() {
183: return (event_type & BEFORE) != 0;
184: }
185:
186: /**
187: * Returns true if this is a AFTER event.
188: */
189: public boolean isAfter() {
190: return (event_type & AFTER) != 0;
191: }
192:
193: /**
194: * Returns true if this is an INSERT event.
195: */
196: public boolean isInsert() {
197: return (event_type & INSERT) != 0;
198: }
199:
200: /**
201: * Returns true if this is an UPDATE event.
202: */
203: public boolean isUpdate() {
204: return (event_type & UPDATE) != 0;
205: }
206:
207: /**
208: * Returns true if this is an DELETE event.
209: */
210: public boolean isDelete() {
211: return (event_type & DELETE) != 0;
212: }
213:
214: /**
215: * Returns the name of the table of this modification.
216: */
217: public TableName getTableName() {
218: return table_name;
219: }
220:
221: /**
222: * Returns the index of the row in the table that was affected by this
223: * event or -1 if event type is INSERT.
224: */
225: public int getRowIndex() {
226: return row_index;
227: }
228:
229: /**
230: * Returns the RowData object that represents the change that is being
231: * made to the table either by an INSERT or UPDATE. For a DELETE event this
232: * return null.
233: */
234: public RowData getRowData() {
235: return row_data;
236: }
237:
238: /**
239: * Returns true if the given listener type should be notified of this type
240: * of table modification event. For example, if this is a BEFORE event then
241: * the BEFORE bit on the given type must be set and if this is an INSERT event
242: * then the INSERT bit on the given type must be set.
243: */
244: public boolean listenedBy(int listen_t) {
245: // If this is a BEFORE trigger, then we must be listening for BEFORE events,
246: // etc.
247: boolean ba_match = ((event_type & BEFORE) != 0 && (listen_t & BEFORE) != 0)
248: || ((event_type & AFTER) != 0 && (listen_t & AFTER) != 0);
249: // If this is an INSERT trigger, then we must be listening for INSERT
250: // events, etc.
251: boolean trig_match = ((event_type & INSERT) != 0 && (listen_t & INSERT) != 0)
252: || ((event_type & DELETE) != 0 && (listen_t & DELETE) != 0)
253: || ((event_type & UPDATE) != 0 && (listen_t & UPDATE) != 0);
254: // If both of the above are true
255: return (ba_match && trig_match);
256: }
257:
258: }
|