A website for producing interactive charts without writing a single line of code. Built with Common Lisp and Python. https://charts.craigoates.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

60 lines
2.1 KiB

(defpackage #:db-management
(:use #:cl
#:mito
#:local-time
#:storage
#:user
#:file)
(:import-from #:hot-line.db
#:connection-settings
#:db
#:with-connection)
(:export #:get-distinct-column-totals
#:get-random-selection
#:seed-database))
(in-package #:db-management)
(defun seed-database()
"A quick way to reset the database."
(hot-line.db:with-connection (db)
(mito:ensure-table-exists 'user)
(mito:create-dao 'user:user
:username "admin"
:display-name "Dave from Accounting"
:administrator 1
:password "password")
(mito:ensure-table-exists 'file)
(mito:create-dao 'file:file
:filename "Testing.png"
:username (mito:find-dao 'user:user :username "admin")
:slug "testing"
:content-type "image/png")))
(defun get-distinct-column-totals (table column)
"Creates a list of distinct values and their totals in `COLUMN' in DB `TABLE'."
(with-connection (db)
(mito:retrieve-by-sql
(format nil
"SELECT ~S, COUNT(~S) AS col_totals FROM ~S GROUP BY ~S;"
column column table column))))
(defun get-random-selection (table amount exclude-id)
"Returns a random selection of rows from the `TABLE' in the database.
`AMOUNT' specifies how many rows to return and the `EXCLUDE-ID' is the row which
should NOT be returned. The intented use for this function is to generate a list
of 'other articles/artworks' Etc. when viewing a 'content.html' page. The need
for 'raw SQL' is because I couldn't find a way to apply the 'RANDOM()' function
using mito and sxql.
Please note, this function does not map the results to their classes like mito
usually does, especially when used with sxql. Because this functions main reason
to be is to provide a list for populating the 'Other X' columns in the
content.html templates, you will need to use the 'raw-date' djula filter (custom
made and in view.lisp) to render the date-time stamp."
(with-connection (db)
(mito:retrieve-by-sql
(format nil
"SELECT * FROM ~a WHERE ID != ~a ORDER BY RANDOM() LIMIT ~a"
table
exclude-id
amount))))