Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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: 397 lines (16 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 0 files

Defines 10 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 = null, $type, $extrainfo = null)   X-Ref
Called before each db query.

param: string $sql
param: array $params array of parameters
param: int $type type of query
param: mixed $extrainfo driver specific extra information
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