-- File queries.sql
-- autogenerated by adl.to-hugsql-queries at
-- 2018-05-26T15:03:25.295Z
-- 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)
returning id

-- :name create-authority!  :! :n
-- :doc creates a new authority record
INSERT INTO authorities (id)
VALUES (:id)
returning 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)
returning id

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

-- :name create-dwelling!  :! :n
-- :doc creates a new dwelling record
INSERT INTO dwellings (address_id,
	sub-address)
VALUES (:address_id,
	:sub-address)
returning id

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

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

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

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

-- :name create-gender!  :! :n
-- :doc creates a new gender record
INSERT INTO genders (id)
VALUES (:id)
returning 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)
returning Id

-- :name create-issue!  :! :n
-- :doc creates a new issue record
INSERT INTO issues (url,
	current,
	id)
VALUES (:url,
	:current,
	:id)
returning 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)
returning Id

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

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

-- :name create-rolemembership!  :! :n
-- :doc creates a new rolemembership record
INSERT INTO rolememberships (role_id,
	canvasser_id)
VALUES (:role_id,
	:canvasser_id)
returning 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)
returning id

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

-- :name create-teamorganisership!  :! :n
-- :doc creates a new teamorganisership record
INSERT INTO teamorganiserships (team_id,
	canvasser_id)
VALUES (:team_id,
	:canvasser_id)
returning 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)
returning id

-- :name delete-address! :! :n
-- :doc updates an existing address record
DELETE FROM addresses
WHERE addresses.id = :id

-- :name delete-authority! :! :n
-- :doc updates an existing authority record
DELETE FROM authorities
WHERE authorities.id = :id

-- :name delete-canvasser! :! :n
-- :doc updates an existing canvasser record
DELETE FROM canvassers
WHERE canvassers.id = :id

-- :name delete-district! :! :n
-- :doc updates an existing district record
DELETE FROM districts
WHERE districts.id = :id

-- :name delete-dwelling! :! :n
-- :doc updates an existing dwelling record
DELETE FROM dwellings
WHERE dwellings.id = :id

-- :name delete-elector! :! :n
-- :doc updates an existing elector record
DELETE FROM electors
WHERE electors.id = :id

-- :name delete-followupaction! :! :n
-- :doc updates an existing followupaction record
DELETE FROM followupactions
WHERE followupactions.id = :id

-- :name delete-followupmethod! :! :n
-- :doc updates an existing followupmethod record
DELETE FROM followupmethods
WHERE followupmethods.id = :id

-- :name delete-followuprequest! :! :n
-- :doc updates an existing followuprequest record
DELETE FROM followuprequests
WHERE followuprequests.id = :id

-- :name delete-gender! :! :n
-- :doc updates an existing gender record
DELETE FROM genders
WHERE genders.id = :id

-- :name delete-intention! :! :n
-- :doc updates an existing intention record
DELETE FROM intentions
WHERE intentions.Id = :Id

-- :name delete-issue! :! :n
-- :doc updates an existing issue record
DELETE FROM issues
WHERE issues.id = :id

-- :name delete-issueexpertis! :! :n
-- :doc updates an existing issueexpertis record
DELETE FROM issueexpertise
WHERE issueexpertise.Id = :Id

-- :name delete-option! :! :n
-- :doc updates an existing option record
DELETE FROM options
WHERE options.id = :id

-- :name delete-role! :! :n
-- :doc updates an existing role record
DELETE FROM roles
WHERE roles.id = :id

-- :name delete-rolemembership! :! :n
-- :doc updates an existing rolemembership record
DELETE FROM rolememberships
WHERE rolememberships.Id = :Id

-- :name delete-team! :! :n
-- :doc updates an existing team record
DELETE FROM teams
WHERE teams.id = :id

-- :name delete-teammembership! :! :n
-- :doc updates an existing teammembership record
DELETE FROM teammemberships
WHERE teammemberships.Id = :Id

-- :name delete-teamorganisership! :! :n
-- :doc updates an existing teamorganisership record
DELETE FROM teamorganiserships
WHERE teamorganiserships.Id = :Id

-- :name delete-visit! :! :n
-- :doc updates an existing visit record
DELETE FROM visits
WHERE visits.id = :id

-- :name get-address :? :1
-- :doc selects an existing address record
SELECT * FROM addresses
WHERE addresses.id = :id
ORDER BY addresses.address,
	addresses.postcode,
	addresses.id

-- :name get-authority :? :1
-- :doc selects an existing authority record
SELECT * FROM authorities
WHERE authorities.id = :id

-- :name get-canvasser :? :1
-- :doc selects an existing canvasser record
SELECT * FROM canvassers
WHERE canvassers.id = :id
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email,
	canvassers.id

-- :name get-district :? :1
-- :doc selects an existing district record
SELECT * FROM districts
WHERE districts.id = :id
ORDER BY districts.name,
	districts.id

-- :name get-dwelling :? :1
-- :doc selects an existing dwelling record
SELECT * FROM dwellings
WHERE dwellings.id = :id

-- :name get-elector :? :1
-- :doc selects an existing elector record
SELECT * FROM electors
WHERE electors.id = :id
ORDER BY electors.name,
	electors.phone,
	electors.email,
	electors.id

-- :name get-followupaction :? :1
-- :doc selects an existing followupaction record
SELECT * FROM followupactions
WHERE followupactions.id = :id

-- :name get-followupmethod :? :1
-- :doc selects an existing followupmethod record
SELECT * FROM followupmethods
WHERE followupmethods.id = :id

-- :name get-followuprequest :? :1
-- :doc selects an existing followuprequest record
SELECT * FROM followuprequests
WHERE followuprequests.id = :id

