Skip to content

Commit 04d8401

Browse files
committed
drop db_connection
1 parent 5b9891b commit 04d8401

27 files changed

+1153
-2892
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unlreleased
44

5+
- removed: `db_connection`. It's just a NIF now.
6+
57
## v0.17.0
68

79
- changed: Updaetd sqlite3 to `3.44.0`.

README.md

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,25 @@ Package: https://hex.pm/packages/exqlite
1515

1616
## Caveats
1717

18-
* Prepared statements are not cached.
1918
* Prepared statements are not immutable. You must be careful when manipulating
2019
statements and binding values to statements. Do not try to manipulate the
2120
statements concurrently. Keep it isolated to one process.
22-
* Simultaneous writing is not supported by SQLite3 and will not be supported
23-
here.
24-
* All native calls are run through the Dirty NIF scheduler.
25-
* Datetimes are stored without offsets. This is due to how SQLite3 handles date
26-
and times. If you would like to store a timezone, you will need to create a
27-
second column somewhere storing the timezone name and shifting it when you
28-
get it from the database. This is more reliable than storing the offset as
29-
`+03:00` as it does not respect daylight savings time.
21+
* All native calls are run through the Dirty IO NIF scheduler.
3022
* When storing `BLOB` values, you have to use `{:blob, the_binary}`, otherwise
31-
it will be interpreted as a string.
23+
it will be interpreted as a string.
3224

3325
## Installation
3426

3527
```elixir
3628
defp deps do
3729
[
30+
# TODO
3831
{:exqlite, "~> 0.17"}
3932
]
4033
end
4134
```
4235

43-
4436
## Configuration
45-
46-
### Runtime Configuration
47-
48-
```elixir
49-
config :exqlite, default_chunk_size: 100
50-
```
51-
52-
* `default_chunk_size` - The chunk size that is used when multi-stepping when
53-
not specifying the chunk size explicitly.
5437

5538
### Compile-time Configuration
5639

@@ -115,38 +98,30 @@ export EXQLITE_SYSTEM_CFLAGS=-I/usr/local/include/sqlcipher
11598
export EXQLITE_SYSTEM_LDFLAGS=-L/usr/local/lib -lsqlcipher
11699
```
117100

118-
Once you have `exqlite` configured, you can use the `:key` option in the database config to enable encryption:
101+
Once you have `exqlite` build configured, you can use the `key` pragma to enable encryption:
119102

120103
```elixir
121-
config :exqlite, key: "super-secret'
104+
{:ok, conn} = Exqlite.open("sqlcipher.db")
105+
:ok = Exqlite.execute(conn, "pragma key='super-secret'")
122106
```
123107

124108
## Usage
125109

126-
The `Exqlite.Sqlite3` module usage is fairly straight forward.
110+
The `Exqlite` module usage is fairly straight forward.
127111

128112
```elixir
129113
# We'll just keep it in memory right now
130-
{:ok, conn} = Exqlite.Sqlite3.open(":memory:")
114+
{:ok, conn} = Exqlite.open(":memory:")
131115

132116
# Create the table
133-
:ok = Exqlite.Sqlite3.execute(conn, "create table test (id integer primary key, stuff text)")
117+
:ok = Exqlite.execute(conn, "create table test (id integer primary key, stuff text)")
134118

135119
# Prepare a statement
136-
{:ok, statement} = Exqlite.Sqlite3.prepare(conn, "insert into test (stuff) values (?1)")
137-
:ok = Exqlite.Sqlite3.bind(conn, statement, ["Hello world"])
120+
{:ok, statement} = Exqlite.prepare(conn, "insert into test (stuff) values (?1)")
121+
:ok = Exqlite.bind(conn, statement, ["Hello world"])
138122

139123
# Step is used to run statements
140-
:done = Exqlite.Sqlite3.step(conn, statement)
141-
142-
# Prepare a select statement
143-
{:ok, statement} = Exqlite.Sqlite3.prepare(conn, "select id, stuff from test")
144-
145-
# Get the results
146-
{:row, [1, "Hello world"]} = Exqlite.Sqlite3.step(conn, statement)
147-
148-
# No more results
149-
:done = Exqlite.Sqlite3.step(conn, statement)
124+
:done = Exqlite.step(conn, statement)
150125

