From a157004f4aefaa5e2265f852f9432becedee1d66 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 23 Jul 2017 11:46:54 +0200 Subject: [PATCH] handle failed db queries in Db class --- .../controller/event_config_controller.php | 6 +--- .../controller/shift_entries_controller.php | 5 +--- includes/controller/shifts_controller.php | 5 +--- includes/controller/shifttypes_controller.php | 10 ++----- includes/controller/users_controller.php | 5 +--- includes/model/AngelType_model.php | 14 +++------- includes/model/EventConfig_model.php | 2 +- includes/model/NeededAngelTypes_model.php | 5 +--- includes/model/Room_model.php | 5 +--- includes/model/ShiftEntry_model.php | 3 -- includes/model/ShiftTypes_model.php | 9 +----- includes/model/Shifts_model.php | 4 +-- includes/model/UserAngelTypes_model.php | 28 ++----------------- includes/model/UserDriverLicenses_model.php | 10 +------ includes/model/User_model.php | 12 +------- includes/pages/admin_import.php | 4 +-- includes/pages/admin_rooms.php | 4 +-- includes/pages/user_myshifts.php | 5 +--- includes/pages/user_questions.php | 6 ++-- includes/pages/user_settings.php | 6 ++-- includes/sys_auth.php | 7 +---- src/Database/Db.php | 8 +++--- 22 files changed, 35 insertions(+), 128 deletions(-) diff --git a/includes/controller/event_config_controller.php b/includes/controller/event_config_controller.php index dcdcf54a..7d11ecb4 100644 --- a/includes/controller/event_config_controller.php +++ b/includes/controller/event_config_controller.php @@ -91,7 +91,7 @@ function event_config_edit_controller() } if ($valid) { - $result = EventConfig_update( + EventConfig_update( $event_name, $buildup_start_date, $event_start_date, @@ -100,10 +100,6 @@ function event_config_edit_controller() $event_welcome_msg ); - if ($result === false) { - engelsystem_error('Unable to update event config.'); - } - engelsystem_log( 'Changed event config: $event_name, $event_welcome_msg, ' . date('Y-m-d', $buildup_start_date) . ', ' . date('Y-m-d', $event_start_date) . ', ' diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 38aad5bb..54c57332 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -130,7 +130,7 @@ function shift_entry_add_controller() } $comment = strip_request_item_nl('comment'); - $result = ShiftEntry_create([ + ShiftEntry_create([ 'SID' => $shift_id, 'TID' => $selected_type_id, 'UID' => $user_id, @@ -138,9 +138,6 @@ function shift_entry_add_controller() 'freeloaded' => $freeloaded, 'freeload_comment' => $freeload_comment ]); - if ($result === false) { - engelsystem_error('Unable to create shift entry.'); - } if ( $type['restricted'] == 0 diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 21c6e160..b3ebd18b 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -135,10 +135,7 @@ function shift_edit_controller() $shift['start'] = $start; $shift['end'] = $end; - $result = Shift_update($shift); - if ($result === false) { - engelsystem_error('Unable to update shift.'); - } + Shift_update($shift); NeededAngelTypes_delete_by_shift($shift_id); $needed_angel_types_info = []; foreach ($needed_angel_types as $type_id => $count) { diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index acdeb982..790bbb56 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -93,17 +93,13 @@ function shifttype_edit_controller() if ($valid) { if ($shifttype_id) { - $result = ShiftType_update($shifttype_id, $name, $angeltype_id, $description); - if ($result === false) { - engelsystem_error('Unable to update shifttype.'); - } + ShiftType_update($shifttype_id, $name, $angeltype_id, $description); + engelsystem_log('Updated shifttype ' . $name); success(_('Updated shifttype.')); } else { $shifttype_id = ShiftType_create($name, $angeltype_id, $description); - if ($shifttype_id === false) { - engelsystem_error('Unable to create shifttype.'); - } + engelsystem_log('Created shifttype ' . $name); success(_('Created shifttype.')); } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index b747cc83..f441c8af 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -166,10 +166,7 @@ function user_edit_vouchers_controller() if ($valid) { $user_source['got_voucher'] = $vouchers; - $result = User_update($user_source); - if ($result === false) { - engelsystem_error('Unable to update user.'); - } + User_update($user_source); success(_('Saved the number of vouchers.')); engelsystem_log(User_Nick_render($user_source) . ': ' . sprintf('Got %s vouchers', diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index d437f526..af213432 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -97,11 +97,10 @@ function AngelType_delete($angeltype) * Update Angeltype. * * @param array $angeltype The angeltype - * @return bool */ function AngelType_update($angeltype) { - $result = DB::update(' + DB::update(' UPDATE `AngelTypes` SET `name` = ?, `restricted` = ?, @@ -126,15 +125,12 @@ function AngelType_update($angeltype) $angeltype['id'], ] ); - if (is_null($result)) { - engelsystem_error('Unable to update angeltype.'); - } + engelsystem_log( 'Updated angeltype: ' . $angeltype['name'] . ($angeltype['restricted'] ? ', restricted' : '') . ($angeltype['no_self_signup'] ? ', no_self_signup' : '') . ($angeltype['requires_driver_license'] ? ', requires driver license' : '') ); - return true; } /** @@ -145,7 +141,7 @@ function AngelType_update($angeltype) */ function AngelType_create($angeltype) { - $result = DB::insert(' + DB::insert(' INSERT INTO `AngelTypes` ( `name`, `restricted`, @@ -171,9 +167,7 @@ function AngelType_create($angeltype) $angeltype['contact_email'], ] ); - if (is_null($result)) { - engelsystem_error('Unable to create angeltype.'); - } + $angeltype['id'] = DB::getPdo()->lastInsertId(); engelsystem_log( 'Created angeltype: ' . $angeltype['name'] diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index 112ad457..b5d3cc73 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -31,7 +31,7 @@ function EventConfig() * @param int $event_end_date * @param int $teardown_end_date * @param string $event_welcome_msg - * @return bool + * @return int Rows updated */ function EventConfig_update( $event_name, diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 97b085f0..e77c715f 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -17,7 +17,7 @@ use Engelsystem\Database\DB; */ function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) { - $result = DB::insert(' + DB::insert(' INSERT INTO `NeededAngelTypes` ( `shift_id`, `angel_type_id`, `room_id`, `count`) VALUES (?, ?, ?, ?) ', @@ -27,9 +27,6 @@ function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) $room_id, $count, ]); - if ($result === false) { - return false; - } return DB::getPdo()->lastInsertId(); } diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 17617b39..fdd9dddc 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -35,7 +35,7 @@ function Room_delete($room_id) */ function Room_create($name, $from_frab, $public, $number = null) { - $result = DB::insert(' + DB::insert(' INSERT INTO `Room` (`Name`, `FromPentabarf`, `show`, `Number`) VALUES (?, ?, ?, ?) ', @@ -46,9 +46,6 @@ function Room_create($name, $from_frab, $public, $number = null) (int)$number, ] ); - if (!$result) { - return false; - } return DB::getPdo()->lastInsertId(); } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index d7810feb..3a282efc 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -102,7 +102,6 @@ function ShiftEntry_create($shift_entry) * Update a shift entry. * * @param array $shift_entry - * @return bool */ function ShiftEntry_update($shift_entry) { @@ -120,8 +119,6 @@ function ShiftEntry_update($shift_entry) $shift_entry['id'] ] ); - - return (DB::getStm()->errorCode() == '00000'); } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index 96a823d4..12fe38be 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -20,7 +20,6 @@ function ShiftType_delete($shifttype_id) * @param string $name * @param int $angeltype_id * @param string $description - * @return bool */ function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) { @@ -38,8 +37,6 @@ function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) $shifttype_id, ] ); - - return DB::getStm()->errorCode() == '00000'; } /** @@ -52,7 +49,7 @@ function ShiftType_update($shifttype_id, $name, $angeltype_id, $description) */ function ShiftType_create($name, $angeltype_id, $description) { - $result = DB::insert(' + DB::insert(' INSERT INTO `ShiftTypes` (`name`, `angeltype_id`, `description`) VALUES(?, ?, ?) ', @@ -63,10 +60,6 @@ function ShiftType_create($name, $angeltype_id, $description) ] ); - if ($result === false) { - return false; - } - return DB::getPdo()->lastInsertId(); } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 21abc888..6e69fe35 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -436,7 +436,7 @@ function Shift_delete($shift_id) * Update a shift. * * @param array $shift - * @return bool + * @return int Updated row count */ function Shift_update($shift) { @@ -444,7 +444,7 @@ function Shift_update($shift) $shift['name'] = ShiftType($shift['shifttype_id'])['name']; mail_shift_change(Shift($shift['SID']), $shift); - return (bool)DB::update(' + return DB::update(' UPDATE `Shifts` SET `shifttype_id` = ?, `start` = ?, diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 82f390ee..f8277fd9 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -110,22 +110,15 @@ function User_is_AngelType_supporter(&$user, $angeltype) * * @param int $user_angeltype_id * @param bool $supporter - * @return int */ function UserAngelType_update($user_angeltype_id, $supporter) { - $result = DB::update(' + DB::update(' UPDATE `UserAngelTypes` SET `supporter`=? WHERE `id`=? LIMIT 1 ', [$supporter, $user_angeltype_id]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to update supporter rights.'); - } - - return $result; } /** @@ -154,22 +147,15 @@ function UserAngelTypes_delete_all($angeltype_id) * * @param int $angeltype_id * @param array $confirm_user - * @return bool */ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { - $result = DB::update(' + DB::update(' UPDATE `UserAngelTypes` SET `confirm_user_id`=? WHERE `angeltype_id`=? AND `confirm_user_id` IS NULL ', [$confirm_user['UID'], $angeltype_id]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to confirm all users.'); - } - - return (bool)$result; } /** @@ -181,15 +167,11 @@ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) */ function UserAngelType_confirm($user_angeltype_id, $confirm_user) { - $result = DB::update(' + DB::update(' UPDATE `UserAngelTypes` SET `confirm_user_id`=? WHERE `id`=? LIMIT 1', [$confirm_user['UID'], $user_angeltype_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to confirm user angeltype.'); - } - return (bool)$result; } /** @@ -225,10 +207,6 @@ function UserAngelType_create($user, $angeltype) ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to create user angeltype.'); - } - return DB::getPdo()->lastInsertId(); } diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index 525d2016..ee93ac95 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -94,9 +94,6 @@ function UserDriverLicenses_create($user_driver_license, $user) (bool)$user_driver_license['has_license_forklift'], ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to create user driver license'); - } return $user_driver_license; } @@ -105,11 +102,10 @@ function UserDriverLicenses_create($user_driver_license, $user) * Update a user's driver license entry * * @param array $user_driver_license The UserDriverLicense to update - * @return bool */ function UserDriverLicenses_update($user_driver_license) { - $result = DB::update(' + DB::update(' UPDATE `UserDriverLicenses` SET `has_car`=?, @@ -130,10 +126,6 @@ function UserDriverLicenses_update($user_driver_license) $user_driver_license['user_id'], ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to update user driver license information'); - } - return $result; } /** diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 2913c1a1..9f767b74 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -24,11 +24,10 @@ function User_delete($user_id) * Update user. * * @param array $user - * @return bool */ function User_update($user) { - return (bool)DB::update(' + DB::update(' UPDATE `User` SET `Nick`=?, `Name`=?, @@ -481,7 +480,6 @@ function User_by_password_recovery_token($token) * * @param array $user * @param bool $log - * @return bool */ function User_reset_api_key(&$user, $log = true) { @@ -497,15 +495,10 @@ function User_reset_api_key(&$user, $log = true) $user['UID'] ] ); - if (DB::getStm()->errorCode() != '00000') { - return false; - } if ($log) { engelsystem_log(sprintf('API key resetted (%s).', User_Nick_render($user))); } - - return true; } /** @@ -528,9 +521,6 @@ function User_generate_password_recovery_token(&$user) $user['UID'], ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to generate password recovery token.'); - } engelsystem_log('Password recovery for ' . User_Nick_render($user) . ' started.'); return $user['password_recovery_token']; } diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index 3cbed9f9..959a9d2e 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -251,9 +251,7 @@ function admin_import() list($rooms_new, $rooms_deleted) = prepare_rooms($import_file); foreach ($rooms_new as $room) { $result = Room_create($room, true, true); - if ($result === false) { - engelsystem_error('Unable to create room.'); - } + $rooms_import[trim($room)] = $result; } foreach ($rooms_deleted as $room) { diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 3045242b..ad8eab83 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -147,9 +147,7 @@ function admin_rooms() ); } else { $room_id = Room_create($name, $from_pentabarf, $public, $number); - if ($room_id === false) { - engelsystem_error('Unable to create room.'); - } + engelsystem_log( 'Room created: ' . $name . ', pentabarf import: ' diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index 14b5b8ee..94116fc9 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -93,15 +93,12 @@ function user_myshifts() $user_source = User($shift['UID']); if ($valid) { - $result = ShiftEntry_update([ + ShiftEntry_update([ 'id' => $user_id, 'Comment' => $comment, 'freeloaded' => $freeloaded, 'freeload_comment' => $freeload_comment ]); - if ($result === false) { - engelsystem_error('Unable to update shift entry.'); - } engelsystem_log( 'Updated ' . User_Nick_render($user_source) . '\'s shift ' . $shift['name'] diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index fdf76aee..e90ea011 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -39,15 +39,13 @@ function user_questions() case 'ask': $question = strip_request_item_nl('question'); if ($question != '') { - $result = DB::insert(' + DB::insert(' INSERT INTO `Questions` (`UID`, `Question`) VALUES (?, ?) ', [$user['UID'], $question] ); - if (!$result) { - engelsystem_error(_('Unable to save question.')); - } + success(_('You question was saved.')); redirect(page_link_to('user_questions')); } else { diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index 667e73d9..7edee7b5 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -84,6 +84,7 @@ function user_settings_main($user_source, $enable_tshirt_size, $tshirt_sizes) if ($valid) { User_update($user_source); + success(_('Settings saved.')); redirect(page_link_to('user_settings')); } @@ -108,10 +109,9 @@ function user_settings_password($user_source) error(_('Your password is to short (please use at least 6 characters).')); } elseif ($request->post('new_password') != $request->post('new_password2')) { error(_('Your passwords don\'t match.')); - } elseif (set_password($user_source['UID'], $request->post('new_password'))) { - success(_('Password saved.')); } else { - error(_('Failed setting password.')); + set_password($user_source['UID'], $request->post('new_password')); + success(_('Password saved.')); } redirect(page_link_to('user_settings')); } diff --git a/includes/sys_auth.php b/includes/sys_auth.php index 856ed4ab..f1ec3192 100644 --- a/includes/sys_auth.php +++ b/includes/sys_auth.php @@ -55,11 +55,10 @@ function generate_salt($length = 16) * * @param int $uid * @param string $password - * @return bool */ function set_password($uid, $password) { - $result = DB::update(' + DB::update(' UPDATE `User` SET `Passwort` = ?, `password_recovery_token`=NULL @@ -71,10 +70,6 @@ function set_password($uid, $password) $uid ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to update password.'); - } - return $result; } /** diff --git a/src/Database/Db.php b/src/Database/Db.php index 4116ffda..46edc96b 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -85,13 +85,13 @@ class Db * * @param string $query * @param array $bindings - * @return int|bool + * @return int Row count */ public static function insert($query, array $bindings = []) { self::query($query, $bindings); - return (self::$lastStatus ? self::$stm->rowCount() : false); + return self::$stm->rowCount(); } /** @@ -99,13 +99,13 @@ class Db * * @param string $query * @param array $bindings - * @return int|bool + * @return int */ public static function update($query, array $bindings = []) { self::query($query, $bindings); - return (self::$lastStatus ? self::$stm->rowCount() : false); + return self::$stm->rowCount(); } /**