Skip to content

Commit 11550a8

Browse files
committed
Connect/Erlang: Add dedicated page
1 parent 746c3b6 commit 11550a8

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

docs/connect/erlang/index.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
(connect-erlang)=
2+
3+
# Erlang
4+
5+
:::{div} sd-text-muted
6+
Connect to CrateDB from Erlang applications.
7+
:::
8+
9+
## ODBC
10+
11+
:::{rubric} About
12+
:::
13+
14+
Erlang includes an [ODBC application] out of the box that provides an
15+
interface to communicate with relational SQL-databases, see also
16+
[Erlang ODBC examples].
17+
18+
::::{todo}
19+
Enable this include with the [ODBC patch](https://github.com/crate/cratedb-guide/pull/411).
20+
```md
21+
:::{include} _odbc-setup-widget.md
22+
:::
23+
```
24+
::::
25+
26+
:::{rubric} Synopsis
27+
:::
28+
29+
Before running the example, ensure the PostgreSQL ODBC driver is
30+
installed on your system.
31+
32+
`odbc_example.erl`
33+
```erlang
34+
-module(odbc_example).
35+
36+
main(_) ->
37+
odbc:start(),
38+
{ok, Ref} = odbc:connect("Driver={PostgreSQL ODBC};Server=localhost;Port=5432;Uid=crate;Pwd=crate", []),
39+
io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),
40+
init:stop().
41+
```
42+
43+
:::{include} ../_cratedb.md
44+
:::
45+
```shell
46+
escript odbc_example.erl
47+
```
48+
49+
:::{rubric} CrateDB Cloud
50+
:::
51+
52+
For connecting to CrateDB Cloud, start the Erlang [SSL application],
53+
add `sslmode=require`, and replace `Server`, `Uid`, and `Pwd` with
54+
values matching your environment.
55+
56+
`odbc_example.erl`
57+
```erlang
58+
-module(odbc_example).
59+
60+
main(_) ->
61+
ssl:start(),
62+
odbc:start(),
63+
{ok, Ref} = odbc:connect("Driver={PostgreSQL};Server=testcluster.cratedb.net;Port=5432;sslmode=require;Uid=admin;Pwd=password", []),
64+
io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),
65+
init:stop().
66+
```
67+
68+
69+
## epgsql
70+
71+
[epgsql] is the designated Erlang PostgreSQL client library.
72+
73+
`rebar.config`
74+
```erlang
75+
{deps,
76+
[
77+
{epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}}
78+
]}.
79+
```
80+
`epgsql_example.erl`
81+
```erlang
82+
-module(epgsql_example).
83+
-export([start/0]).
84+
85+
start() ->
86+
{ok, C} = epgsql:connect(#{
87+
host => "localhost",
88+
username => "crate",
89+
password => "crate",
90+
database => "doc",
91+
port => 5432,
92+
timeout => 4000
93+
}),
94+
{ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"),
95+
io:fwrite("~p~n", [Result]),
96+
ok = epgsql:close(C),
97+
init:stop().
98+
```
99+
100+
:::{include} ../_cratedb.md
101+
:::
102+
```shell
103+
rebar3 compile
104+
erlc epgsql_example.erl
105+
rebar3 shell --eval 'epgsql_example:start().'
106+
```
107+
108+
:::{rubric} CrateDB Cloud
109+
:::
110+
111+
For connecting to CrateDB Cloud, start the Erlang [SSL application] first,
112+
use the `ssl` and `ssl_opts` arguments on `epgsql:connect`, and
113+
replace username, password, and hostname with values matching
114+
your environment.
115+
```erlang
116+
start() ->
117+
ssl:start(),
118+
{ok, C} = epgsql:connect(#{
119+
host => "testcluster.cratedb.net",
120+
username => "admin",
121+
password => "password",
122+
database => "doc",
123+
port => 5432,
124+
ssl => true,
125+
ssl_opts => [{verify, verify_none}],
126+
timeout => 4000
127+
}),
128+
```
129+
130+
131+
[epgsql]: https://github.com/epgsql/epgsql
132+
[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html
133+
[ODBC application]: https://www.erlang.org/docs/28/apps/odbc/odbc.html
134+
[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html

docs/connect/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an
4949
:gutter: 3
5050
:padding: 0
5151

52+
::::{grid-item-card} Erlang
53+
:link: connect-erlang
54+
:link-type: ref
55+
:link-alt: Connect to CrateDB using Erlang
56+
:padding: 3
57+
:text-align: center
58+
:class-card: sd-pt-3
59+
:class-body: sd-fs-1
60+
:class-title: sd-fs-6
61+
{fab}`erlang`
62+
::::
63+
5264
::::{grid-item-card} Java
5365
:link: connect-java
5466
:link-type: ref
@@ -176,6 +188,7 @@ application
176188
:maxdepth: 1
177189
:hidden:
178190
191+
erlang/index
179192
java/index
180193
javascript
181194
php

0 commit comments

Comments
 (0)