diff options
| author | Fuwn <[email protected]> | 2024-05-20 14:48:04 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-05-20 14:48:04 -0700 |
| commit | 5659c5000c8785bf576adc3f9331f75ad6af5621 (patch) | |
| tree | 38453f08712470b802790cffd8b902166d35a45a /src | |
| parent | 9aa7197c647e634a8eb685fdc266f4bd5d907848 (diff) | |
| download | mayu-5659c5000c8785bf576adc3f9331f75ad6af5621.tar.xz mayu-5659c5000c8785bf576adc3f9331f75ad6af5621.zip | |
feat(database): more error checking
Diffstat (limited to 'src')
| -rw-r--r-- | src/database.gleam | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/database.gleam b/src/database.gleam index 1e26a31..875231d 100644 --- a/src/database.gleam +++ b/src/database.gleam @@ -1,5 +1,6 @@ import birl import gleam/dynamic +import gleam/io import gleam/string import sqlight @@ -7,8 +8,19 @@ 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) { - let assert Ok(_) = + check_error( sqlight.exec( "pragma foreign_keys = off; @@ -18,7 +30,10 @@ pub fn setup(connection) { num int not null default (0) ) strict;", connection, - ) + ), + "Failed to create table tb_count", + ) + let add_column = fn(name) { let _ = sqlight.exec( @@ -54,20 +69,24 @@ pub fn get_counter(connection, name) { case name { "demo" -> Counter("demo", 0_123_456_789, "", "") _ -> { - let assert Ok(_) = + 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), - ) - let assert Ok(_) = + ), + "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( |