Matthew Stobbs
e68723738b
- simplified configuration file - simplified to only use ipv4 and macaddresses for now - added update query - if insert fails with already exists error, try to update instead
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
package postgres
|
|
|
|
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
|
|
dhcp_identifier_type,
|
|
dhcp4_subnet_id,
|
|
ipv4_address,
|
|
hostname)
|
|
VALUES (DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5);`
|
|
|
|
const getHostIDWithMacAddr = `SELECT host_id
|
|
FROM hosts
|
|
WHERE dhcp4_subnet_id = $1
|
|
AND dhcp_identifier_type = $2
|
|
AND dhcp_identifier = (DECODE(REPLACE($3, ':', ''), 'hex'));`
|
|
|
|
const insertIfNotExists = `INSERT INTO hosts (dhcp_identifier,
|
|
dhcp_identifier_type,
|
|
dhcp4_subnet_id,
|
|
ipv4_address,
|
|
hostname)
|
|
SELECT DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5
|
|
WHERE NOT EXISTS (
|
|
SELECT host_id FROM hosts
|
|
WHERE dhcp4_subnet_id = $3
|
|
AND dhcp_identifier_type = $2
|
|
AND dhcp_identifier = DECODE(REPLACE($1, ':', ''), 'hex'));`
|
|
|
|
const update = `UPDATE hosts
|
|
SET ipv4_address = (SELECT ($4::inet - '0.0.0.0'::inet)),
|
|
hostname = $5
|
|
WHERE dhcp_identifier = (SELECT DECODE(REPLACE($1, ':', ''), 'hex')) AND
|
|
dhcp_identifier_type = $2 AND
|
|
dhcp4_subnet_id = $3;`
|