Skip to content

Commit

Permalink
Merge branch 'MDL-27325_master' of git://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed May 9, 2011
2 parents 644b14c + 86f456d commit 8f25ae2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/dml/mysqli_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ protected function normalise_value($column, $value) {
if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
$value = 0; // prevent '' problems in numeric fields
}
// Any float value being stored in varchar or text field is converted to string to avoid
// any implicit conversion by MySQL
} else if (is_float($value) and ($column->meta_type == 'C' or $column->meta_type == 'X')) {
$value = "$value";
}
// workaround for problem with wrong enums in mysql - TODO: Out in Moodle 2.1
if (!empty($column->enums)) {
Expand Down
87 changes: 87 additions & 0 deletions lib/dml/simpletest/testdml.php
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,34 @@ public function test_insert_record() {
$DB->delete_records($tablename, array());
$id4 = $DB->insert_record($tablename, array('course' => 3));
$this->assertTrue($id3 < $id4);

// Test saving a float in a CHAR column, and reading it back.
$id = $DB->insert_record($tablename, array('onechar' => 1.0));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onechar' => 1e20));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onechar' => 1e-4));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onechar' => 1e-5));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onechar' => 1e-300));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onechar' => 1e300));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id)));

// Test saving a float in a TEXT column, and reading it back.
$id = $DB->insert_record($tablename, array('onetext' => 1.0));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e20));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e-4));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e-5));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e-300));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$id = $DB->insert_record($tablename, array('onetext' => 1e300));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
}

public function test_import_record() {
Expand Down Expand Up @@ -2178,6 +2206,35 @@ public function test_update_record() {
$this->assertEqual($newclob, $record->onetext, 'Test "small" CLOB update (full contents output disabled)');
$this->assertEqual($newblob, $record->onebinary, 'Test "small" BLOB update (full contents output disabled)');

// Test saving a float in a CHAR column, and reading it back.
$id = $DB->insert_record($tablename, array('onechar' => 'X'));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1.0));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1e20));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-4));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-5));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-300));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onechar' => 1e300));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id)));

// Test saving a float in a TEXT column, and reading it back.
$id = $DB->insert_record($tablename, array('onetext' => 'X'));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1.0));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1e20));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-4));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-5));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-300));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->update_record($tablename, array('id' => $id, 'onetext' => 1e300));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
}

public function test_set_field() {
Expand Down Expand Up @@ -2243,6 +2300,36 @@ public function test_set_field() {
$this->assertEqual($e->errorcode, 'textconditionsnotallowed');
}

// Test saving a float in a CHAR column, and reading it back.
$id = $DB->insert_record($tablename, array('onechar' => 'X'));
$DB->set_field($tablename, 'onechar', 1.0, array('id' => $id));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->set_field($tablename, 'onechar', 1e20, array('id' => $id));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->set_field($tablename, 'onechar', 1e-4, array('id' => $id));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->set_field($tablename, 'onechar', 1e-5, array('id' => $id));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->set_field($tablename, 'onechar', 1e-300, array('id' => $id));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id)));
$DB->set_field($tablename, 'onechar', 1e300, array('id' => $id));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id)));

// Test saving a float in a TEXT column, and reading it back.
$id = $DB->insert_record($tablename, array('onetext' => 'X'));
$DB->set_field($tablename, 'onetext', 1.0, array('id' => $id));
$this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->set_field($tablename, 'onetext', 1e20, array('id' => $id));
$this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->set_field($tablename, 'onetext', 1e-4, array('id' => $id));
$this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->set_field($tablename, 'onetext', 1e-5, array('id' => $id));
$this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->set_field($tablename, 'onetext', 1e-300, array('id' => $id));
$this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id)));
$DB->set_field($tablename, 'onetext', 1e300, array('id' => $id));
$this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id)));

// Note: All the nulls, booleans, empties, quoted and backslashes tests
// go to set_field_select() because set_field() is just one wrapper over it
}
Expand Down

0 comments on commit 8f25ae2

Please sign in to comment.