Skip to content

feat: allow URI filenames #571

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 3 commits into from
Oct 30, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## next / unreleased

### Added

- URI filenames are now allowed. This allows the injection of some behavior via recognized query parameters. See https://www.sqlite.org/uri.html for more information. #571 @flavorjones


### Improved

- SQL Syntax errors during `Database#prepare` will raise a verbose exception with a multiline message indicating with a "^" exactly where in the statement the error occurred. [#554] @fractaledmind @flavorjones
Expand Down
3 changes: 2 additions & 1 deletion ext/sqlite3/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def configure_packaged_libraries
"-fPIC", # needed for linking the static library into a shared library
"-O2", # see https://github.com/sparklemotion/sqlite3-ruby/issues/335 for some benchmarks
"-fvisibility=hidden", # see https://github.com/rake-compiler/rake-compiler-dock/issues/87
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1"
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
"-DSQLITE_USE_URI=1"
]
env["CFLAGS"] = [user_cflags, env["CFLAGS"], more_cflags].flatten.join(" ")
recipe.configure_options += env.select { |k, v| ENV_ALLOWLIST.include?(k) }
Expand Down
4 changes: 4 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ def i_am_running_in_valgrind
# https://stackoverflow.com/questions/365458/how-can-i-detect-if-a-program-is-running-from-within-valgrind/62364698#62364698
ENV["LD_PRELOAD"] =~ /valgrind|vgpreload/
end

def windows?
::RUBY_PLATFORM =~ /mingw|mswin/
end
end
end
47 changes: 47 additions & 0 deletions test/test_database_uri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "helper"
require "tempfile"
require "pathname"

module SQLite3
class TestDatabaseURI < SQLite3::TestCase
def test_open_absolute_file_uri
skip("windows uri paths are hard") if windows?
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?

Tempfile.open "test.db" do |file|
db = SQLite3::Database.new("file:#{file.path}")
assert db
db.close
end
end

def test_open_relative_file_uri
skip("windows uri paths are hard") if windows?
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?

Dir.mktmpdir do |dir|
Dir.chdir dir do
db = SQLite3::Database.new("file:test.db")
assert db
assert_path_exists "test.db"
db.close
end
end
end

def test_open_file_uri_readonly
skip("windows uri paths are hard") if windows?
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?

Tempfile.open "test.db" do |file|
db = SQLite3::Database.new("file:#{file.path}?mode=ro")

assert_raise(SQLite3::ReadOnlyException) do
db.execute("CREATE TABLE foos (id integer)")
end

db.close
end
end
end
end