Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting custom pragmas #229

Merged
merged 1 commit into from
Jan 11, 2023

Conversation

adri
Copy link
Contributor

@adri adri commented Jan 7, 2023

This allows for someone to set any PRAGMA keys and values, next to the most common ones. See the documentation for a full list: https://www2.sqlite.org/draft/pragma.html

One use-case is to set the cipher_compatibility PRAGMA which is supported by the SQLCipher extension:
https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_compatibility

Fixes #227

@adri adri force-pushed the adri/support-custom-pragmas branch from 00e19b4 to 0a860ba Compare January 7, 2023 14:31
This allows for someone to set any PRAGMA keys and values, next to the most common ones.
See the documentation for a full list: https://www2.sqlite.org/draft/pragma.html

One use-case is to set the `cipher_compatibility` PRAGMA which is supported by the
SQLCipher extension:
https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_compatibility
@adri adri force-pushed the adri/support-custom-pragmas branch from 0a860ba to 5087ef2 Compare January 7, 2023 14:36
end
end

defp do_set_custom_pragmas(db, list) do
Copy link
Contributor Author

@adri adri Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I switched to two separate functions I tried to...

Use a case statement, which was seen as one "indentation depth" too much by the linter.

Warning: 
┃ [F] → Function body is nested too deep (max depth is 2, was 3).
┃       lib/exqlite/connection.ex:384:11 #(Exqlite.Connection.set_custom_pragma)
[13](https://github.com/elixir-sqlite/exqlite/actions/runs/3862474027/jobs/6584066856#step:9:14)

I tried a with statement here, but that didn't work ;)

┃ [R] → `with` contains only one <- clause and an `else` branch, consider using `case` instead
┃       lib/exqlite/connection.ex:380 #(Exqlite.Connection.set_custom_pragmas)
[15](https://github.com/elixir-sqlite/exqlite/actions/runs/3862541622/jobs/6584174897#step:9:16)

So here we are with a separate function ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh credo. We should just relax it instead. I prefer readability over pedantics.

Copy link
Member

@warmwaffles warmwaffles Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it was complaining about this

with {:ok, list} <- Keyword.fetch(options, :custom_pragmas) do
  Enum.reduce_while(list, :ok, fn {key, value}, :ok ->
    case set_pragma(db, key, value) do
      :ok -> {:cont, :ok}
      {:error, _reason} -> {:halt, :error}
    end
  end)
else
  _otherwise -> :ok
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that was it, and about a nested case statement :)

@adri adri marked this pull request as ready for review January 7, 2023 14:38
@warmwaffles
Copy link
Member

I swear I'll get to this soon. I've got another bit of work for ecto_sqlite3 I need to knock out and then will refocus on this today.

Copy link
Member

@warmwaffles warmwaffles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great.

@warmwaffles warmwaffles merged commit 78e5ca3 into elixir-sqlite:main Jan 11, 2023
@adri adri deleted the adri/support-custom-pragmas branch January 11, 2023 15:52
@adri
Copy link
Contributor Author

adri commented Jan 11, 2023

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQLCipher: How to set cipher_compatibility?
2 participants