Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

Experimental pdo database class

Copyright: 2008 Andrei Bautu
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 626 lines (22 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 2 files
 lib/dml/pdo_moodle_recordset.php
 lib/dml/moodle_database.php

Defines 1 class


Class: pdo_moodle_database  - X-Ref

Experimental pdo database class

__construct($external=false)   X-Ref
Constructor - instantiates the database, specifying if it's external (connect to other systems) or no (Moodle DB)
note this has effect to decide if prefix checks must be performed or no

param: bool true means external database used

connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null)   X-Ref
Connect to db
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 success

get_pdooptions()   X-Ref
Returns the driver-dependent connection attributes for PDO based on members stored by connect.
Must be called after $dbname, $dbhost, etc. members have been set.

return: array A key=>value array of PDO driver-specific connection options

configure_dbconnection()   X-Ref
No description

get_dblibrary()   X-Ref
Returns general database library name
Note: can be used before connect()

return: string db type pdo, native

get_name()   X-Ref
Returns localised database type name
Note: can be used before connect()

return: string

get_configuration_help()   X-Ref
Returns localised database configuration help.
Note: can be used before connect()

return: string

get_server_info()   X-Ref
Returns database server info array

return: array Array containing 'description' and 'version' info

allowed_param_types()   X-Ref
Returns supported query parameter types

return: int bitmask of accepted SQL_PARAMS_*

get_last_error()   X-Ref
Returns last error reported by database engine.

return: string error message

debug_query($sql, $params = null)   X-Ref
Function to print/save/ignore debugging messages related to SQL queries.


change_database_structure($sql, $tablenames = null)   X-Ref
Do NOT use in code, to be used by database_manager only!

param: string|array $sql query
param: array|null $tablenames an array of xmldb table names affected by this request.
return: bool true

delete_records_select($table, $select, array $params=null)   X-Ref
No description

create_recordset($sth)   X-Ref
Factory method that creates a recordset for return by a query. The generic pdo_moodle_recordset
class should fit most cases, but pdo_moodle_database subclasses can override this method to return
a subclass of pdo_moodle_recordset.

param: object $sth instance of PDOStatement
return: object instance of pdo_moodle_recordset

execute($sql, array $params=null)   X-Ref
Execute general sql query. Should be used only when no other method suitable.
Do NOT use this to make changes in db structure, use database_manager methods instead!

param: string $sql query
param: array $params query parameters
return: bool success

get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0)   X-Ref
Get a number of records as an moodle_recordset.  $sql must be a complete SQL query.
Since this method is a little less readable, use of it should be restricted to
code where it's possible there might be large datasets being returned.  For known
small datasets use get_records_sql - it leads to simpler code.

The return type is like:
param: string $sql the SQL select query to execute.
param: array $params array of sql parameters
param: int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
param: int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
return: moodle_recordset instance

get_fieldset_sql($sql, array $params=null)   X-Ref
Selects rows and return values of first column as array.

param: string $sql The SQL query
param: array $params array of sql parameters
return: array of values

get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0)   X-Ref
Get a number of records as an array of objects.

Return value is like:
param: string $sql the SQL select query to execute. The first column of this SELECT statement
param: array $params array of sql parameters
param: int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
param: int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
return: array of objects, or empty array if no records were found, or false if an error occurred.

insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false)   X-Ref
Insert new record into database, as fast as possible, no safety checks, lobs not supported.

param: string $table name
param: mixed $params data record as object or array
param: bool $returnit return it of inserted record
param: bool $bulk true means repeated inserts expected
param: bool $customsequence true if 'id' included in $params, disables $returnid
return: bool|int true or new id

insert_record($table, $dataobject, $returnid=true, $bulk=false)   X-Ref
Insert a record into a table and return the "id" field if required,
Some conversions and safety checks are carried out. Lobs are supported.
If the return ID isn't required, then this just reports success as true/false.
$data is an object containing needed data

param: string $table The database table to be inserted into
param: object|array $dataobject A data object with values for one or more fields in the record
param: bool $returnid Should the id of the newly created record entry be returned? If this option is not requested then true/false is returned.
param: bool $bulk true means repeated inserts expected
return: bool|int true or new id

update_record_raw($table, $params, $bulk=false)   X-Ref
Update record in database, as fast as possible, no safety checks, lobs not supported.

param: string $table name
param: mixed $params data record as object or array
param: bool true means repeated updates expected
return: bool success

update_record($table, $dataobject, $bulk=false)   X-Ref
Update a record in a table

$dataobject is an object containing needed data
Relies on $dataobject having a variable "id" to
specify the record to update

param: string $table The database table to be checked against.
param: object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
param: bool true means repeated updates expected
return: bool success

set_field_select($table, $newfield, $newvalue, $select, array $params=null)   X-Ref
Set a single field in every table row where the select statement evaluates to true.

param: string $table The database table to be checked against.
param: string $newfield the field to set.
param: string $newvalue the value to set the field to.
param: string $select A fragment of SQL to be used in a where clause in the SQL call.
param: array $params array of sql parameters
return: bool success

sql_concat()   X-Ref
No description

sql_concat_join($separator="' '", $elements=array()   X-Ref
No description

sql_group_concat(string $field, string $separator = ', ', string $sort = '')   X-Ref
Return SQL for performing group concatenation on given field/expression

param: string $field
param: string $separator
param: string $sort
return: string

begin_transaction()   X-Ref
No description

commit_transaction()   X-Ref
No description

rollback_transaction()   X-Ref
No description

import_record($table, $dataobject)   X-Ref
Import a record into a table, id field is required.
Basic safety checks only. Lobs are supported.

param: string $table name of database table to be inserted into
param: mixed $dataobject object or array with fields in the record
return: bool success

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

Overridden to ensure $this->lastErorr is reset each 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