Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sqlx-mysql/src/connection/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub(crate) enum Waiting {
impl<S: Socket> MySqlStream<S> {
pub(crate) fn with_socket(options: &MySqlConnectOptions, socket: S) -> Self {
let mut capabilities = Capabilities::PROTOCOL_41
| Capabilities::IGNORE_SPACE
| Capabilities::DEPRECATE_EOF
| Capabilities::TRANSACTIONS
| Capabilities::SECURE_CONNECTION
Expand All @@ -52,6 +51,10 @@ impl<S: Socket> MySqlStream<S> {
capabilities |= Capabilities::FOUND_ROWS;
}

if options.ignore_space {
capabilities |= Capabilities::IGNORE_SPACE;
}

Self {
waiting: VecDeque::new(),
capabilities,
Expand Down
16 changes: 16 additions & 0 deletions sqlx-mysql/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct MySqlConnectOptions {
pub(crate) timezone: Option<String>,
pub(crate) set_names: bool,
pub(crate) found_rows: bool,
pub(crate) ignore_space: bool,
}

impl Default for MySqlConnectOptions {
Expand Down Expand Up @@ -126,6 +127,7 @@ impl MySqlConnectOptions {
timezone: Some(String::from("+00:00")),
set_names: true,
found_rows: true,
ignore_space: true,
}
}

Expand Down Expand Up @@ -447,6 +449,20 @@ impl MySqlConnectOptions {
self.found_rows = flag_val;
self
}

/// Sets the flag that enables or disables CLIENT_IGNORE_SPACE,
/// which makes the server add `IGNORE_SPACE` to the session's `sql_mode`.
///
/// With `IGNORE_SPACE`, the server permits spaces between a built-in function name
/// and the following `(` character, and treats built-in function names as reserved words.
/// Disabling it matches the default of most other MySQL clients, which some proxies
/// (such as ProxySQL) require before they will share backend connections between clients.
///
/// The default value is set to true.
pub fn ignore_space(mut self, flag_val: bool) -> Self {
self.ignore_space = flag_val;
self
}
}

impl MySqlConnectOptions {
Expand Down
43 changes: 43 additions & 0 deletions tests/mysql/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,46 @@ async fn test_client_found_rows() -> anyhow::Result<()> {

Ok(())
}

#[sqlx_macros::test]
async fn test_client_ignore_space() -> anyhow::Result<()> {
setup_if_needed();

let url = url::Url::parse(&env::var("DATABASE_URL")?)?;
let ignore_space_mode = "SELECT FIND_IN_SET('IGNORE_SPACE', @@SESSION.sql_mode) > 0";
let spaced_call = "SELECT COUNT (*) FROM (SELECT 1) AS t";

// CLIENT_IGNORE_SPACE unspecified = true by default.
let mut conn = MySqlConnectOptions::from_url(&url)?.connect().await?;

let enabled: i64 = sqlx::query_scalar(ignore_space_mode)
.fetch_one(&mut conn)
.await?;
assert_eq!(enabled, 1);

let count: i64 = sqlx::query_scalar(spaced_call).fetch_one(&mut conn).await?;
assert_eq!(count, 1);

conn.close().await?;

// Explicitly unset CLIENT_IGNORE_SPACE.
let mut conn = MySqlConnectOptions::from_url(&url)?
.ignore_space(false)
.connect()
.await?;

let enabled: i64 = sqlx::query_scalar(ignore_space_mode)
.fetch_one(&mut conn)
.await?;
assert_eq!(enabled, 0);

// Without IGNORE_SPACE, a space after a built-in function name is a syntax error.
let result = sqlx::query_scalar::<_, i64>(spaced_call)
.fetch_one(&mut conn)
.await;
assert!(result.is_err());

conn.close().await?;

Ok(())
}
Loading