Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

Trait that adds read-only slave connection capability

Copyright: 2018 Srdjan Janković, Catalyst IT
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 406 lines (16 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 0 files

Defines 11 functions


Functions that are not part of a class:

connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions = null)   X-Ref
Connect to db
The connection parameters processor that sets up stage for master write and slave readonly handles.
Must be called before other methods.

param: string $dbhost The database host.
param: string $dbuser The database username.
param: string $dbpass The database username's password.
param: string $dbname The name of the database being connected to.
param: mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
param: array $dboptions driver specific options
return: bool true

set_dbhwrite()   X-Ref
Set database handle to readwrite master
Will connect if required. Calls set_db_handle()

return: void

want_read_slave()   X-Ref
Returns whether we want to connect to slave database for read queries.

return: bool Want read only connection

perf_get_reads_slave()   X-Ref
Returns the number of reads done by the read only database.

return: int Number of reads.

start_delegated_transaction()   X-Ref
On DBs that support it, switch to transaction mode and begin a transaction

return: moodle_transaction

query_start($sql, ?array $params, $type, $extrainfo = null)   X-Ref
Called before each db query.

param: string $sql
param: array|null $params An array of parameters.
param: int $type type of query
param: mixed $extrainfo driver specific extra information
return: void

query_end($result)   X-Ref
This should be called immediately after each db query. It does a clean up of resources.

param: mixed $result The db specific result obtained from running a query.
return: void

select_db_handle(int $type, string $sql)   X-Ref
Select appropriate db handle - readwrite or readonly

param: int $type type of query
param: string $sql
return: void

can_use_readonly(int $type, string $sql)   X-Ref
Check if The query qualifies for readonly connection execution
Logging queries are exempt, those are write operations that circumvent
standard query_start/query_end paths.

param: int $type type of query
param: string $sql
return: bool

commit_delegated_transaction(moodle_transaction $transaction)   X-Ref
Indicates delegated transaction finished successfully.
Set written times after outermost transaction finished

param: moodle_transaction $transaction The transaction to commit
return: void

table_names(string $sql)   X-Ref
Parse table names from query

param: string $sql
return: array