From 5077560a6ec7e0df12ed8168e8a14dfbc45f367c Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 3 Mar 2017 00:41:07 +0100 Subject: [PATCH] Implement the database tables and queries --- .../20170216222925-add-users-table.down.sql | 1 - .../20170216222925-add-users-table.up.sql | 9 --- ...02232609-create-initial-structure.down.sql | 13 ++++ ...0302232609-create-initial-structure.up.sql | 39 ++++++++++++ resources/sql/queries.sql | 59 ++++++++++++++----- 5 files changed, 95 insertions(+), 26 deletions(-) delete mode 100644 resources/migrations/20170216222925-add-users-table.down.sql delete mode 100644 resources/migrations/20170216222925-add-users-table.up.sql create mode 100644 resources/migrations/20170302232609-create-initial-structure.down.sql create mode 100644 resources/migrations/20170302232609-create-initial-structure.up.sql diff --git a/resources/migrations/20170216222925-add-users-table.down.sql b/resources/migrations/20170216222925-add-users-table.down.sql deleted file mode 100644 index cc1f647..0000000 --- a/resources/migrations/20170216222925-add-users-table.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE users; diff --git a/resources/migrations/20170216222925-add-users-table.up.sql b/resources/migrations/20170216222925-add-users-table.up.sql deleted file mode 100644 index 05ad4af..0000000 --- a/resources/migrations/20170216222925-add-users-table.up.sql +++ /dev/null @@ -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)); diff --git a/resources/migrations/20170302232609-create-initial-structure.down.sql b/resources/migrations/20170302232609-create-initial-structure.down.sql new file mode 100644 index 0000000..958470d --- /dev/null +++ b/resources/migrations/20170302232609-create-initial-structure.down.sql @@ -0,0 +1,13 @@ +DROP TABLE images; + +--;; + +DROP TABLE tags; + +--;; + +DROP TABLE image_tags; + +--;; + +DROP TABLE comments; diff --git a/resources/migrations/20170302232609-create-initial-structure.up.sql b/resources/migrations/20170302232609-create-initial-structure.up.sql new file mode 100644 index 0000000..b509a38 --- /dev/null +++ b/resources/migrations/20170302232609-create-initial-structure.up.sql @@ -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); diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 4191f67..ded7372 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -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