Implement the database tables and queries
This commit is contained in:
parent
4434700343
commit
5077560a6e
5 changed files with 95 additions and 26 deletions
|
@ -1 +0,0 @@
|
|||
DROP TABLE users;
|
|
@ -1,9 +0,0 @@
|
|||
CREATE TABLE users
|
||||
(id VARCHAR(20) PRIMARY KEY,
|
||||
first_name VARCHAR(30),
|
||||
last_name VARCHAR(30),
|
||||
email VARCHAR(30),
|
||||
admin BOOLEAN,
|
||||
last_login TIME,
|
||||
is_active BOOLEAN,
|
||||
pass VARCHAR(300));
|
|
@ -0,0 +1,13 @@
|
|||
DROP TABLE images;
|
||||
|
||||
--;;
|
||||
|
||||
DROP TABLE tags;
|
||||
|
||||
--;;
|
||||
|
||||
DROP TABLE image_tags;
|
||||
|
||||
--;;
|
||||
|
||||
DROP TABLE comments;
|
|
@ -0,0 +1,39 @@
|
|||
CREATE TABLE images (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
hash TEXT NOT NULL UNIQUE,
|
||||
title TEXT,
|
||||
description TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
--;;
|
||||
|
||||
CREATE TABLE tags (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
tagname TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
--;;
|
||||
|
||||
CREATE TABLE image_tags (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
image_id INTEGER NOT NULL,
|
||||
tag_id INTEGER NOT NULL
|
||||
)
|
||||
|
||||
--;;
|
||||
|
||||
CREATE TABLE comments (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
image_id INTEGER NOT NULL,
|
||||
author TEXT,
|
||||
comment TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
--;;
|
||||
|
||||
CREATE INDEX hash_index ON images (hash);
|
||||
CREATE INDEX tagname_index ON tags (tagname);
|
||||
CREATE INDEX image_tags_index ON image_tags (image_id, tag_id);
|
||||
CREATE INDEX author_index ON comments (author);
|
|
@ -1,21 +1,48 @@
|
|||
-- :name create-user! :! :n
|
||||
-- :doc creates a new user record
|
||||
INSERT INTO users
|
||||
(id, first_name, last_name, email, pass)
|
||||
VALUES (:id, :first_name, :last_name, :email, :pass)
|
||||
-- :name create-image! :! :n
|
||||
INSERT INTO images (hash, title, description)
|
||||
VALUES (:hash, :title, :description)
|
||||
|
||||
-- :name update-user! :! :n
|
||||
-- :doc update an existing user record
|
||||
UPDATE users
|
||||
SET first_name = :first_name, last_name = :last_name, email = :email
|
||||
-- :name update-image! :! :n
|
||||
UPDATE images
|
||||
SET hash = :hash, title = :title, description = :description
|
||||
WHERE id = :id
|
||||
|
||||
-- :name get-user :? :1
|
||||
-- :doc retrieve a user given the id.
|
||||
SELECT * FROM users
|
||||
-- :name get-image :? :1
|
||||
SELECT * FROM images WHERE id = :id
|
||||
|
||||
-- :name delete-image! :! :n
|
||||
DELETE FROM image
|
||||
WHERE id = :id
|
||||
|
||||
-- :name delete-user! :! :n
|
||||
-- :doc delete a user given the id
|
||||
DELETE FROM users
|
||||
WHERE id = :id
|
||||
|
||||
-- :name create-tag! :! :n
|
||||
INSERT OR IGNORE INTO tags (tagname)
|
||||
VALUES (:tagname)
|
||||
|
||||
-- :name get-tags-for-image :? :n
|
||||
SELECT tags.tagname FROM image_tags
|
||||
INNER JOIN tags ON tags.id = image_tags.tag_id
|
||||
WHERE image_id = :id
|
||||
ORDER BY tags.tagname ASC
|
||||
|
||||
-- :name get-images-for-tagname :? :n
|
||||
SELECT images.* FROM image_tags
|
||||
INNER JOIN images ON images.id = image_tags.image_id
|
||||
INNER JOIN tags on tags.id = image_tags.tag_id
|
||||
WHERE tags.tagname = :tagname
|
||||
ORDER BY images.created_at DESC
|
||||
|
||||
|
||||
-- :name create-comment! :! :n
|
||||
INSERT INTO comments (image_id, author, comment)
|
||||
VALUES (:image_id, :author, :comment)
|
||||
|
||||
-- :name get-comments-for-image :? :n
|
||||
SELECT * FROM comments
|
||||
WHERE image_id = :image_id
|
||||
ORDER BY created_at DESC
|
||||
|
||||
-- :name get-recent-comments
|
||||
SELECT * FROM comments
|
||||
ORDER BY created_at DESC
|
||||
LIMIT :num-comments
|
||||
|
|
Loading…
Reference in a new issue