php - PDO pgsql - Count the rows affected by pgsql function -
i have following function:
create function user_delete( in id int4 ) returns void $body$ begin select * "user" user_id = id update; delete user_role user_id = id; delete user_permission user_id = id; delete permission_cache user_id = id; delete access user_id = id; delete "user" user_id = id; end; $body$ language plpgsql volatile;
i use php pdo:
$stmt = $pdo->prepare('select * user_delete(?)'); $stmt->execute(array($user['id']));
the result contains now
array( array('user_delete' => '') )
and the
$stmt->rowcount();
is one.
is possible fix this: function return nothing (because void), , rowcount return count of affected rows?
solution:
php:
public function delete($id) { try { $this->__call('user_delete', array($id)); } catch (\pdoexception $e) { if ($e->getcode() === 'ue404') throw new notfoundexception(); else throw $e; } }
sql:
create function user_delete( in id int4 ) returns void $body$ begin delete user_role user_id = id; delete user_permission user_id = id; delete permission_cache user_id = id; delete access user_id = id; delete "user" user_id = id; if not found raise sqlstate 'ue404' using message = 'not found delete'; end if; end; $body$ language plpgsql volatile;
i can achieve return 0 length result setof void return type, not necessary if force throw pdoexception when resource not found...
you can use
get diagnostics integer_var = row_count;
.. , let function return count. details in manual here.
example:
create function x.user_delete(id int, out del_ct int) returns int $func$ declare int; -- helper var begin select * "user" user_id = id update; delete user_role user_id := id; diagnostics = row_count; del_ct = i; delete user_permission user_id = id; diagnostics = row_count; del_ct := del_ct + i; delete permission_cache user_id = id; diagnostics = row_count; del_ct := del_ct + i; delete access user_id = id; diagnostics = row_count; del_ct := del_ct + i; delete "user" user_id = id; diagnostics = row_count; del_ct := del_ct + i; end $func$ language plpgsql;
Comments
Post a Comment