CREATE DEFINER=`jhise`@`localhost` PROCEDURE `user_update`(in i_username char(32), in i_password char(32), in i_account_key char(32), in i_name char(50), in i_active int)
BEGIN

declare user_found boolean;

insert into app_log values (now(), concat('i_password: ', i_password));

select
	if(count(*) > 0, true, false) into user_found
from
	users
where
	account_key = i_account_key;

if user_found then
	update users set
		username = i_username,
        name = i_name,
        active = i_active
	where
		account_key = i_account_key;

    if i_password != 'd41d8cd98f00b204e9800998ecf8427e' then
		update users set
			password_hash = i_password
        where
			account_key = i_account_key;
    end if;

    select id from users where account_key = i_account_key;
else
	insert into users values
		(null, i_username, i_password, i_account_key, i_name, i_active);

    select last_insert_id() as id;
end if;

END
