MDL-81282 dml: define SQL_INT_MAX, to determine large supported ints.

The constant can be used by calling code in place of various adhoc
versions of the same, when they require a large value with known cross
platform support.
This commit is contained in:
Paul Holden
2024-05-24 22:19:54 +01:00
parent d32844ce29
commit 96ded90e68
3 changed files with 48 additions and 0 deletions
@@ -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
+3
View File
@@ -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);
+38
View File
@@ -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();