151126
# Release the statement.
152127
#
@@ -155,7 +130,19 @@ The `Exqlite.Sqlite3` module usage is fairly straight forward.
155130
#
156131
# If you are operating at a high load issuing thousands of statements, it would be
157132
# possible to run out of memory or cause a lot of pressure on memory.
158-
:ok = Exqlite.Sqlite3.release(conn, statement)
133+
:ok = Exqlite.release(statement)
134+
135+
# Prepare a select statement
136+
{:ok, statement} = Exqlite.prepare(conn, "select id, stuff from test")
137+
138+
# Get the results
139+
{:row, [1, "Hello world"]} = Exqlite.step(conn, statement)
140+
141+
# No more results
142+
:done = Exqlite.step(conn, statement)
143+
144+
# Release the statement
145+
:ok = Exqlite.release(statement)
159146
```
160147

161148
### Using SQLite3 native extensions
@@ -164,23 +151,23 @@ Exqlite supports loading [run-time loadable SQLite3 extensions](https://www.sqli
164151
A selection of precompiled extensions for popular CPU types / architectures is available by installing the [ExSqlean](https://github.com/mindreframer/ex_sqlean) package. This package wraps [SQLean: all the missing SQLite functions](https://github.com/nalgeon/sqlean).
165152

166153
```elixir
167-
alias Exqlite.Basic
168-
{:ok, conn} = Basic.open("db.sqlite3")
169-
:ok = Basic.enable_load_extension(conn)
154+
{:ok, conn} = Exqlite.open(":memory:")
155+
:ok = Exqlite.enable_load_extension(conn)
170156

171157
# load the regexp extension - https://github.com/nalgeon/sqlean/blob/main/docs/re.md
172-
Basic.load_extension(conn, ExSqlean.path_for("re"))
158+
{:ok, _rows} = Exqlite.prepare_fetch_all(conn, "select load_extension(?)", [ExSqlean.path_for("re")])
173159

174160
# run some queries to test the new `regexp_like` function
175-
{:ok, [[1]], ["value"]} = Basic.exec(conn, "select regexp_like('the year is 2021', ?) as value", ["2021"]) |> Basic.rows()
176-
{:ok, [[0]], ["value"]} = Basic.exec(conn, "select regexp_like('the year is 2021', ?) as value", ["2020"]) |> Basic.rows()
161+
{:ok, [[1]]} = Exqlite.prepare_fetch_all(conn, "select regexp_like('the year is 2021', ?)", ["2021"])
162+
{:ok, [[0]]} = Exqlite.prepare_fetch_all(conn, "select regexp_like('the year is 2021', ?)", ["2020"])
177163

178164
# prevent loading further extensions
179-
:ok = Basic.disable_load_extension(conn)
180-
{:error, %Exqlite.Error{message: "not authorized"}, _} = Basic.load_extension(conn, ExSqlean.path_for("re"))
165+
:ok = Exqlite.disable_load_extension(conn)
166+
{:error, %Exqlite.Error{message: "not authorized"}} =
167+
Exqlite.prepare_fetch_all(conn, "select load_extension(?)", [ExSqlean.path_for("re")])
181168

182169
# close connection
183-
Basic.close(conn)
170+
Exqlite.close(conn)
184171
```
185172

186173
## Why SQLite3

c_src/sqlite3_nif.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -859,17 +859,12 @@ exqlite_release(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
859859
assert(env);
860860

861861
statement_t* statement = NULL;
862-
connection_t* conn = NULL;
863862

864-
if (argc != 2) {
863+
if (argc != 1) {
865864
return enif_make_badarg(env);
866865
}
867866

868-
if (!enif_get_resource(env, argv[0], connection_type, (void**)&conn)) {
869-
return make_error_tuple(env, "invalid_connection");
870-
}
871-
872-
if (!enif_get_resource(env, argv[1], statement_type, (void**)&statement)) {
867+
if (!enif_get_resource(env, argv[0], statement_type, (void**)&statement)) {
873868
return make_error_tuple(env, "invalid_statement");
874869
}
875870

@@ -1147,10 +1142,10 @@ static ErlNifFunc nif_funcs[] = {
11471142
{"transaction_status", 1, exqlite_transaction_status, ERL_NIF_DIRTY_JOB_IO_BOUND},
11481143
{"serialize", 2, exqlite_serialize, ERL_NIF_DIRTY_JOB_IO_BOUND},
11491144
{"deserialize", 3, exqlite_deserialize, ERL_NIF_DIRTY_JOB_IO_BOUND},
1150-
{"release", 2, exqlite_release, ERL_NIF_DIRTY_JOB_IO_BOUND},
1145+
{"release", 1, exqlite_release, ERL_NIF_DIRTY_JOB_IO_BOUND},
11511146
{"enable_load_extension", 2, exqlite_enable_load_extension, ERL_NIF_DIRTY_JOB_IO_BOUND},
11521147
{"set_update_hook", 2, exqlite_set_update_hook, ERL_NIF_DIRTY_JOB_IO_BOUND},
11531148
{"set_log_hook", 1, exqlite_set_log_hook, ERL_NIF_DIRTY_JOB_IO_BOUND},
11541149
};
11551150

1156-
ERL_NIF_INIT(Elixir.Exqlite.Sqlite3NIF, nif_funcs, on_load, NULL, NULL, on_unload)
1151+
ERL_NIF_INIT(Elixir.Exqlite.Nif, nif_funcs, on_load, NULL, NULL, on_unload)

0 commit comments

Comments
 (0)