class db_support

internal  
 

Wrapper for Moodle's global database object ($DB).

Provides a clean interface for database operations, allowing for better testability and isolation of Moodle's global state.

Methods

static stdClass|null
get_record(string $table, array $conditions, string $fields = '*', int $strictness = IGNORE_MISSING)

Retrieves a single database record as an object.

static mixed
get_field(string $table, string $return, array $conditions, int $strictness = IGNORE_MISSING)

Retrieves a single field value from a database record.

static mixed
get_field_sql(string $sql, array|null $params = null)

Retrieves a single field value using a custom SQL query.

static array
get_records(string $table, array $conditions = [], string $sort = '', string $fields = '*', int $limitfrom = 0, int $limitnum = 0)

Retrieves multiple database records as an array of objects.

static array
get_records_sql(string $sql, array|null $params = null, int $limitfrom = 0, int $limitnum = 0)

Retrieves records using a custom SQL query.

static int
insert_record(string $table, stdClass $dataobject, bool $returnid = true, bool $bulk = false)

Inserts a new record into a table.

static bool
update_record(string $table, stdClass $dataobject, bool $bulk = false)

Updates an existing record in a table.

static bool
delete_records(string $table, array $conditions = [])

Deletes records from a table.

static bool
record_exists(string $table, array $conditions)

Checks if a record exists in a table.

static moodle_transaction
start_delegated_transaction()

Starts a delegated database transaction.

static bool
execute(string $sql, array|null $params = null)

Executes a SQL query (for non-SELECT queries).

static string
sql_fullname(string $firstname = 'firstname', string $lastname = 'lastname')

Retrieves the SQL fragment for matching a full name.

static string
sql_like(string $field, string $param, bool $casesensitive = true, bool $accentsensitive = true, bool $notlike = false, string $escapechar = '\\')

Retrieves the SQL fragment for a LIKE clause.

static array
get_in_or_equal(mixed $items, int $type = SQL_PARAMS_NAMED, string $prefix = 'p', bool $equal = true, mixed $onemptyitems = false)

Retrieves an IN or EQUAL SQL fragment.

static array
get_records_menu(string $table, array $conditions = [], string $sort = '', string $fields = '*')

No description

static moodle_recordset
get_recordset_sql(string $sql, array|null $params = null, int $limitfrom = 0, int $limitnum = 0)

Retrieves a recordset using a custom SQL query.

static string
sql_compare_text(string $fieldname)

Retrieves the SQL fragment for comparing text fields.

static int
count_records(string $table, array $conditions = [])

Counts records in a table.

Details

at line 47
static stdClass|null get_record(string $table, array $conditions, string $fields = '*', int $strictness = IGNORE_MISSING)

Retrieves a single database record as an object.

Parameters

string $table

Table name

array $conditions

array of field => value conditions

string $fields

Comma separated list of fields (default: all)

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

stdClass|null

the record object or null if not found

Exceptions

dml_exception

at line 68
static mixed get_field(string $table, string $return, array $conditions, int $strictness = IGNORE_MISSING)

Retrieves a single field value from a database record.

Parameters

string $table

Table name

string $return

Field name to return

array $conditions

array of field => value conditions

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

mixed

the field value or false if not found

Exceptions

dml_exception

at line 85
static mixed get_field_sql(string $sql, array|null $params = null)

Retrieves a single field value using a custom SQL query.

Parameters

string $sql

the SQL query

array|null $params

array of parameters for the SQL query

Return Value

mixed

the field value or false if not found

Exceptions

dml_exception

at line 106
static array get_records(string $table, array $conditions = [], string $sort = '', string $fields = '*', int $limitfrom = 0, int $limitnum = 0)

Retrieves multiple database records as an array of objects.

Parameters

string $table

Table name

array $conditions

array of field => value conditions

string $sort

SQL sort order

string $fields

Comma separated list of fields

int $limitfrom

return records starting from this index

int $limitnum

return this many records

Return Value

array

list of record objects

Exceptions

dml_exception

at line 125
static array get_records_sql(string $sql, array|null $params = null, int $limitfrom = 0, int $limitnum = 0)

Retrieves records using a custom SQL query.

