FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
feather/src/feather.gleam at master · violetbuse/feather · GitHub
violetbuse
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
9
Code
Issues
0
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
feather
/
src
/
feather.gleam
Copy path
More file actions
More file actions
Latest commit
History
History
History
157 lines (138 loc) · 3.56 KB
Breadcrumbs
feather
/
src
/
feather.gleam
Copy path
File metadata and controls
157 lines (138 loc) · 3.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import
feather/migrate
import
gleam/int
import
gleam/option
.
{
type
Option
,
None
}
import
gleam/result
import
sqlight
.
{
type
Connection
,
type
Error
}
/// Runs the feather cli to generate new migrations and dump the schema
/// you probably don't wanna run this yourself...
/// run `gleam run -m feather` to find out more
pub
fn
main
(
)
{
migrate
.
main
(
)
}
pub
type
JournalMode
{
JournalDelete
JournalTruncate
JournalPersist
JournalMemory
JournalWal
JournalOff
}
pub
type
SyncMode
{
SyncExtra
SyncFull
SyncNormal
SyncOff
}
pub
type
TempStore
{
TempStoreDefault
TempStoreFile
TempStoreMemory
}
pub
type
Config
{
Config
(
file
:
String
,
journal_mode
:
JournalMode
,
synchronous
:
SyncMode
,
temp_store
:
TempStore
,
mmap_size
:
Option
(
Int
)
,
page_size
:
Option
(
Int
)
,
foreign_keys
:
Bool
,
)
}
pub
fn
default_config
(
)
->
Config
{
Config
(
"./sqlite.db"
,
journal_mode
:
JournalWal
,
synchronous
:
SyncNormal
,
temp_store
:
TempStoreMemory
,
mmap_size
:
None
,
page_size
:
None
,
foreign_keys
:
True
,
)
}
pub
fn
connect
(
config
:
Config
)
->
Result
(
Connection
,
Error
)
{
use
connection
<-
result
.
try
(
sqlight
.
open
(
config
.
file
)
)
let
journal_mode
=
case
config
.
journal_mode
{
JournalOff
->
"OFF"
JournalWal
->
"WAL"
JournalDelete
->
"DELETE"
JournalMemory
->
"MEMORY"
JournalTruncate
->
"TRUNCATE"
JournalPersist
->
"PERSIST"
}
let
sync
=
case
config
.
synchronous
{
SyncOff
->
"OFF"
SyncFull
->
"FULL"
SyncExtra
->
"EXTRA"
SyncNormal
->
"NORMAL"
}
let
temp_store
=
case
config
.
temp_store
{
TempStoreFile
->
"FILE"
TempStoreMemory
->
"MEMORY"
TempStoreDefault
->
"DEFAULT"
}
let
foreign_keys
=
case
config
.
foreign_keys
{
True
->
"on"
False
->
"off"
}
use
_
<-
result
.
try
(
sqlight
.
exec
(
"PRAGMA journal_mode = "
<>
journal_mode
<>
";"
,
connection
,
)
)
use
_
<-
result
.
try
(
sqlight
.
exec
(
"PRAGMA synchronous = "
<>
sync
<>
";"
,
connection
,
)
)
use
_
<-
result
.
try
(
sqlight
.
exec
(
"PRAGMA temp_store = "
<>
temp_store
<>
";"
,
connection
,
)
)
use
_
<-
result
.
try
(
option
.
map
(
config
.
mmap_size
,
int
.
to_string
)
|>
option
.
map
(
fn
(
size
)
{
sqlight
.
exec
(
"PRAGMA mmap_size = "
<>
size
<>
";"
,
connection
)
}
)
|>
option
.
unwrap
(
Ok
(
Nil
)
)
,
)
use
_
<-
result
.
try
(
option
.
map
(
config
.
page_size
,
int
.
to_string
)
|>
option
.
map
(
fn
(
size
)
{
sqlight
.
exec
(
"PRAGMA page_size = "
<>
size
<>
";"
,
connection
)
}
)
|>
option
.
unwrap
(
Ok
(
Nil
)
)
,
)
use
_
<-
result
.
try
(
sqlight
.
exec
(
"PRAGMA foreign_keys = "
<>
foreign_keys
<>
";"
,
connection
,
)
)
Ok
(
connection
)
}
/// runs "PRAGMA optimize;" before closing the connection.
/// If the connections are long-lived, then consider running
/// this periodically anyways.
pub
fn
disconnect
(
connection
:
Connection
)
{
let
_
=
sqlight
.
exec
(
"PRAGMA optimize;"
,
connection
)
sqlight
.
close
(
connection
)
}
pub
fn
optimize
(
connection
:
Connection
)
{
sqlight
.
exec
(
"PRAGMA optimize;"
,
connection
)
}
/// Created a short lived connection, runs the included function on it,
/// and then disconnects. If the connection could not be established,
/// the callback function is not run, and this function returns an `Error`.
///
/// This is particularly nice to use with use expressions:
/// ```gleam
/// use connection <- with_connection(default_config())
///
/// let result = sqlight.query(...)
/// ```
pub
fn
with_connection
(
config
:
Config
,
fxn
:
fn
(
Connection
)
->
a
,
)
->
Result
(
a
,
Error
)
{
use
connection
<-
result
.
try
(
connect
(
config
)
)
let
result
=
fxn
(
connection
)
let
_
=
disconnect
(
connection
)
Ok
(
result
)
}
Back
|
FazBrowse Home
|
New Git URL