allocate_begin = "BEGIN EXCLUSIVE"
allocate_commit = "COMMIT"
+#
+# Attempt to find the most recent existing IP address for the client
+#
+allocate_existing = "\
+ SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND pool_key = '${pool_key}' \
+ AND status IN ('dynamic', 'static') \
+ ORDER BY expiry_time DESC \
+ LIMIT 1"
+
+#
+# Determine whether the requested IP address is available
+#
+allocate_requested = "\
+ SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND framedipaddress = '%{%{${req_attribute_name}}:-0.0.0.0}' \
+ AND status = 'dynamic' \
+ AND ( pool_key = '${pool_key}' OR expiry_time < datetime('now') )"
+
+#
+# If the existing address can't be found this query will be run to
+# find a free address
+#
+allocate_find = "\
+ SELECT framedipaddress \
+ FROM ${ippool_table} \
+ JOIN dhcpstatus \
+ ON ${ippool_table}.status_id = dhcpstatus.status_id \
+ WHERE pool_name = '%{control:${pool_name}}' \
+ AND expiry_time < datetime('now') \
+ AND status = 'dynamic' \
+ ORDER BY expiry_time LIMIT 1"
+
#
# This series of queries allocates an IP address
#
# Sorting the result by expiry_time DESC will return the client specific
# IP if it exists, otherwise an expired one.
#
-allocate_find = "\
- SELECT framedipaddress, expiry_time \
- FROM ( \
- SELECT framedipaddress, expiry_time \
- FROM ${ippool_table} \
- JOIN dhcpstatus \
- ON ${ippool_table}.status_id = dhcpstatus.status_id \
- WHERE pool_name = '%{control:${pool_name}}' \
- AND pool_key = '${pool_key}' \
- AND status IN ('dynamic', 'static') \
- ORDER BY expiry_time DESC \
- LIMIT 1 \
- ) UNION \
- SELECT framedipaddress, expiry_time \
- FROM ( \
- SELECT framedipaddress, expiry_time \
- FROM ${ippool_table} \
- JOIN dhcpstatus \
- ON ${ippool_table}.status_id = dhcpstatus.status_id \
- WHERE pool_name = '%{control:${pool_name}}' \
- AND expiry_time < datetime('now') \
- AND status = 'dynamic' \
- ORDER BY expiry_time LIMIT 1 \
- ) \
- ORDER BY \
- expiry_time DESC \
- LIMIT 1"
+#allocate_find = "\
+# SELECT framedipaddress, 1 AS o \
+# FROM ( \
+# SELECT framedipaddress \
+# FROM ${ippool_table} \
+# JOIN dhcpstatus \
+# ON ${ippool_table}.status_id = dhcpstatus.status_id \
+# WHERE pool_name = '%{control:${pool_name}}' \
+# AND pool_key = '${pool_key}' \
+# AND status IN ('dynamic', 'static') \
+# ORDER BY expiry_time DESC \
+# LIMIT 1 \
+# ) UNION \
+# SELECT framedipaddress, 2 AS o \
+# FROM ( \
+# SELECT framedipaddress \
+# FROM ${ippool_table} \
+# JOIN dhcpstatus \
+# ON ${ippool_table}.status_id = dhcpstatus.status_id \
+# WHERE pool_name = '%{control:${pool_name}}' \
+# AND framedipaddress = '%{%{${req_attribute_name}}:-0.0.0.0}' \
+# AND status = 'dynamic' \
+# AND ( pool_key = '${pool_key}' OR expiry_time < datetime('now') ) \
+# ) UNION \
+# SELECT framedipaddress, 3 AS o \
+# FROM ( \
+# SELECT framedipaddress \
+# FROM ${ippool_table} \
+# JOIN dhcpstatus \
+# ON ${ippool_table}.status_id = dhcpstatus.status_id \
+# WHERE pool_name = '%{control:${pool_name}}' \
+# AND expiry_time < datetime('now') \
+# AND status = 'dynamic' \
+# ORDER BY expiry_time LIMIT 1 \
+# ) \
+# ORDER BY o \
+# LIMIT 1"
#
# If you prefer to allocate a random IP address every time, i