I currently develop a token reader (RFID) service for, belive it or not, my own pleasure and knowledge. I speak french better than english, so please appologies. Google translate is my friend, but...
My project is really simple:
- RFID Reader buyed on Wish
- An HTML webpage receiving the RFID code read
An Ajax Query making a SELECT on my table t_rfids codes to retrieve the attached user
IF the_users_id IS NOT NULL INSERT t_accesslogs entry.
This is the table structure:
CREATE TABLE test_punch.t_accesslogs (
id_tokenaccesslog bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
punchtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
id_ref_user bigint(20) UNSIGNED NOT NULL,
PRIMARY KEY (id_tokenaccesslog)
)
ENGINE = INNODB,
CHARACTER SET latin1,
COLLATE latin1_swedish_ci;
So by making a simple INSERT INTO t_accesslogs (id_ref_user) VALUES (3); i got my entry for the CURRENT_TIMESTAMP.
Everything work well but I try to optimize the accesslogs into a timecard entry with a date, time_start, time_end. Table structure:
CREATE TABLE test_punch.t_timecards (
id_timecard bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
id_ref_user bigint(20) UNSIGNED NOT NULL,
day_date date NOT NULL,
time_begin time NOT NULL,
time_end time DEFAULT NULL,
PRIMARY KEY (id_timecard)
)
ENGINE = INNODB,
CHARACTER SET latin1,
COLLATE latin1_swedish_ci;
I try to figure out the best way to fillup this tables without using outside codes, so I give a try to the Triggers. There is the trigger code:
CREATE
DEFINER = 'root'@'localhost'
TRIGGER test_punch.add_timecard_entry
AFTER INSERT
ON test_punch.t_accesslogs
FOR EACH ROW
BEGIN
DECLARE bint_IdTimeCard BIGINT;
DECLARE dt_Punch DATE;
DECLARE time_Start TIME;
DECLARE time_End TIME;
SELECT t_timecards.id_timecard INTO bint_IdTimeCard
FROM t_timecards
WHERE t_timecards.id_ref_user = NEW.id_ref_user
AND day_date = CAST(NEW.punchtime as DATE)
AND time_begin IS NOT NULL
AND time_end IS NULL;
IF (bint_IdTimeCard IS NOT NULL) THEN
UPDATE t_timecards
SET t_timecards.time_end = CAST(NEW.punchtime AS TIME)
WHERE t_timecards.id_timecard=bint_IdTimeCard
AND t_timecards.id_ref_user=NEW.id_ref_user;
ELSE
INSERT INTO t_timecards (id_ref_user,day_date,time_begin)
VALUES (NEW.id_ref_user,
CAST(NEW.punchtime AS DATE),
CAST(NEW.punchtime AS TIME));
END IF;
END
The first thing I'm not sure is the value returned if the SELECT value is Null. it is a real NULL or Empty? Like said earlier, hard to debug even I use dbForge Studio (see Debugging MySQL Triggers)
Actually the code behavior didn't looks like to handle correctly the IF statement. Any idea about what I'm doing wrong?
Any help will be appreciated.
Thank you
Martin
bint_IdTimeCardvariable anywhere... Edited never mind i readed overSELECT t_timecards.id_timecard INTO bint_IdTimeCardDECLARE bint_IdTimeCard BIGINT UNSIGNED DEFAULT 0andIF (bint_IdTimeCard > 0) THENinstead ..SELECT t_timecards.id_timecard INTO bint_IdTimeCard FROM t_timecards WHERE t_timecards.id_ref_user = NEW.id_ref_user AND day_date = CAST(NEW.punchtime as DATE) AND time_begin IS NOT NULL AND time_end IS NULLbecause there can be only one record in a a MySQL variable.