001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.ddl;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.constant.ErrorCode;
011: import org.h2.engine.Database;
012: import org.h2.engine.Session;
013: import org.h2.message.Message;
014: import org.h2.schema.Schema;
015: import org.h2.schema.TriggerObject;
016: import org.h2.table.Table;
017:
018: /**
019: * This class represents the statement
020: * CREATE TRIGGER
021: */
022: public class CreateTrigger extends SchemaCommand {
023:
024: private String triggerName;
025: private boolean ifNotExists;
026:
027: private boolean before;
028: private int typeMask;
029: private boolean rowBased;
030: private int queueSize = TriggerObject.DEFAULT_QUEUE_SIZE;
031: private boolean noWait;
032: private String tableName;
033: private String triggerClassName;
034: private boolean force;
035:
036: public CreateTrigger(Session session, Schema schema) {
037: super (session, schema);
038: }
039:
040: public void setBefore(boolean before) {
041: this .before = before;
042: }
043:
044: public void setTriggerClassName(String triggerClassName) {
045: this .triggerClassName = triggerClassName;
046: }
047:
048: public void setTypeMask(int typeMask) {
049: this .typeMask = typeMask;
050: }
051:
052: public void setRowBased(boolean rowBased) {
053: this .rowBased = rowBased;
054: }
055:
056: public void setQueueSize(int size) {
057: this .queueSize = size;
058: }
059:
060: public void setNoWait(boolean noWait) {
061: this .noWait = noWait;
062: }
063:
064: public void setTableName(String tableName) {
065: this .tableName = tableName;
066: }
067:
068: public void setTriggerName(String name) {
069: this .triggerName = name;
070: }
071:
072: public void setIfNotExists(boolean ifNotExists) {
073: // TODO trigger: if exists - probably better use 'or replace'
074: this .ifNotExists = ifNotExists;
075: }
076:
077: public int update() throws SQLException {
078: // TODO rights: what rights are required to create a trigger?
079: session.commit(true);
080: Database db = session.getDatabase();
081: if (getSchema().findTrigger(triggerName) != null) {
082: if (ifNotExists) {
083: return 0;
084: }
085: throw Message.getSQLException(
086: ErrorCode.TRIGGER_ALREADY_EXISTS_1, triggerName);
087: }
088: int id = getObjectId(false, true);
089: Table table = getSchema().getTableOrView(session, tableName);
090: TriggerObject trigger = new TriggerObject(getSchema(), id,
091: triggerName, table);
092: trigger.setBefore(before);
093: trigger.setNoWait(noWait);
094: trigger.setQueueSize(queueSize);
095: trigger.setRowBased(rowBased);
096: trigger.setTypeMask(typeMask);
097: trigger.setTriggerClassName(session, triggerClassName, force);
098: db.addSchemaObject(session, trigger);
099: table.addTrigger(trigger);
100: return 0;
101: }
102:
103: public void setForce(boolean force) {
104: this.force = force;
105: }
106:
107: }
|