From d7e0fa6059e178a94af8b49510d3d7750b4fe1da Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 8 Sep 2019 12:22:52 +0200 Subject: [PATCH] Simplify db code --- nitter.nimble | 2 +- src/api/media.nim | 2 +- src/cache.nim | 12 +++++++----- src/prefs.nim | 13 +++++++------ src/types.nim | 5 ++++- src/views/preferences.nim | 2 +- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/nitter.nimble b/nitter.nimble index 30e43cf..6f3fd66 100644 --- a/nitter.nimble +++ b/nitter.nimble @@ -11,7 +11,7 @@ bin = @["nitter"] # Dependencies requires "nim >= 0.19.9" -requires "norm >= 1.0.13" +requires "norm >= 1.0.15" requires "jester >= 0.4.3" requires "regex >= 0.11.2" requires "q >= 0.0.7" diff --git a/src/api/media.nim b/src/api/media.nim index 608b034..2b3b667 100644 --- a/src/api/media.nim +++ b/src/api/media.nim @@ -102,7 +102,7 @@ proc getVideoVar(tweet: Tweet): var Option[Video] = return tweet.video proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} = - withDb: + withCustomDb("cache.db", "", "", ""): try: getVideoVar(tweet) = some(Video.getOne("videoId = ?", tweet.id)) except KeyError: diff --git a/src/cache.nim b/src/cache.nim index ebba87a..ec4c1bd 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -1,7 +1,9 @@ -import asyncdispatch, times +import asyncdispatch, times, strutils import types, api -withCustomDb("cache.db", "", "", ""): +dbFromTypes("cache.db", "", "", "", [Profile, Video]) + +withDb: try: createTables() except DbError: @@ -13,7 +15,7 @@ proc isOutdated*(profile: Profile): bool = getTime() - profile.updated > profileCacheTime proc cache*(profile: var Profile) = - withCustomDb("cache.db", "", "", ""): + withDb: try: let p = Profile.getOne("lower(username) = ?", toLower(profile.username)) profile.id = p.id @@ -23,7 +25,7 @@ proc cache*(profile: var Profile) = profile.insert() proc hasCachedProfile*(username: string): Option[Profile] = - withCustomDb("cache.db", "", "", ""): + withDb: try: let p = Profile.getOne("lower(username) = ?", toLower(username)) doAssert not p.isOutdated @@ -32,7 +34,7 @@ proc hasCachedProfile*(username: string): Option[Profile] = result = none(Profile) proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} = - withCustomDb("cache.db", "", "", ""): + withDb: try: result.getOne("lower(username) = ?", toLower(username)) doAssert not result.isOutdated diff --git a/src/prefs.nim b/src/prefs.nim index e2aea5c..be50e4e 100644 --- a/src/prefs.nim +++ b/src/prefs.nim @@ -1,6 +1,5 @@ -import sequtils, macros -import types -import prefs_impl +import strutils, sequtils, macros +import prefs_impl, types export genUpdatePrefs @@ -15,14 +14,16 @@ static: if missing.len > 0: raiseAssert("{$1} missing from the Prefs type" % missing.join(", ")) -withCustomDb("prefs.db", "", "", ""): +dbFromTypes("prefs.db", "", "", "", [Prefs]) + +withDb: try: createTables() except DbError: discard proc cache*(prefs: var Prefs) = - withCustomDb("prefs.db", "", "", ""): + withDb: try: doAssert prefs.id != 0 discard Prefs.getOne("id = ?", prefs.id) @@ -33,7 +34,7 @@ proc cache*(prefs: var Prefs) = proc getPrefs*(id: string): Prefs = if id.len == 0: return genDefaultPrefs() - withCustomDb("prefs.db", "", "", ""): + withDb: try: result.getOne("id = ?", id) except KeyError: diff --git a/src/types.nim b/src/types.nim index 4837a83..07cd332 100644 --- a/src/types.nim +++ b/src/types.nim @@ -7,7 +7,7 @@ type VideoType* = enum vmap, m3u8, mp4 -db("cache.db", "", "", ""): +dbTypes: type Profile* = object username*: string @@ -62,6 +62,9 @@ db("cache.db", "", "", ""): replaceYouTube*: string replaceTwitter*: string + +dbFromTypes("cache.db", "", "", "", [Profile, Video]) + type QueryKind* = enum replies, media, multi, custom = "search" diff --git a/src/views/preferences.nim b/src/views/preferences.nim index afe9092..39db5f9 100644 --- a/src/views/preferences.nim +++ b/src/views/preferences.nim @@ -1,4 +1,4 @@ -import tables, macros, strformat, xmltree +import tables, macros, strformat, strutils, xmltree import karax/[karaxdsl, vdom, vstyles] import renderutils