Skip to content

Commit ec6c531

Browse files
committed
fix(macros): added SQLX_SQLITE_REGISTER_REGEXP environment variable
- fix issue #3070 - allows sqlx-cli prepare to work with sqlite regexp
1 parent 3da582a commit ec6c531

5 files changed

Lines changed: 29 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ mac_address = ["sqlx-core/mac_address", "sqlx-macros?/mac_address", "sqlx-postgr
160160
rust_decimal = ["sqlx-core/rust_decimal", "sqlx-macros?/rust_decimal", "sqlx-mysql?/rust_decimal", "sqlx-postgres?/rust_decimal"]
161161
time = ["sqlx-core/time", "sqlx-macros?/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"]
162162
uuid = ["sqlx-core/uuid", "sqlx-macros?/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"]
163-
regexp = ["sqlx-sqlite?/regexp"]
163+
regexp = ["sqlx-sqlite?/regexp", "sqlx-macros?/regexp"]
164164
bstr = ["sqlx-core/bstr"]
165165

166166
[workspace.dependencies]

sqlx-macros-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ rust_decimal = ["sqlx-core/rust_decimal", "sqlx-mysql?/rust_decimal", "sqlx-post
5454
time = ["sqlx-core/time", "sqlx-mysql?/time", "sqlx-postgres?/time", "sqlx-sqlite?/time"]
5555
uuid = ["sqlx-core/uuid", "sqlx-mysql?/uuid", "sqlx-postgres?/uuid", "sqlx-sqlite?/uuid"]
5656

57+
regexp = ["sqlx-sqlite?/regexp"]
58+
5759
[dependencies]
5860
sqlx-core = { workspace = true, features = ["offline"] }
5961
sqlx-mysql = { workspace = true, features = ["offline", "migrate"], optional = true, default-features = false }

sqlx-macros/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ time = ["sqlx-macros-core/time"]
5353
uuid = ["sqlx-macros-core/uuid"]
5454
json = ["sqlx-macros-core/json"]
5555

56+
regexp = ["sqlx-macros-core/regexp"]
57+
5658
[dependencies]
5759
sqlx-core = { workspace = true, features = ["any"] }
5860
sqlx-macros-core = { workspace = true }

sqlx-sqlite/src/options/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ impl SqliteConnectOptions {
193193
// Soft limit on the number of rows that `ANALYZE` touches per index.
194194
pragmas.insert("analysis_limit".into(), None);
195195

196+
#[cfg(feature = "regexp")]
197+
let register_regexp_function = std::env::var("SQLX_SQLITE_REGISTER_REGEXP")
198+
.ok()
199+
.is_some_and(|val| {
200+
val.eq_ignore_ascii_case("true") || val == "1"
201+
});
202+
196203
Self {
197204
filename: Cow::Borrowed(Path::new(":memory:")),
198205
in_memory: false,
@@ -214,7 +221,7 @@ impl SqliteConnectOptions {
214221
row_channel_size: 50,
215222
optimize_on_close: OptimizeOnClose::Disabled,
216223
#[cfg(feature = "regexp")]
217-
register_regexp_function: false,
224+
register_regexp_function,
218225
}
219226
}
220227

tests/sqlite/macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,20 @@ async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
358358
Ok(())
359359
}
360360

361+
/// Requires SQLX_SQLITE_REGISTER_REGEXP environment variable to compile and succeed.
362+
#[cfg(feature = "regexp")]
363+
#[sqlx_macros::test]
364+
async fn test_regexp_function_is_registered_via_env() -> anyhow::Result<()> {
365+
366+
let mut conn = new::<Sqlite>().await?;
367+
368+
let record = sqlx::query!(r#"select id as "id?: MyInt" from tweet where text regexp 'is pretty cool'"#)
369+
.fetch_one(&mut conn)
370+
.await?;
371+
372+
assert_eq!(record.id, Some(MyInt(1)));
373+
374+
Ok(())
375+
}
376+
361377
// we don't emit bind parameter typechecks for SQLite so testing the overrides is redundant

0 commit comments

Comments
 (0)