@@ -2922,6 +2922,26 @@ impl Spanned for RenameTableNameKind {
2922
2922
}
2923
2923
}
2924
2924
2925
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2926
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2927
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
2928
+ /// Whether the syntax used for the trigger object (ROW or STATEMENT) is `FOR` or `FOR EACH`.
2929
+ pub enum TriggerObjectKind {
2930
+ /// The `FOR` syntax is used.
2931
+ For ( TriggerObject ) ,
2932
+ /// The `FOR EACH` syntax is used.
2933
+ ForEach ( TriggerObject ) ,
2934
+ }
2935
+
2936
+ impl Display for TriggerObjectKind {
2937
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2938
+ match self {
2939
+ TriggerObjectKind :: For ( obj) => write ! ( f, "FOR {obj}" ) ,
2940
+ TriggerObjectKind :: ForEach ( obj) => write ! ( f, "FOR EACH {obj}" ) ,
2941
+ }
2942
+ }
2943
+ }
2944
+
2925
2945
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2926
2946
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
2927
2947
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -2943,6 +2963,23 @@ pub struct CreateTrigger {
2943
2963
///
2944
2964
/// [MsSql](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-ver16#arguments)
2945
2965
pub or_alter : bool ,
2966
+ /// True if this is a temporary trigger.
2967
+ ///
2968
+ /// Examples:
2969
+ ///
2970
+ /// ```sql
2971
+ /// CREATE TEMP TRIGGER trigger_name
2972
+ /// ```
2973
+ ///
2974
+ /// or
2975
+ ///
2976
+ /// ```sql
2977
+ /// CREATE TEMPORARY TRIGGER trigger_name;
2978
+ /// CREATE TEMP TRIGGER trigger_name;
2979
+ /// ```
2980
+ ///
2981
+ /// [SQLite](https://sqlite.org/lang_createtrigger.html#temp_triggers_on_non_temp_tables)
2982
+ pub temporary : bool ,
2946
2983
/// The `OR REPLACE` clause is used to re-create the trigger if it already exists.
2947
2984
///
2948
2985
/// Example:
@@ -2987,6 +3024,8 @@ pub struct CreateTrigger {
2987
3024
/// ```
2988
3025
pub period : TriggerPeriod ,
2989
3026
/// Whether the trigger period was specified before the target table name.
3027
+ /// This does not refer to whether the period is BEFORE, AFTER, or INSTEAD OF,
3028
+ /// but rather the position of the period clause in relation to the table name.
2990
3029
///
2991
3030
/// ```sql
2992
3031
/// -- period_before_table == true: Postgres, MySQL, and standard SQL
@@ -3006,9 +3045,9 @@ pub struct CreateTrigger {
3006
3045
pub referencing : Vec < TriggerReferencing > ,
3007
3046
/// This specifies whether the trigger function should be fired once for
3008
3047
/// every row affected by the trigger event, or just once per SQL statement.
3009
- pub trigger_object : TriggerObject ,
3010
- /// Whether to include the `EACH` term of the `FOR EACH`, as it is optional syntax .
3011
- pub include_each : bool ,
3048
+ /// This is optional in some SQL dialects, such as SQLite, and if not specified, in
3049
+ /// those cases, the implied default is `FOR EACH ROW` .
3050
+ pub trigger_object : Option < TriggerObjectKind > ,
3012
3051
/// Triggering conditions
3013
3052
pub condition : Option < Expr > ,
3014
3053
/// Execute logic block
@@ -3025,6 +3064,7 @@ impl Display for CreateTrigger {
3025
3064
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3026
3065
let CreateTrigger {
3027
3066
or_alter,
3067
+ temporary,
3028
3068
or_replace,
3029
3069
is_constraint,
3030
3070
name,
@@ -3036,15 +3076,15 @@ impl Display for CreateTrigger {
3036
3076
referencing,
3037
3077
trigger_object,
3038
3078
condition,
3039
- include_each,
3040
3079
exec_body,
3041
3080
statements_as,
3042
3081
statements,
3043
3082
characteristics,
3044
3083
} = self ;
3045
3084
write ! (
3046
3085
f,
3047
- "CREATE {or_alter}{or_replace}{is_constraint}TRIGGER {name} " ,
3086
+ "CREATE {temporary}{or_alter}{or_replace}{is_constraint}TRIGGER {name} " ,
3087
+ temporary = if * temporary { "TEMPORARY " } else { "" } ,
3048
3088
or_alter = if * or_alter { "OR ALTER " } else { "" } ,
3049
3089
or_replace = if * or_replace { "OR REPLACE " } else { "" } ,
3050
3090
is_constraint = if * is_constraint { "CONSTRAINT " } else { "" } ,
@@ -3076,10 +3116,8 @@ impl Display for CreateTrigger {
3076
3116
write ! ( f, " REFERENCING {}" , display_separated( referencing, " " ) ) ?;
3077
3117
}
3078
3118
3079
- if * include_each {
3080
- write ! ( f, " FOR EACH {trigger_object}" ) ?;
3081
- } else if exec_body. is_some ( ) {
3082
- write ! ( f, " FOR {trigger_object}" ) ?;
3119
+ if let Some ( trigger_object) = trigger_object {
3120
+ write ! ( f, " {trigger_object}" ) ?;
3083
3121
}
3084
3122
if let Some ( condition) = condition {
3085
3123
write ! ( f, " WHEN {condition}" ) ?;
0 commit comments