@@ -19,7 +19,8 @@ defmodule Exqlite.Sqlite3 do
1919 @ type statement ( ) :: reference ( )
2020 @ type reason ( ) :: atom ( ) | String . t ( )
2121 @ type row ( ) :: list ( )
22- @ type open_opt :: { :mode , :readwrite | :readonly }
22+ @ type open_mode :: :readwrite | :readonly | :nomutex
23+ @ type open_opt :: { :mode , :readwrite | :readonly | [ open_mode ( ) ] }
2324
2425 @ doc """
2526 Opens a new sqlite database at the Path provided.
@@ -29,26 +30,53 @@ defmodule Exqlite.Sqlite3 do
2930 ## Options
3031
3132 * `:mode` - use `:readwrite` to open the database for reading and writing
32- or `:readonly` to open it in read-only mode. `:readwrite` will also create
33+ , `:readonly` to open it in read-only mode or `[:readonly | :readwrite, :nomutex]`
34+ to open it with no mutex mode. `:readwrite` will also create
3335 the database if it doesn't already exist. Defaults to `:readwrite`.
36+ Note: [:readwrite, :nomutex] is not recommended.
3437 """
3538 @ spec open ( String . t ( ) , [ open_opt ( ) ] ) :: { :ok , db ( ) } | { :error , reason ( ) }
3639 def open ( path , opts \\ [ ] ) do
3740 mode = Keyword . get ( opts , :mode , :readwrite )
3841 Sqlite3NIF . open ( String . to_charlist ( path ) , flags_from_mode ( mode ) )
3942 end
4043
44+ defp flags_from_mode ( :nomutex ) do
45+ raise ArgumentError ,
46+ "expected mode to be `:readwrite` or `:readonly`, can't use a single :nomutex mode"
47+ end
48+
4149 defp flags_from_mode ( :readwrite ) ,
42- do: Flags . put_file_open_flags ( [ :sqlite_open_readwrite , :sqlite_open_create ] )
50+ do: do_flags_from_mode ( [ :readwrite ] , [ ] )
4351
4452 defp flags_from_mode ( :readonly ) ,
45- do: Flags . put_file_open_flags ( [ :sqlite_open_readonly ] )
53+ do: do_flags_from_mode ( [ :readonly ] , [ ] )
54+
55+ defp flags_from_mode ( [ _ | _ ] = modes ) ,
56+ do: do_flags_from_mode ( modes , [ ] )
4657
4758 defp flags_from_mode ( mode ) do
4859 raise ArgumentError ,
49- "expected mode to be `:readwrite` or `:readonly`, but received #{ inspect ( mode ) } "
60+ "expected mode to be `:readwrite`, `:readonly` or list of modes , but received #{ inspect ( mode ) } "
5061 end
5162
63+ defp do_flags_from_mode ( [ :readwrite | tail ] , acc ) ,
64+ do: do_flags_from_mode ( tail , [ :sqlite_open_readwrite , :sqlite_open_create | acc ] )
65+
66+ defp do_flags_from_mode ( [ :readonly | tail ] , acc ) ,
67+ do: do_flags_from_mode ( tail , [ :sqlite_open_readonly | acc ] )
68+
69+ defp do_flags_from_mode ( [ :nomutex | tail ] , acc ) ,
70+ do: do_flags_from_mode ( tail , [ :sqlite_open_nomutex | acc ] )
71+
72+ defp do_flags_from_mode ( [ mode | _tail ] , _acc ) do
73+ raise ArgumentError ,
74+ "expected mode to be `:readwrite`, `:readonly` or `:nomutex`, but received #{ inspect ( mode ) } "
75+ end
76+
77+ defp do_flags_from_mode ( [ ] , acc ) ,
78+ do: Flags . put_file_open_flags ( acc )
79+
5280 @ doc """
5381 Closes the database and releases any underlying resources.
5482 """
0 commit comments