@@ -20,7 +20,7 @@ defmodule Exqlite.Sqlite3 do
2020 @ type reason ( ) :: atom ( ) | String . t ( )
2121 @ type row ( ) :: list ( )
2222 @ type open_mode :: :readwrite | :readonly | :nomutex | :create
23- @ type open_opt :: { :mode , :readwrite | :readonly | [ open_mode ( ) ] }
23+ @ type open_opt :: { :mode , :readwrite | :readonly | :create | [ open_mode ( ) ] }
2424
2525 @ doc """
2626 Opens a new sqlite database at the Path provided.
@@ -31,25 +31,26 @@ defmodule Exqlite.Sqlite3 do
3131
3232 * `:mode` - controls the flags for sqlite3_open_v2 (see
3333 https://www.sqlite.org/c3ref/c_open_autoproxy.html). Defaults to
34- `:readwrite` (opens for reading and writing and creates the file if it
35- does not exist — this atom form is preserved for backward compatibility ).
34+ `[ :readwrite, :create] ` (opens for reading and writing and creates the
35+ file if it does not exist ).
3636
37- Finer-grained control (to align with sqlite3_open_v2 flags, see
38- https://github.com/elixir-sqlite/exqlite/issues/347):
37+ Single modes are permitted:
38+ - `:readwrite` - read/write to the database. Does not create the database
39+ if it is not present. Use in combination with `:create` to create the
40+ database if it does not exist.
41+ - `:readonly` - read-only (file must exist).
42+ - `:create` - creates the database if it does not exist.
3943
40- - `:readwrite` (atom) — read/write + create if needed (compat default).
41- - `:readonly` — read-only (file must exist).
42- - Lists (recommended for new code):
43- - `[:readwrite]` — read/write only; will *not* create the file.
44- - `[:readwrite, :create]` — read/write + create if needed.
45- - `[:readonly, :nomutex]`, `[:readwrite, :nomutex]`, `[:create]` (in lists), etc.
46- - `:create` is now a valid list element to request the CREATE flag.
44+ Combinations are permitted:
45+ - `[:readwrite, :create]` - read/write + create if needed. This is the
46+ default if not specified.
47+ - `[:readonly, :nomutex]`
4748
4849 Note: `[:readwrite, :nomutex]` is not recommended.
4950 """
5051 @ spec open ( String . t ( ) , [ open_opt ( ) ] ) :: { :ok , db ( ) } | { :error , reason ( ) }
5152 def open ( path , opts \\ [ ] ) do
52- mode = Keyword . get ( opts , :mode , :readwrite )
53+ mode = opts [ :mode ] || [ :readwrite , :create ]
5354 Sqlite3NIF . open ( path , flags_from_mode ( mode ) )
5455 end
5556
@@ -58,13 +59,8 @@ defmodule Exqlite.Sqlite3 do
5859 "expected mode to be `:readwrite` or `:readonly`, can't use a single :nomutex mode"
5960 end
6061
61- # Atom `:readwrite` keeps the historical "also create the file" behavior
62- # for backward compatibility (most existing code and the default rely on it).
63- defp flags_from_mode ( :readwrite ) ,
64- do: do_flags_from_mode ( [ :readwrite , :create ] , [ ] )
65-
66- defp flags_from_mode ( :readonly ) ,
67- do: do_flags_from_mode ( [ :readonly ] , [ ] )
62+ defp flags_from_mode ( mode ) when mode in [ :readwrite , :readonly , :create ] ,
63+ do: do_flags_from_mode ( List . wrap ( mode ) , [ ] )
6864
6965 defp flags_from_mode ( [ _ | _ ] = modes ) ,
7066 do: do_flags_from_mode ( modes , [ ] )
0 commit comments