Skip to content

Commit c15fc69

Browse files
authored
feat: allow URI filenames (#571)
* 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
1 parent 9899f06 commit c15fc69

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## next / unreleased
44

5+
### Added
6+
7+
- 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
8+
9+
510
### Improved
611

712
- 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

ext/sqlite3/extconf.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def configure_packaged_libraries
6161
"-fPIC", # needed for linking the static library into a shared library
6262
"-O2", # see https://github.com/sparklemotion/sqlite3-ruby/issues/335 for some benchmarks
6363
"-fvisibility=hidden", # see https://github.com/rake-compiler/rake-compiler-dock/issues/87
64-
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1"
64+
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
65+
"-DSQLITE_USE_URI=1"
6566
]
6667
env["CFLAGS"] = [user_cflags, env["CFLAGS"], more_cflags].flatten.join(" ")
6768
recipe.configure_options += env.select { |k, v| ENV_ALLOWLIST.include?(k) }

test/helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ def i_am_running_in_valgrind
2121
# https://stackoverflow.com/questions/365458/how-can-i-detect-if-a-program-is-running-from-within-valgrind/62364698#62364698
2222
ENV["LD_PRELOAD"] =~ /valgrind|vgpreload/
2323
end
24+
25+
def windows?
26+
::RUBY_PLATFORM =~ /mingw|mswin/
27+
end
2428
end
2529
end

test/test_database_uri.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "helper"
2+
require "tempfile"
3+
require "pathname"
4+
5+
module SQLite3
6+
class TestDatabaseURI < SQLite3::TestCase
7+
def test_open_absolute_file_uri
8+
skip("windows uri paths are hard") if windows?
9+
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?
10+
11+
Tempfile.open "test.db" do |file|
12+
db = SQLite3::Database.new("file:#{file.path}")
13+
assert db
14+
db.close
15+
end
16+
end
17+
18+
def test_open_relative_file_uri
19+
skip("windows uri paths are hard") if windows?
20+
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?
21+
22+
Dir.mktmpdir do |dir|
23+
Dir.chdir dir do
24+
db = SQLite3::Database.new("file:test.db")
25+
assert db
26+
assert_path_exists "test.db"
27+
db.close
28+
end
29+
end
30+
end
31+
32+
def test_open_file_uri_readonly
33+
skip("windows uri paths are hard") if windows?
34+
skip("sqlcipher may not allow URIs") if SQLite3.sqlcipher?
35+
36+
Tempfile.open "test.db" do |file|
37+
db = SQLite3::Database.new("file:#{file.path}?mode=ro")
38+
39+
assert_raise(SQLite3::ReadOnlyException) do
40+
db.execute("CREATE TABLE foos (id integer)")
41+
end
42+
43+
db.close
44+
end
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)