Skip to content
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
30 changes: 24 additions & 6 deletions lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Exqlite.Connection do

defstruct [
:db,
:directory,
:path,
:transaction_status,
:status,
Expand All @@ -39,6 +40,7 @@ defmodule Exqlite.Connection do

@type t() :: %__MODULE__{
db: Sqlite3.db(),
directory: String.t() | nil,
path: String.t(),
transaction_status: :idle | :transaction,
status: :idle | :busy
Expand Down Expand Up @@ -451,9 +453,10 @@ defmodule Exqlite.Connection do
set_pragma(db, "busy_timeout", Pragma.busy_timeout(options))
end

defp do_connect(path, options) do
with :ok <- mkdir_p(path),
{:ok, db} <- Sqlite3.open(path, options),
defp do_connect(database, options) do
with {:ok, directory} <- resolve_directory(database),
:ok <- mkdir_p(directory),
{:ok, db} <- Sqlite3.open(database, options),
:ok <- set_key(db, options),
:ok <- set_journal_mode(db, options),
:ok <- set_temp_store(db, options),
Expand All @@ -472,7 +475,8 @@ defmodule Exqlite.Connection do
:ok <- set_hard_heap_limit(db, options) do
state = %__MODULE__{
db: db,
path: path,
directory: directory,
path: database,
transaction_status: :idle,
status: :idle,
chunk_size: Keyword.get(options, :chunk_size)
Expand Down Expand Up @@ -619,10 +623,24 @@ defmodule Exqlite.Connection do
end
end

defp resolve_directory(":memory:"), do: {:ok, nil}

defp resolve_directory("file:" <> _ = uri) do
case URI.parse(uri) do
%{path: path} when is_binary(path) ->
{:ok, Path.dirname(path)}

_ ->
{:error, "No path in #{inspect(uri)}"}
end
end

defp resolve_directory(path), do: {:ok, Path.dirname(path)}

# SQLITE_OPEN_CREATE will create the DB file if not existing, but
# will not create intermediary directories if they are missing.
# So let's preemptively create the intermediate directories here
# before trying to open the DB file.
defp mkdir_p(":memory:"), do: :ok
defp mkdir_p(path), do: File.mkdir_p(Path.dirname(path))
defp mkdir_p(nil), do: :ok
defp mkdir_p(directory), do: File.mkdir_p(directory)
end
46 changes: 46 additions & 0 deletions test/exqlite/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,52 @@ defmodule Exqlite.ConnectionTest do
File.rm(path)
end

test "connects to a file from URL" do
path = Temp.path!()

{:ok, state} = Connection.connect(database: "file:#{path}?mode=rwc")

assert state.directory == Path.dirname(path)
assert state.db
end

test "fails to write a file from URL with mode=ro" do
path = Temp.path!()

{:ok, db} = Sqlite3.open(path)

:ok =
Sqlite3.execute(db, "create table test (id ingeger primary key, stuff text)")

:ok =
Sqlite3.execute(db, "insert into test (id, stuff) values (999, 'Some stuff')")

:ok = Sqlite3.close(db)

{:ok, conn} = Connection.connect(database: "file:#{path}?mode=ro")

assert conn.directory == Path.dirname(path)
assert conn.db

assert match?(
{:ok, _, %{rows: [[1]]}, _},
%Query{statement: "select count(*) from test"}
|> Connection.handle_execute([], [], conn)
)

{:error, %{message: message}, _} =
%Query{
statement: "insert into test (id, stuff) values (888, 'some more stuff')"
}
|> Connection.handle_execute([], [], conn)

# In most of the test matrix the message is "attempt to write a readonly database",
# but in Elixir 1.13, OTP 23, OS windows-2019 it is "not an error".
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's bizarre.

assert message in ["attempt to write a readonly database", "not an error"]

File.rm(path)
end

test "setting journal_size_limit" do
path = Temp.path!()
size_limit = 20 * 1024 * 1024
Expand Down