aboutsummaryrefslogtreecommitdiff
path: root/src/database.gleam
blob: 875231d70481a18555fad20526447e38f16fdaf5 (plain) (blame)
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
import birl
import gleam/dynamic
import gleam/io
import gleam/string
import sqlight

pub type Counter {
  Counter(name: String, num: Int, created_at: String, updated_at: String)
}

fn check_error(result, message) {
  case result {
    Ok(_) -> Nil
    Error(_) -> {
      io.print(message)

      Nil
    }
  }
}

pub fn setup(connection) {
  check_error(
    sqlight.exec(
      "pragma foreign_keys = off;

      create table if not exists tb_count (
        id integer primary key autoincrement not null unique,
        name text not null unique,
        num int not null default (0)
      ) strict;",
      connection,
    ),
    "Failed to create table tb_count",
  )

  let add_column = fn(name) {
    let _ =
      sqlight.exec(
        "alter table tb_count add column " <> name <> " text;",
        connection,
      )

    Nil
  }

  add_column("created_at")
  add_column("updated_at")

  Nil
}

pub fn add_counter(connection, name) {
  sqlight.query(
    "insert into tb_count (name) values (?);",
    with: [sqlight.text(name)],
    on: connection,
    expecting: dynamic.optional(dynamic.int),
  )
}

fn sqlite_now() {
  birl.to_iso8601(birl.utc_now())
  |> string.slice(0, 19)
  |> string.replace("T", " ")
}

pub fn get_counter(connection, name) {
  case name {
    "demo" -> Counter("demo", 0_123_456_789, "", "")
    _ -> {
      check_error(
        sqlight.query(
          "insert or ignore into tb_count (name, created_at) values (?, ?);",
          with: [sqlight.text(name), sqlight.text(sqlite_now())],
          on: connection,
          expecting: dynamic.optional(dynamic.int),
        ),
        "Failed to insert or ignore into tb_count",
      )
      check_error(
        sqlight.query(
          "update tb_count set num = num + 1, updated_at = ? where name = ?;",
          with: [sqlight.text(sqlite_now()), sqlight.text(name)],
          on: connection,
          expecting: dynamic.int,
        ),
        "Failed to update tb_count",
      )

      case
        sqlight.query(
          "select name, num, created_at, updated_at from tb_count where name = ?;",
          with: [sqlight.text(name)],
          on: connection,
          expecting: dynamic.tuple4(
            dynamic.string,
            dynamic.int,
            dynamic.string,
            dynamic.string,
          ),
        )
      {
        Ok([first_element]) -> {
          Counter(
            first_element.0,
            first_element.1,
            first_element.2,
            first_element.3,
          )
        }
        _ -> Counter(name, 0, "", "")
      }
    }
  }
}