open-bounty/resources/sql/queries.sql

60 lines
1.6 KiB
MySQL
Raw Normal View History

2016-08-24 22:46:29 +00:00
-- Users ---------------------------------------------------------------------------
-- :name create-user! :! :n
-- :doc creates a new user record
INSERT INTO users
2016-08-22 00:22:39 +00:00
(id, login, name, email, token, address, created)
VALUES (:id, :login, :name, :email, :token, :address, :created);
-- :name update-user! :! :n
2016-08-24 22:46:29 +00:00
-- :doc updates an existing user record
UPDATE users
2016-08-22 00:22:39 +00:00
SET login = :login, name = :name, email = :email, token = :token, address = :address
WHERE id = :id;
2016-08-24 22:46:29 +00:00
-- :name update-user-token! :<! :1
-- :doc updates user token and returns updated user
UPDATE users
SET token = :token
WHERE login = :login
RETURNING id, login, name, email, token, address, created;
2016-08-22 00:22:39 +00:00
-- :name update-user-address! :! :n
UPDATE users
SET address = :address
2016-08-22 23:27:49 +00:00
WHERE login = :login;
-- :name get-user :? :1
2016-08-22 00:22:39 +00:00
-- :doc retrieve a user given the login.
SELECT *
FROM users
WHERE login = :login;
2016-08-24 22:46:29 +00:00
-- Repositories --------------------------------------------------------------------
-- :name toggle-repository! :<! :1
-- :doc toggles 'enabled' flag of a given repository
UPDATE repositories
SET enabled = NOT enabled
WHERE repo_id = :repo_id
RETURNING repo_id, login, repo, enabled;
-- :name create-repository! :<! :1
-- :doc creates repository if not exists
INSERT INTO repositories (repo_id, login, repo, enabled)
SELECT
:repo_id,
:login,
:repo,
:enabled
WHERE NOT exists(SELECT 1
FROM repositories
WHERE repo_id = :repo_id)
RETURNING repo_id, login, repo, enabled;
-- :name get-enabled-repositories :? :*
-- :doc returns enabled repositories for a given login
SELECT repo_id
FROM repositories
WHERE login = :login AND enabled = TRUE