@@ -19,7 +19,8 @@ defmodule Exqlite.Sqlite3 do
19
19
@ type statement ( ) :: reference ( )
20
20
@ type reason ( ) :: atom ( ) | String . t ( )
21
21
@ 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 ( ) ] }
23
24
24
25
@ doc """
25
26
Opens a new sqlite database at the Path provided.
@@ -29,26 +30,53 @@ defmodule Exqlite.Sqlite3 do
29
30
## Options
30
31
31
32
* `:mode` - use `:readwrite` to open the database for reading and writing
32
- or `:readonly` to open it in read-only mode with no mutex. `: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
33
35
the database if it doesn't already exist. Defaults to `:readwrite`.
36
+ Note: [:readwrite, :nomutex] is not recommended.
34
37
"""
35
38
@ spec open ( String . t ( ) , [ open_opt ( ) ] ) :: { :ok , db ( ) } | { :error , reason ( ) }
36
39
def open ( path , opts \\ [ ] ) do
37
40
mode = Keyword . get ( opts , :mode , :readwrite )
38
41
Sqlite3NIF . open ( String . to_charlist ( path ) , flags_from_mode ( mode ) )
39
42
end
40
43
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
+
41
49
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 ] , [ ] )
43
51
44
52
defp flags_from_mode ( :readonly ) ,
45
- do: Flags . put_file_open_flags ( [ :sqlite_open_readonly , :sqlite_open_nomutex ] )
53
+ do: do_flags_from_mode ( [ :readonly ] , [ ] )
54
+
55
+ defp flags_from_mode ( [ _ | _ ] = modes ) ,
56
+ do: do_flags_from_mode ( modes , [ ] )
46
57
47
58
defp flags_from_mode ( mode ) do
59
+ raise ArgumentError ,
60
+ "expected mode to be `:readwrite`, `:readonly` or list of modes, but received #{ inspect ( mode ) } "
61
+ end
62
+
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
48
73
raise ArgumentError ,
49
- "expected mode to be `:readwrite` or `:readonly `, but received #{ inspect ( mode ) } "
74
+ "expected mode to be `:readwrite`, `:readonly` or `:nomutex `, but received #{ inspect ( mode ) } "
50
75
end
51
76
77
+ defp do_flags_from_mode ( [ ] , acc ) ,
78
+ do: Flags . put_file_open_flags ( acc )
79
+
52
80
@ doc """
53
81
Closes the database and releases any underlying resources.
54
82
"""
0 commit comments