MDL-65168 dml: Move preload SQL functions to self-contained class

This commit is contained in:
Andrew Nicols 2019-03-25 09:20:28 +08:00 committed by Eloy Lafuente (stronk7)
parent e5a501de13
commit 71cbc0550f
7 changed files with 390 additions and 224 deletions

View file

@ -828,65 +828,6 @@ abstract class moodle_database {
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.

View file

@ -0,0 +1,228 @@
<?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/>.
/**
* DML Table tests.
*
* @package core_dml
* @category phpunit
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use \core\dml\table;
/**
* DML Table tests.
*
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\dml\table
* @covers ::<!public>
*/
class core_dml_table_testcase extends database_driver_testcase {
/**
* Data provider for various \core\dml\table method tests.
*
* @return array
*/
public function get_field_select_provider() : array {
return [
'single field' => [
'tablename' => 'test_table_single',
'fieldlist' => [
'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
],
'primarykey' => 'id',
'fieldprefix' => 'ban',
'tablealias' => 'banana',
'banana.id AS banid',
],
'multiple fields' => [
'tablename' => 'test_table_multiple',
'fieldlist' => [
'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
'name' => ['name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
],
'primarykey' => 'id',
'fieldprefix' => 'ban',
'tablealias' => 'banana',
'banana.id AS banid, banana.course AS bancourse, banana.name AS banname',
],
];
}
/**
* Ensure that \core\dml\table::get_field_select() works as expected.
*
* @dataProvider get_field_select_provider
* @covers ::get_field_select
* @param string $tablename The name of the table
* @param array $fieldlist The list of fields
* @param string $primarykey The name of the primary key
* @param string $fieldprefix The prefix to use for each field
* @param string $tablealias The table AS alias name
* @param string $expected The expected SQL
*/
public function test_get_field_select(
string $tablename,
array $fieldlist,
string $primarykey,
string $fieldprefix,
string $tablealias,
string $expected
) {
$dbman = $this->tdb->get_manager();
$xmldbtable = new xmldb_table($tablename);
$xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
foreach ($fieldlist as $args) {
call_user_func_array([$xmldbtable, 'add_field'], $args);
}
$xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
$dbman->create_table($xmldbtable);
$table = new table($tablename, $tablealias, $fieldprefix);
$this->assertEquals($expected, $table->get_field_select());
}
/**
* Data provider for \core\dml\table::extract_from_result() tests.
*
* @return array
*/
public function extract_from_result_provider() : array {
return [
'single table' => [
'fieldlist' => [
'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
'flag' => ['flag', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
],
'primarykey' => 'id',
'prefix' => 's',
'result' => (object) [
'sid' => 1,
'scourse' => 42,
'sflag' => 'foo',
],
'expectedrecord' => (object) [
'id' => 1,
'course' => 42,
'flag' => 'foo',
],
],
'single table amongst others' => [
'fieldlist' => [
'id' => ['id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null],
'course' => ['course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'],
'flag' => ['flag', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'],
],
'primarykey' => 'id',
'prefix' => 's',
'result' => (object) [
'sid' => 1,
'scourse' => 42,
'sflag' => 'foo',
'oid' => 'id',
'ocourse' => 'course',
'oflag' => 'flag',
],
'expectedrecord' => (object) [
'id' => 1,
'course' => 42,
'flag' => 'foo',
],
],
];
}
/**
* Ensure that \core\dml\table::extract_from_result() works as expected.
*
* @dataProvider extract_from_result_provider
* @covers ::extract_from_result
* @param array $fieldlist The list of fields
* @param string $primarykey The name of the primary key
* @param string $fieldprefix The prefix to use for each field
* @param stdClass $result The result of the get_records_sql
* @param stdClass $expected The expected output
*/
public function test_extract_fields_from_result(
array $fieldlist,
string $primarykey,
string $fieldprefix,
stdClass $result,
stdClass $expected
) {
$dbman = $this->tdb->get_manager();
$tablename = 'test_table_extraction';
$xmldbtable = new xmldb_table($tablename);
$xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
foreach ($fieldlist as $args) {
call_user_func_array([$xmldbtable, 'add_field'], $args);
}
$xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
$dbman->create_table($xmldbtable);
$table = new table($tablename, 'footable', $fieldprefix);
$this->assertEquals($expected, $table->extract_from_result($result));
}
/**
* Ensure that \core\dml\table::get_from_sql() works as expected.
*
* @dataProvider get_field_select_provider
* @covers ::get_from_sql
* @param string $tablename The name of the table
* @param array $fieldlist The list of fields
* @param string $primarykey The name of the primary key
* @param string $fieldprefix The prefix to use for each field
* @param string $tablealias The table AS alias name
* @param string $expected The expected SQL
*/
public function test_get_from_sql(
string $tablename,
array $fieldlist,
string $primarykey,
string $fieldprefix,
string $tablealias,
string $expected
) {
$dbman = $this->tdb->get_manager();
$tablename = 'test_table_extraction';
$xmldbtable = new xmldb_table($tablename);
$xmldbtable->setComment("This is a test'n drop table. You can drop it safely");
foreach ($fieldlist as $args) {
call_user_func_array([$xmldbtable, 'add_field'], $args);
}
$xmldbtable->add_key('primary', XMLDB_KEY_PRIMARY, [$primarykey]);
$dbman->create_table($xmldbtable);
$table = new table($tablename, $tablealias, $fieldprefix);
$this->assertEquals("{{$tablename}} {$tablealias}", $table->get_from_sql());
}
}

View file

@ -794,136 +794,6 @@ class core_dml_testcase extends database_driver_testcase {
$this->assertFalse($columns['id']->auto_increment);
}
public function test_get_preload_columns() {
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();
$table = $this->get_test_table();
$tablename = $table->getName();
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);
$expected = [
'aid' => 'id',
'acourse' => 'course',
'aname' => 'name',
];
$columns = $DB->get_preload_columns($tablename, 'a');
$this->assertCount(3, $columns);
$this->assertEquals($expected, $columns);
}
/**
* Ensure that get_preload_columns_sql works as expected.
*
* @dataProvider get_preload_columns_sql_provider
* @param array $fieldlist The list of fields
* @param string $tablealias The alias to use
* @param string $expected The string to match
*/
public function test_get_preload_columns_sql(array $fieldlist, string $tablealias, string $expected) {
$this->assertEquals($expected, $this->tdb->get_preload_columns_sql($fieldlist, $tablealias));
}
/**
* Data provider for get_preload_columns_sql tests.
*
* @return array
*/
public function get_preload_columns_sql_provider() : array {
return [
'single field' => [
[
'xid' => 'id',
],
'x',
'x.id AS xid',
],
'multiple fields' => [
[
'bananaid' => 'id',
'bananacourse' => 'course',
'bananafoo' => 'foo',
],
'banana',
'banana.id AS bananaid, banana.course AS bananacourse, banana.foo AS bananafoo',
],
];
}
/**
* Ensure that extract_fields_from_object works as expected.
*
* @dataProvider extract_fields_from_object_provider
* @param array $fieldlist The list of fields
* @param stdClass $in Input values for the test
* @param stdClass $out The expected output
* @param stdClass $modified Expected value of $in after it's been modified
*/
public function test_extract_fields_from_object(array $fieldlist, \stdClass $in, \stdClass $out, \stdClass $modified) {
$result = $this->tdb->extract_fields_from_object($fieldlist, $in);
$this->assertEquals($out, $result);
$this->assertEquals($modified, $in);
}
/**
* Data provider for extract_fields_from_object tests.
*
* @return array
*/
public function extract_fields_from_object_provider() : array {
return [
'single table' => [
[
'sid' => 'id',
'scourse' => 'course',
'sflag' => 'flag',
],
(object) [
'sid' => 1,
'scourse' => 42,
'sflag' => 'foo',
],
(object) [
'id' => 1,
'course' => 42,
'flag' => 'foo',
],
(object) [
],
],
'single table amongst others' => [
[
'sid' => 'id',
'scourse' => 'course',
'sflag' => 'flag',
],
(object) [
'sid' => 1,
'scourse' => 42,
'sflag' => 'foo',
'oid' => 'id',
'ocourse' => 'course',
'oflag' => 'flag',
],
(object) [
'id' => 1,
'course' => 42,
'flag' => 'foo',
],
(object) [
'oid' => 'id',
'ocourse' => 'course',
'oflag' => 'flag',
],
],
];
}
public function test_get_manager() {
$DB = $this->tdb;
$dbman = $this->tdb->get_manager();