Skip to main content

postgrest_parser/error/
sql.rs

1use thiserror::Error;
2
3#[derive(Error, Debug, PartialEq, Clone)]
4pub enum SqlError {
5    #[error("table not found: {0}")]
6    TableNotFound(String),
7
8    #[error("relationship not found: {0}")]
9    RelationshipNotFound(String),
10
11    #[error("relationship is ambiguous, use hint: {0}")]
12    RelationshipAmbiguous(String),
13
14    #[error("invalid table name: {0}")]
15    InvalidTableName(String),
16
17    #[error("empty table name")]
18    EmptyTableName,
19
20    #[error("no select items specified")]
21    NoSelectItems,
22
23    #[error("invalid parameter: {0}")]
24    InvalidParameter(String),
25
26    #[error("failed to build WHERE clause")]
27    FailedToBuildWhereClause,
28
29    #[error("failed to build SELECT clause")]
30    FailedToBuildSelectClause,
31
32    #[error("failed to build ORDER BY clause")]
33    FailedToBuildOrderByClause,
34
35    #[error("failed to build LIMIT/OFFSET clause")]
36    FailedToBuildLimitOffset,
37
38    #[error("failed to build LATERAL JOIN")]
39    FailedToBuildLateralJoin,
40
41    #[error("invalid JSON path for SQL generation")]
42    InvalidJsonPathForSql,
43
44    #[error("invalid type cast for SQL generation")]
45    InvalidTypeCastForSql,
46
47    // Mutation safety errors
48    #[error("unsafe UPDATE: no WHERE clause specified. Updates without filters are not allowed for safety reasons")]
49    UnsafeUpdate,
50
51    #[error("unsafe DELETE: no WHERE clause specified. Deletes without filters are not allowed for safety reasons")]
52    UnsafeDelete,
53
54    #[error("LIMIT without ORDER BY: non-deterministic result set. Use ORDER BY when using LIMIT")]
55    LimitWithoutOrder,
56
57    #[error("no values provided for INSERT")]
58    NoInsertValues,
59
60    #[error("no SET clause for UPDATE")]
61    NoUpdateSet,
62
63    // Relation resolution errors
64    #[error("no table context for relation resolution")]
65    NoTableContext,
66
67    #[error("relation not found: no foreign key between '{from_table}' and '{to_table}'")]
68    RelationNotFound {
69        from_table: String,
70        to_table: String,
71    },
72
73    #[error("many-to-many relationships not yet supported (junction table: '{junction_table}')")]
74    ManyToManyNotYetSupported { junction_table: String },
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_sql_error_table_not_found() {
83        let err = SqlError::TableNotFound("users".to_string());
84        assert!(err.to_string().contains("not found"));
85    }
86
87    #[test]
88    fn test_sql_error_eq() {
89        let err1 = SqlError::TableNotFound("test".to_string());
90        let err2 = SqlError::TableNotFound("test".to_string());
91        assert_eq!(err1, err2);
92    }
93
94    #[test]
95    fn test_sql_error_clone() {
96        let err = SqlError::TableNotFound("users".to_string());
97        let cloned = err.clone();
98        assert_eq!(err, cloned);
99    }
100
101    #[test]
102    fn test_sql_error_relationship_ambiguous() {
103        let err = SqlError::RelationshipAmbiguous("client".to_string());
104        assert!(err.to_string().contains("ambiguous"));
105    }
106}