You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Database Interface for Erlang. This is an abstract implementation to use the most common database libraries ([p1_mysql][1], [epgsql][2] and [esqlite][4], and others you want) to use with standard SQL in your programs and don't worry about if you need to change between the main databases in the market.
9
+
Database Interface for Erlang and Elixir. This is an abstract implementation to use the most common database libraries ([p1_mysql][1], [epgsql][2] and [esqlite][4], and others you want) to use with standard SQL in your programs and don't worry about if you need to change between the main databases in the market.
10
10
11
11
### Install (rebar3)
12
12
13
13
To use it, with rebar, you only need to add the dependency to the rebar.config file:
14
14
15
15
```erlang
16
16
{deps, [
17
-
{dbi, "0.1.0"}
17
+
{dbi, "0.2.0"}
18
18
]}
19
19
```
20
20
21
+
### Install (mix)
22
+
23
+
To use it, with mix, you only need to add the dependency to the mix.exs file:
24
+
25
+
```elixir
26
+
{:dbi, "~> 0.2.0"}
27
+
```
28
+
21
29
### Configuration
22
30
23
31
The configuration is made in the configuration file (`sys.config` or `app.config`) so, you can add a new block for config the database connection as follow:
@@ -49,21 +57,59 @@ The configuration is made in the configuration file (`sys.config` or `app.config
49
57
50
58
The available types in this moment are: `mysql`, `pgsql` and `sqlite`.
51
59
60
+
In case you're using Elixir, you can define the configuration for your project in this way:
61
+
62
+
```elixir
63
+
confg :dbi, mydatabase: [
64
+
type::mysql,
65
+
host:'localhost',
66
+
user:'root',
67
+
pass:'root',
68
+
database:'mydatabase',
69
+
poolsize:10
70
+
],
71
+
mylocaldb: [
72
+
type::sqlite,
73
+
database:':memory'
74
+
],
75
+
mystrongdb: [
76
+
type::pgsql,
77
+
host:'localhost',
78
+
user:'root',
79
+
pass:'root',
80
+
database:'mystrongdb',
81
+
poolsize:100
82
+
]
83
+
```
84
+
52
85
### Using DBI
53
86
54
-
To do a query:
87
+
To do a query (Erlang):
55
88
56
89
```erlang
57
90
{ok, Count, Rows} =dbi:do_query(mydatabase, "SELECT * FROM users", []),
58
91
```
59
92
60
-
Or with params:
93
+
Elixir:
94
+
95
+
```elixir
96
+
{:ok, count, rows} =DBI.do_query(:mydatabase, "SELECT * FROM users", [])
97
+
```
98
+
99
+
Or with params (Erlang):
61
100
62
101
```erlang
63
102
{ok, Count, Rows} =dbi:do_query(mydatabase,
64
103
"SELECT * FROM users WHERE id = $1", [12]),
65
104
```
66
105
106
+
Elixir:
107
+
108
+
```elixir
109
+
{:ok, count, rows} =DBI.do_query(:mydatabase,
110
+
"SELECT * FROM users WHERE id = $1", [12])
111
+
```
112
+
67
113
Rows has the format: `[{field1, field2, ..., fieldN}, ...]`
68
114
69
115
**IMPORTANT** the use of $1..$100 in the query is extracted from pgsql, in mysql and sqlite is converted to the `?` syntax so, if you write this query:
@@ -73,19 +119,34 @@ Rows has the format: `[{field1, field2, ..., fieldN}, ...]`
73
119
"UPDATE users SET name = $2 WHERE id = $1", [12, "Mike"]),
74
120
```
75
121
122
+
Elixir:
123
+
124
+
```elixir
125
+
{:ok, count, rows} =DBI.do_query(:mydatabase,
126
+
"UPDATE users SET name = $2 WHERE id = $1", [12, "Mike"])
127
+
```
128
+
76
129
That should works well in pgsql, but **NOT for mysql and NOT for sqlite**. For avoid this situations, the best to do is **always keep the order of the params**.
77
130
78
131
### Delayed or Queued queries
79
132
80
-
If you want to create a connection to send only commands like INSERT, UPDATE or DELETE but without saturate the database (and run out database connections in the pool) you can use `dbi_delayed`:
133
+
If you want to create a connection to send only commands like INSERT, UPDATE or DELETE but without saturate the database (and run out database connections in the pool) you can use `dbi_delayed` (Erlang):
This use only one connection from the pool `myconn`, when the query ends then `dbi_delayed` gets another query to run from the queue. You get statistics about the progress and the queue size:
This use only one connection from the pool `myconn`, when the query ends then `dbi_delayed` gets another query to run from the queue. You get statistics about the progress and the queue size (Erlang):
@@ -112,6 +184,20 @@ The delayed can be added to the configuration:
112
184
]}
113
185
```
114
186
187
+
Elixir:
188
+
189
+
```elixir
190
+
config :dbi, mydatabase: [
191
+
type::mysql,
192
+
host:'localhost',
193
+
user:'root',
194
+
pass:'root',
195
+
database:'mydatabase',
196
+
poolsize:10,
197
+
delayed::delay_myconn
198
+
]
199
+
```
200
+
115
201
### Cache queries
116
202
117
203
Another thing you can do is use a cache for SQL queries. The cache store the SQL as `key` and the result as `value` and keep the values for the time you specify in the configuration file:
@@ -130,6 +216,20 @@ Another thing you can do is use a cache for SQL queries. The cache store the SQL
130
216
]}
131
217
```
132
218
219
+
Elixir:
220
+
221
+
```elixir
222
+
config :dbi, mydatabase: [
223
+
type::mysql,
224
+
host:'localhost',
225
+
user:'root',
226
+
pass:'root',
227
+
database:'mydatabase',
228
+
poolsize:10,
229
+
cache:5
230
+
]
231
+
```
232
+
133
233
The cache param is in seconds. The ideal time to keep the cache values depends on the size of your tables, the data to store in the cache and how frequent are the changes in that data. For avoid flood and other issues due to fast queries or a lot of queries in little time you can use 5 or 10 seconds. To store the information about constants or other data without frequent changes you can use 3600 (one hour) or more time.
134
234
135
235
To use the cache you should to use the following function from `dbi_cache`:
@@ -138,13 +238,26 @@ To use the cache you should to use the following function from `dbi_cache`:
138
238
dbi_cache:do_query(mydatabase, "SELECT items FROM table"),
139
239
```
140
240
241
+
Elixir:
242
+
243
+
```elixir
244
+
DBI.Cache.do_query(:mydatabase, "SELECT items FROM table")
245
+
```
246
+
141
247
You can use `do_query/2` or `do_query/3` if you want to use params. And if you want to use a specific TTL (time-to-live) for your query, you can use `do_query/4`:
0 commit comments