MDL-16669 dml: improved sql_substring(), added unit tests
This commit is contained in:
@@ -15,6 +15,7 @@ $string['cannotsetupsite'] = 'Serious Error! Could not set up the site!';
|
||||
$string['cannotsetuptable'] = '$a tables could NOT be set up successfully!';
|
||||
$string['cannotfindadmin'] = 'Could not find an admin user!';
|
||||
$string['cannotupgradedbcustom'] = 'Upgrade of local database customisations failed! (Could not update version in config table)';
|
||||
$string['codingerror'] = 'Coding error detected, it must be fixed by a programmer: $a';
|
||||
$string['configmoodle'] = 'Moodle has not been configured yet. You need to edit config.php first.';
|
||||
$string['dbnotinsert'] = 'Database error - Cannot insert ($a)';
|
||||
$string['dbnotupdate'] = 'Database error - Cannot update ($a)';
|
||||
|
||||
+1
-7
@@ -5402,14 +5402,8 @@ function context_moved($context, $newparent) {
|
||||
$params = array($newpath, $frompath);
|
||||
$DB->execute($sql, $params);
|
||||
|
||||
$len = strlen($frompath);
|
||||
/// MDL-16655 - Substring MSSQL function *requires* 3rd parameter
|
||||
$substr3rdparam = '';
|
||||
if ($DB->get_dbfamily() == 'mssql') {
|
||||
$substr3rdparam = ', len(path)';
|
||||
}
|
||||
$sql = "UPDATE {context}
|
||||
SET path = ".$DB->sql_concat("?", $DB->sql_substr() .'(path, '.$len.' +1'.$substr3rdparam.')')."
|
||||
SET path = ".$DB->sql_concat("?", $DB->sql_substr("path", strlen($frompath)+1))."
|
||||
$setdepth
|
||||
WHERE path LIKE ?";
|
||||
$params = array($newpath, "{$frompath}/%");
|
||||
|
||||
@@ -487,10 +487,6 @@ abstract class adodb_moodle_database extends moodle_database {
|
||||
}
|
||||
}
|
||||
|
||||
public function sql_substr() {
|
||||
return $this->adodb->substr;
|
||||
}
|
||||
|
||||
public function sql_concat() {
|
||||
$args = func_get_args();
|
||||
return call_user_func_array(array($this->adodb, 'Concat'), $args);
|
||||
|
||||
@@ -1398,10 +1398,23 @@ abstract class moodle_database {
|
||||
|
||||
/**
|
||||
* Returns the proper substr() function for each DB.
|
||||
* NOTE: this was originally returning only function name
|
||||
*
|
||||
* Relies on ADOdb $adodb->substr property
|
||||
* @param string $expr some string field, no aggregates
|
||||
* @param mixed $start integer or expresion evaluating to int (1 based value; first char has index 1)
|
||||
* @param mixed $length optional integer or expresion evaluating to int
|
||||
* @return string sql fragment
|
||||
*/
|
||||
public abstract function sql_substr();
|
||||
public function sql_substr($expr, $start, $length=false) {
|
||||
if (count(func_get_args()) < 2) {
|
||||
throw new coding_exception('moodle_database::sql_substr() requires at least two parameters', 'Originaly this function was only returning name of SQL substring function, it now requires all parameters.');
|
||||
}
|
||||
if ($length === false) {
|
||||
return "SUBSTR($expr, $start";
|
||||
} else {
|
||||
return "SUBSTR($expr, $start, $length)";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SQL for returning searching one string for the location of another.
|
||||
|
||||
@@ -142,6 +142,23 @@ class mssql_adodb_moodle_database extends adodb_moodle_database {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the proper substr() function for each DB.
|
||||
* NOTE: this was originally returning only function name
|
||||
*
|
||||
* @param string $expr some string field, no aggregates
|
||||
* @param mixed $start integer or expresion evaluating to int
|
||||
* @param mixed $length optional integer or expresion evaluating to int
|
||||
* @return string sql fragment
|
||||
*/
|
||||
public function sql_substr($expr, $start, $length=false) {
|
||||
if ($length === false) {
|
||||
return "SUBSTRING($expr, $start, (LEN($expr) - $start + 1))";
|
||||
} else {
|
||||
return "SUBSTRING($expr, $start, $length)";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a record in a table
|
||||
*
|
||||
|
||||
@@ -868,11 +868,6 @@ class mysqli_native_moodle_database extends moodle_database {
|
||||
return "CONCAT ($s)";
|
||||
}
|
||||
|
||||
public function sql_substr() {
|
||||
return "SUBSTRING";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Does this driver suppoer regex syntax when searching
|
||||
*/
|
||||
|
||||
@@ -541,10 +541,6 @@ abstract class pdo_moodle_database extends moodle_database {
|
||||
return $this->execute($sql, $params);
|
||||
}
|
||||
|
||||
public function sql_substr() {
|
||||
error('TODO');
|
||||
}
|
||||
|
||||
public function sql_concat() {
|
||||
error('TODO');
|
||||
}
|
||||
|
||||
@@ -1036,10 +1036,6 @@ class pgsql_native_moodle_database extends moodle_database {
|
||||
return " $s ";
|
||||
}
|
||||
|
||||
public function sql_substr() {
|
||||
return "SUBSTRING";
|
||||
}
|
||||
|
||||
public function sql_regex_supported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1387,6 +1387,37 @@ class dml_test extends UnitTestCase {
|
||||
$this->assertEqual(1, $DB->count_records('testtable'));
|
||||
}
|
||||
|
||||
function test_sql_substring() {
|
||||
$DB = $this->tdb;
|
||||
$dbman = $DB->get_manager();
|
||||
|
||||
$table = $this->get_test_table($dbman, "testtable");
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
|
||||
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
||||
$dbman->create_table($table);
|
||||
$this->tables[$table->getName()] = $table;
|
||||
|
||||
$string = 'abcdefghij';
|
||||
|
||||
$DB->insert_record('testtable', array('name'=>$string));
|
||||
|
||||
$sql = "SELECT id, ".$DB->sql_substr("name", 5)." AS name FROM {testtable}";
|
||||
$record = $DB->get_record_sql($sql);
|
||||
$this->assertEqual(substr($string, 5-1), $record->name);
|
||||
|
||||
$sql = "SELECT id, ".$DB->sql_substr("name", 5, 2)." AS name FROM {testtable}";
|
||||
$record = $DB->get_record_sql($sql);
|
||||
$this->assertEqual(substr($string, 5-1, 2), $record->name);
|
||||
|
||||
try {
|
||||
@$DB->sql_substr("name");
|
||||
$this->fail("Expecting an exception, none occurred");
|
||||
} catch (Exception $e) {
|
||||
$this->assertTrue($e instanceof coding_exception);
|
||||
}
|
||||
}
|
||||
|
||||
function test_sql_position() {
|
||||
$DB = $this->tdb;
|
||||
$this->assertEqual($DB->get_field_sql(
|
||||
@@ -1502,5 +1533,5 @@ class moodle_database_for_testing extends moodle_database {
|
||||
public function delete_records_select($table, $select, array $params=null){}
|
||||
public function sql_concat(){}
|
||||
public function sql_concat_join($separator="' '", $elements=array()){}
|
||||
public function sql_substr(){}
|
||||
public function sql_substr($expr, $start, $length=false){}
|
||||
}
|
||||
|
||||
@@ -301,13 +301,6 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
|
||||
return $this->delete_records_select($table, $select, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the proper substr() function for each DB
|
||||
*/
|
||||
public function sql_substr() {
|
||||
return 'substr';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the proper SQL to do CONCAT between the elements passed
|
||||
* Can take many parameters
|
||||
|
||||
@@ -183,7 +183,7 @@ class file_storage {
|
||||
$sql = "SELECT *
|
||||
FROM {files}
|
||||
WHERE contextid = :contextid AND filearea = :filearea AND itemid = :itemid
|
||||
AND ".$DB->sql_substr()."(filepath, 1, $length) = :filepath
|
||||
AND ".$DB->sql_substr("filepath", 1, $length)." = :filepath
|
||||
AND id <> :dirid
|
||||
$dirs
|
||||
ORDER BY $sort";
|
||||
@@ -212,7 +212,7 @@ class file_storage {
|
||||
FROM {files}
|
||||
WHERE contextid = :contextid AND filearea = :filearea
|
||||
AND itemid = :itemid AND filename = '.'
|
||||
AND ".$DB->sql_substr()."(filepath, 1, $length) = :filepath
|
||||
AND ".$DB->sql_substr("filepath", 1, $length)." = :filepath
|
||||
AND id <> :dirid
|
||||
ORDER BY $sort";
|
||||
$reqlevel = substr_count($filepath, '/') + 1;
|
||||
|
||||
@@ -44,6 +44,21 @@ class moodle_exception extends Exception {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception indicating programming error, must be fixed by a programeer.
|
||||
*/
|
||||
class coding_exception extends moodle_exception {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param string $hint short description of problem
|
||||
* @param string $debuginfo detailed information how to fix problem
|
||||
*/
|
||||
function __construct($hint, $debuginfo=null) {
|
||||
parent::__construct('codingerror', 'debug', '', $hint, $debuginfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default exception handler, uncought exceptions are equivalent to using print_error()
|
||||
*/
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
} else {
|
||||
$usernamefield = $DB->sql_fullname('u.lastname' , 'u.firstname');
|
||||
}
|
||||
$where = "AND " . $DB->sql_substr() . "(upper($usernamefield),1," . $textlib->strlen($hook) . ") = :hookup";
|
||||
$where = "AND " . $DB->sql_substr("upper($usernamefield)", 1, $textlib->strlen($hook)) . " = :hookup";
|
||||
|
||||
if ( $hook == 'ALL' ) {
|
||||
$where = '';
|
||||
@@ -115,7 +115,7 @@
|
||||
$params['hookup'] = $textlib->strtoupper($hook);
|
||||
|
||||
if ($hook != 'ALL' and $hook != 'SPECIAL') {
|
||||
$where = 'AND ' . $DB->sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = :hookup';
|
||||
$where = "AND " . $DB->sql_substr("upper(concept)", 1, $textlib->strlen($hook)) . " = :hookup";
|
||||
}
|
||||
|
||||
$sqlselect = "SELECT ge.*, ge.concept AS glossarypivot";
|
||||
@@ -228,14 +228,14 @@
|
||||
case 'letter':
|
||||
if ($hook != 'ALL' and $hook != 'SPECIAL') {
|
||||
$params['hookup'] = $textlib->strtoupper($hook);
|
||||
$where = 'AND ' . $DB->sql_substr() . '(upper(concept),1,' . $textlib->strlen($hook) . ') = :hookup';
|
||||
$where = "AND " . $DB->sql_substr("upper(concept)", 1, $textlib->strlen($hook)) . " = :hookup";
|
||||
}
|
||||
if ($hook == 'SPECIAL') {
|
||||
//Create appropiate IN contents
|
||||
$alphabet = explode(",", get_string("alphabet"));
|
||||
list($nia, $aparams) = $DB->get_in_or_equal($alphabet, SQL_PARAMS_NAMED, $start='a0', false);
|
||||
$params = array_merge($params, $aparams);
|
||||
$where = 'AND ' . $DB->sql_substr() . "(upper(concept),1,1) $nia";
|
||||
$where = "AND " . $DB->sql_substr("upper(concept)", 1, 1) . " $nia";
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user