Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [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: 399 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.

return: bool true
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

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.

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

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

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

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

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

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.

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

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

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

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

return: array
param: string $sql