-- :name get-gender :? :1
-- :doc selects an existing gender record
SELECT * FROM genders
WHERE genders.id = :id

-- :name get-intention :? :1
-- :doc selects an existing intention record
SELECT * FROM intentions
WHERE intentions.Id = :Id

-- :name get-issue :? :1
-- :doc selects an existing issue record
SELECT * FROM issues
WHERE issues.id = :id

-- :name get-issueexpertis :? :1
-- :doc selects an existing issueexpertis record
SELECT * FROM issueexpertise
WHERE issueexpertise.Id = :Id

-- :name get-option :? :1
-- :doc selects an existing option record
SELECT * FROM options
WHERE options.id = :id

-- :name get-role :? :1
-- :doc selects an existing role record
SELECT * FROM roles
WHERE roles.id = :id
ORDER BY roles.name,
	roles.id

-- :name get-rolemembership :? :1
-- :doc selects an existing rolemembership record
SELECT * FROM rolememberships
WHERE rolememberships.Id = :Id

-- :name get-team :? :1
-- :doc selects an existing team record
SELECT * FROM teams
WHERE teams.id = :id
ORDER BY teams.name,
	teams.id

-- :name get-teammembership :? :1
-- :doc selects an existing teammembership record
SELECT * FROM teammemberships
WHERE teammemberships.Id = :Id

-- :name get-teamorganisership :? :1
-- :doc selects an existing teamorganisership record
SELECT * FROM teamorganiserships
WHERE teamorganiserships.Id = :Id

-- :name get-visit :? :1
-- :doc selects an existing visit record
SELECT * FROM visits
WHERE visits.id = :id

-- :name list-addresses :? :*
-- :doc lists all existing address records
SELECT * FROM addresses
ORDER BY addresses.address,
	addresses.postcode,
	addresses.id
--~ (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,
	addresses.id

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

-- :name list-canvassers :? :*
-- :doc lists all existing canvasser records
SELECT * FROM canvassers
ORDER BY canvassers.username,
	canvassers.fullname,
	canvassers.email,
	canvassers.id
--~ (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,
	canvassers.id

-- :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,
	canvassers.id

-- :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,
	canvassers.id

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

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

-- :name list-dwellings-by-addres :? :*
-- :doc lists all existing dwelling records related to a given addres
SELECT *
FROM dwellings
WHERE dwellings.address_id = :id

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

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

-- :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,
	electors.id

-- :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-followupmethods :? :*
-- :doc lists all existing followupmethod records
SELECT * FROM followupmethods
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

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

-- :name list-followuprequests-by-elector :? :*
-- :doc lists all existing followuprequest records related to a given elector
SELECT *
FROM followuprequests
WHERE followuprequests.elector_id = :id

-- :name list-followuprequests-by-followupmethod :? :*
-- :doc lists all existing followuprequest records related to a given followupmethod
SELECT *
FROM followuprequests
WHERE followuprequests.method_id = :id

-- :name list-followuprequests-by-issue :? :*
-- :doc lists all existing followuprequest records related to a given issue
SELECT *
FROM followuprequests
WHERE followuprequests.issue_id = :id

-- :name list-followuprequests-by-visit :? :*
-- :doc lists all existing followuprequest records related to a given visit
SELECT *
FROM followuprequests
WHERE followuprequests.visit_id = :id

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

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

-- :name list-intentions-by-elector :? :*
-- :doc lists all existing intention records related to a given elector
SELECT *
FROM intentions
WHERE intentions.elector_id = :id

-- :name list-intentions-by-option :? :*
-- :doc lists all existing intention records related to a given option
SELECT *
FROM intentions
WHERE intentions.option_id = :id

-- :name list-intentions-by-visit :? :*
-- :doc lists all existing intention records related to a given visit
SELECT *
FROM intentions
WHERE intentions.visit_id = :id

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

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

-- :name list-issueexpertise-by-followupmethod :? :*
-- :doc lists all existing issueexpertis records related to a given followupmethod
SELECT *
FROM issueexpertise
WHERE issueexpertise.method_id = :id

-- :name list-issueexpertise-by-issue :? :*
-- :doc lists all existing issueexpertis records related to a given issue
SELECT *
FROM issueexpertise
WHERE issueexpertise.issue_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-options :? :*
-- :doc lists all existing option records
SELECT * FROM options
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

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

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

-- :name list-rolememberships-by-role :? :*
-- :doc lists all existing rolemembership records related to a given role
SELECT *
FROM rolememberships
WHERE rolememberships.role_id = :id

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

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

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

-- :name list-teammemberships-by-team :? :*
-- :doc lists all existing teammembership records related to a given team
SELECT *
FROM teammemberships
WHERE teammemberships.team_id = :id

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

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

-- :name list-teamorganiserships-by-team :? :*
-- :doc lists all existing teamorganisership records related to a given team
SELECT *
FROM teamorganiserships
WHERE teamorganiserships.team_id = :id

-- :name list-teams :? :*
-- :doc lists all existing team records
SELECT * FROM teams
ORDER BY teams.name,
	teams.id
--~ (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,
	teams.id

-- :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,
	addresses.id
--~ (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,
	canvassers.id
--~ (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,
	districts.id
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")

-- :name search-strings-dwelling :? :1
-- :doc selects existing dwelling records having any string field matching `:pattern` by substring match
SELECT * FROM dwellings
WHERE
sub-address LIKE '%:pattern%'
--~ (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,
	electors.id
--~ (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,
	roles.id
--~ (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,
	teams.id
--~ (if (:offset params) "OFFSET :offset ")
--~ (if (:limit params) "LIMIT :limit" "LIMIT 100")
