handle failed db queries in Db class

This commit is contained in:
msquare 2017-07-23 11:46:54 +02:00
parent 36446dd644
commit a157004f4a
22 changed files with 35 additions and 128 deletions

View File

@ -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) . ', '

View File

@ -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

View File

@ -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) {

View File

@ -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.'));
}

View File

@ -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',

View File

@ -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']

View File

@ -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,

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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');
}
/**

View File

@ -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();
}

View File

@ -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` = ?,

View File

@ -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();
}

View File

@ -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;
}
/**

View File

@ -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'];
}

View File

@ -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) {

View File

@ -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: '

View File

@ -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']

View File

@ -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 {

View File

@ -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'));
}

View File

@ -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;
}
/**

View File

@ -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();
}
/**