src/database.lisp @ 4df358dc538b default tip

Update to build
author Steve Losh <steve@stevelosh.com>
date Sun, 28 Aug 2022 12:48:04 -0400
parents 421231bbf89f
children (none)
(in-package :magitek.database)

(defvar *database* nil)

(defun db-connect (&key (path "database.sqlite"))
  (setf *database* (connect path))
  (values))

(defun check-db ()
  (when (null *database*)
    (error "You forgot to run (spinup) again you fuckin moron.")))

(defun db-initialize ()
  (execute-non-query *database*
    "CREATE TABLE IF NOT EXISTS tweets(
        id INTEGER PRIMARY KEY,
        account TEXT NOT NULL,
        content TEXT NOT NULL,
        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
     )")
  (execute-non-query *database*
    "CREATE INDEX IF NOT EXISTS idx_tweets_account_timestamp
     ON tweets (account, timestamp)")
  (values))

(defun db-insert-tweet (account tweet)
  (check-db)
  (execute-non-query *database*
    "INSERT INTO tweets (account, content) VALUES (?, ?)"
    (aesthetic-string account)
    (aesthetic-string tweet))
  (values))

(defun db-tweeted-since-p (account minutes-ago)
  (check-type minutes-ago (integer 1))
  (check-db)
  (if (execute-single *database*
        "SELECT content FROM tweets
        WHERE account = ?
          AND timestamp > datetime('now', ?)
        LIMIT 1
      "
        (aesthetic-string account)
        (format nil "-~D minutes" minutes-ago))
      t
      nil))