Skip to content

Commit fd4a44b

Browse files
amotlmatriv
andauthored
Driver: Add page about Erlang (#420)
* Add dedicated page * Put each item on a separate page * Copy editing * Fix card on index page (merge conflict unresolved) * ODBC: Include ODBC installation instructions * epgsql: Generalize "SSL connection" section --------- Co-authored-by: Marios Trivyzas <5058131+matriv@users.noreply.github.com>
1 parent 6e212de commit fd4a44b

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

docs/connect/erlang/epgsql.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(erlang-epgsql)=
2+
3+
# Erlang epgsql
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from Erlang using epgsql.
7+
:::
8+
9+
:::{rubric} About
10+
:::
11+
12+
[epgsql] is the designated Erlang PostgreSQL client library.
13+
14+
:::{rubric} Synopsis
15+
:::
16+
17+
`rebar.config`
18+
```erlang
19+
{deps,
20+
[
21+
{epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}}
22+
]}.
23+
```
24+
`epgsql_example.erl`
25+
```erlang
26+
-module(epgsql_example).
27+
-export([start/0]).
28+
29+
start() ->
30+
{ok, C} = epgsql:connect(#{
31+
host => "localhost",
32+
username => "crate",
33+
password => "crate",
34+
database => "doc",
35+
port => 5432,
36+
timeout => 4000
37+
}),
38+
{ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"),
39+
io:fwrite("~p~n", [Result]),
40+
ok = epgsql:close(C),
41+
init:stop().
42+
```
43+
44+
:::{rubric} SSL connection
45+
:::
46+
47+
Start the Erlang [SSL application] first,
48+
use the `ssl` and `ssl_opts` arguments on `epgsql:connect`, and
49+
replace username, password, and hostname with values matching
50+
your environment.
51+
52+
Also use this variant to connect to CrateDB Cloud.
53+
```erlang
54+
start() ->
55+
ssl:start(),
56+
{ok, C} = epgsql:connect(#{
57+
host => "testcluster.cratedb.net",
58+
username => "admin",
59+
password => "password",
60+
database => "doc",
61+
port => 5432,
62+
ssl => true,
63+
ssl_opts => [{verify, verify_none}],
64+
timeout => 4000
65+
}),
66+
```
67+
68+
:::{rubric} Example
69+
:::
70+
71+
Create the files `rebar.config` and `epgsql_example.erl` including the synopsis code shared above.
72+
73+
:::{include} ../_cratedb.md
74+
:::
75+
```shell
76+
rebar3 compile
77+
erlc epgsql_example.erl
78+
rebar3 shell --eval 'epgsql_example:start().'
79+
```
80+
81+
82+
[epgsql]: https://github.com/epgsql/epgsql
83+
[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html

docs/connect/erlang/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(connect-erlang)=
2+
3+
# Erlang
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from Erlang applications.
7+
:::
8+
9+
:::{toctree}
10+
odbc
11+
epgsql
12+
:::

docs/connect/erlang/odbc.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(erlang-odbc)=
2+
3+
# Erlang ODBC
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from Erlang using ODBC.
7+
:::
8+
9+
:::{rubric} About
10+
:::
11+
12+
Erlang includes an [ODBC application] out of the box that provides an
13+
interface to communicate with relational SQL-databases, see also
14+
[Erlang ODBC examples].
15+
16+
:::{rubric} Install
17+
:::
18+
19+
:::{include} ../odbc/install.md
20+
:::
21+
22+
:::{rubric} Synopsis
23+
:::
24+
25+
Before running the example, ensure the PostgreSQL ODBC driver is
26+
installed on your system.
27+
28+
`odbc_example.erl`
29+
```erlang
30+
-module(odbc_example).
31+
32+
main(_) ->
33+
odbc:start(),
34+
{ok, Ref} = odbc:connect("Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Uid=crate;Pwd=crate", []),
35+
io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),
36+
init:stop().
37+
```
38+
39+
:::{rubric} CrateDB Cloud
40+
:::
41+
42+
For connecting to CrateDB Cloud, start the Erlang [SSL application],
43+
add `sslmode=require`, and replace `Server`, `Uid`, and `Pwd` with
44+
values matching your environment.
45+
46+
`odbc_example.erl`
47+
```erlang
48+
-module(odbc_example).
49+
50+
main(_) ->
51+
ssl:start(),
52+
odbc:start(),
53+
{ok, Ref} = odbc:connect("Driver={PostgreSQL Unicode};Server=testcluster.cratedb.net;Port=5432;sslmode=require;Uid=admin;Pwd=password", []),
54+
io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),
55+
init:stop().
56+
```
57+
58+
:::{rubric} Example
59+
:::
60+
61+
Create the file `odbc_example.erl` including the synopsis code shared above.
62+
63+
:::{include} ../_cratedb.md
64+
:::
65+
```shell
66+
escript odbc_example.erl
67+
```
68+
69+
70+
[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html
71+
[ODBC application]: https://www.erlang.org/docs/28/apps/odbc/odbc.html
72+
[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html

docs/connect/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ Any (ODBC)
7676
Elixir
7777
::::
7878

79+
::::{grid-item-card}
80+
:link: connect-erlang
81+
:link-type: ref
82+
:class-body: sd-fs-1 sd-text-center
83+
:class-footer: sd-fs-5 sd-font-weight-bold
84+
{fab}`erlang`
85+
+++
86+
Erlang
87+
::::
88+
7989
::::{grid-item-card}
8090
:link: connect-go
8191
:link-type: ref
@@ -229,6 +239,7 @@ application
229239
:hidden:
230240
231241
elixir/index
242+
erlang/index
232243
go/index
233244
java/index
234245
javascript

0 commit comments

Comments
 (0)