-- File queries.sql
-- autogenerated by adl.to-hugsql-queries at
-- 2018-05-13T16:47:04.188Z
-- See [Application Description Language](https://github.com/simon-brooke/adl).



-- :name create-address!  :! :n
-- :doc creates a new address record
INSERT INTO addresses (address,
	postcode,
	phone,
	district_id,
	latitude,
	longitude)
VALUES (:address,
	:postcode,
	:phone,
	:district_id,
	:latitude,
	:longitude)

-- :name create-authority!  :! :n
-- :doc creates a new authority record
INSERT INTO authorities (id)
VALUES (:id)

-- :name create-canvasser!  :! :n
-- :doc creates a new canvasser record
INSERT INTO canvassers (username,
	fullname,
	elector_id,
	address_id,
	phone,
	email,
	authority_id,
	authorised)
VALUES (:username,
	:fullname,
	:elector_id,
	:address_id,
	:phone,
	:email,
	:authority_id,
	:authorised)

-- :name create-district!  :! :n
-- :doc creates a new district record
INSERT INTO districts (name)
VALUES (:name)

-- :name create-elector!  :! :n
-- :doc creates a new elector record
INSERT INTO electors (name,
	address_id,
	phone,
	email,
	gender)
VALUES (:name,
	:address_id,
	:phone,
	:email,
	:gender)

-- :name create-followupaction!  :! :n
-- :doc creates a new followupaction record
INSERT INTO followupactions (request_id,
	actor,
	date,
	notes,
	closed,
	id)
VALUES (:request_id,
	:actor,
	:date,
	:notes,
	:closed,
	:id)

-- :name create-followupmethod!  :! :n
-- :doc creates a new followupmethod record
INSERT INTO followupmethods (id)
VALUES (:id)

-- :name create-followuprequest!  :! :n
-- :doc creates a new followuprequest record
INSERT INTO followuprequests (elector_id,
	visit_id,
	issue_id,
	method_id,
	id)
VALUES (:elector_id,
	:visit_id,
	:issue_id,
	:method_id,
	:id)

-- :name create-gender!  :! :n
-- :doc creates a new gender record
INSERT INTO genders (id)
VALUES (:id)

-- :name create-intention!  :! :n
-- :doc creates a new intention record
INSERT INTO intentions (visit_id,
	elector_id,
	option_id)
VALUES (:visit_id,
	:elector_id,
	:option_id)

-- :name create-issue!  :! :n
-- :doc creates a new issue record
INSERT INTO issues (url,
	current,
	id)
VALUES (:url,
	:current,
	:id)

-- :name create-issueexpertis!  :! :n
-- :doc creates a new issueexpertis record
INSERT INTO issueexpertise (canvasser_id,
	issue_id,
	method_id)
VALUES (:canvasser_id,
	:issue_id,
	:method_id)

-- :name create-option!  :! :n
-- :doc creates a new option record
INSERT INTO options (id)
VALUES (:id)

-- :name create-role!  :! :n
-- :doc creates a new role record
INSERT INTO roles (name)
VALUES (:name)

-- :name create-rolemembership!  :! :n
-- :doc creates a new rolemembership record
INSERT INTO rolememberships (role_id,
	canvasser_id)
VALUES (:role_id,
	:canvasser_id)

-- :name create-team!  :! :n
-- :doc creates a new team record
INSERT INTO teams (name,
	district_id,
	latitude,
	longitude)
VALUES (:name,
	:district_id,
	:latitude,
	:longitude)

-- :name create-teammembership!  :! :n
-- :doc creates a new teammembership record
INSERT INTO teammemberships (team_id,
	canvasser_id)
VALUES (:team_id,
	:canvasser_id)

-- :name create-teamorganisership!  :! :n
-- :doc creates a new teamorganisership record
INSERT INTO teamorganiserships (team_id,
	canvasser_id)
VALUES (:team_id,
	:canvasser_id)

-- :name create-visit!  :! :n
-- :doc creates a new visit record
INSERT INTO visits (address_id,
	canvasser_id,
	date)
VALUES (:address_id,
	:canvasser_id,
	:date)

-- :name list-addresses :? :*
-- :doc lists all existing address records
SELECT * FROM addresses
ORDER BY addresses.address,
	addresses.postcode
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-addresses-by-district :? :*
-- :doc lists all existing address records related to a given district
SELECT * 
FROM addresses
WHERE addresses.district_id = :id
ORDER BY addresses.address,
	addresses.postcode

-- :name list-canvassers :? :*
-- :doc lists all existing canvasser records
SELECT * FROM canvassers
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-canvassers-by-address :? :*
-- :doc lists all existing canvasser records related to a given address
SELECT * 
FROM canvassers
WHERE canvassers.address_id = :id
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email

-- :name list-canvassers-by-authority :? :*
-- :doc lists all existing canvasser records related to a given authority
SELECT * 
FROM canvassers
WHERE canvassers.authority_id = :id
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email

-- :name list-canvassers-by-elector :? :*
-- :doc lists all existing canvasser records related to a given elector
SELECT * 
FROM canvassers
WHERE canvassers.elector_id = :id
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email

-- :name list-districts :? :*
-- :doc lists all existing district records
SELECT * FROM districts
ORDER BY districts.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-electors :? :*
-- :doc lists all existing elector records
SELECT * FROM electors
ORDER BY electors.name,
	electors.phone,
	electors.email
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-electors-by-address :? :*
-- :doc lists all existing elector records related to a given address
SELECT * 
FROM electors
WHERE electors.address_id = :id
ORDER BY electors.name,
	electors.phone,
	electors.email

-- :name list-electors-by-gender :? :*
-- :doc lists all existing elector records related to a given gender
SELECT * 
FROM electors
WHERE electors.gender = :id
ORDER BY electors.name,
	electors.phone,
	electors.email

-- :name list-followupactions :? :*
-- :doc lists all existing followupaction records
SELECT * FROM followupactions
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-followupactions-by-canvasser :? :*
-- :doc lists all existing followupaction records related to a given canvasser
SELECT * 
FROM followupactions
WHERE followupactions.actor = :id

-- :name list-followupactions-by-followuprequest :? :*
-- :doc lists all existing followupaction records related to a given followuprequest
SELECT * 
FROM followupactions
WHERE followupactions.request_id = :id

-- :name list-issues :? :*
-- :doc lists all existing issue records
SELECT * FROM issues
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-roles :? :*
-- :doc lists all existing role records
SELECT * FROM roles
ORDER BY roles.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-teams :? :*
-- :doc lists all existing team records
SELECT * FROM teams
ORDER BY teams.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-teams-by-district :? :*
-- :doc lists all existing team records related to a given district
SELECT * 
FROM teams
WHERE teams.district_id = :id
ORDER BY teams.name

-- :name list-visits :? :*
-- :doc lists all existing visit records
SELECT * FROM visits
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name list-visits-by-address :? :*
-- :doc lists all existing visit records related to a given address
SELECT * 
FROM visits
WHERE visits.address_id = :id

-- :name list-visits-by-canvasser :? :*
-- :doc lists all existing visit records related to a given canvasser
SELECT * 
FROM visits
WHERE visits.canvasser_id = :id

-- :name search-strings-address :? :1
-- :doc selects existing address records having any string field matching `:pattern` by substring match
SELECT * FROM addresses
WHERE 
address LIKE '%:pattern%'
	OR phone LIKE '%:pattern%'
ORDER BY addresses.address,
	addresses.postcode
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-canvasser :? :1
-- :doc selects existing canvasser records having any string field matching `:pattern` by substring match
SELECT * FROM canvassers
WHERE 
username LIKE '%:pattern%'
	OR fullname LIKE '%:pattern%'
	OR phone LIKE '%:pattern%'
	OR email LIKE '%:pattern%'
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-district :? :1
-- :doc selects existing district records having any string field matching `:pattern` by substring match
SELECT * FROM districts
WHERE 
name LIKE '%:pattern%'
ORDER BY districts.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-elector :? :1
-- :doc selects existing elector records having any string field matching `:pattern` by substring match
SELECT * FROM electors
WHERE 
name LIKE '%:pattern%'
	OR phone LIKE '%:pattern%'
	OR email LIKE '%:pattern%'
ORDER BY electors.name,
	electors.phone,
	electors.email
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-issue :? :1
-- :doc selects existing issue records having any string field matching `:pattern` by substring match
SELECT * FROM issues
WHERE 
url LIKE '%:pattern%'
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-role :? :1
-- :doc selects existing role records having any string field matching `:pattern` by substring match
SELECT * FROM roles
WHERE 
name LIKE '%:pattern%'
ORDER BY roles.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-team :? :1
-- :doc selects existing team records having any string field matching `:pattern` by substring match
SELECT * FROM teams
WHERE 
name LIKE '%:pattern%'
ORDER BY teams.name
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")