Skip to content

Commit

Permalink
feat: allow URI filenames (#571)
Browse files Browse the repository at this point in the history
* feat: allow URI filenames

See https://www.sqlite.org/uri.html

* test: skil URI tests on windows

there are a lot of caveats about them in
https://www.sqlite.org/uri.html and I don't have the energy to deal
with it today.

* test: skip URI tests for sqlcipher

since the compile-time option may not be on
  • Loading branch information
flavorjones authored Oct 30, 2024
1 parent 9899f06 commit c15fc69
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
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

0 comments on commit c15fc69

Please sign in to comment.