Ola
What is an audit trail? As per Wikipedia...
An audit trail (also called audit log) is a security-relevant chronological record, set of records, and/or destination and source of records that provide documentary evidence of the sequence of activities that have affected at any time a specific operation, procedure, or event.[1][2] Audit records typically result from activities such as financial transactions,[3] scientific research and health care data transactions,[4] or communications by individual people, systems, accounts, or other entities.
The process that creates an audit trail is typically required to always run in a privileged mode, so it can access and supervise all actions from all users; a normal user should not be allowed to stop/change it. Furthermore, for the same reason, trail file or database table with a trail should not be accessible to normal users.
What is an SQLite Trigger?
An SQLite trigger is a named database object that is executed automatically when an INSERT, UPDATE, or DELETE statement is issued against the associated table. You often use triggers to enable sophisticated auditing. For example, you want to log the changes of the sensitive data such as salary, address, etc., whenever it changes. In addition, you use triggers to enforce complex business rules centrally at the database level and prevent invalid transactions.
The purpose of this tutorial?
This tutorial is a step by step process of how one can implement audit trails for changes in their tables, whether INSERT,UPDATE,DELETE statements as explained below. I will attempt to use TRIGGERS to achieve this.
SQLite Triggers have an OLD and a NEW object. The OLD object will store the values of the fields per record before any changes are made and NEW will store the updated field values.
Assumptions:
1. a database table exists that need to be audited
2. a clone of this table can be used to keep track of audit related transactions
3. each time a record is INSERTED, UPDATED and DELETED, the cloned table will be updated
There are about 9 variants of triggers that can be created using the events.
These are translated as
Inserts
Updates
Deletes
The Subs below will use methods already in DBUtils. These will take care of
1. creating a clone of an existing table and make the cloned table ready for audit inserts
2. create the triggers that will be fired each time INSERT,UPDATE and DELETE on the master records happen. Here we go...
Step 1:
For each table that we need to audit, we need to create a clone. We can create a clone that will have old_ and new_ values for EACH ROW.
To begin, we need to get the structure of an existing table. I have defined a GetStructure method to do this. This returns a map from a PRAGMA statement to get the name of each name and type of column.
Step 2:
We have the structure of the table to audit read from the database, we need to create a new table with the same structure but having prefixes for each field. For each field in the table to be audited we will have two fields old_xxx and new_xxx
To be continued...
What is an audit trail? As per Wikipedia...
An audit trail (also called audit log) is a security-relevant chronological record, set of records, and/or destination and source of records that provide documentary evidence of the sequence of activities that have affected at any time a specific operation, procedure, or event.[1][2] Audit records typically result from activities such as financial transactions,[3] scientific research and health care data transactions,[4] or communications by individual people, systems, accounts, or other entities.
The process that creates an audit trail is typically required to always run in a privileged mode, so it can access and supervise all actions from all users; a normal user should not be allowed to stop/change it. Furthermore, for the same reason, trail file or database table with a trail should not be accessible to normal users.
What is an SQLite Trigger?
An SQLite trigger is a named database object that is executed automatically when an INSERT, UPDATE, or DELETE statement is issued against the associated table. You often use triggers to enable sophisticated auditing. For example, you want to log the changes of the sensitive data such as salary, address, etc., whenever it changes. In addition, you use triggers to enforce complex business rules centrally at the database level and prevent invalid transactions.
The purpose of this tutorial?
This tutorial is a step by step process of how one can implement audit trails for changes in their tables, whether INSERT,UPDATE,DELETE statements as explained below. I will attempt to use TRIGGERS to achieve this.
SQLite Triggers have an OLD and a NEW object. The OLD object will store the values of the fields per record before any changes are made and NEW will store the updated field values.
- For INSERTS, only the NEW object is valid
- FOR UPDATES, the OLD and NEW objects are available
- FOR DELETE, only the OLD object is valid
Assumptions:
1. a database table exists that need to be audited
2. a clone of this table can be used to keep track of audit related transactions
3. each time a record is INSERTED, UPDATED and DELETED, the cloned table will be updated
There are about 9 variants of triggers that can be created using the events.
B4X:
Public const TRIGGER_WHEN_BEFORE As String = "BEFORE"
Public const TRIGGER_WHEN_AFTER As String = "AFTER"
Public const TRIGGER_WHEN_INSTEADOF As String = "INSTEAD OF"
Public const TRIGGER_EVENT_INSERT As String = "INSERT"
Public const TRIGGER_EVENT_UPDATE As String = "UPDATE"
Public const TRIGGER_EVENT_DELETE As String = "DELETE"
These are translated as
Inserts
- BEFORE INSERT
- AFTER INSERT
- INSTEAD OF INSERT
Updates
- BEFORE UPDATE
- AFTER UPDATE
- INSTEAD OF UPDATE
Deletes
- BEFORE DELETE
- AFTER DELETE
- INSTEAD OF DELETE
The Subs below will use methods already in DBUtils. These will take care of
1. creating a clone of an existing table and make the cloned table ready for audit inserts
2. create the triggers that will be fired each time INSERT,UPDATE and DELETE on the master records happen. Here we go...
Step 1:
For each table that we need to audit, we need to create a clone. We can create a clone that will have old_ and new_ values for EACH ROW.
To begin, we need to get the structure of an existing table. I have defined a GetStructure method to do this. This returns a map from a PRAGMA statement to get the name of each name and type of column.
B4X:
'get the table structure from the pragma statement
Sub GetTableStructure(jSQL As SQL, tblName As String) As Map
Dim fld As Map
fld.Initialize
Dim fields As List = ExecuteMaps(jSQL,"PRAGMA table_info ('" & tblName & "')",Null)
For Each fldm As Map In fields
Dim fldname As String = fldm.GetDefault("name","")
Dim fldtype As String = fldm.GetDefault("type","")
fldname = fldname.tolowercase
fld.put(fldname,fldtype)
Next
Return fld
End Sub
Step 2:
We have the structure of the table to audit read from the database, we need to create a new table with the same structure but having prefixes for each field. For each field in the table to be audited we will have two fields old_xxx and new_xxx
To be continued...
Attachments
Last edited: