Skip to content

Commit a13e994

Browse files
committed
Update documentation about user defined variables.
1 parent 6cbe7e2 commit a13e994

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

docs/audit-config-file.rst

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Recording the state of the row.
281281
Example 4: Database Session
282282
:::::::::::::::::::::::::::
283283

284-
Recording the database session (a single connection by a client). See :ref:`additional-sql-section` for setting the variable ``@audit_uuid``.
284+
Recording the database session (a single connection from your PHP application to the MySQL instance). See :ref:`additional-sql-section` for setting the `user defined variable <https://mariadb.com/kb/en/user-defined-variables/>`_ ``@audit_uuid`` in MySQL.
285285

286286
.. code-block:: json
287287
@@ -292,6 +292,11 @@ Recording the database session (a single connection by a client). See :ref:`addi
292292
"column_type": "bigint(20) unsigned not null",
293293
"expression": "@audit_uuid"
294294
}
295+
],
296+
"additional_sql": [
297+
"if (@audit_uuid is null) then",
298+
" set @audit_uuid = uuid_short();",
299+
"end if;",
295300
]
296301
}
297302
@@ -300,7 +305,7 @@ Recording the database session (a single connection by a client). See :ref:`addi
300305
Example 5: Order
301306
::::::::::::::::
302307

303-
Recording the order of the data changes. See :ref:`additional-sql-section` for setting the variable ``@audit_rownum``.
308+
Recording the order of the data changes. See :ref:`additional-sql-section` for setting the `user defined variable <https://mariadb.com/kb/en/user-defined-variables/>`_ ``@audit_rownum`` in MySQL.
304309

305310
.. code-block:: json
306311
@@ -311,6 +316,9 @@ Recording the order of the data changes. See :ref:`additional-sql-section` for s
311316
"column_type": "int(10) unsigned not null",
312317
"expression": "@audit_rownum"
313318
}
319+
],
320+
"additional_sql": [
321+
"set @audit_rownum = ifnull(@audit_rownum, 0) + 1;"
314322
]
315323
}
316324
@@ -345,17 +353,17 @@ Recording the session ID. This example is useful tracking data changes made in m
345353
{
346354
"column_name": "audit_ses_id",
347355
"column_type": "int(10) unsigned",
348-
"expression": "@abc_g_ses_id"
356+
"expression": "@audit_ses_id"
349357
}
350358
]
351359
}
352360
353-
When retrieving the session you must set the variable MySQL ``@abc_g_ses_id`` in your web application.
361+
When retrieving the session you must set the `user defined variable <https://mariadb.com/kb/en/user-defined-variables/>`_ ``@audit_ses_id`` in MySQL from your PHP application. See :ref:`setting-user-defined-variables-in-mysql` for examples of setting `user defined variables <https://mariadb.com/kb/en/user-defined-variables/>`_ in MySQL.
354362

355363
Example 8: End User
356364
:::::::::::::::::::
357365

358-
Recording the user ID. This example is useful recording the end user who has modified the data in your (web) application.
366+
Recording the user ID. This example is useful for recording the end user who has modified the data using your PHP application.
359367

360368
.. code-block:: json
361369
@@ -364,12 +372,12 @@ Recording the user ID. This example is useful recording the end user who has mod
364372
{
365373
"column_name": "audit_usr_id",
366374
"column_type": "int(10) unsigned",
367-
"expression": "@abc_g_usr_id"
375+
"expression": "@audit_usr_id"
368376
}
369377
]
370378
}
371379
372-
When retrieving the session and when signing in you must set the variable MySQL ``@abc_g_usr_id`` in your (web) application.
380+
When retrieving the session and when signing in or off you must set the `user defined variable <https://mariadb.com/kb/en/user-defined-variables/>`_ ``@audit_usr_id`` in MySQL from your PHP application. See :ref:`setting-user-defined-variables-in-mysql` for examples of setting `user defined variables <https://mariadb.com/kb/en/user-defined-variables/>`_ in MySQL.
373381

374382
.. _additional-sql-section:
375383

@@ -381,7 +389,7 @@ The additional SQL section specifies additional SQL statements that are placed a
381389
Example
382390
```````
383391

384-
This example show how to set the variables ``@audit_uuid`` and ``@audit_rownum`` mentioned in :ref:`example_database_session` and :ref:`example_order`.
392+
This example show how to set the variables ``@audit_uuid`` and ``@audit_rownum`` mentioned in sections :ref:`example_database_session` and :ref:`example_order`.
385393

386394
.. code-block:: json
387395
@@ -450,21 +458,20 @@ An audit trail will be recorded for table ``FOO_USER``.
450458
"FOO_USER": {
451459
"audit": true,
452460
"alias": "usr",
453-
"skip": "@g_skip_foo_user"
461+
"skip": "@audit_skip_foo_user"
454462
}
455463
}
456464
}
457465
458-
When MySQL variable ``@g_skip_foo_user`` no audit triggers will record a data change. In the SQL code below updating column ``usr_last_login`` will not be recorded.
466+
When `user defined variable <https://mariadb.com/kb/en/user-defined-variables/>`_ ``@audit_skip_foo_user`` in MySQL is set no audit triggers will record data changes. In the SQL code below updating column ``usr_last_login`` will not be recorded.
459467

460468
.. code-block:: sql
461469
462-
set @g_skip_foo_user = 1;
470+
set @audit_skip_foo_user = 1;
463471
464472
update FOO_USER
465473
set usr_last_login = now()
466474
where usr_id = p_usr_id
467475
;
468476
469-
set @g_skip_foo_user = null;
470-
477+
set @audit_skip_foo_user = null;

docs/example.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ Below is the code for the update statement (the code for the other triggers look
6464
end if;
6565
set @audit_rownum = ifnull(@audit_rownum, 0) + 1;
6666
insert into `nahouw_audit`.`NAH_TOURNAMENT`(audit_timestamp,audit_type,audit_state,audit_uuid,rownum,audit_ses_id,audit_usr_id,trn_id,trn_name)
67-
values(now(),'UPDATE','OLD',@audit_uuid,@audit_rownum,@abc_g_ses_id,@abc_g_usr_id,OLD.`trn_id`,OLD.`trn_name`);
67+
values(now(),'UPDATE','OLD',@audit_uuid,@audit_rownum,@audit_ses_id,@audit_usr_id,OLD.`trn_id`,OLD.`trn_name`);
6868
insert into `nahouw_audit`.`NAH_TOURNAMENT`(audit_timestamp,audit_type,audit_state,audit_uuid,rownum,audit_ses_id,audit_usr_id,trn_id,trn_name)
69-
values(now(),'UPDATE','NEW',@audit_uuid,@audit_rownum,@abc_g_ses_id,@abc_g_usr_id,NEW.`trn_id`,NEW.`trn_name`);
69+
values(now(),'UPDATE','NEW',@audit_uuid,@audit_rownum,@audit_ses_id,@audit_usr_id,NEW.`trn_id`,NEW.`trn_name`);
7070
end
7171
7272
.. _Nahouw: https://www.nahouw.net

docs/miscellaneous.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ If your application is querying on tables in the ``audit schema`` you are free t
4242

4343
Be careful with unique indexes. A key of a table in the ``data schema`` will (very likely) not be a key of the corresponding table in the ``audit schema``.
4444

45+
.. _setting-user-defined-variables-in-mysql:
46+
47+
Setting User Defined Variables in MySQL
48+
---------------------------------------
49+
50+
There are several ways for setting user defined variables in MySQL from your PHP application. In this section we discuss two methods. More information about user defined variables in MySQL can be found at `https://mariadb.com/kb/en/user-defined-variables/ <https://mariadb.com/kb/en/user-defined-variables/>`_ and `https://dev.mysql.com/doc/refman/8.0/en/user-variables.html <https://dev.mysql.com/doc/refman/8.0/en/user-variables.html>`_
51+
52+
Explicit Query From PHP
53+
```````````````````````
54+
55+
The PHP snippet below is an example of setting a user defined variable in MySQL from a PHP application.
56+
57+
.. code-block:: PHP
58+
59+
// User has signed in and variable $usrId holds the ID of the user and
60+
// $mysql is the connection to MySQL.
61+
$mysql->real_query(sprintf('set @audit_usr_id = %s', $usrId ?? 'null'));
62+
63+
Implicit in SQL Query
64+
`````````````````````
65+
66+
The SQL statement below is an example of setting user defined variables in MySQL in a SQL statement (in this example session data is stored in table `FOO_SESSION`).
67+
68+
.. code-block:: php
69+
70+
select @audit_ses_id := ses_id
71+
, @audit_usr_id := usr_id
72+
, ses_data
73+
from FOO_SESSION
74+
where ses_token = 'the-long-token-stored-in-the-session-cookie-of-the-user-agent'
75+
;
76+
4577
Limitations
4678
-----------
4779

0 commit comments

Comments
 (0)