-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ExceptionStatement raises custom SQL errors
- Loading branch information
Showing
13 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Custom SQL exceptions and errors | ||
use inherent::inherent; | ||
|
||
use crate::backend::SchemaBuilder; | ||
|
||
/// SQL Exceptions | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct ExceptionStatement { | ||
pub(crate) message: String, | ||
} | ||
|
||
impl ExceptionStatement { | ||
pub fn new(message: String) -> Self { | ||
Self { message } | ||
} | ||
} | ||
|
||
pub trait ExceptionStatementBuilder { | ||
/// Build corresponding SQL statement for certain database backend and return SQL string | ||
fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String; | ||
|
||
/// Build corresponding SQL statement for certain database backend and return SQL string | ||
fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String; | ||
|
||
/// Build corresponding SQL statement for certain database backend and return SQL string | ||
fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String { | ||
self.build(schema_builder) | ||
} | ||
} | ||
|
||
#[inherent] | ||
impl ExceptionStatementBuilder for ExceptionStatement { | ||
pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String { | ||
let mut sql = String::with_capacity(256); | ||
schema_builder.prepare_exception_statement(self, &mut sql); | ||
sql | ||
} | ||
|
||
pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String { | ||
let mut sql = String::with_capacity(256); | ||
schema_builder.prepare_exception_statement(self, &mut sql); | ||
sql | ||
} | ||
|
||
pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn signal_sqlstate() { | ||
let message = "Some error occurred"; | ||
assert_eq!( | ||
ExceptionStatement::new(message.to_string()).to_string(MysqlQueryBuilder), | ||
format!("SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '{message}'") | ||
); | ||
} | ||
|
||
#[test] | ||
fn escapes_message() { | ||
let unescaped_message = "Does this 'break'?"; | ||
assert_eq!( | ||
ExceptionStatement::new(unescaped_message.to_string()).to_string(MysqlQueryBuilder), | ||
format!("SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Does this \\'break\\'?'") | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use sea_query::{extension::mysql::*, tests_cfg::*, *}; | ||
|
||
mod exception; | ||
mod foreign_key; | ||
mod index; | ||
mod query; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn raise_exception() { | ||
let message = "Some error occurred"; | ||
assert_eq!( | ||
ExceptionStatement::new(message.to_string()).to_string(PostgresQueryBuilder), | ||
format!("RAISE EXCEPTION '{message}'") | ||
); | ||
} | ||
|
||
#[test] | ||
fn escapes_message() { | ||
let unescaped_message = "Does this 'break'?"; | ||
assert_eq!( | ||
ExceptionStatement::new(unescaped_message.to_string()).to_string(PostgresQueryBuilder), | ||
format!("RAISE EXCEPTION E'Does this \\'break\\'?'") | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use sea_query::{tests_cfg::*, *}; | ||
|
||
mod exception; | ||
mod foreign_key; | ||
mod index; | ||
mod query; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn select_raise_abort() { | ||
let message = "Some error occurred here"; | ||
assert_eq!( | ||
ExceptionStatement::new(message.to_string()).to_string(SqliteQueryBuilder), | ||
format!("SELECT RAISE(ABORT, '{}')", message) | ||
); | ||
} | ||
|
||
#[test] | ||
fn escapes_message() { | ||
let unescaped_message = "Does this 'break'?"; | ||
let escaped_message = "Does this ''break''?"; | ||
assert_eq!( | ||
ExceptionStatement::new(unescaped_message.to_string()).to_string(SqliteQueryBuilder), | ||
format!("SELECT RAISE(ABORT, '{}')", escaped_message) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use sea_query::{tests_cfg::*, *}; | ||
|
||
mod exception; | ||
mod foreign_key; | ||
mod index; | ||
mod query; | ||
|