Skip to content

Deprecate @sr_str macro by Base.@raw_str and suggest users to switch. #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2023
Merged
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
8 changes: 2 additions & 6 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ julia> DBInterface.execute(db, "SELECT * FROM MediaType WHERE Name REGEXP '-d'")

This can be avoided in two ways.
You can either escape each backslash yourself
or you can use the sr"..." string literal that SQLite.jl exports.
or you can use the raw"..." string literal.
The previous query can then successfully be run like so:

```julia
Expand All @@ -128,18 +128,14 @@ julia> DBInterface.execute(db, "SELECT * FROM MediaType WHERE Name REGEXP '-\\d'
|-----|---------------|-------------------------------|
| 1 | 3 | "Protected MPEG-4 video file" |

julia> # using sr"..."

julia> DBInterface.execute(db, sr"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
julia> DBInterface.execute(db, raw"SELECT * FROM MediaType WHERE Name REGEXP '-\d'") |> DataFrame
1x2 ResultSet
| Row | "MediaTypeId" | "Name" |
|-----|---------------|-------------------------------|
| 1 | 3 | "Protected MPEG-4 video file" |
```

The `sr"..."` currently escapes all special characters in a string
but it may be changed in the future to escape only characters which are part of a regex.


### Custom Scalar Functions

Expand Down
2 changes: 2 additions & 0 deletions src/UDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ regexp(r::AbstractString, s::AbstractString) = occursin(Regex(r), s)

This string literal is used to escape all special characters in the string,
useful for using regex in a query.

This literal is deprecated and users should switch to `Base.@raw_str` instead.
"""
macro sr_str(s)
s
Expand Down
26 changes: 9 additions & 17 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ end
r =
DBInterface.execute(
db,
SQLite.@sr_str(
@raw_str(
"SELECT LastName FROM Employee WHERE BirthDate REGEXP '^\\d{4}-08'"
)
) |> columntable
Expand Down Expand Up @@ -907,7 +907,9 @@ function Base.iterate(::UnknownSchemaTable, st = 1)
end

@testset "SQLite Open Flags" begin
@test_throws SQLiteException("unable to open database file") SQLite.DB("file:test.db?mode=ro")
@test_throws SQLiteException("unable to open database file") SQLite.DB(
"file:test.db?mode=ro",
)

db = SQLite.DB("file:test.db?mode=rwc")
@test db isa SQLite.DB
Expand Down Expand Up @@ -953,36 +955,26 @@ end
UnknownSchemaTable(),
db,
"tmp",
on_conflict = "ROLLBACK"
on_conflict = "ROLLBACK",
)
tbl = DBInterface.execute(db, "select * from tmp") |> columntable
@test tbl == (a = [], b = [], c = [])
@test_throws SQLite.SQLiteException SQLite.load!(
UnknownSchemaTable(),
db,
"tmp",
on_conflict = "ABORT"
on_conflict = "ABORT",
)
@test_throws SQLite.SQLiteException SQLite.load!(
UnknownSchemaTable(),
db,
"tmp",
on_conflict = "FAIL"
)
SQLite.load!(
UnknownSchemaTable(),
db,
"tmp",
on_conflict = "IGNORE"
on_conflict = "FAIL",
)
SQLite.load!(UnknownSchemaTable(), db, "tmp"; on_conflict = "IGNORE")
tbl = DBInterface.execute(db, "select * from tmp") |> columntable
@test tbl == (a = [1], b = [3], c = [4])
SQLite.load!(
UnknownSchemaTable(),
db,
"tmp",
on_conflict = "REPLACE"
)
SQLite.load!(UnknownSchemaTable(), db, "tmp"; on_conflict = "REPLACE")
tbl = DBInterface.execute(db, "select * from tmp") |> columntable
@test tbl == (a = [1], b = [5], c = [6])

Expand Down