mysql custom query logging for inter-software data exchange
Ok, after some research, here's the answers I found.

Q1: Is it possible to detect the user that executed the query in MySQL?

It is possible to detect with which user by using: select user();

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_user

Beware: In triggers and events current_user() returns the user who defined the object (in SP and views unless if defined with the SQL SECURITY INVOKER). You really need to use the user() function to return the invoker in those contexts.

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user

Q2: Is it possible to fire more than one action (INSERT, UPDATE, DELETE) on the same trigger?

Most unfortunately, at the current time (2016/02/01), it is not possible to fire two actions on the same time in MySQL. The best we can do is to regroup the common coding within a stored procedure.

The following procedure is one possible way to work around this problem. It expects in input:


callingTable: The name of the table for which we want to record the query (must be in the white list).
action: Either 'DELETE', 'INSERT' or 'UPDATE'.
fieldsValues: a string containing a valid pair of field/value in XML. By example: &lt;field&gt;ID&lt;/field&gt;&lt;value&gt;1616&lt;/value&gt;

CREATE PROCEDURE `dataLinkRecorder` (
IN callingTable VARCHAR (25),
IN action ENUM('DELETE', 'INSERT', 'UPDATE'),
IN fieldsValues TEXT
)
BEGIN
DECLARE callingUser VARCHAR(25) DEFAULT '';
DECLARE fieldCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE fieldCCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE valueCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE valueCCount SMALLINT UNSIGNED DEFAULT 0;

IF (TRIM(fieldsValues) &lt;&gt; '') THEN

    /* the calling table must be one in the white list */
    SELECT CASE 
        WHEN UPPER(callingTable) IN (
            'CATEGORY',
            'CUSTOMER',
            'MAKE',
            'MODEL',
            'PRODUCT',
            'VEHICLE'
        )
        THEN TRIM(UPPER(callingTable))
        ELSE '' 
        END AS myTable
        INTO callingTable;

     SELECT TRIM(
     UPPER(
        IFNULL(USER(), 'CLIENT')
     )
     ) AS myUser
INTO callingUser;

/* do not record modifications from tier software */
IF (callingTable &lt;&gt; '' AND INSTR(callingUser, 'CLIENT') &lt; 1) THEN

    SELECT IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;field&gt;', ''))) / 7)
    , 0) AS fCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;/field&gt;', ''))) / 8)
    , 0) AS fCCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;value&gt;', ''))) / 7)
    , 0) AS vCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;/value&gt;', ''))) / 8)
    , 0) AS vCCount
    INTO fieldCount, fieldCCount, valueCount, valueCCount;

    /* validate the field/value XML pair */
    IF (fieldCount &gt; 0 AND
        valueCount &gt; 0 AND
        fieldCount = fieldCCount AND
        valueCount = valueCCount AND
        fieldCount = valueCCount
    ) THEN

        INSERT INTO `pc_datalink` 
            (`table_name`, `query_xml`) 
        VALUES(
            callingTable,
            CONCAT(
                '&lt;query&gt;',
                    '&lt;table&gt;', callingTable, '&lt;/table&gt;',
                    '&lt;action&gt;', action, '&lt;/action&gt;',
                    fieldsValues,
                '&lt;/query&gt;'
            )
        );
    END IF;
END IF;
END IF;
END //



Will insert values in the following table:

CREATE TABLE `pc_datalink` (
    `id` int(10) UNSIGNED NOT NULL,
    `table_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
    `query_xml` text COLLATE utf8_unicode_ci NOT NULL,
    `date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `AK_DATE_ADDED` (`date_added`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


By example, a very simple trigger like this one:

CREATE TRIGGER `dataLinkDeleteVehicle` AFTER DELETE ON `pc_vehicle`
FOR EACH ROW
BEGIN
IF (OLD.original_id &lt;&gt; '') THEN

    CALL dataLinkRecorder(
        'VEHICLE',
        'DELETE',
        CONCAT(
            '&lt;field&gt;ID&lt;/field&gt;&lt;value&gt;',
            OLD.original_id,
            '&lt;/value&gt;'
        )
    );
END IF; 
END //


Will generate a line in the table like this one:



Don't hesitate to ask for details if you still have questions,

Good success everyone