@@ -10,6 +10,15 @@ use serde::{Deserialize, Serialize};
1010use std:: collections:: HashMap ;
1111use uuid:: Uuid ;
1212
13+ #[ cfg( any( feature = "postgres" , feature = "mysql" ) ) ]
14+ use sqlx:: { Decode , Encode , Type } ;
15+
16+ #[ cfg( feature = "postgres" ) ]
17+ use sqlx:: Postgres ;
18+
19+ #[ cfg( feature = "mysql" ) ]
20+ use sqlx:: MySql ;
21+
1322/// Unique identifier for a job batch.
1423pub type BatchId = Uuid ;
1524
@@ -48,6 +57,95 @@ pub enum BatchStatus {
4857 Failed ,
4958}
5059
60+ // SQLx implementations for BatchStatus
61+ #[ cfg( feature = "postgres" ) ]
62+ impl Type < Postgres > for BatchStatus {
63+ fn type_info ( ) -> sqlx:: postgres:: PgTypeInfo {
64+ <String as Type < Postgres > >:: type_info ( )
65+ }
66+ }
67+
68+ #[ cfg( feature = "postgres" ) ]
69+ impl Encode < ' _ , Postgres > for BatchStatus {
70+ fn encode_by_ref (
71+ & self ,
72+ buf : & mut sqlx:: postgres:: PgArgumentBuffer ,
73+ ) -> std:: result:: Result < sqlx:: encode:: IsNull , Box < dyn std:: error:: Error + Send + Sync + ' static > >
74+ {
75+ let status_str = match self {
76+ BatchStatus :: Pending => "Pending" ,
77+ BatchStatus :: Processing => "Processing" ,
78+ BatchStatus :: Completed => "Completed" ,
79+ BatchStatus :: PartiallyFailed => "PartiallyFailed" ,
80+ BatchStatus :: Failed => "Failed" ,
81+ } ;
82+ <& str as Encode < ' _ , Postgres > >:: encode_by_ref ( & status_str, buf)
83+ }
84+ }
85+
86+ #[ cfg( feature = "postgres" ) ]
87+ impl Decode < ' _ , Postgres > for BatchStatus {
88+ fn decode (
89+ value : sqlx:: postgres:: PgValueRef < ' _ > ,
90+ ) -> std:: result:: Result < Self , sqlx:: error:: BoxDynError > {
91+ let status_str = <String as Decode < Postgres > >:: decode ( value) ?;
92+ // Handle both quoted (old format) and unquoted (new format) status values
93+ let cleaned_str = status_str. trim_matches ( '"' ) ;
94+ match cleaned_str {
95+ "Pending" => Ok ( BatchStatus :: Pending ) ,
96+ "Processing" => Ok ( BatchStatus :: Processing ) ,
97+ "Completed" => Ok ( BatchStatus :: Completed ) ,
98+ "PartiallyFailed" => Ok ( BatchStatus :: PartiallyFailed ) ,
99+ "Failed" => Ok ( BatchStatus :: Failed ) ,
100+ _ => Err ( format ! ( "Unknown batch status: {}" , status_str) . into ( ) ) ,
101+ }
102+ }
103+ }
104+
105+ #[ cfg( feature = "mysql" ) ]
106+ impl Type < MySql > for BatchStatus {
107+ fn type_info ( ) -> sqlx:: mysql:: MySqlTypeInfo {
108+ <String as Type < MySql > >:: type_info ( )
109+ }
110+ }
111+
112+ #[ cfg( feature = "mysql" ) ]
113+ impl Encode < ' _ , MySql > for BatchStatus {
114+ fn encode_by_ref (
115+ & self ,
116+ buf : & mut Vec < u8 > ,
117+ ) -> std:: result:: Result < sqlx:: encode:: IsNull , Box < dyn std:: error:: Error + Send + Sync + ' static > >
118+ {
119+ let status_str = match self {
120+ BatchStatus :: Pending => "Pending" ,
121+ BatchStatus :: Processing => "Processing" ,
122+ BatchStatus :: Completed => "Completed" ,
123+ BatchStatus :: PartiallyFailed => "PartiallyFailed" ,
124+ BatchStatus :: Failed => "Failed" ,
125+ } ;
126+ <& str as Encode < ' _ , MySql > >:: encode_by_ref ( & status_str, buf)
127+ }
128+ }
129+
130+ #[ cfg( feature = "mysql" ) ]
131+ impl Decode < ' _ , MySql > for BatchStatus {
132+ fn decode (
133+ value : sqlx:: mysql:: MySqlValueRef < ' _ > ,
134+ ) -> std:: result:: Result < Self , sqlx:: error:: BoxDynError > {
135+ let status_str = <String as Decode < MySql > >:: decode ( value) ?;
136+ // Handle both quoted (old format) and unquoted (new format) status values
137+ let cleaned_str = status_str. trim_matches ( '"' ) ;
138+ match cleaned_str {
139+ "Pending" => Ok ( BatchStatus :: Pending ) ,
140+ "Processing" => Ok ( BatchStatus :: Processing ) ,
141+ "Completed" => Ok ( BatchStatus :: Completed ) ,
142+ "PartiallyFailed" => Ok ( BatchStatus :: PartiallyFailed ) ,
143+ "Failed" => Ok ( BatchStatus :: Failed ) ,
144+ _ => Err ( format ! ( "Unknown batch status: {}" , status_str) . into ( ) ) ,
145+ }
146+ }
147+ }
148+
51149/// Summary of batch processing results.
52150#[ derive( Debug , Clone , Serialize , Deserialize ) ]
53151pub struct BatchResult {
0 commit comments