Skip to content

Commit

Permalink
Make open readonly with nomutex by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColaCheng committed Apr 9, 2024
1 parent 643e27c commit 9998c00
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 33 deletions.
5 changes: 2 additions & 3 deletions lib/exqlite/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule Exqlite.Connection do

@type connection_opt() ::
{:database, String.t()}
| {:mode, :readwrite | :readonly | :readonly_nomutex}
| {:mode, :readwrite | :readonly}
| {:journal_mode, journal_mode()}
| {:temp_store, temp_store()}
| {:synchronous, synchronous()}
Expand Down Expand Up @@ -92,8 +92,7 @@ defmodule Exqlite.Connection do
* `:database` - The path to the database. In memory is allowed. You can use
`:memory` or `":memory:"` to designate that.
* `:mode` - use `:readwrite` to open the database for reading and writing
, `:readonly` to open it in read-only mode or `:readonly_nomutex` to open
it in read-only with nomutex mode. `:readwrite` will also create
or `:readonly` to open it in read-only mode with no mutex. `:readwrite` will also create
the database if it doesn't already exist. Defaults to `:readwrite`.
* `:journal_mode` - Sets the journal mode for the sqlite connection. Can be
one of the following `:delete`, `:truncate`, `:persist`, `:memory`,
Expand Down
7 changes: 2 additions & 5 deletions lib/exqlite/sqlite3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Exqlite.Sqlite3 do
@type statement() :: reference()
@type reason() :: atom() | String.t()
@type row() :: list()
@type open_opt :: {:mode, :readwrite | :readonly | :readonly_nomutex}
@type open_opt :: {:mode, :readwrite | :readonly}

@doc """
Opens a new sqlite database at the Path provided.
Expand All @@ -29,7 +29,7 @@ defmodule Exqlite.Sqlite3 do
## Options
* `:mode` - use `:readwrite` to open the database for reading and writing
, `:readonly` to open it in read-only mode or `:readonly_nomutex` to open it in read-only with nomutex mode. `:readwrite` will also create
or `:readonly` to open it in read-only mode with no mutex. `:readwrite` will also create
the database if it doesn't already exist. Defaults to `:readwrite`.
"""
@spec open(String.t(), [open_opt()]) :: {:ok, db()} | {:error, reason()}
Expand All @@ -42,9 +42,6 @@ defmodule Exqlite.Sqlite3 do
do: Flags.put_file_open_flags([:sqlite_open_readwrite, :sqlite_open_create])

defp flags_from_mode(:readonly),
do: Flags.put_file_open_flags([:sqlite_open_readonly])

defp flags_from_mode(:readonly_nomutex),
do: Flags.put_file_open_flags([:sqlite_open_readonly, :sqlite_open_nomutex])

defp flags_from_mode(mode) do
Expand Down
25 changes: 0 additions & 25 deletions test/exqlite/sqlite3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,6 @@ defmodule Exqlite.Sqlite3Test do
Sqlite3.execute(ro_conn, insert_value_query)
end

test "opens a database in readonly_nomutex mode" do
# Create database with readwrite connection
{:ok, path} = Temp.path()
{:ok, rw_conn} = Sqlite3.open(path)

create_table_query = "create table test (id integer primary key, stuff text)"
:ok = Sqlite3.execute(rw_conn, create_table_query)

insert_value_query = "insert into test (stuff) values ('This is a test')"
:ok = Sqlite3.execute(rw_conn, insert_value_query)

# Read from database with a readonly_nomutex connection
{:ok, ro_conn} = Sqlite3.open(path, mode: :readonly_nomutex)

select_query = "select id, stuff from test order by id asc"
{:ok, statement} = Sqlite3.prepare(ro_conn, select_query)
{:row, columns} = Sqlite3.step(ro_conn, statement)

assert [1, "This is a test"] == columns

# Readonly nomutex connection cannot insert
assert {:error, "attempt to write a readonly database"} ==
Sqlite3.execute(ro_conn, insert_value_query)
end

test "opens a database with invalid mode" do
{:ok, path} = Temp.path()

Expand Down

0 comments on commit 9998c00

Please sign in to comment.