Skip to content

Commit

Permalink
MDL-65168 dml: Move preload SQL functions to self-contained class
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and stronk7 committed Mar 25, 2019
1 parent e5a501d commit 71cbc05
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 224 deletions.
133 changes: 133 additions & 0 deletions lib/classes/dml/table.php
@@ -0,0 +1,133 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Helpers and methods relating to DML tables.
*
* @since Moodle 3.7
* @package core
* @category dml
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\dml;

use stdClass;

defined('MOODLE_INTERNAL') || die();

/**
* Helpers and methods relating to DML tables.
*
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class table {

/** @var string Name of the table that this class represents */
protected $tablename;

/** @var string Table alias */
protected $tablealias;

/** @var string Prefix to place before each field */
protected $fieldprefix;

/** @var array List of fields */
protected $fields;

/**
* Constructor for the table class.
*
* @param string $tablename The name of the table that this instance represents.
* @param string $tablealias The alias to use when selecting the table
* @param string $fieldprefix The prefix to use when selecting fields.
*/
public function __construct(string $tablename, string $tablealias, string $fieldprefix) {
$this->tablename = $tablename;
$this->tablealias = $tablealias;
$this->fieldprefix = $fieldprefix;
}

/**
* Get the from TABLE ALIAS part of the FROM/JOIN string.
*
* @return string
*/
public function get_from_sql() : string {
return "{{$this->tablename}} {$this->tablealias}";
}

/**
* Get the list of fields in a table for use in preloading fields.
*
* @return array The list of columns in a table. The array key is the column name with an applied prefix.
*/
protected function get_fieldlist() : array {
global $DB;

if (null === $this->fields) {
$fields = [];
foreach (array_keys($DB->get_columns($this->tablename)) as $fieldname) {
$fields["{$this->fieldprefix}{$fieldname}"] = $fieldname;
}

$this->fields = $fields;
}

return $this->fields;
}

/**
* Get the SELECT SQL to select a set of columns for this table.
*
* This function is intended to be used in combination with extract_from_result().
*
* @return string The SQL to use in the SELECT
*/
public function get_field_select() : string {
$fieldlist = $this->get_fieldlist();

return implode(', ', array_map(function($fieldname, $fieldalias) {
return "{$this->tablealias}.{$fieldname} AS {$fieldalias}";
}, $fieldlist, array_keys($fieldlist)));
}

/**
* Extract fields from the specified result. The fields are removed from the original object.
*
* This function is intended to be used in combination with get_field_select().
*
* @param stdClass $result The result retrieved from the database with fields to be extracted
* @return stdClass The extracted result
*/
public function extract_from_result(stdClass $result) : stdClass {
$record = new stdClass();

$fieldlist = $this->get_fieldlist();
foreach ($fieldlist as $fieldalias => $fieldname) {
if (property_exists($result, $fieldalias)) {
$record->$fieldname = $result->$fieldalias;
unset($result->$fieldalias);
} else {
debugging("Field '{$fieldname}' not found", DEBUG_DEVELOPER);
}
}

return $record;
}
}
59 changes: 0 additions & 59 deletions lib/dml/moodle_database.php
Expand Up @@ -828,65 +828,6 @@ public function get_in_or_equal($items, $type=SQL_PARAMS_QM, $prefix='param', $e
return array($sql, $params);
}

/**
* Get the SELECT SQL to preload columns for the specified fieldlist and table alias.
*
* This function is intended to be used in combination with get_preload_columns_sql and extract_from_fields.
*
* @param array $fieldlist The list of fields from get_preload_columns
* @param string $tablealias The table alias used in the FROM/JOIN field
* @return string The SQL to use in the SELECT
*/
public function get_preload_columns_sql(array $fieldlist, string $tablealias) : string {
return implode(', ', array_map(function($fieldname, $alias) use ($tablealias) {
return "{$tablealias}.{$fieldname} AS {$alias}";
}, $fieldlist, array_keys($fieldlist)));
}

/**
* Extract fields from the specified data.
* The fields are removed from the original object.
*
* This function is intended to be used in combination with get_preload_columns and get_preload_columns_sql.
*
* @param array $fieldlist The list of fields from get_preload_columns
* @param \stdClass $data The data retrieved from the database with fields to be extracted
* @return string The SQL to use in the SELECT
*/
public function extract_fields_from_object(array $fieldlist, \stdClass $data) : \stdClass {
$newdata = (object) [];
foreach ($fieldlist as $alias => $fieldname) {
if (property_exists($data, $alias)) {
$newdata->$fieldname = $data->$alias;
unset($data->$alias);
} else {
debugging("Field '{$fieldname}' not found", DEBUG_DEVELOPER);
}
}

return $newdata;
}

/**
* Get the preload columns for the specified table and use the specified prefix in the column alias.
*
* This function is intended to be used in combination with get_preload_columns_sql and extract_from_fields.
*
* @param string $table
* @param string $prefix
* @return array The list of columns in a table. The array key is the column name with an applied prefix.
*/
public function get_preload_columns(string $table, string $prefix) : array {
global $DB;

$fields = [];
foreach (array_keys($this->get_columns($table)) as $fieldname) {
$fields["{$prefix}{$fieldname}"] = $fieldname;
}

return $fields;
}

/**
* Converts short table name {tablename} to the real prefixed table name in given sql.
* @param string $sql The sql to be operated on.
Expand Down

0 comments on commit 71cbc05

Please sign in to comment.