From f82e5456d22af7e39a22a9a64e74072cf01e0a31 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 20:11:09 +0200 Subject: dried code by introducing selectOne for select queries with only one result line expected --- includes/model/AngelType_model.php | 8 +---- includes/model/EventConfig_model.php | 8 +---- includes/model/Message_model.php | 6 +--- includes/model/Room_model.php | 8 +---- includes/model/ShiftEntry_model.php | 13 ++------ includes/model/ShiftTypes_model.php | 8 +---- includes/model/Shifts_model.php | 18 +++-------- includes/model/UserAngelTypes_model.php | 16 ++-------- includes/model/UserDriverLicenses_model.php | 8 +---- includes/model/User_model.php | 49 ++++++----------------------- 10 files changed, 26 insertions(+), 116 deletions(-) (limited to 'includes/model') diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index f08733d5..bc535667 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -256,14 +256,8 @@ function AngelType_ids() */ function AngelType($angeltype_id) { - $angelType_source = DB::select( + return DB::selectOne( 'SELECT * FROM `AngelTypes` WHERE `id`=?', [$angeltype_id] ); - - if (empty($angelType_source)) { - return null; - } - - return array_shift($angelType_source); } diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index c5caf4d5..646d19c5 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -9,13 +9,7 @@ use Engelsystem\Database\DB; */ function EventConfig() { - $event_config = DB::select('SELECT * FROM `EventConfig` LIMIT 1'); - - if (empty($event_config)) { - return null; - } - - return array_shift($event_config); + return DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1'); } /** diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php index ebd4b37e..9bb037af 100644 --- a/includes/model/Message_model.php +++ b/includes/model/Message_model.php @@ -20,11 +20,7 @@ function Message_ids() */ function Message($message_id) { - $message_source = DB::select('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); - if (empty($message_source)) { - return null; - } - return array_shift($message_source); + return DB::selectOne('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); } /** diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 08e0f7bf..8425e5ad 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -58,17 +58,11 @@ function Room_create($name, $from_frab, $public, $number = null) */ function Room($room_id, $onlyVisible = true) { - $room_source = DB::select(' + return DB::selectOne(' SELECT * FROM `Room` WHERE `RID` = ? ' . ($onlyVisible ? 'AND `show` = \'Y\'' : ''), [$room_id] ); - - if (empty($room_source)) { - return null; - } - - return array_shift($room_source); } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 563a611f..3a7254ad 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -28,10 +28,9 @@ function ShiftEntry_new() */ function ShiftEntries_freeleaded_count() { - $result = DB::select('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); - if (!is_array($result)) { + if (empty($result)) { return 0; } @@ -129,13 +128,7 @@ function ShiftEntry_update($shift_entry) */ function ShiftEntry($shift_entry_id) { - $shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); - - if (empty($shift_entry)) { - return null; - } - - return $shift_entry[0]; + return DB::selectOne('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index 05c1a949..227df367 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -70,13 +70,7 @@ function ShiftType_create($name, $angeltype_id, $description) */ function ShiftType($shifttype_id) { - $shifttype = DB::select('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); - - if (empty($shifttype)) { - return null; - } - - return array_shift($shifttype); + return DB::selectOne('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 939a4f4e..88b28998 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -112,7 +112,7 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) */ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) { - $result = DB::select(' + return DB::selectOne(' SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, @@ -150,12 +150,6 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) $angeltype['id'] ] ); - - if (empty($result)) { - return null; - } - - return $result[0]; } /** @@ -453,13 +447,13 @@ function Shift_update($shift) */ function Shift_update_by_psid($shift) { - $shift_source = DB::select('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); + $shift_source = DB::selectOne('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); if (empty($shift_source)) { throw new Exception('Shift not found.'); } - $shift['SID'] = $shift_source[0]['SID']; + $shift['SID'] = $shift_source['SID']; return Shift_update($shift); } @@ -537,18 +531,16 @@ function Shifts_by_user($user, $include_freeload_comments = false) */ function Shift($shift_id) { - $shifts_source = DB::select(' + $result = DB::selectOne(' SELECT `Shifts`.*, `ShiftTypes`.`name` FROM `Shifts` JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) WHERE `SID`=?', [$shift_id]); - if (empty($shifts_source)) { + if (empty($result)) { return null; } - $result = $shifts_source[0]; - $shiftsEntry_source = DB::select(' SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 3ec151fb..5b0caf98 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -197,17 +197,11 @@ function UserAngelType_create($user, $angeltype) */ function UserAngelType($user_angeltype_id) { - $angeltype = DB::select(' + return DB::selectOne(' SELECT * FROM `UserAngelTypes` WHERE `id`=? LIMIT 1', [$user_angeltype_id]); - - if (empty($angeltype)) { - return null; - } - - return $angeltype[0]; } /** @@ -219,7 +213,7 @@ function UserAngelType($user_angeltype_id) */ function UserAngelType_by_User_and_AngelType($user, $angeltype) { - $angeltype = DB::select(' + return DB::selectOne(' SELECT * FROM `UserAngelTypes` WHERE `user_id`=? @@ -231,10 +225,4 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype) $angeltype['id'] ] ); - - if (empty($angeltype)) { - return null; - } - - return array_shift($angeltype); } diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index 515a2701..798aa6ab 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -45,16 +45,10 @@ function UserDriverLicense_valid($user_driver_license) */ function UserDriverLicense($user_id) { - $user_driver_license = DB::select(' + return DB::selectOne(' SELECT * FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); - - if (empty($user_driver_license)) { - return null; - } - - return array_shift($user_driver_license); } /** diff --git a/includes/model/User_model.php b/includes/model/User_model.php index f86d5bf6..4757ed3c 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -87,8 +87,7 @@ function User_update($user) */ function User_force_active_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); if (empty($result)) { return 0; @@ -102,8 +101,7 @@ function User_force_active_count() */ function User_active_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); if (empty($result)) { return 0; @@ -117,8 +115,7 @@ function User_active_count() */ function User_got_voucher_count() { - $result = DB::select('SELECT SUM(`got_voucher`) FROM `User`'); - $result = array_shift($result); + $result = DB::selectOne('SELECT SUM(`got_voucher`) FROM `User`'); if (empty($result)) { return 0; @@ -132,8 +129,7 @@ function User_got_voucher_count() */ function User_arrived_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); if (empty($result)) { return 0; @@ -147,8 +143,7 @@ function User_arrived_count() */ function User_tshirts_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); if (empty($result)) { return 0; @@ -382,13 +377,7 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de */ function User($user_id) { - $user_source = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); - - if (empty($user_source)) { - return null; - } - - return array_shift($user_source); + return DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); } /** @@ -400,13 +389,7 @@ function User($user_id) */ function User_by_api_key($api_key) { - $user = DB::select('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); - - if (empty($user)) { - return null; - } - - return $user[0]; + return DB::selectOne('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); } /** @@ -417,30 +400,18 @@ function User_by_api_key($api_key) */ function User_by_email($email) { - $user = DB::select('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); - - if (empty($user)) { - return null; - } - - return array_shift($user); + return DB::selectOne('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); } /** * Returns User by password token. * * @param string $token - * @return array|null Matching user, null or false on error + * @return array|null Matching user, null when not found */ function User_by_password_recovery_token($token) { - $user = DB::select('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); - - if (empty($user)) { - return null; - } - - return array_shift($user); + return DB::selectOne('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); } /** -- cgit v1.2.3-70-g09d2