-
Notifications
You must be signed in to change notification settings - Fork 75
Add support for the geometry library?
#208
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
Merged
wojtekmach
merged 5 commits into
elixir-ecto:master
from
aseigo:feature/add-support-for-gepmetry-lib
Jan 29, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a2f1cc
Make geometry handling more generic, suitable for use by libraries ot…
aseigo 97edefc
If there is no protocol for the struct, attempt to json encode it
aseigo c3e6e41
Provide a GeometryCodec behaviour to en/decode geometry data
aseigo 808dcb6
Apply suggestions from code review
aseigo 0cf16d0
Apply suggestions from code review
aseigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,39 @@ | ||
| defmodule MyXQL.Protocol.GeometryCodec do | ||
| @type no_srid() :: 0 | ||
| @type some_srid() :: pos_integer() | ||
|
|
||
| @callback encode(struct()) :: {srid :: integer(), wkb :: binary()} | :unknown | ||
| @callback decode(srid :: no_srid() | some_srid(), wkb :: binary()) :: struct() | :unknown | ||
|
|
||
| import MyXQL.Protocol.Types | ||
|
|
||
| @doc false | ||
| def do_encode(struct) do | ||
| with codec when not is_nil(codec) <- geometry_codec(), | ||
| {srid, wkb} <- codec.encode(struct) do | ||
| { | ||
| :mysql_type_var_string, | ||
| MyXQL.Protocol.Types.encode_string_lenenc(<<srid::uint4(), wkb::binary>>) | ||
| } | ||
| else | ||
| _ -> :unknown | ||
| end | ||
| end | ||
|
|
||
| @doc false | ||
| def do_decode(srid, wkb) do | ||
| codec = geometry_codec() | ||
|
|
||
| if codec != nil do | ||
| codec.decode(srid, wkb) | ||
| else | ||
| :unknown | ||
| end | ||
| end | ||
|
|
||
| default_codec = if Code.ensure_loaded?(Geo), do: MyXQL.Protocol.GeometryCodec.Geo, else: nil | ||
|
|
||
| defp geometry_codec do | ||
| Application.get_env(:myxql, :geometry_codec, unquote(default_codec)) | ||
| end | ||
| end |
This file contains hidden or 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,26 @@ | ||
| if Code.ensure_loaded?(Geo) do | ||
| defmodule MyXQL.Protocol.GeometryCodec.Geo do | ||
| @behaviour MyXQL.Protocol.GeometryCodec | ||
|
|
||
| supported_structs = [ | ||
| Geo.Point, | ||
| Geo.GeometryCollection, | ||
| Geo.LineString, | ||
| Geo.MultiPoint, | ||
| Geo.MultiLineString, | ||
| Geo.MultiPolygon, | ||
| Geo.Polygon | ||
| ] | ||
|
|
||
| def encode(%x{} = geo) when x in unquote(supported_structs) do | ||
| srid = geo.srid || 0 | ||
| wkb = %{geo | srid: nil} |> Geo.WKB.encode_to_iodata(:ndr) |> IO.iodata_to_binary() | ||
| {srid, wkb} | ||
| end | ||
|
|
||
| def encode(_), do: :unknown | ||
|
|
||
| def decode(0, wkb), do: Geo.WKB.decode!(wkb) | ||
| def decode(srid, wkb), do: Geo.WKB.decode!(wkb) |> Map.put(:srid, srid) | ||
| end | ||
| end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.