-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.odin
97 lines (81 loc) · 2.53 KB
/
main.odin
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
package easy_api
import sqlite "../.."
import sa "../../addons"
import "core:fmt"
import "core:mem" // needed only for the tracking allocator
Album :: struct {
id: u32 `sqlite:"AlbumId"`,
title: string `sqlite:"Title"`,
// Not the best representation of an ID but it showcases that enums work
artist_id: ArtistId `sqlite:"ArtistId"`,
}
ArtistId :: enum {
AC_DC = 1,
Accept = 2,
Aerosmith = 3,
}
main :: proc() {
// setup tracking allocator. not necessary
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
context.allocator = mem.tracking_allocator(&track)
defer {
if len(track.allocation_map) > 0 {
fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map))
for _, entry in track.allocation_map {
fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location)
}
}
if len(track.bad_free_array) > 0 {
fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array))
for entry in track.bad_free_array {
fmt.eprintf("- %p @ %v\n", entry.memory, entry.location)
}
}
mem.tracking_allocator_destroy(&track)
}
fmt.printfln("default config: %#v\n", sa.config)
// actual example code
sa.config.log_level = .Info // if nil, does not log
sa.config.extra_runtime_checks = true // does extra checks on enum values
db: ^sqlite.Connection
if rc := sqlite.open("./sample.sqlite", &db); rc != .Ok {
fmt.panicf("failed to open database. result code {}", rc)
}
fmt.printfln("connected to database\n")
defer {
sqlite.close(db)
fmt.printfln("\nconnection closed")
}
// query example
{
fmt.println("======= query example begin =======\n")
defer fmt.println("\n======= query example end =======")
albums: [dynamic]Album
defer {
for album in albums {
delete(album.title)
}
delete(albums)
}
if rc := sa.query(
db,
&albums,
"select AlbumId, Title, ArtistId from Album where ArtistId <= ? limit ?",
{{1, i64(ArtistId.Aerosmith)}, {2, i32(5)}},
); rc != .Ok {
fmt.panicf("failed to execute query. result code {}", rc)
}
fmt.printfln("albums: %#v", albums)
}
// execute example. this is a type of query that you want to update/insert/delete stuff
// but not return data.
// p.s. I know that the query below does not mutate the data - I don't want the data to be changed
{
fmt.println("\n======= execute example begin =======\n")
defer fmt.println("\n======= execute example end =======")
if rc := sa.execute(db, "select 1"); rc != .Ok {
fmt.panicf("failed to execute query. result code {}", rc)
}
}
}