From: Marcin Siodelski Date: Thu, 1 Oct 2020 11:59:39 +0000 (+0200) Subject: [#1428] Use SQL select 1 consistently X-Git-Tag: Kea-1.9.1~136 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ffba02c72740a52f8fb414b3ec919e109cdd38e;p=thirdparty%2Fkea.git [#1428] Use SQL select 1 consistently The new INSERT SELECT statements are also better described to explain why SELECT 1 is used as a result of the review. --- diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index bc006f58d3..fb19fac700 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -2608,10 +2608,13 @@ TaggedStatementArray tagged_statements = { { // Inserts a host into the 'hosts' table with checking that reserved IP // address is unique. The innermost query checks if there is at least - // one host for the given IP/subnet combination. If it not exists the - // new host is inserted. DUAL is a special MySQL table from which we - // can select the values to be inserted. If the host with the given - // IP address already exists the new host won't be inserted. The caller + // one host for the given IP/subnet combination. For checking whether + // hosts exists or not it doesn't matter if we select actual columns, + // thus SELECT 1 was used as an optimization to avoid selecting data + // that will be ignored anyway. If the host does not exist the new + // host is inserted. DUAL is a special MySQL table from which we can + // select the values to be inserted. If the host with the given IP + // address already exists the new host won't be inserted. The caller // can check the number of affected rows to detect that there was // a duplicate host in the database. {MySqlHostDataSourceImpl::INSERT_HOST_UNIQUE_IP, @@ -2622,7 +2625,7 @@ TaggedStatementArray tagged_statements = { { "dhcp4_server_hostname, dhcp4_boot_file_name, auth_key) " "SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM DUAL " "WHERE NOT EXISTS (" - "SELECT ipv4_address FROM hosts " + "SELECT 1 FROM hosts " "WHERE ipv4_address = ? AND dhcp4_subnet_id = ? " "LIMIT 1" ")"}, diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index abc3c83811..edbc630951 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -2009,11 +2009,14 @@ TaggedStatementArray tagged_statements = { { // PgSqlHostDataSourceImpl::INSERT_HOST_UNIQUE_IP // Inserts a host into the 'hosts' table with checking that reserved IP // address is unique. The innermost query checks if there is at least - // one host for the given IP/subnet combination. If it not exists the - // new host is inserted. If the host with the given IP address already - // exists the new host won't be inserted. The caller can check the - // number of affected rows to detect that there was a duplicate host - // in the database. Returns the inserted host id. + // one host for the given IP/subnet combination. For checking whether + // hosts exists or not it doesn't matter if we select actual columns, + // thus SELECT 1 was used as an optimization to avoid selecting data + // that will be ignored anyway. If it does not exist the new host is + // inserted. If the host with the given IP address already exists the + // new host won't be inserted. The caller can check the number of + // affected rows to detect that there was a duplicate host in the + // database. Returns the inserted host id. {15, { OID_BYTEA, OID_INT2, OID_INT8, OID_INT8, OID_INT8, OID_VARCHAR,