Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 42... Línea 42...
42
 * @category   test
42
 * @category   test
43
 * @copyright  2008 Nicolas Connault
43
 * @copyright  2008 Nicolas Connault
44
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 * @covers \moodle_database
45
 * @covers \moodle_database
46
 */
46
 */
47
class dml_test extends \database_driver_testcase {
47
final class dml_test extends \database_driver_testcase {
Línea 48... Línea 48...
48
 
48
 
49
    protected function setUp(): void {
49
    protected function setUp(): void {
50
        parent::setUp();
50
        parent::setUp();
51
        $dbman = $this->tdb->get_manager(); // Loads DDL libs.
51
        $dbman = $this->tdb->get_manager(); // Loads DDL libs.
Línea 485... Línea 485...
485
    public function test_add_sql_debugging(): void {
485
    public function test_add_sql_debugging(): void {
486
        global $CFG;
486
        global $CFG;
487
        $DB = $this->tdb;
487
        $DB = $this->tdb;
Línea 488... Línea 488...
488
 
488
 
-
 
489
        require_once($CFG->dirroot . '/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php');
489
        require_once($CFG->dirroot . '/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php');
490
        $databasemock = $this->getMockBuilder(\moodle_database::class)->getMock();
Línea 490... Línea 491...
490
        $fixture = new \test_dml_sql_debugging_fixture($this);
491
        $fixture = new \test_dml_sql_debugging_fixture($databasemock);
Línea 491... Línea 492...
491
 
492
 
Línea 497... Línea 498...
497
        $this->assertEquals("SELECT * FROM {users}", $out);
498
        $this->assertEquals("SELECT * FROM {users}", $out);
Línea 498... Línea 499...
498
 
499
 
499
        $CFG->debugsqltrace = 1;
500
        $CFG->debugsqltrace = 1;
500
        $out = $fixture->four($sql);
501
        $out = $fixture->four($sql);
501
        $expected = <<<EOD
502
        $expected = <<<EOD
502
SELECT * FROM {users}
503
SELECT \* FROM {users}
503
-- line 64 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke()
504
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
504
EOD;
505
EOD;
Línea 505... Línea 506...
505
        $this->assertEquals($this->unix_to_os_dirsep($expected), $out);
506
        $this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
506
 
507
 
507
        $CFG->debugsqltrace = 2;
508
        $CFG->debugsqltrace = 2;
508
        $out = $fixture->four($sql);
509
        $out = $fixture->four($sql);
509
        $expected = <<<EOD
510
        $expected = <<<EOD
510
SELECT * FROM {users}
511
SELECT \* FROM {users}
511
-- line 64 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke()
512
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
512
-- line 73 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one()
513
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
Línea 513... Línea 514...
513
EOD;
514
EOD;
514
        $this->assertEquals($this->unix_to_os_dirsep($expected), $out);
515
        $this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
515
 
516
 
516
        $CFG->debugsqltrace = 5;
517
        $CFG->debugsqltrace = 5;
517
        $out = $fixture->four($sql);
518
        $out = $fixture->four($sql);
518
        $expected = <<<EOD
519
        $expected = <<<EOD
519
SELECT * FROM {users}
520
SELECT \* FROM {users}
520
-- line 64 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke()
521
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
521
-- line 73 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one()
522
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
522
-- line 82 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->two()
523
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->two\(\)
523
-- line 91 of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->three()
524
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->three\(\)
Línea 524... Línea 525...
524
-- line 517 of /lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four()
525
-- line \d+ of /lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four\(\)
525
EOD;
526
EOD;
Línea 526... Línea 527...
526
        $this->assertEquals($this->unix_to_os_dirsep($expected), $out);
527
        $this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
527
 
528
 
528
        $CFG->debugsqltrace = 0;
-
 
529
    }
-
 
530
 
529
        $CFG->debugsqltrace = 0;
531
    /**
530
    }
532
     * Test the database debugging as SQL comment in anon class
531
 
533
     *
532
    /**
Línea 573... Línea 572...
573
        $DB->get_records($tablename, array('id'=>1));
572
        $DB->get_records($tablename, array('id'=>1));
Línea 574... Línea 573...
574
 
573
 
575
        $this->assertSame(strtok('?'), 'b');
574
        $this->assertSame(strtok('?'), 'b');
Línea 576... Línea -...
576
    }
-
 
577
 
-
 
578
    public function test_tweak_param_names(): void {
-
 
579
 
-
 
580
        // Note the tweak_param_names() method is only available in the oracle driver,
-
 
581
        // hence we look for expected results indirectly, by testing various DML methods.
-
 
582
        // with some "extreme" conditions causing the tweak to happen.
-
 
583
        $DB = $this->tdb;
-
 
584
        $dbman = $this->tdb->get_manager();
-
 
585
 
-
 
586
        $table = $this->get_test_table();
-
 
587
        $tablename = $table->getName();
-
 
588
 
-
 
589
        // Prepare some long column names.
-
 
590
        $intnearmax = str_pad('long_int_columnname_near_', \xmldb_field::NAME_MAX_LENGTH - 1, 'x');
-
 
591
        $decnearmax = str_pad('long_dec_columnname_near_', \xmldb_field::NAME_MAX_LENGTH - 1, 'x');
-
 
592
        $strnearmax = str_pad('long_str_columnname_near_', \xmldb_field::NAME_MAX_LENGTH - 1, 'x');
-
 
593
        $intmax = str_pad('long_int_columnname_max', \xmldb_field::NAME_MAX_LENGTH, 'x');
-
 
594
        $decmax = str_pad('long_dec_columnname_max', \xmldb_field::NAME_MAX_LENGTH, 'x');
-
 
595
        $strmax = str_pad('long_str_columnname_max', \xmldb_field::NAME_MAX_LENGTH, 'x');
-
 
596
 
-
 
597
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
-
 
598
        // Add some correct columns with \xmldb_field::NAME_MAX_LENGTH minus 1 chars in the name.
-
 
599
        $table->add_field($intnearmax, XMLDB_TYPE_INTEGER, '10');
-
 
600
        $table->add_field($decnearmax, XMLDB_TYPE_NUMBER, '10,2');
-
 
601
        $table->add_field($strnearmax, XMLDB_TYPE_CHAR, '100');
-
 
602
        // Add some correct columns with xmldb_table::NAME_MAX_LENGTH chars in the name.
-
 
603
        $table->add_field($intmax, XMLDB_TYPE_INTEGER, '10');
-
 
604
        $table->add_field($decmax, XMLDB_TYPE_NUMBER, '10,2');
-
 
605
        $table->add_field($strmax, XMLDB_TYPE_CHAR, '100');
-
 
606
 
-
 
607
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
-
 
608
 
-
 
609
        $dbman->create_table($table);
-
 
610
 
-
 
611
        $this->assertTrue($dbman->table_exists($tablename));
-
 
612
 
-
 
613
        // Test insert record.
-
 
614
        $rec1 = new \stdClass();
-
 
615
        $rec1->{$intnearmax} = 62;
-
 
616
        $rec1->{$decnearmax} = 62.62;
-
 
617
        $rec1->{$strnearmax} = '62';
-
 
618
        $rec1->{$intmax} = 63;
-
 
619
        $rec1->{$decmax} = 63.63;
-
 
620
        $rec1->{$strmax} = '63';
-
 
621
 
-
 
622
        // Insert_record().
-
 
623
        $rec1->id = $DB->insert_record($tablename, $rec1);
-
 
624
        $this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
-
 
625
 
-
 
626
        // Update_record().
-
 
627
        $DB->update_record($tablename, $rec1);
-
 
628
        $this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
-
 
629
 
-
 
630
        // Set_field().
-
 
631
        $rec1->{$intnearmax} = 620;
-
 
632
        $DB->set_field($tablename, $intnearmax, $rec1->{$intnearmax},
-
 
633
            array('id' => $rec1->id, $intnearmax => 62));
-
 
634
        $rec1->{$decnearmax} = 620.62;
-
 
635
        $DB->set_field($tablename, $decnearmax, $rec1->{$decnearmax},
-
 
636
            array('id' => $rec1->id, $decnearmax => 62.62));
-
 
637
        $rec1->{$strnearmax} = '620';
-
 
638
        $DB->set_field($tablename, $strnearmax, $rec1->{$strnearmax},
-
 
639
            array('id' => $rec1->id, $strnearmax => '62'));
-
 
640
        $rec1->{$intmax} = 630;
-
 
641
        $DB->set_field($tablename, $intmax, $rec1->{$intmax},
-
 
642
            array('id' => $rec1->id, $intmax => 63));
-
 
643
        $rec1->{$decmax} = 630.63;
-
 
644
        $DB->set_field($tablename, $decmax, $rec1->{$decmax},
-
 
645
            array('id' => $rec1->id, $decmax => 63.63));
-
 
646
        $rec1->{$strmax} = '630';
-
 
647
        $DB->set_field($tablename, $strmax, $rec1->{$strmax},
-
 
648
            array('id' => $rec1->id, $strmax => '63'));
-
 
649
        $this->assertEquals($rec1, $DB->get_record($tablename, array('id' => $rec1->id)));
-
 
650
 
-
 
651
        // Delete_records().
-
 
652
        $rec2 = $DB->get_record($tablename, array('id' => $rec1->id));
-
 
653
        $rec2->id = $DB->insert_record($tablename, $rec2);
-
 
654
        $this->assertEquals(2, $DB->count_records($tablename));
-
 
655
        $DB->delete_records($tablename, (array) $rec2);
-
 
656
        $this->assertEquals(1, $DB->count_records($tablename));
-
 
657
 
-
 
658
        // Get_recordset().
-
 
659
        $rs = $DB->get_recordset($tablename, (array) $rec1);
-
 
660
        $iterations = 0;
-
 
661
        foreach ($rs as $rec2) {
-
 
662
            $iterations++;
-
 
663
        }
-
 
664
        $rs->close();
-
 
665
        $this->assertEquals(1, $iterations);
-
 
666
        $this->assertEquals($rec1, $rec2);
-
 
667
 
-
 
668
        // Get_records().
-
 
669
        $recs = $DB->get_records($tablename, (array) $rec1);
-
 
670
        $this->assertCount(1, $recs);
-
 
671
        $this->assertEquals($rec1, reset($recs));
-
 
672
 
-
 
673
        // Get_fieldset_select().
-
 
674
        $select = "id = :id AND
-
 
675
                   $intnearmax = :$intnearmax AND
-
 
676
                   $decnearmax = :$decnearmax AND
-
 
677
                   $strnearmax = :$strnearmax AND
-
 
678
                   $intmax = :$intmax AND
-
 
679
                   $decmax = :$decmax AND
-
 
680
                   $strmax = :$strmax";
-
 
681
        $fields = $DB->get_fieldset_select($tablename, $intnearmax, $select, (array)$rec1);
-
 
682
        $this->assertCount(1, $fields);
-
 
683
        $this->assertEquals($rec1->{$intnearmax}, reset($fields));
-
 
684
        $fields = $DB->get_fieldset_select($tablename, $decnearmax, $select, (array)$rec1);
-
 
685
        $this->assertEquals($rec1->{$decnearmax}, reset($fields));
-
 
686
        $fields = $DB->get_fieldset_select($tablename, $strnearmax, $select, (array)$rec1);
-
 
687
        $this->assertEquals($rec1->{$strnearmax}, reset($fields));
-
 
688
        $fields = $DB->get_fieldset_select($tablename, $intmax, $select, (array)$rec1);
-
 
689
        $this->assertEquals($rec1->{$intmax}, reset($fields));
-
 
690
        $fields = $DB->get_fieldset_select($tablename, $decmax, $select, (array)$rec1);
-
 
691
        $this->assertEquals($rec1->{$decmax}, reset($fields));
-
 
692
        $fields = $DB->get_fieldset_select($tablename, $strmax, $select, (array)$rec1);
-
 
693
        $this->assertEquals($rec1->{$strmax}, reset($fields));
-
 
694
 
-
 
695
        // Overlapping placeholders (progressive str_replace).
-
 
696
        $nearmaxparam = str_pad('allowed_long_param', \xmldb_field::NAME_MAX_LENGTH - 1, 'x');
-
 
697
        $maxparam = str_pad('allowed_long_param', \xmldb_field::NAME_MAX_LENGTH, 'x');
-
 
698
        $overlapselect = "id = :p AND
-
 
699
                   $intnearmax = :param1 AND
-
 
700
                   $decnearmax = :param2 AND
-
 
701
                   $strnearmax = :{$nearmaxparam} AND
-
 
702
                   $intmax = :{$maxparam} AND
-
 
703
                   $decmax = :param_ AND
-
 
704
                   $strmax = :param__";
-
 
705
        $overlapparams = array(
-
 
706
            'p' => $rec1->id,
-
 
707
            'param1' => $rec1->{$intnearmax},
-
 
708
            'param2' => $rec1->{$decnearmax},
-
 
709
            $nearmaxparam => $rec1->{$strnearmax},
-
 
710
            $maxparam => $rec1->{$intmax},
-
 
711
            'param_' => $rec1->{$decmax},
-
 
712
            'param__' => $rec1->{$strmax});
-
 
713
        $recs = $DB->get_records_select($tablename, $overlapselect, $overlapparams);
-
 
714
        $this->assertCount(1, $recs);
-
 
715
        $this->assertEquals($rec1, reset($recs));
-
 
716
 
-
 
717
        // Execute().
-
 
718
        $DB->execute("DELETE FROM {{$tablename}} WHERE $select", (array)$rec1);
-
 
719
        $this->assertEquals(0, $DB->count_records($tablename));
-
 
720
    }
575
    }
721
 
576
 
722
    public function test_get_tables(): void {
577
    public function test_get_tables(): void {
Línea 723... Línea 578...
723
        $DB = $this->tdb;
578
        $DB = $this->tdb;
Línea 3922... Línea 3777...
3922
        $this->assertEquals(666, $DB->get_field_sql($sql));
3777
        $this->assertEquals(666, $DB->get_field_sql($sql));
3923
    }
3778
    }
Línea 3924... Línea 3779...
3924
 
3779
 
3925
    /**
3780
    /**
3926
     * Test DML libraries sql_cast_to_char method
-
 
3927
     *
-
 
3928
     * @covers ::sql_cast_to_char
3781
     * Test DML libraries sql_cast_to_char method
3929
     */
3782
     */
3930
    public function test_cast_to_char(): void {
3783
    public function test_cast_to_char(): void {
3931
        $DB = $this->tdb;
3784
        $DB = $this->tdb;
Línea 4086... Línea 3939...
4086
        $DB->insert_record($tablename, array('name'=>'abcd',   'description'=>'abcd'));
3939
        $DB->insert_record($tablename, array('name'=>'abcd',   'description'=>'abcd'));
4087
        $DB->insert_record($tablename, array('name'=>'abcdef', 'description'=>'bbcdef'));
3940
        $DB->insert_record($tablename, array('name'=>'abcdef', 'description'=>'bbcdef'));
4088
        $DB->insert_record($tablename, array('name'=>'aaaa', 'description'=>'aaaacccccccccccccccccc'));
3941
        $DB->insert_record($tablename, array('name'=>'aaaa', 'description'=>'aaaacccccccccccccccccc'));
4089
        $DB->insert_record($tablename, array('name'=>'xxxx',   'description'=>'123456789a123456789b123456789c123456789d'));
3942
        $DB->insert_record($tablename, array('name'=>'xxxx',   'description'=>'123456789a123456789b123456789c123456789d'));
Línea 4090... Línea 3943...
4090
 
3943
 
4091
        // Only some supported databases truncate TEXT fields for comparisons, currently MSSQL and Oracle.
3944
        // Only some supported databases truncate TEXT fields for comparisons, currently MSSQL.
Línea 4092... Línea 3945...
4092
        $dbtruncatestextfields = ($DB->get_dbfamily() == 'mssql' || $DB->get_dbfamily() == 'oracle');
3945
        $dbtruncatestextfields = ($DB->get_dbfamily() == 'mssql');
4093
 
3946
 
Línea 4094... Línea 3947...
4094
        if ($dbtruncatestextfields) {
3947
        if ($dbtruncatestextfields) {
Línea 4164... Línea 4017...
4164
        } catch (\moodle_exception $e) {
4017
        } catch (\moodle_exception $e) {
4165
            $family = $DB->get_dbfamily();
4018
            $family = $DB->get_dbfamily();
4166
            if ($family === 'mysql' or $family === 'mssql') {
4019
            if ($family === 'mysql' or $family === 'mssql') {
4167
                $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages. This is usually caused by accent insensitive default collation.");
4020
                $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages. This is usually caused by accent insensitive default collation.");
4168
            } else {
4021
            } else {
4169
                // This should not happen, PostgreSQL and Oracle do not support accent insensitive uniqueness.
4022
                // This should not happen, PostgreSQL does not support accent insensitive uniqueness.
4170
                $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages.");
4023
                $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages.");
4171
            }
4024
            }
4172
            throw($e);
4025
            throw($e);
4173
        }
4026
        }
4174
    }
4027
    }
Línea 4416... Línea 4269...
4416
        $table->add_field('charlong', XMLDB_TYPE_CHAR, '1333');
4269
        $table->add_field('charlong', XMLDB_TYPE_CHAR, '1333');
4417
        $table->add_field('description', XMLDB_TYPE_TEXT, 'big');
4270
        $table->add_field('description', XMLDB_TYPE_TEXT, 'big');
4418
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
4271
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
4419
        $dbman->create_table($table);
4272
        $dbman->create_table($table);
Línea 4420... Línea 4273...
4420
 
4273
 
-
 
4274
        // Regarding the 1300 length - all supported drivers allow larger values (2K+),
4421
        // Regarding 1300 length - all drivers except Oracle support larger values (2K+), but this hits a limit on Oracle.
4275
        // previously limited by Oracle, which no longer applies as Oracle support has been removed.
4422
        $DB->insert_record($tablename, [
4276
        $DB->insert_record($tablename, [
4423
            'charshort' => 'áéíóú',
4277
            'charshort' => 'áéíóú',
4424
            'charlong' => str_repeat('A', 512),
4278
            'charlong' => str_repeat('A', 512),
4425
            'description' => str_repeat('X', 1300),
4279
            'description' => str_repeat('X', 1300),
Línea 4475... Línea 4329...
4475
            '333',
4329
            '333',
4476
        ], $DB->get_fieldset_select($tablename, $fieldsql, ''));
4330
        ], $DB->get_fieldset_select($tablename, $fieldsql, ''));
Línea 4477... Línea 4331...
4477
 
4331
 
Línea 4478... Línea 4332...
4478
    }
4332
    }
4479
 
4333
 
4480
    public function sql_concat_join_provider() {
4334
    public static function sql_concat_join_provider(): array {
4481
        return array(
4335
        return array(
4482
            // All strings.
4336
            // All strings.
4483
            array(
4337
            array(
Línea 4668... Línea 4522...
4668
            ],
4522
            ],
4669
            (object) [
4523
            (object) [
4670
                'name' => 'Bob',
4524
                'name' => 'Bob',
4671
                'falias' => 'Dan, Grace',
4525
                'falias' => 'Dan, Grace',
4672
            ],
4526
            ],
4673
        ], $DB->get_records_sql($sql));
4527
        ], array_values($DB->get_records_sql($sql)));
-
 
4528
    }
-
 
4529
 
-
 
4530
    /**
-
 
4531
     * Test that the SQL_INT_MAX constant can be used for all insert, update, select and delete queries
-
 
4532
     */
-
 
4533
    public function test_sql_max_int(): void {
-
 
4534
        $DB = $this->tdb;
-
 
4535
        $dbman = $DB->get_manager();
-
 
4536
 
-
 
4537
        $table = $this->get_test_table();
-
 
4538
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
-
 
4539
        $table->add_field('intfield', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
-
 
4540
        $table->add_field('charfield', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
-
 
4541
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
-
 
4542
        $dbman->create_table($table);
-
 
4543
 
-
 
4544
        $tablename = $table->getName();
-
 
4545
 
-
 
4546
        // Insert.
-
 
4547
        $id = $DB->insert_record($tablename, ['intfield' => SQL_INT_MAX, 'charfield' => 'Test']);
-
 
4548
        $this->assertEquals((object) [
-
 
4549
            'intfield' => SQL_INT_MAX,
-
 
4550
            'charfield' => 'Test',
-
 
4551
        ], $DB->get_record($tablename, ['id' => $id], 'intfield, charfield'));
-
 
4552
 
-
 
4553
        // Update.
-
 
4554
        $DB->set_field($tablename, 'charfield', 'Test 2', ['intfield' => SQL_INT_MAX]);
-
 
4555
        $this->assertEquals((object) [
-
 
4556
            'intfield' => SQL_INT_MAX,
-
 
4557
            'charfield' => 'Test 2',
-
 
4558
        ], $DB->get_record($tablename, ['id' => $id], 'intfield, charfield'));
-
 
4559
 
-
 
4560
        // Select.
-
 
4561
        $this->assertEquals('Test 2', $DB->get_field($tablename, 'charfield', ['intfield' => SQL_INT_MAX]));
-
 
4562
 
-
 
4563
        // Delete.
-
 
4564
        $DB->delete_records($tablename, ['intfield' => SQL_INT_MAX]);
-
 
4565
        $this->assertFalse($DB->record_exists($tablename, ['id' => $id]));
4674
    }
4566
    }
Línea 4675... Línea 4567...
4675
 
4567
 
4676
    public function test_sql_fullname(): void {
4568
    public function test_sql_fullname(): void {
4677
        $DB = $this->tdb;
4569
        $DB = $this->tdb;
Línea 5034... Línea 4926...
5034
        $DB->insert_record($tablename, array('course' => 3, 'content' => 'world', 'name'=>'abc'));
4926
        $DB->insert_record($tablename, array('course' => 3, 'content' => 'world', 'name'=>'abc'));
5035
        $DB->insert_record($tablename, array('course' => 5, 'content' => 'hello', 'name'=>'def'));
4927
        $DB->insert_record($tablename, array('course' => 5, 'content' => 'hello', 'name'=>'def'));
5036
        $DB->insert_record($tablename, array('course' => 2, 'content' => 'universe', 'name'=>'abc'));
4928
        $DB->insert_record($tablename, array('course' => 2, 'content' => 'universe', 'name'=>'abc'));
Línea 5037... Línea 4929...
5037
 
4929
 
5038
        // Test grouping by expressions in the query. MDL-26819. Note that there are 4 ways:
4930
        // Test grouping by expressions in the query. MDL-26819. Note that there are 4 ways:
5039
        // - By column position (GROUP by 1) - Not supported by mssql & oracle
4931
        // - By column position (GROUP by 1) - Not supported by mssql
5040
        // - By column name (GROUP by course) - Supported by all, but leading to wrong results
4932
        // - By column name (GROUP by course) - Supported by all, but leading to wrong results
5041
        // - By column alias (GROUP by casecol) - Not supported by mssql & oracle
4933
        // - By column alias (GROUP by casecol) - Not supported by mssql
5042
        // - By complete expression (GROUP BY CASE ...) - 100% cross-db, this test checks it
4934
        // - By complete expression (GROUP BY CASE ...) - 100% cross-db, this test checks it
5043
        $sql = "SELECT (CASE WHEN course = 3 THEN 1 ELSE 0 END) AS casecol,
4935
        $sql = "SELECT (CASE WHEN course = 3 THEN 1 ELSE 0 END) AS casecol,
5044
                       COUNT(1) AS countrecs,
4936
                       COUNT(1) AS countrecs,
5045
                       MAX(name) AS maxname
4937
                       MAX(name) AS maxname
Línea 5107... Línea 4999...
5107
        // Both limitfrom and limitnum.
4999
        // Both limitfrom and limitnum.
5108
        $records = $DB->get_records_sql($sql, null, 2, 2);
5000
        $records = $DB->get_records_sql($sql, null, 2, 2);
5109
        $this->assertCount(1, $records);
5001
        $this->assertCount(1, $records);
5110
        $this->assertEquals(5, reset($records)->course);
5002
        $this->assertEquals(5, reset($records)->course);
Línea 5111... Línea 5003...
5111
 
5003
 
5112
        // We have sql like this in moodle, this syntax breaks on older versions of sqlite for example..
5004
        // We have sql like this in moodle.
5113
        $sql = "SELECT a.id AS id, a.course AS course
5005
        $sql = "SELECT a.id AS id, a.course AS course
5114
                  FROM {{$tablename}} a
5006
                  FROM {{$tablename}} a
5115
                  JOIN (SELECT * FROM {{$tablename}}) b ON a.id = b.id
5007
                  JOIN (SELECT * FROM {{$tablename}}) b ON a.id = b.id
Línea 5670... Línea 5562...
5670
        // Open second connection.
5562
        // Open second connection.
5671
        $cfg = $DB->export_dbconfig();
5563
        $cfg = $DB->export_dbconfig();
5672
        if (!isset($cfg->dboptions)) {
5564
        if (!isset($cfg->dboptions)) {
5673
            $cfg->dboptions = array();
5565
            $cfg->dboptions = array();
5674
        }
5566
        }
5675
        // If we have a readonly slave situation, we need to either observe
5567
        // If we have a readonly replica situation, we need to either observe
5676
        // the latency, or if the latency is not specified we need to take
5568
        // the latency, or if the latency is not specified we need to take
5677
        // the slave out because the table may not have propagated yet.
5569
        // the replica out because the table may not have propagated yet.
5678
        if (isset($cfg->dboptions['readonly'])) {
5570
        if (isset($cfg->dboptions['readonly'])) {
5679
            if (isset($cfg->dboptions['readonly']['latency'])) {
5571
            if (isset($cfg->dboptions['readonly']['latency'])) {
5680
                usleep(intval(1000000 * $cfg->dboptions['readonly']['latency']));
5572
                usleep(intval(1000000 * $cfg->dboptions['readonly']['latency']));
5681
            } else {
5573
            } else {
5682
                unset($cfg->dboptions['readonly']);
5574
                unset($cfg->dboptions['readonly']);
Línea 6279... Línea 6171...
6279
     * - a string representing the expected DB version
6171
     * - a string representing the expected DB version
6280
     *
6172
     *
6281
     * @return array[]
6173
     * @return array[]
6282
     * @see \mysqli_native_moodle_database::get_server_info
6174
     * @see \mysqli_native_moodle_database::get_server_info
6283
     */
6175
     */
6284
    public function get_server_info_mysql_provider() {
6176
    public static function get_server_info_mysql_provider(): array {
6285
        return [
6177
        return [
6286
            'MySQL 5.7.39 - MySQLi version' => [
6178
            'MySQL 5.7.39 - MySQLi version' => [
6287
                '5.7.39-log',
6179
                '5.7.39-log',
6288
                '',
6180
                '',
6289
                false,
6181
                false,
Línea 6396... Línea 6288...
6396
            "Found invalid DB server version when reading version from DB i.e. RPL_VERSION_HACK: '{$version}' ({$description}).");
6288
            "Found invalid DB server version when reading version from DB i.e. RPL_VERSION_HACK: '{$version}' ({$description}).");
6397
        $this->assertMatchesRegularExpression('/^\d+\.\d+\.\d+$/', $version,
6289
        $this->assertMatchesRegularExpression('/^\d+\.\d+\.\d+$/', $version,
6398
            "Found invalid DB server version format when reading version from DB: '{$version}' ({$description}).");
6290
            "Found invalid DB server version format when reading version from DB: '{$version}' ({$description}).");
6399
        $db2->dispose();
6291
        $db2->dispose();
6400
    }
6292
    }
-
 
6293
 
-
 
6294
    /**
-
 
6295
     * Test the COUNT() window function with the actual DB Server.
-
 
6296
     *
-
 
6297
     * @covers \moodle_database::get_counted_recordset_sql()
-
 
6298
     * @covers \moodle_database::get_counted_records_sql()
-
 
6299
     * @covers \moodle_database::generate_fullcount_sql()
-
 
6300
     * @return void
-
 
6301
     */
-
 
6302
    public function test_count_window_function(): void {
-
 
6303
        $DB = $this->tdb;
-
 
6304
        $dbman = $DB->get_manager();
-
 
6305
 
-
 
6306
        $table = $this->get_test_table();
-
 
6307
        $tablename = $table->getName();
-
 
6308
 
-
 
6309
        $table->add_field('id', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
-
 
6310
        $table->add_field('course', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
-
 
6311
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
-
 
6312
        $dbman->create_table($table);
-
 
6313
 
-
 
6314
        for ($i = 1; $i <= 5; $i++) {
-
 
6315
            $DB->insert_record($tablename, ['course' => $i], false);
-
 
6316
        }
-
 
6317
 
-
 
6318
        // Test with the get_recordset_select().
-
 
6319
        $rs = $DB->get_counted_recordset_sql(
-
 
6320
            sql: "SELECT * FROM {{$tablename}}",
-
 
6321
            fullcountcolumn: 'fullcount',
-
 
6322
            sort: "course DESC",
-
 
6323
            limitfrom: 1,
-
 
6324
            limitnum: 3,
-
 
6325
        );
-
 
6326
        // Check whether the fullcount column returns the correct number.
-
 
6327
        $this->assertEquals(5, $rs->current()->fullcount);
-
 
6328
        // Check whether the `limitfrom` works properly.
-
 
6329
        $this->assertEquals(4, $rs->current()->course);
-
 
6330
        // Check whether the 'limitnum' works properly.
-
 
6331
        $this->assertEquals(3, iterator_count($rs));
-
 
6332
 
-
 
6333
        // Test with the get_records_select().
-
 
6334
        $rs = $DB->get_counted_records_sql(
-
 
6335
            sql: "SELECT * FROM {{$tablename}}",
-
 
6336
            fullcountcolumn: 'fullcount',
-
 
6337
            sort: "course DESC",
-
 
6338
            limitfrom: 3,
-
 
6339
            limitnum: 2,
-
 
6340
        );
-
 
6341
        $resetrs = reset($rs);
-
 
6342
        // Check whether the fullcount column returns the correct number.
-
 
6343
        $this->assertEquals(5, $resetrs->fullcount);
-
 
6344
        // Check whether the 'limitfrom' works properly.
-
 
6345
        $this->assertEquals(2, $resetrs->course);
-
 
6346
        // Check whether the 'limitnum' works properly.
-
 
6347
        $this->assertEquals(2, count($rs));
-
 
6348
    }
6401
}
6349
}
Línea 6402... Línea 6350...
6402
 
6350
 
6403
/**
6351
/**
6404
 * This class is not a proper subclass of moodle_database. It is
6352
 * This class is not a proper subclass of moodle_database. It is
Línea 6416... Línea 6364...
6416
    public function get_dbfamily() {}
6364
    public function get_dbfamily() {}
6417
    protected function get_dbtype() {}
6365
    protected function get_dbtype() {}
6418
    protected function get_dblibrary() {}
6366
    protected function get_dblibrary() {}
6419
    public function get_name() {}
6367
    public function get_name() {}
6420
    public function get_configuration_help() {}
6368
    public function get_configuration_help() {}
6421
    public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {}
6369
    public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, ?array $dboptions=null) {}
6422
    public function get_server_info() {}
6370
    public function get_server_info() {}
6423
    protected function allowed_param_types() {}
6371
    protected function allowed_param_types() {}
6424
    public function get_last_error() {}
6372
    public function get_last_error() {}
6425
    public function get_tables($usecache=true) {}
6373
    public function get_tables($usecache=true) {}
6426
    public function get_indexes($table) {}
6374
    public function get_indexes($table) {}
Línea 6429... Línea 6377...
6429
    }
6377
    }
6430
    protected function normalise_value($column, $value) {}
6378
    protected function normalise_value($column, $value) {}
6431
    public function set_debug($state) {}
6379
    public function set_debug($state) {}
6432
    public function get_debug() {}
6380
    public function get_debug() {}
6433
    public function change_database_structure($sql, $tablenames = null) {}
6381
    public function change_database_structure($sql, $tablenames = null) {}
6434
    public function execute($sql, array $params=null) {}
6382
    public function execute($sql, ?array $params=null) {}
6435
    public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {}
6383
    public function get_recordset_sql($sql, ?array $params=null, $limitfrom=0, $limitnum=0) {}
6436
    public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {}
6384
    public function get_records_sql($sql, ?array $params=null, $limitfrom=0, $limitnum=0) {}
6437
    public function get_fieldset_sql($sql, array $params=null) {}
6385
    public function get_fieldset_sql($sql, ?array $params=null) {}
6438
    public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {}
6386
    public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {}
6439
    public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {}
6387
    public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {}
6440
    public function import_record($table, $dataobject) {}
6388
    public function import_record($table, $dataobject) {}
6441
    public function update_record_raw($table, $params, $bulk=false) {}
6389
    public function update_record_raw($table, $params, $bulk=false) {}
6442
    public function update_record($table, $dataobject, $bulk=false) {}
6390
    public function update_record($table, $dataobject, $bulk=false) {}
6443
    public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {}
6391
    public function set_field_select($table, $newfield, $newvalue, $select, ?array $params=null) {}
6444
    public function delete_records_select($table, $select, array $params=null) {}
6392
    public function delete_records_select($table, $select, ?array $params=null) {}
6445
    public function sql_concat(...$arr) {}
6393
    public function sql_concat(...$arr) {}
6446
    public function sql_concat_join($separator="' '", $elements=array()) {}
6394
    public function sql_concat_join($separator="' '", $elements=array()) {}
6447
    public function sql_group_concat(string $field, string $separator = ', ', string $sort = ''): string {
6395
    public function sql_group_concat(string $field, string $separator = ', ', string $sort = ''): string {
6448
        return '';
6396
        return '';
6449
    }
6397
    }