Parameters

string $sql

the SQL query

array|null $params

array of parameters for the SQL query

int $limitfrom

return records starting from this index

int $limitnum

return this many records

Return Value

array

list of record objects

Exceptions

dml_exception

at line 144
static int insert_record(string $table, stdClass $dataobject, bool $returnid = true, bool $bulk = false)

Inserts a new record into a table.

Parameters

string $table

Table name

stdClass $dataobject

the data object to insert

bool $returnid

whether to return the new record ID

bool $bulk

whether this is a bulk insert

Return Value

int

the new record ID (if $returnid is true)

Exceptions

dml_exception

at line 162
static bool update_record(string $table, stdClass $dataobject, bool $bulk = false)

Updates an existing record in a table.

Parameters

string $table

Table name

stdClass $dataobject

the data object with new values (must include 'id')

bool $bulk

whether this is a bulk update

Return Value

bool

True on success, false otherwise

Exceptions

dml_exception

at line 179
static bool delete_records(string $table, array $conditions = [])

Deletes records from a table.

Parameters

string $table

Table name

array $conditions

array of field => value conditions

Return Value

bool

True on success, false otherwise

Exceptions

dml_exception

at line 196
static bool record_exists(string $table, array $conditions)

Checks if a record exists in a table.

Parameters

string $table

Table name

array $conditions

array of field => value conditions

Return Value

bool

True if record exists, false otherwise

Exceptions

dml_exception

at line 208
static moodle_transaction start_delegated_transaction()

Starts a delegated database transaction.

Return Value

moodle_transaction

the transaction object

at line 225
static bool execute(string $sql, array|null $params = null)

Executes a SQL query (for non-SELECT queries).

Parameters

string $sql

the SQL query

array|null $params

array of parameters for the SQL query

Return Value

bool

True on success, false otherwise

Exceptions

dml_exception

at line 240
static string sql_fullname(string $firstname = 'firstname', string $lastname = 'lastname')

Retrieves the SQL fragment for matching a full name.

Parameters

string $firstname

the first name field name

string $lastname

the last name field name

Return Value

string

the SQL fragment

at line 259
static string sql_like(string $field, string $param, bool $casesensitive = true, bool $accentsensitive = true, bool $notlike = false, string $escapechar = '\\')

Retrieves the SQL fragment for a LIKE clause.

Parameters

string $field

Field name

string $param

The parameter name (e.g., :param).

bool $casesensitive

whether to be case sensitive

bool $accentsensitive

whether to be accent sensitive

bool $notlike

whether to use NOT LIKE

string $escapechar

escape character

Return Value

string

the SQL fragment

at line 283
static array get_in_or_equal(mixed $items, int $type = SQL_PARAMS_NAMED, string $prefix = 'p', bool $equal = true, mixed $onemptyitems = false)

Retrieves an IN or EQUAL SQL fragment.

Parameters

mixed $items

the items to match

int $type

the parameter type (SQL_PARAMS_NAMED or SQL_PARAMS_QM)

string $prefix

the parameter prefix

bool $equal

whether to use = or IN

mixed $onemptyitems

behavior when items is empty

Return Value

array

at line 307
static array get_records_menu(string $table, array $conditions = [], string $sort = '', string $fields = '*')

No description

Parameters

string $table
array $conditions
string $sort
string $fields

Return Value

array

at line 326
static moodle_recordset get_recordset_sql(string $sql, array|null $params = null, int $limitfrom = 0, int $limitnum = 0)

Retrieves a recordset using a custom SQL query.

Parameters

string $sql

the SQL query

array|null $params

array of parameters for the SQL query

int $limitfrom

return records starting from this index

int $limitnum

return this many records

Return Value

moodle_recordset

the recordset object

Exceptions

dml_exception

at line 340
static string sql_compare_text(string $fieldname)

Retrieves the SQL fragment for comparing text fields.

Parameters

string $fieldname

the field name

Return Value

string

the SQL fragment

at line 357
static int count_records(string $table, array $conditions = [])

Counts records in a table.

Parameters

string $table

Table name

array $conditions

array of field => value conditions

Return Value

int

the number of records

Exceptions

dml_exception