Skip to content

Commit

Permalink
added support for CREATE TRIGGER ... UPDATE OF column
Browse files Browse the repository at this point in the history
  • Loading branch information
fordfrog committed Sep 20, 2012
1 parent f855af5 commit 532b079
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ website at http://apgdiff.startnet.biz/

#### New Features
* Added support for ALTER SEQUENCE OWNED BY (patch by Mikhail Petrov).
* Added support for CREATE TRIGGER ... UPDATE OF column.

#### Fixes
* Fixed issue with comments not being added on newly created columns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public static void parse(final PgDatabase database,
trigger.setOnInsert(true);
} else if (parser.expectOptional("UPDATE")) {
trigger.setOnUpdate(true);

if (parser.expectOptional("OF")) {
do {
trigger.addUpdateColumn(parser.parseIdentifier());
} while (parser.expectOptional(","));
}
} else if (parser.expectOptional("DELETE")) {
trigger.setOnDelete(true);
} else if (parser.expectOptional("TRUNCATE")) {
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package cz.startnet.utils.pgdiff.schema;

import cz.startnet.utils.pgdiff.PgDiffUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Stores trigger information.
Expand Down Expand Up @@ -52,6 +55,11 @@ public class PgTrigger {
* Whether the trigger should be fired on TRUNCATE.
*/
private boolean onTruncate;
/**
* Optional list of columns for UPDATE event.
*/
@SuppressWarnings("CollectionWithoutInitialCapacity")
private final List<String> updateColumns = new ArrayList<String>();
/**
* WHEN condition.
*/
Expand Down Expand Up @@ -124,6 +132,23 @@ public String getCreationSQL() {
}

sbSQL.append(" UPDATE");

if (!updateColumns.isEmpty()) {
sbSQL.append(" OF");

boolean first = true;

for (final String columnName : updateColumns) {
if (first) {
first = false;
} else {
sbSQL.append(',');
}

sbSQL.append(' ');
sbSQL.append(columnName);
}
}
}

if (isOnDelete()) {
Expand Down Expand Up @@ -324,6 +349,24 @@ public String getTableName() {
return tableName;
}

/**
* Getter for {@link #updateColumns}.
*
* @return {@link #updateColumns}
*/
public List<String> getUpdateColumns() {
return Collections.unmodifiableList(updateColumns);
}

/**
* Adds column name to the list of update columns.
*
* @param columnName column name
*/
public void addUpdateColumn(final String columnName) {
updateColumns.add(columnName);
}

/**
* Getter for {@link #when}.
*
Expand Down Expand Up @@ -359,6 +402,17 @@ public boolean equals(final Object object) {
&& (onUpdate == trigger.isOnUpdate())
&& (onTruncate == trigger.isOnTruncate())
&& tableName.equals(trigger.getTableName());

if (equals) {
final List<String> sorted1 =
new ArrayList<String>(updateColumns);
final List<String> sorted2 =
new ArrayList<String>(trigger.getUpdateColumns());
Collections.sort(sorted1);
Collections.sort(sorted2);

equals = sorted1.equals(sorted2);
}
}

return equals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ALTER TABLE public.test_table OWNER TO fordfrog;
--

CREATE TRIGGER test_table_trigger
BEFORE INSERT OR UPDATE ON test_table
BEFORE INSERT OR UPDATE OF id ON test_table
FOR EACH ROW
EXECUTE PROCEDURE test_table_trigger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
DROP TRIGGER test_table_trigger ON test_table;

CREATE TRIGGER test_table_trigger
BEFORE INSERT ON test_table
BEFORE INSERT OR UPDATE OF id ON test_table
FOR EACH STATEMENT
EXECUTE PROCEDURE test_table_trigger();
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ALTER TABLE public.test_table OWNER TO fordfrog;
--

CREATE TRIGGER test_table_trigger
BEFORE INSERT ON test_table
BEFORE INSERT OR UPDATE OF id ON test_table
FOR EACH STATEMENT
EXECUTE PROCEDURE test_table_trigger();

Expand Down

0 comments on commit 532b079

Please sign in to comment.