diff --git a/.upgradenotes/MDL-81282-2024052421193784.yml b/.upgradenotes/MDL-81282-2024052421193784.yml new file mode 100644 index 00000000000..0a76c9712c0 --- /dev/null +++ b/.upgradenotes/MDL-81282-2024052421193784.yml @@ -0,0 +1,7 @@ +issueNumber: MDL-81282 +notes: + core: + - message: >- + New DML constant `SQL_INT_MAX` to define the size of a large integer + with cross database platform support + type: improved diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index a97f884217d..8ff9c802854 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -37,6 +37,9 @@ define('SQL_PARAMS_QM', 2); /** SQL_PARAMS_DOLLAR - Bitmask, indicates $1, $2, ... type parameters are supported by db backend. */ define('SQL_PARAMS_DOLLAR', 4); +/** SQL_INT_MAX - Size of a large integer with cross database platform support. */ +define('SQL_INT_MAX', 9999999999); + /** SQL_QUERY_SELECT - Normal select query, reading only. */ define('SQL_QUERY_SELECT', 1); diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 653f158c329..584d3ba691f 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -4673,6 +4673,44 @@ EOD; ], $DB->get_records_sql($sql)); } + /** + * Test that the SQL_MAX_INT constant can be used for all insert, update, select and delete queries + */ + public function test_sql_max_int(): void { + $DB = $this->tdb; + $dbman = $DB->get_manager(); + + $table = $this->get_test_table(); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('intfield', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_field('charfield', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $dbman->create_table($table); + + $tablename = $table->getName(); + + // Insert. + $id = $DB->insert_record($tablename, ['intfield' => SQL_INT_MAX, 'charfield' => 'Test']); + $this->assertEquals((object) [ + 'intfield' => SQL_INT_MAX, + 'charfield' => 'Test', + ], $DB->get_record($tablename, ['id' => $id], 'intfield, charfield')); + + // Update. + $DB->set_field($tablename, 'charfield', 'Test 2', ['intfield' => SQL_INT_MAX]); + $this->assertEquals((object) [ + 'intfield' => SQL_INT_MAX, + 'charfield' => 'Test 2', + ], $DB->get_record($tablename, ['id' => $id], 'intfield, charfield')); + + // Select. + $this->assertEquals('Test 2', $DB->get_field($tablename, 'charfield', ['intfield' => SQL_INT_MAX])); + + // Delete. + $DB->delete_records($tablename, ['intfield' => SQL_INT_MAX]); + $this->assertFalse($DB->record_exists($tablename, ['id' => $id])); + } + public function test_sql_fullname() { $DB = $this->tdb; $sql = "SELECT ".$DB->sql_fullname(':first', ':last')." AS fullname ".$DB->sql_null_from_clause();