1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.Session; 11 import org.h2.message.Message; 12 import org.h2.schema.Schema; 13 import org.h2.schema.TriggerObject; 14 import org.h2.table.Table; 15 16 19 20 public class CreateTrigger extends SchemaCommand { 21 23 private String triggerName; 24 private boolean ifNotExists; 25 26 private boolean before; 27 private int typeMask; 28 private boolean rowBased; 29 private int queueSize = TriggerObject.DEFAULT_QUEUE_SIZE; 30 private boolean noWait; 31 private String tableName; 32 private String triggerClassName; 33 34 public CreateTrigger(Session session, Schema schema) { 35 super(session, schema); 36 } 37 38 public void setBefore(boolean before) { 39 this.before = before; 40 } 41 42 public void setTriggerClassName(String triggerClassName) { 43 this.triggerClassName = triggerClassName; 44 } 45 46 public void setTypeMask(int typeMask) { 47 this.typeMask = typeMask; 48 } 49 50 public void setRowBased(boolean rowBased) { 51 this.rowBased = rowBased; 52 } 53 54 public void setQueueSize(int size) { 55 this.queueSize = size; 56 } 57 58 public void setNoWait(boolean noWait) { 59 this.noWait = noWait; 60 } 61 62 public void setTableName(String tableName) { 63 this.tableName = tableName; 64 } 65 66 public void setTriggerName(String name) { 67 this.triggerName = name; 68 } 69 70 public void setIfNotExists(boolean ifNotExists) { 71 this.ifNotExists = ifNotExists; 73 } 74 75 public int update() throws SQLException { 76 session.commit(); 78 Database db = session.getDatabase(); 79 if(getSchema().findTrigger(triggerName)!=null) { 80 if (ifNotExists) { 81 return 0; 82 } 83 throw Message.getSQLException(Message.TRIGGER_ALREADY_EXISTS_1, 84 triggerName); 85 } 86 int id = getObjectId(false, true); 87 Table table = getSchema().getTableOrView(session, tableName); 88 TriggerObject trigger = new TriggerObject(getSchema(), id, triggerName, table); 89 trigger.setBefore(before); 90 trigger.setNoWait(noWait); 91 trigger.setQueueSize(queueSize); 92 trigger.setRowBased(rowBased); 93 trigger.setTriggerClassName(session, triggerClassName); 94 trigger.setTypeMask(typeMask); 95 db.addSchemaObject(session, trigger); 96 table.addTrigger(trigger); 97 return 0; 98 } 99 100 } 101 | Popular Tags |