-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implements SQLite extensions loading As initial selection for SQLITE extensions we take https://github.com/nalgeon/sqlean with some additions in this fork (https://github.com/mindreframer/sqlean) + packaged as Hex package here: https://hex.pm/packages/ex_sqlean. To keep the usage for Exqlite somewhat simpler and consistent, a new module is introduced: BasicAPI. It allows simple operations without the confusion between Exqlite.Sqlite3 and Exqlite.Connection modules to execute queries and dealing with results. * Bump the oldest supported elixir version to 1.9 (from 1.8) * Rename module: BasicAPI -> Basic * Bump lowest supported OTP to `21` + Elixir `1.8`
- Loading branch information
1 parent
b7852b6
commit 69a6c27
Showing
9 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Exqlite.Basic do | ||
@moduledoc """ | ||
A very basis API without lots of options to allow simpler usage for basic needs. | ||
""" | ||
|
||
alias Exqlite.Connection | ||
alias Exqlite.Query | ||
alias Exqlite.Sqlite3 | ||
alias Exqlite.Error | ||
alias Exqlite.Result | ||
|
||
def open(path) do | ||
Connection.connect(database: path) | ||
end | ||
|
||
def close(conn = %Connection{}) do | ||
with :ok <- Sqlite3.close(conn.db) do | ||
:ok | ||
else | ||
{:error, reason} -> {:error, %Error{message: reason}} | ||
end | ||
end | ||
|
||
def exec(conn = %Connection{}, stmt, args \\ []) do | ||
%Query{statement: stmt} |> Connection.handle_execute(args, [], conn) | ||
end | ||
|
||
def rows(exec_result) do | ||
case exec_result do | ||
{:ok, %Query{}, %Result{rows: rows, columns: columns}, %Connection{}} -> | ||
{:ok, rows, columns} | ||
|
||
{:error, %Error{message: message}, %Connection{}} -> | ||
{:error, message} | ||
end | ||
end | ||
|
||
def load_extension(conn, path) do | ||
exec(conn, "select load_extension(?)", [path]) | ||
end | ||
|
||
def enable_load_extension(conn) do | ||
Sqlite3.enable_load_extension(conn.db, true) | ||
end | ||
|
||
def disable_load_extension(conn) do | ||
Sqlite3.enable_load_extension(conn.db, false) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Exqlite.ExtensionsTest do | ||
use ExUnit.Case | ||
alias Exqlite.Basic | ||
|
||
describe "enable_load_extension" do | ||
test "loading can be enabled / disabled" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
:ok = Basic.enable_load_extension(conn) | ||
|
||
{:ok, [[nil]], _} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
|
||
{:ok, [[1]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") | ||
|> Basic.rows() | ||
|
||
:ok = Basic.disable_load_extension(conn) | ||
|
||
{:error, "not authorized"} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
end | ||
|
||
test "works for 're' (regex)" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
|
||
:ok = Basic.enable_load_extension(conn) | ||
|
||
{:ok, [[nil]], _} = | ||
Basic.load_extension(conn, ExSqlean.path_for("re")) |> Basic.rows() | ||
|
||
{:ok, [[0]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2k21')") | ||
|> Basic.rows() | ||
|
||
{:ok, [[1]], _} = | ||
Basic.exec(conn, "select regexp_like('the year is 2021', '2021')") | ||
|> Basic.rows() | ||
end | ||
|
||
test "stats extension" do | ||
{:ok, path} = Temp.path() | ||
{:ok, conn} = Basic.open(path) | ||
|
||
:ok = Basic.enable_load_extension(conn) | ||
Basic.load_extension(conn, ExSqlean.path_for("stats")) | ||
Basic.load_extension(conn, ExSqlean.path_for("series")) | ||
|
||
{:ok, [[50.5]], ["median(value)"]} = | ||
Basic.exec(conn, "select median(value) from generate_series(1, 100)") | ||
|> Basic.rows() | ||
end | ||
end | ||
end |