Skip to content

Commit

Permalink
Merge branch 'w28_MDL-33568_m24_recordcounts' of git://github.com/sko…
Browse files Browse the repository at this point in the history
…dak/moodle
  • Loading branch information
danpoltawski committed Jul 10, 2012
2 parents 5c509b0 + 63224de commit a87425a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,11 +1578,11 @@ public function count_records_select($table, $select, array $params=null, $count
* @throws dml_exception A DML specific exception is thrown for any errors.
*/
public function count_records_sql($sql, array $params=null) {
if ($count = $this->get_field_sql($sql, $params)) {
return $count;
} else {
return 0;
$count = $this->get_field_sql($sql, $params);
if ($count === false or !is_number($count) or $count < 0) {
throw new coding_exception("count_records_sql() expects the first field to contain non-negative number from COUNT(), '$count' found instead.");
}
return (int)$count;
}

/**
Expand Down
34 changes: 25 additions & 9 deletions lib/dml/tests/dml_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2833,13 +2833,13 @@ public function test_count_records() {
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);

$this->assertEquals(0, $DB->count_records($tablename));
$this->assertSame(0, $DB->count_records($tablename));

$DB->insert_record($tablename, array('course' => 3));
$DB->insert_record($tablename, array('course' => 4));
$DB->insert_record($tablename, array('course' => 5));

$this->assertEquals(3, $DB->count_records($tablename));
$this->assertSame(3, $DB->count_records($tablename));

// test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int)
$conditions = array('onetext' => '1');
Expand Down Expand Up @@ -2868,13 +2868,13 @@ public function test_count_records_select() {
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);

$this->assertEquals(0, $DB->count_records($tablename));
$this->assertSame(0, $DB->count_records($tablename));

$DB->insert_record($tablename, array('course' => 3));
$DB->insert_record($tablename, array('course' => 4));
$DB->insert_record($tablename, array('course' => 5));

$this->assertEquals(2, $DB->count_records_select($tablename, 'course > ?', array(3)));
$this->assertSame(2, $DB->count_records_select($tablename, 'course > ?', array(3)));
}

public function test_count_records_sql() {
Expand All @@ -2886,16 +2886,32 @@ public function test_count_records_sql() {

$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$dbman->create_table($table);

$this->assertEquals(0, $DB->count_records($tablename));
$this->assertSame(0, $DB->count_records($tablename));

$DB->insert_record($tablename, array('course' => 3));
$DB->insert_record($tablename, array('course' => 4));
$DB->insert_record($tablename, array('course' => 5));
$DB->insert_record($tablename, array('course' => 3, 'onechar' => 'a'));
$DB->insert_record($tablename, array('course' => 4, 'onechar' => 'b'));
$DB->insert_record($tablename, array('course' => 5, 'onechar' => 'c'));

$this->assertSame(2, $DB->count_records_sql("SELECT COUNT(*) FROM {{$tablename}} WHERE course > ?", array(3)));

$this->assertEquals(2, $DB->count_records_sql("SELECT COUNT(*) FROM {{$tablename}} WHERE course > ?", array(3)));
// test invalid use
try {
$DB->count_records_sql("SELECT onechar FROM {{$tablename}} WHERE course = ?", array(3));
$this->fail('Exception expected when non-number field used in count_records_sql');
} catch (exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}

try {
$DB->count_records_sql("SELECT course FROM {{$tablename}} WHERE 1 = 2");
$this->fail('Exception expected when non-number field used in count_records_sql');
} catch (exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}
}

public function test_record_exists() {
Expand Down

0 comments on commit a87425a

Please sign in to comment.