Skip to content

Commit 97a69ad

Browse files
committed
feat: initialize sql_parser
0 parents  commit 97a69ad

15 files changed

Lines changed: 431 additions & 0 deletions

File tree

.formatter.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
]

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
/priv/native/*.so
14+
15+
# Ignore .fetch files in case you like to edit your project deps locally.
16+
/.fetch
17+
18+
# If the VM crashes, it generates a dump, let's ignore it too.
19+
erl_crash.dump
20+
21+
# Also ignore archive artifacts (built via "mix archive.build").
22+
*.ez
23+
24+
# Ignore package tarball (built via "mix hex.build").
25+
sql_parser-*.tar
26+
27+
# Temporary files, for example, from tests.
28+
/tmp/

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SQLParser
2+
3+
An SQL parser that uses [sqlparser](https://github.com/sqlparser-rs/sqlparser-rs.git) underneath.
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `sql_parser` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[
13+
{:sql_parser, "~> 0.1.0"}
14+
]
15+
end
16+
```
17+
18+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20+
be found at <https://hexdocs.pm/sql_parser>.
21+

lib/sql_parser.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule SQLParser do
2+
@moduledoc """
3+
An SQL parser that uses [sqlparser](https://github.com/sqlparser-rs/sqlparser-rs.git) underneath.
4+
"""
5+
6+
@typep ast() :: map()
7+
8+
@spec parse(sql :: binary(), dialect :: binary()) :: {:ok, [ast()]} | {:error, binary()}
9+
def parse(sql, dialect \\ "postgres") when dialect in ["bigquery", "postgres"] do
10+
with {:ok, json} <- SQLParser.Native.parse(sql, dialect) do
11+
Jason.decode(json)
12+
end
13+
end
14+
15+
@spec to_sql(asts :: ast() | [ast()]) :: {:ok, binary()}
16+
def to_sql(ast) when is_map(ast), do: to_sql([ast])
17+
18+
def to_sql(asts) when is_list(asts) do
19+
asts
20+
|> Jason.encode!()
21+
|> SQLParser.Native.to_sql()
22+
end
23+
end

lib/sql_parser/native.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule SQLParser.Native do
2+
@moduledoc false
3+
4+
use Rustler, otp_app: :sql_parser, crate: :sql_parser
5+
6+
def parse(_sql, _dialect), do: :erlang.nif_error(:nif_not_loaded)
7+
8+
def to_sql(_ast), do: :erlang.nif_error(:nif_not_loaded)
9+
end

mix.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule SQLParser.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :sql_parser,
7+
version: "0.1.0",
8+
elixir: "~> 1.15",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
def application do
15+
[
16+
extra_applications: [:logger]
17+
]
18+
end
19+
20+
defp deps do
21+
[
22+
{:rustler, "~> 0.30.0"}
23+
]
24+
end
25+
end

mix.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%{
2+
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
3+
"rustler": {:hex, :rustler, "0.30.0", "cefc49922132b072853fa9b0ca4dc2ffcb452f68fb73b779042b02d545e097fb", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "9ef1abb6a7dda35c47cfc649e6a5a61663af6cf842a55814a554a84607dee389"},
4+
"toml": {:hex, :toml, "0.7.0", "fbcd773caa937d0c7a02c301a1feea25612720ac3fa1ccb8bfd9d30d822911de", [:mix], [], "hexpm", "0690246a2478c1defd100b0c9b89b4ea280a22be9a7b313a8a058a2408a2fa70"},
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.'cfg(target_os = "macos")']
2+
rustflags = [
3+
"-C", "link-arg=-undefined",
4+
"-C", "link-arg=dynamic_lookup",
5+
]

native/sql_parser/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

native/sql_parser/Cargo.lock

Lines changed: 211 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)