$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)';
$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}/%");
}
}
- 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);
/**
* 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.
}
}
+ /**
+ * 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
*
return "CONCAT ($s)";
}
- public function sql_substr() {
- return "SUBSTRING";
- }
-
-
/**
* Does this driver suppoer regex syntax when searching
*/
return $this->execute($sql, $params);
}
- public function sql_substr() {
- error('TODO');
- }
-
public function sql_concat() {
error('TODO');
}
return " $s ";
}
- public function sql_substr() {
- return "SUBSTRING";
- }
-
public function sql_regex_supported() {
return true;
}
$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(
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){}
}
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
$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";
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;
}
}
+/**
+ * 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()
*/
} 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 = '';
$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";
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;
}