MDL-76459 dml: Remove other remaining harcoded uses of old 28 & 30

Let's require lib/ddllib.php and use the constant properly,
so we don't need to go searching for cases in future changes.

They are a hand of files, but already are included often each
time the database manager is included. Possible alternative
for this, if we want to reduce the number of included files
is to move the constants to xmldb_constants and only include
that file, but I don't think that makes much sense.

Also, fix the xmldb schema definition to validate table and
column names with the new allowed lengths.
This commit is contained in:
Eloy Lafuente (stronk7) 2023-03-10 18:11:14 +01:00
parent 8996ee0eaa
commit d2b6a1b7f3
No known key found for this signature in database
GPG Key ID: 53487A05E6228820
3 changed files with 14 additions and 6 deletions

View File

@ -891,6 +891,10 @@ abstract class moodle_database {
* @return array (sql, params, type of params)
*/
public function fix_sql_params($sql, array $params=null) {
global $CFG;
require_once($CFG->libdir . '/ddllib.php');
$params = (array)$params; // mke null array if needed
$allowed_types = $this->allowed_param_types();
@ -974,9 +978,9 @@ abstract class moodle_database {
if (!array_key_exists($key, $params)) {
throw new dml_exception('missingkeyinsql', $key, '');
}
if (strlen($key) > 30) {
if (strlen($key) > xmldb_field::NAME_MAX_LENGTH) {
throw new coding_exception(
"Placeholder names must be 30 characters or shorter. '" .
"Placeholder names must be " . xmldb_field::NAME_MAX_LENGTH . " characters or shorter. '" .
$key . "' is too long.", $sql);
}
$finalparams[$key] = $params[$key];

View File

@ -361,6 +361,10 @@ class oci_native_moodle_database extends moodle_database {
* @return array ($sql, $params) updated query and parameters
*/
protected function tweak_param_names($sql, array $params) {
global $CFG;
require_once($CFG->libdir . '/ddllib.php');
if (empty($params)) {
return array($sql, $params);
}
@ -368,8 +372,8 @@ class oci_native_moodle_database extends moodle_database {
$newparams = array();
$searcharr = array(); // search => replace pairs
foreach ($params as $name => $value) {
// Keep the name within the 30 chars limit always (prefixing/replacing)
if (strlen($name) <= 28) {
// Keep the name within the xmldb_field::NAME_MAX_LENGTH chars limit always (prefixing/replacing).
if (strlen($name) <= (xmldb_field::NAME_MAX_LENGTH - 2)) {
$newname = 'o_' . $name;
} else {
$newname = 'o_' . substr($name, 2);

View File

@ -10,13 +10,13 @@
<xs:simpleType name="tableName" >
<xs:restriction base="xs:string" >
<xs:pattern value='[0-9a-z_]{1,28}'/>
<xs:pattern value='[0-9a-z_]{1,53}'/>
</xs:restriction >
</xs:simpleType >
<xs:simpleType name="fieldName" >
<xs:restriction base="xs:string" >
<xs:pattern value='[0-9a-z_]{1,30}'/>
<xs:pattern value='[0-9a-z_]{1,63}'/>
</xs:restriction >
</xs:simpleType >