From 245ac557918d16144e69102e055a3e87ed0993b1 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 9 Jun 2008 19:48:24 +0000 Subject: [PATCH] MDL-14679 last forgotten bits of dml conversion --- auth/db/auth.php | 6 +- enrol/authorize/localfuncs.php | 16 +- enrol/database/enrol.php | 12 +- lib/datalib.php | 12 +- lib/db/upgradelib.php | 2 +- lib/ddl/mssql_sql_generator.php | 37 +- lib/ddl/oracle_sql_generator.php | 47 +- lib/ddl/postgres_sql_generator.php | 37 +- lib/ddl/sql_generator.php | 2 +- lib/deprecatedlib.php | 265 +++++++++- lib/dmllib.php | 323 ------------ lib/dmllib_todo.php | 712 -------------------------- lib/simpletest/fixtures/gradetest.php | 8 +- lib/simpletest/testeventslib.php | 2 +- lib/simpletestlib.php | 239 --------- message/lib.php | 4 +- mod/data/field/date/field.class.php | 4 +- mod/data/view.php | 2 +- mod/forum/lib.php | 4 +- mod/glossary/editcategories.php | 2 +- mod/hotpot/index.php | 2 +- mod/quiz/backuplib.php | 2 +- mod/quiz/edit.php | 2 +- mod/quiz/locallib.php | 2 +- 24 files changed, 369 insertions(+), 1375 deletions(-) delete mode 100644 lib/dmllib_todo.php diff --git a/auth/db/auth.php b/auth/db/auth.php index a09ba11ab0..ac42e62919 100644 --- a/auth/db/auth.php +++ b/auth/db/auth.php @@ -174,12 +174,12 @@ class auth_plugin_db extends auth_plugin_base { " WHERE {$this->config->fielduser} = '".$this->ext_addslashes($extusername)."'"; if ($rs = $authdb->Execute($sql)) { if ( !$rs->EOF ) { - $fields_obj = rs_fetch_record($rs); + $fields_obj = $rs->FetchObj(); foreach ($selectfields as $localname=>$externalname) { $result[$localname] = $textlib->convert($fields_obj->{$localname}, $this->config->extencoding, 'utf-8'); } } - rs_close($rs); + $rs->Close(); } } $authdb->Close(); @@ -418,7 +418,7 @@ class auth_plugin_db extends auth_plugin_base { if (!$rs) { print_error('auth_dbcantconnect','auth'); } else if ( !$rs->EOF ) { - while ($rec = rs_fetch_next_record($rs)) { + while ($rec = $rs->FetchRow()) { array_push($result, $rec->username); } } diff --git a/enrol/authorize/localfuncs.php b/enrol/authorize/localfuncs.php index 5262e95c17..887cf6aa14 100644 --- a/enrol/authorize/localfuncs.php +++ b/enrol/authorize/localfuncs.php @@ -137,11 +137,15 @@ function send_welcome_messages($orderdata) $sql = "SELECT e.id, e.courseid, e.userid, c.fullname FROM {enrol_authorize} e - INNER JOIN {course} c ON c.id = e.courseid + JOIN {course} c ON c.id = e.courseid WHERE e.id IN(" . implode(',', $orderdata) . ") ORDER BY e.userid"; - if (($rs = $DB->get_recordset_sql($sql)) && ($ei = rs_fetch_next_record($rs))) + if (!$rs = $DB->get_recordset_sql($sql)) { + return; + } + + if ($ts->valid() and $ei = current($rs)) { if (1 < count($orderdata)) { $sender = get_admin(); @@ -159,7 +163,11 @@ function send_welcome_messages($orderdata) while ($ei && $ei->userid == $lastuserid) { $usercourses[] = $ei->fullname; - $ei = rs_fetch_next_record($rs); + if (!$rs->valid()) { + break; + } + $rs->next(); + $ei = $rs->current(); } if (($user = $DB->get_record('user', array('id'=>$lastuserid)))) { @@ -174,7 +182,7 @@ function send_welcome_messages($orderdata) } while ($ei); - rs_close($rs); + $rs->close(); } } diff --git a/enrol/database/enrol.php b/enrol/database/enrol.php index 17841e0582..f848a0ff6e 100644 --- a/enrol/database/enrol.php +++ b/enrol/database/enrol.php @@ -83,11 +83,11 @@ function setup_enrolments(&$user) { //$count = 0; $courselist = array(); - while ($fields_obj = rs_fetch_next_record($rs)) { // Make a nice little array of courses to process + while ($fields_obj = (object)$rs->FetchRow()) { // Make a nice little array of courses to process $courselist[] = $fields_obj->enrolremotecoursefield; //$count++; } - rs_close($rs); + $rs->close(); //error_log('[ENROL_DB] Found '.count($existing).' existing roles and '.$count.' in external database'); @@ -220,7 +220,7 @@ function sync_enrolments($role = null) { $DB->begin_sql(); $extcourses = array(); - while ($extcourse_obj = rs_fetch_next_record($rs)) { // there are more course records + while ($extcourse_obj = (object)$rs->FetchRow()) { // there are more course records $extcourse = $extcourse_obj->{$CFG->enrol_remotecoursefield}; array_push($extcourses, $extcourse); @@ -275,10 +275,10 @@ function sync_enrolments($role = null) { } // slurp results into an array - while ($crs_obj = rs_fetch_next_record($crs)) { + while ($crs_obj = (object)$crs->FetchRow()) { array_push($extenrolments, $crs_obj->{$CFG->enrol_remoteuserfield}); } - rs_close($crs); // release the handle + $crs->close(); // release the handle // // prune enrolments to users that are no longer in ext auth @@ -362,7 +362,7 @@ function sync_enrolments($role = null) { } // end foreach member } // end while course records - rs_close($rs); //Close the main course recordset + $rs->close(); //Close the main course recordset // // prune enrolments to courses that are no longer in ext auth diff --git a/lib/datalib.php b/lib/datalib.php index f9a96df79b..04fdd688cb 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -138,8 +138,8 @@ function count_courses_notin_metacourse($metacourseid) { function search_users($courseid, $groupid, $searchtext, $sort='', array $exceptions=null) { global $DB; - $LIKE = sql_ilike(); - $fullname = sql_fullname('u.firstname', 'u.lastname'); + $LIKE = $DB->sql_ilike(); + $fullname = $DB->sql_fullname('u.firstname', 'u.lastname'); if (!empty($exceptions)) { list($exceptions, $params) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'ex0000', false); @@ -220,8 +220,8 @@ function get_users($get=true, $search='', $confirmed=false, array $exceptions=nu 'load so much data into memory.', DEBUG_DEVELOPER); } - $LIKE = sql_ilike(); - $fullname = sql_fullname(); + $LIKE = $DB->sql_ilike(); + $fullname = $DB->sql_fullname(); $select = " username <> :guest AND deleted = 0"; $params = array('guest'=>'guest'); @@ -286,8 +286,8 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp $search='', $firstinitial='', $lastinitial='', $extraselect='', array $extraparams=null) { global $DB; - $LIKE = sql_ilike(); - $fullname = sql_fullname(); + $LIKE = $DB->sql_ilike(); + $fullname = $DB->sql_fullname(); $select = "deleted <> 1"; $params = array(); diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index ced4b2897d..26d653770e 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -26,7 +26,7 @@ function upgrade_fix_category_depths() { // now add path and depth to top level categories $sql = "UPDATE {course_categories} - SET depth = 1, path = ".sql_concat("'/'", "id")." + SET depth = 1, path = ".$DB->sql_concat("'/'", "id")." WHERE parent = 0"; $DB->execute($sql); diff --git a/lib/ddl/mssql_sql_generator.php b/lib/ddl/mssql_sql_generator.php index 0269d4e577..f6b995d5db 100644 --- a/lib/ddl/mssql_sql_generator.php +++ b/lib/ddl/mssql_sql_generator.php @@ -460,10 +460,10 @@ class mssql_sql_generator extends sql_generator { $fieldname = $xmldb_field->getName(); /// Look for any default constraint in this field and drop it - if ($default = get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint - FROM syscolumns - WHERE id = object_id('{$tablename}') - AND name = '{$fieldname}'")) { + if ($default = $this->mdb->get_record_sql("SELECT id, object_name(cdefault) AS defaultconstraint + FROM syscolumns + WHERE id = object_id(?) + AND name = ?", array($tablename, $fieldname))) { return $default->defaultconstraint; } else { return false; @@ -479,19 +479,20 @@ class mssql_sql_generator extends sql_generator { * If no check constraints are found, returns an empty array */ public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) { + $results = array(); $tablename = $this->getTableName($xmldb_table); - if ($constraints = get_records_sql("SELECT o.name, c.text AS description - FROM sysobjects o, - sysobjects p, - syscomments c - WHERE p.id = o.parent_obj - AND o.id = c.id - AND o.xtype = 'C' - AND p.name = '{$tablename}'")) { + if ($constraints = $this->mdb->get_records_sql("SELECT o.name, c.text AS description + FROM sysobjects o, + sysobjects p, + syscomments c + WHERE p.id = o.parent_obj + AND o.id = c.id + AND o.xtype = 'C' + AND p.name = ?", array($tablename))) { foreach ($constraints as $constraint) { $results[$constraint->name] = $constraint; } @@ -529,17 +530,17 @@ class mssql_sql_generator extends sql_generator { case 'uk': case 'fk': case 'ck': - if ($check = get_records_sql("SELECT name - FROM sysobjects - WHERE lower(name) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT name + FROM sysobjects + WHERE lower(name) = ?", array(strtolower($object_name)))) { return true; } break; case 'ix': case 'uix': - if ($check = get_records_sql("SELECT name - FROM sysindexes - WHERE lower(name) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT name + FROM sysindexes + WHERE lower(name) = ?", array(strtolower($object_name)))) { return true; } break; diff --git a/lib/ddl/oracle_sql_generator.php b/lib/ddl/oracle_sql_generator.php index 7579579901..da67f8c809 100644 --- a/lib/ddl/oracle_sql_generator.php +++ b/lib/ddl/oracle_sql_generator.php @@ -325,10 +325,10 @@ class oracle_sql_generator extends sql_generator { if ($oldmetatype == 'N') { $uppertablename = strtoupper($tablename); $upperfieldname = strtoupper($fieldname); - if ($col = get_record_sql("SELECT cname, precision - FROM col - WHERE tname = '$uppertablename' - AND cname = '$upperfieldname'")) { + if ($col = $this->mdb->get_record_sql("SELECT cname, precision + FROM col + WHERE tname = ? AND cname = ?", + array($uppertablename, $upperfieldname))) { $oldlength = $col->precision; } } @@ -517,11 +517,12 @@ class oracle_sql_generator extends sql_generator { $tablename = strtoupper($this->getTableName($xmldb_table)); - if ($constraints = get_records_sql("SELECT lower(c.constraint_name) AS name, c.search_condition AS description - FROM user_constraints c - WHERE c.table_name = '{$tablename}' - AND c.constraint_type = 'C' - AND c.constraint_name not like 'SYS%'")) { + if ($constraints = $this->mdb->get_records_sql("SELECT lower(c.constraint_name) AS name, c.search_condition AS description + FROM user_constraints c + WHERE c.table_name = ? + AND c.constraint_type = 'C' + AND c.constraint_name not like 'SYS%'", + array($tablename))) { foreach ($constraints as $constraint) { $results[$constraint->name] = $constraint; } @@ -559,10 +560,10 @@ class oracle_sql_generator extends sql_generator { $prefixupper = strtoupper($this->prefix); $sequencename = false; - if ($trigger = get_record_sql("SELECT trigger_name, trigger_body - FROM user_triggers - WHERE table_name = '{$tablename}' - AND trigger_name LIKE '{$prefixupper}%_ID%_TRG'")) { + if ($trigger = $this->mdb->get_record_sql("SELECT trigger_name, trigger_body + FROM user_triggers + WHERE table_name = ? AND trigger_name LIKE ?", + array($tablename, "{$prefixupper}%_ID%_TRG"))) { /// If trigger found, regexp it looking for the sequence name preg_match('/.*SELECT (.*)\.nextval/i', $trigger->trigger_body, $matches); if (isset($matches[1])) { @@ -584,10 +585,10 @@ class oracle_sql_generator extends sql_generator { $prefixupper = strtoupper($this->prefix); $triggername = false; - if ($trigger = get_record_sql("SELECT trigger_name, trigger_body - FROM user_triggers - WHERE table_name = '{$tablename}' - AND trigger_name LIKE '{$prefixupper}%_ID%_TRG'")) { + if ($trigger = $this->mdb->get_record_sql("SELECT trigger_name, trigger_body + FROM user_triggers + WHERE table_name = ? AND trigger_name LIKE ?", + array($tablename, "{$prefixupper}%_ID%_TRG"))) { $triggername = $trigger->trigger_name; } @@ -605,9 +606,9 @@ class oracle_sql_generator extends sql_generator { case 'uix': case 'seq': case 'trg': - if ($check = get_records_sql("SELECT object_name - FROM user_objects - WHERE lower(object_name) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT object_name + FROM user_objects + WHERE lower(object_name) = ?", array(strtolower($object_name)))) { return true; } break; @@ -615,9 +616,9 @@ class oracle_sql_generator extends sql_generator { case 'uk': case 'fk': case 'ck': - if ($check = get_records_sql("SELECT constraint_name - FROM user_constraints - WHERE lower(constraint_name) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT constraint_name + FROM user_constraints + WHERE lower(constraint_name) = ?", array(strtolower($object_name)))) { return true; } break; diff --git a/lib/ddl/postgres_sql_generator.php b/lib/ddl/postgres_sql_generator.php index b5aaccb613..d15b589d1a 100644 --- a/lib/ddl/postgres_sql_generator.php +++ b/lib/ddl/postgres_sql_generator.php @@ -445,12 +445,11 @@ class postgres_sql_generator extends sql_generator { $tablename = $this->getTableName($xmldb_table); - if ($constraints = get_records_sql("SELECT co.conname AS name, co.consrc AS description - FROM pg_constraint co, - pg_class cl - WHERE co.conrelid = cl.oid - AND co.contype = 'c' - AND cl.relname = '{$tablename}'")) { + if ($constraints = $this->mdb->get_records_sql("SELECT co.conname AS name, co.consrc AS description + FROM pg_constraint co, pg_class cl + WHERE co.conrelid = cl.oid + AND co.contype = 'c' AND cl.relname = ?", + array($tablename))) { foreach ($constraints as $constraint) { $results[$constraint->name] = $constraint; } @@ -492,10 +491,10 @@ function getSequenceFromDB($xmldb_table) { $tablename = $this->getTableName($xmldb_table); $sequencename = $tablename . '_id_seq'; - if (!get_record_sql("SELECT * - FROM pg_class - WHERE relname = '{$sequencename}' - AND relkind = 'S'")) { + if (!$this->mdb->get_record_sql("SELECT * + FROM pg_class + WHERE relname = ? AND relkind = 'S'", + array($sequencename))) { $sequencename = false; } @@ -512,9 +511,9 @@ function getSequenceFromDB($xmldb_table) { case 'ix': case 'uix': case 'seq': - if ($check = get_records_sql("SELECT relname - FROM pg_class - WHERE lower(relname) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT relname + FROM pg_class + WHERE lower(relname) = ?", array(strtolower($object_name)))) { return true; } break; @@ -522,16 +521,16 @@ function getSequenceFromDB($xmldb_table) { case 'uk': case 'fk': case 'ck': - if ($check = get_records_sql("SELECT conname - FROM pg_constraint - WHERE lower(conname) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT conname + FROM pg_constraint + WHERE lower(conname) = ?", array(strtolower($object_name)))) { return true; } break; case 'trg': - if ($check = get_records_sql("SELECT tgname - FROM pg_trigger - WHERE lower(tgname) = '" . strtolower($object_name) . "'")) { + if ($check = $this->mdb->get_records_sql("SELECT tgname + FROM pg_trigger + WHERE lower(tgname) = ?", array(strtolower($object_name)))) { return true; } break; diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php index 958e583406..c6881ea1e9 100644 --- a/lib/ddl/sql_generator.php +++ b/lib/ddl/sql_generator.php @@ -1152,7 +1152,7 @@ abstract class sql_generator { } } - /// Now call the standard sql_concat() DML function + /// Now call the standard $DB->sql_concat() DML function return call_user_func_array(array($this->mdb, 'sql_concat'), $elements); } diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 648a4fa0da..27e7dbf833 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -436,7 +436,7 @@ function get_current_group($courseid, $full = false) { * @param string $message The message to display to the user about the error. * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. */ -function error ($message, $link='') { +function error($message, $link='') { global $CFG, $SESSION, $THEME; debugging('error() is a deprecated function, please call print_error() instead of error()', DEBUG_DEVELOPER); @@ -498,7 +498,7 @@ function error ($message, $link='') { } /// removed functions -function addslashes_object( $dataobject ) { +function addslashes_object($dataobject) { error('addslashes() not available anymore'); } @@ -506,6 +506,265 @@ function addslashes_recursive($var) { error('addslashes_recursive() not available anymore'); } +function execute_sql($command, $feedback=true) { + error('execute_sql() not available anymore'); +} + +function record_exists_select($table, $select='') { + error('record_exists_select() not available anymore'); +} + +function record_exists_sql($sql) { + error('record_exists_sql() not available anymore'); +} + +function count_records_select($table, $select='', $countitem='COUNT(*)') { + error('count_records_select() not available anymore'); +} + +function count_records_sql($sql) { + error('count_records_sql() not available anymore'); +} + +function get_record_sql($sql, $expectmultiple=false, $nolimit=false) { + error('get_record_sql() not available anymore'); +} + +function get_record_select($table, $select='', $fields='*') { + error('get_record_select() not available anymore'); +} + +function get_recordset($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_recordset() not available anymore'); +} + +function get_recordset_sql($sql, $limitfrom=null, $limitnum=null) { + error('get_recordset_sql() not available anymore'); +} + +function rs_fetch_record(&$rs) { + error('rs_fetch_record() not available anymore'); +} + +function rs_next_record(&$rs) { + error('rs_next_record() not available anymore'); +} + +function rs_fetch_next_record(&$rs) { + error('rs_fetch_next_record() not available anymore'); +} + +function rs_EOF($rs) { + error('rs_EOF() not available anymore'); +} + +function rs_close(&$rs) { + error('rs_close() not available anymore'); +} + +function get_records_select($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_records_select() not available anymore'); +} + +function get_field_select($table, $return, $select) { + error('get_field_select() not available anymore'); +} + +function get_field_sql($sql) { + error('get_field_sql() not available anymore'); +} +function delete_records_select($table, $select='') { + delete_records_select('get_field_sql() not available anymore'); +} + + +function configure_dbconnection() { + error('configure_dbconnection() removed'); +} + +function sql_max($field) { + error('sql_max() removed - use normal sql MAX() instead'); +} + +function sql_as() { + error('sql_as() removed - do not use AS for tables at all'); +} + +function sql_paging_limit($page, $recordsperpage) { + error('Function sql_paging_limit() is deprecated. Replace it with the correct use of limitfrom, limitnum parameters'); +} + +function db_uppercase() { + error('upper() removed - use normal sql UPPER()'); +} + +function db_lowercase() { + error('upper() removed - use normal sql LOWER()'); +} + +function modify_database($sqlfile='', $sqlstring='') { + error('modify_database() removed - use new XMLDB functions'); +} + +function where_clause($field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { + error('where_clause() removed - use new functions with $conditions parameter'); +} + +function execute_sql_arr($sqlarr, $continue=true, $feedback=true) { + error('execute_sql_arr() removed'); +} + +function get_records_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_records_list() removed'); +} + +function get_recordset_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_recordset_list() removed'); +} + +function get_records_menu($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_records_menu() removed'); +} + +function get_records_select_menu($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_records_select_menu() removed'); +} + +function get_records_sql_menu($sql, $limitfrom='', $limitnum='') { + error('get_records_sql_menu() removed'); +} + +function column_type($table, $column) { + error('column_type() removed'); +} + +function recordset_to_menu($rs) { + error('recordset_to_menu() removed'); +} + +function records_to_menu($records, $field1, $field2) { + error('records_to_menu() removed'); +} + +function set_field_select($table, $newfield, $newvalue, $select, $localcall = false) { + error('set_field_select() removed'); +} + +function get_fieldset_select($table, $return, $select) { + error('get_fieldset_select() removed'); +} + +function get_fieldset_sql($sql) { + error('get_fieldset_sql() removed'); +} + +function sql_ilike() { + error('sql_ilike() not available anymore'); +} + +function sql_fullname($first='firstname', $last='lastname') { + error('sql_fullname() not available anymore'); +} + +function sql_concat() { + error('sql_concat() not available anymore'); +} + +function sql_empty() { + error('sql_empty() not available anymore'); +} + +function sql_substr() { + error('sql_substr() not available anymore'); +} + +function sql_bitand($int1, $int2) { + error('sql_bitand() not available anymore'); +} + +function sql_bitnot($int1) { + error('sql_bitnot() not available anymore'); +} + +function sql_bitor($int1, $int2) { + error('sql_bitor() not available anymore'); +} + +function sql_bitxor($int1, $int2) { + error('sql_bitxor() not available anymore'); +} + +function sql_cast_char2int($fieldname, $text=false) { + error('sql_cast_char2int() not available anymore'); +} + +function sql_compare_text($fieldname, $numchars=32) { + error('sql_compare_text() not available anymore'); +} + +function sql_order_by_text($fieldname, $numchars=32) { + error('sql_order_by_text() not available anymore'); +} + +function sql_concat_join($separator="' '", $elements=array()) { + error('sql_concat_join() not available anymore'); +} + +function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { + error('sql_isempty() not available anymore'); +} + +function sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield) { + error('sql_isnotempty() not available anymore'); +} + +function begin_sql() { + error('begin_sql() not available anymore'); +} + +function commit_sql() { + error('commit_sql() not available anymore'); +} + +function rollback_sql() { + error('rollback_sql() not available anymore'); +} + +function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { + error('insert_record() not available anymore'); +} + +function update_record($table, $dataobject) { + error('update_record() not available anymore'); +} + +function get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { + error('get_records() not available anymore'); +} + +function get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*') { + error('get_record() not available anymore'); +} + +function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { + error('set_field() not available anymore'); +} + +function count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { + error('count_records() not available anymore'); +} + +function record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { + error('record_exists() not available anymore'); +} + +function delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { + error('delete_records() not available anymore'); +} + +function get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { + error('get_field() not available anymore'); +} -?> + \ No newline at end of file diff --git a/lib/dmllib.php b/lib/dmllib.php index c862a635ae..5e599fe346 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -138,326 +138,3 @@ function setup_DB() { return true; } - - - - - - - - - - - - -/////// DEPRECATED - works fine - - -function sql_ilike() { - global $DB; - return $DB->sql_ilike(); -} - -function sql_fullname($first='firstname', $last='lastname') { - global $DB; - return $DB->sql_fullname($first, $last); -} - -function sql_concat() { - global $DB; - - $args = func_get_args(); - return call_user_func_array(array($DB, 'sql_concat'), $args); -} - -function sql_empty() { - global $DB; - return $DB->sql_empty(); -} - -function sql_substr() { - global $DB; - return $DB->sql_substr(); -} - -function sql_bitand($int1, $int2) { - global $DB; - return $DB->sql_bitand($int1, $int2); -} - -function sql_bitnot($int1) { - global $DB; - return $DB->sql_bitnot($int1); -} - -function sql_bitor($int1, $int2) { - global $DB; - return $DB->sql_bitor($int1); - -} - -function sql_bitxor($int1, $int2) { - global $DB; - return $DB->sql_bitxor($int1, $int2); - -} - -function sql_cast_char2int($fieldname, $text=false) { - global $DB; - return $DB->sql_cast_char2int($fieldname, $text); -} - -function sql_compare_text($fieldname, $numchars=32) { - return sql_order_by_text($fieldname, $numchars); -} - -function sql_order_by_text($fieldname, $numchars=32) { - global $DB; - return $DB->sql_order_by_text($fieldname, $numchars); -} - - -function sql_concat_join($separator="' '", $elements=array()) { - global $DB; - return $DB->sql_concat_join($separator, $elements); -} - -function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { - global $DB; - return $DB->sql_isempty($tablename, $fieldname, $nullablefield, $textfield); -} - -function sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield) { - global $DB; - return $DB->sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield); -} - - -function begin_sql() { - global $DB; - return $DB->begin_sql(); -} - -function commit_sql() { - global $DB; - return $DB->commit_sql(); -} - -function rollback_sql() { - global $DB; - return $DB->rollback_sql(); -} - -function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { - global $DB; - - return $DB->insert_record($table, $dataobject, $returnid); -} - -function update_record($table, $dataobject) { - global $DB; - - return $DB->update_record($table, $dataobject, true); -} - -function get_records($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - global $DB; - - $conditions = array(); - if ($field) { - $conditions[$field] = $value; - } - - return $DB->get_records($table, $conditions, $sort, $fields, $limitfrom, $limitnum); -} - -function get_record($table, $field1, $value1, $field2='', $value2='', $field3='', $value3='', $fields='*') { - global $DB; - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->get_record($table, $conditions, $fields); -} - -function set_field($table, $newfield, $newvalue, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { - global $DB; - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->set_field($table, $newfield, $newvalue, $conditions); -} - -function count_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { - global $DB; - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->count_records($table, $conditions); -} - -function record_exists($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { - global $DB; - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->record_exists($table, $conditions); -} - -function delete_records($table, $field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { - global $DB; - - if (is_array($field1)) { - error('Incorrect parameter! (probably missing $DB->)'); - } - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->delete_records($table, $conditions); -} - -function get_field($table, $return, $field1, $value1, $field2='', $value2='', $field3='', $value3='') { - global $DB; - - $conditions = array(); - if ($field1) { - $conditions[$field1] = $value1; - } - if ($field2) { - $conditions[$field2] = $value2; - } - if ($field3) { - $conditions[$field3] = $value3; - } - - return $DB->get_field($table, $return, $conditions); -} - - - - -///// DELETED - must not be used anymore! - -function configure_dbconnection() { - error('configure_dbconnection() removed'); -} - -function sql_max($field) { - error('sql_max() removed - use normal sql MAX() instead'); -} - -function sql_as() { - error('sql_as() removed - do not use AS for tables at all'); -} - -function sql_paging_limit($page, $recordsperpage) { - error('Function sql_paging_limit() is deprecated. Replace it with the correct use of limitfrom, limitnum parameters'); -} - -function db_uppercase() { - error('upper() removed - use normal sql UPPER()'); -} - -function db_lowercase() { - error('upper() removed - use normal sql LOWER()'); -} - -function modify_database($sqlfile='', $sqlstring='') { - error('modify_database() removed - use new XMLDB functions'); -} - -function where_clause($field1='', $value1='', $field2='', $value2='', $field3='', $value3='') { - error('where_clause() removed - use new functions with $conditions parameter'); -} - -function execute_sql_arr($sqlarr, $continue=true, $feedback=true) { - error('execute_sql_arr() removed'); -} - -function get_records_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - error('get_records_list() removed'); -} - -function get_recordset_list($table, $field='', $values='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - error('get_recordset_list() removed'); -} - -function get_records_menu($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - error('get_records_menu() removed'); -} - -function get_records_select_menu($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - error('get_records_select_menu() removed'); -} - -function get_records_sql_menu($sql, $limitfrom='', $limitnum='') { - error('get_records_sql_menu() removed'); -} - -function column_type($table, $column) { - error('column_type() removed'); -} - -function recordset_to_menu($rs) { - error('recordset_to_menu() removed'); -} - -function records_to_menu($records, $field1, $field2) { - error('records_to_menu() removed'); -} - -function set_field_select($table, $newfield, $newvalue, $select, $localcall = false) { - error('set_field_select() removed'); -} - -function get_fieldset_select($table, $return, $select) { - error('get_fieldset_select() removed'); -} - -function get_fieldset_sql($sql) { - error('get_fieldset_sql() removed'); -} diff --git a/lib/dmllib_todo.php b/lib/dmllib_todo.php deleted file mode 100644 index d6a3bcf696..0000000000 --- a/lib/dmllib_todo.php +++ /dev/null @@ -1,712 +0,0 @@ -debug; - - if (!$feedback) { - if ( !defined('CLI_UPGRADE') || !CLI_UPGRADE ) { - $db->debug = false; - } - } - - if ($CFG->version >= 2006101007) { //Look for trailing ; from Moodle 1.7.0 - $command = trim($command); - /// If the trailing ; is there, fix and warn! - if (substr($command, strlen($command)-1, 1) == ';') { - /// One noticeable exception, Oracle PL/SQL blocks require ending in ";" - if ($CFG->dbfamily == 'oracle' && substr($command, -4) == 'END;') { - /// Nothing to fix/warn. The command is one PL/SQL block, so it's ok. - } else { - $command = trim($command, ';'); - debugging('Warning. Avoid to end your SQL commands with a trailing ";".', DEBUG_DEVELOPER); - } - } - } - - if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; - - $rs = $db->Execute($command); - - $DB->reset_columns(); // Clear out the cache, just in case changes were made to table structures - - $db->debug = $olddebug; - - if ($rs) { - if ($feedback) { - notify(get_string('success'), 'notifysuccess'); - } - return true; - } else { - if ($feedback) { - if ( defined('CLI_UPGRADE') && CLI_UPGRADE ) { - notify (get_string('error')); - } else { - notify('' . get_string('error') . ''); - } - } - // these two may go to difference places - debugging($db->ErrorMsg() .'

'. s($command)); - if (!empty($CFG->dblogerror)) { - $debug=array_shift(debug_backtrace()); - error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $command"); - } - return false; - } -} - - -/** - * Test whether any records exists in a table which match a particular WHERE clause. - * - * @uses $CFG - * @param string $table The database table to be checked against. - * @param string $select A fragment of SQL to be used in a WHERE clause in the SQL call. - * @return bool true if a matching record exists, else false. - */ -function record_exists_select($table, $select='') { - - global $CFG; - - if ($select) { - $select = 'WHERE '.$select; - } - - return record_exists_sql('SELECT * FROM '. $CFG->prefix . $table . ' ' . $select); -} - -/** - * Test whether a SQL SELECT statement returns any records. - * - * This function returns true if the SQL statement executes - * without any errors and returns at least one record. - * - * @param string $sql The SQL statement to execute. - * @return bool true if the SQL executes without errors and returns at least one record. - */ -function record_exists_sql($sql) { - - $limitfrom = 0; /// Number of records to skip - $limitnum = 1; /// Number of records to retrieve - - if (!$rs = get_recordset_sql($sql, $limitfrom, $limitnum)) { - return false; - } - - if (rs_EOF($rs)) { - $result = false; - } else { - $result = true; - } - - rs_close($rs); - return $result; -} - -/** - * Count the records in a table which match a particular WHERE clause. - * - * @uses $CFG - * @param string $table The database table to be checked against. - * @param string $select A fragment of SQL to be used in a WHERE clause in the SQL call. - * @param string $countitem The count string to be used in the SQL call. Default is COUNT(*). - * @return int The count of records returned from the specified criteria. - */ -function count_records_select($table, $select='', $countitem='COUNT(*)') { - - global $CFG; - - if ($select) { - $select = 'WHERE '.$select; - } - - return count_records_sql('SELECT '. $countitem .' FROM '. $CFG->prefix . $table .' '. $select); -} - -/** - * Get the result of a SQL SELECT COUNT(...) query. - * - * Given a query that counts rows, return that count. (In fact, - * given any query, return the first field of the first record - * returned. However, this method should only be used for the - * intended purpose.) If an error occurrs, 0 is returned. - * - * @uses $CFG - * @uses $db - * @param string $sql The SQL string you wish to be executed. - * @return int the count. If an error occurrs, 0 is returned. - */ -function count_records_sql($sql) { - $rs = get_recordset_sql($sql); - - if (is_object($rs) and is_array($rs->fields)) { - return reset($rs->fields); - } else { - return 0; - } -} - -/// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA /////////////////////////////////// - - -/** - * Get a single record as an object using an SQL statement - * - * The SQL statement should normally only return one record. In debug mode - * you will get a warning if more record is returned (unless you - * set $expectmultiple to true). In non-debug mode, it just returns - * the first record. - * - * @uses $CFG - * @uses $db - * @param string $sql The SQL string you wish to be executed, should normally only return one record. - * @param bool $expectmultiple If the SQL cannot be written to conveniently return just one record, - * set this to true to hide the debug message. - * @param bool $nolimit sometimes appending ' LIMIT 1' to the SQL causes an error. Set this to true - * to stop your SQL being modified. This argument should probably be deprecated. - * @return Found record as object. False if not found or error - */ -function get_record_sql($sql, $expectmultiple=false, $nolimit=false) { - - global $CFG; - -/// Default situation - $limitfrom = 0; /// Number of records to skip - $limitnum = 1; /// Number of records to retrieve - -/// Only a few uses of the 2nd and 3rd parameter have been found -/// I think that we should avoid to use them completely, one -/// record is one record, and everything else should return error. -/// So the proposal is to change all the uses, (4-5 inside Moodle -/// Core), drop them from the definition and delete the next two -/// "if" sentences. (eloy, 2006-08-19) - - if ($nolimit) { - $limitfrom = 0; - $limitnum = 0; - } else if ($expectmultiple) { - $limitfrom = 0; - $limitnum = 1; - } else if (debugging('', DEBUG_DEVELOPER)) { - // Debugging mode - don't use a limit of 1, but do change the SQL, because sometimes that - // causes errors, and in non-debug mode you don't see the error message and it is - // impossible to know what's wrong. - $limitfrom = 0; - $limitnum = 100; - } - - if (!$rs = get_recordset_sql($sql, $limitfrom, $limitnum)) { - return false; - } - - $recordcount = $rs->RecordCount(); - - if ($recordcount == 0) { // Found no records - return false; - - } else if ($recordcount == 1) { // Found one record - /// DIRTY HACK to retrieve all the ' ' (1 space) fields converted back - /// to '' (empty string) for Oracle. It's the only way to work with - /// all those NOT NULL DEFAULT '' fields until we definitively delete them - if ($CFG->dbfamily == 'oracle') { - array_walk($rs->fields, 'onespace2empty'); - } - /// End of DIRTY HACK - return (object)$rs->fields; - - } else { // Error: found more than one record - notify('Error: Turn off debugging to hide this error.'); - notify($sql . '(with limits ' . $limitfrom . ', ' . $limitnum . ')'); - if ($records = $rs->GetAssoc(true)) { - notify('Found more than one record in get_record_sql !'); - print_object($records); - } else { - notify('Very strange error in get_record_sql !'); - print_object($rs); - } - print_continue("$CFG->wwwroot/$CFG->admin/config.php"); - } -} - -/** - * Gets one record from a table, as an object - * - * @uses $CFG - * @param string $table The database table to be checked against. - * @param string $select A fragment of SQL to be used in a where clause in the SQL call. - * @param string $fields A comma separated list of fields to be returned from the chosen table. - * @return object|false Returns an array of found records (as objects) or false if no records or error occured. - */ -function get_record_select($table, $select='', $fields='*') { - - global $CFG; - - if ($select) { - $select = 'WHERE '. $select; - } - - return get_record_sql('SELECT '. $fields .' FROM '. $CFG->prefix . $table .' '. $select); -} - -/** - * Get a number of records as an ADODB RecordSet. - * - * Selects records from the table $table. - * - * If specified, only records where the field $field has value $value are retured. - * - * If specified, the results will be sorted as specified by $sort. This - * is added to the SQL as "ORDER BY $sort". Example values of $sort - * mightbe "time ASC" or "time DESC". - * - * If $fields is specified, only those fields are returned. - * - * Since this method is a little less readable, use of it should be restricted to - * code where it's possible there might be large datasets being returned. For known - * small datasets use get_records - it leads to simpler code. - * - * If you only want some of the records, specify $limitfrom and $limitnum. - * The query will skip the first $limitfrom records (according to the sort - * order) and then return the next $limitnum records. If either of $limitfrom - * or $limitnum is specified, both must be present. - * - * The return value is an ADODB RecordSet object - * @link http://phplens.com/adodb/reference.functions.adorecordset.html - * if the query succeeds. If an error occurrs, false is returned. - * - * @param string $table the table to query. - * @param string $field a field to check (optional). - * @param string $value the value the field must have (requred if field1 is given, else optional). - * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter). - * @param string $fields a comma separated list of fields to return (optional, by default all fields are returned). - * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). - * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). - * @return mixed an ADODB RecordSet object, or false if an error occured. - */ -function get_recordset($table, $field='', $value='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - - if ($field) { - $select = "$field = '$value'"; - } else { - $select = ''; - } - - return get_recordset_select($table, $select, $sort, $fields, $limitfrom, $limitnum); -} - -/** - * Get a number of records as an ADODB RecordSet. - * - * If given, $select is used as the SELECT parameter in the SQL query, - * otherwise all records from the table are returned. - * - * Other arguments and the return type as for @see function get_recordset. - * - * @uses $CFG - * @param string $table the table to query. - * @param string $select A fragment of SQL to be used in a where clause in the SQL call. - * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter). - * @param string $fields a comma separated list of fields to return (optional, by default all fields are returned). - * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). - * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). - * @return mixed an ADODB RecordSet object, or false if an error occured. - */ -function get_recordset_select($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - - global $CFG; - - if ($select) { - $select = ' WHERE '. $select; - } - - if ($sort) { - $sort = ' ORDER BY '. $sort; - } - - return get_recordset_sql('SELECT '. $fields .' FROM '. $CFG->prefix . $table . $select . $sort, $limitfrom, $limitnum); -} - -/** - * Get a number of records as an ADODB RecordSet. $sql must be a complete SQL query. - * Since this method is a little less readable, use of it should be restricted to - * code where it's possible there might be large datasets being returned. For known - * small datasets use get_records_sql - it leads to simpler code. - * - * The return type is as for @see function get_recordset. - * - * @uses $CFG - * @uses $db - * @param string $sql the SQL select query to execute. - * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). - * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). - * @return mixed an ADODB RecordSet object, or false if an error occured. - */ -function get_recordset_sql($sql, $limitfrom=null, $limitnum=null) { - global $CFG, $db; - - if (empty($db)) { - return false; - } - -/// Temporary hack as part of phasing out all access to obsolete user tables XXX - if (!empty($CFG->rolesactive)) { - if (strpos($sql, ' '.$CFG->prefix.'user_students ') || - strpos($sql, ' '.$CFG->prefix.'user_teachers ') || - strpos($sql, ' '.$CFG->prefix.'user_coursecreators ') || - strpos($sql, ' '.$CFG->prefix.'user_admins ')) { - if (debugging()) { var_dump(debug_backtrace()); } - print_error('sqlrelyonobsoletetable', 'debug', '', - 'user_student, user_teachers, user_coursecreators, user_admins'); - } - } - - - if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; - - if ($limitfrom || $limitnum) { - ///Special case, 0 must be -1 for ADOdb - $limitfrom = empty($limitfrom) ? -1 : $limitfrom; - $limitnum = empty($limitnum) ? -1 : $limitnum; - $rs = $db->SelectLimit($sql, $limitnum, $limitfrom); - } else { - $rs = $db->Execute($sql); - } - if (!$rs) { - debugging($db->ErrorMsg() .'

'. s($sql)); - if (!empty($CFG->dblogerror)) { - $debug=array_shift(debug_backtrace()); - error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql with limits ($limitfrom, $limitnum)"); - } - return false; - } - - return $rs; -} - -/** - * Utility function used by the following 4 methods. Note that for this to work, the first column - * in the recordset must contain unique values, as it is used as the key to the associative array. - * - * @param object an ADODB RecordSet object. - * @return mixed mixed an array of objects, or false if an error occured or the RecordSet was empty. - */ -function recordset_to_array($rs) { - global $CFG; - - $debugging = debugging('', DEBUG_DEVELOPER); - - if ($rs && !rs_EOF($rs)) { - $objects = array(); - /// First of all, we are going to get the name of the first column - /// to introduce it back after transforming the recordset to assoc array - /// See http://docs.moodle.org/en/XMLDB_Problems, fetch mode problem. - $firstcolumn = $rs->FetchField(0); - /// Get the whole associative array - if ($records = $rs->GetAssoc(true)) { - foreach ($records as $key => $record) { - /// Really DIRTY HACK for Oracle, but it's the only way to make it work - /// until we got all those NOT NULL DEFAULT '' out from Moodle - if ($CFG->dbfamily == 'oracle') { - array_walk($record, 'onespace2empty'); - } - /// End of DIRTY HACK - $record[$firstcolumn->name] = $key;/// Re-add the assoc field - if ($debugging && array_key_exists($key, $objects)) { - debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$key' found in column '".$firstcolumn->name."'.", DEBUG_DEVELOPER); - } - $objects[$key] = (object) $record; /// To object - } - return $objects; - /// Fallback in case we only have 1 field in the recordset. MDL-5877 - } else if ($rs->_numOfFields == 1 && $records = $rs->GetRows()) { - foreach ($records as $key => $record) { - /// Really DIRTY HACK for Oracle, but it's the only way to make it work - /// until we got all those NOT NULL DEFAULT '' out from Moodle - if ($CFG->dbfamily == 'oracle') { - array_walk($record, 'onespace2empty'); - } - /// End of DIRTY HACK - if ($debugging && array_key_exists($record[$firstcolumn->name], $objects)) { - debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '".$record[$firstcolumn->name]."' found in column '".$firstcolumn->name."'.", DEBUG_DEVELOPER); - } - $objects[$record[$firstcolumn->name]] = (object) $record; /// The key is the first column value (like Assoc) - } - return $objects; - } else { - return false; - } - } else { - return false; - } -} - -/** - * This function is used to get the current record from the recordset. It - * doesn't advance the recordset position. You'll need to do that by - * using the rs_next_record($recordset) function. - * @param ADORecordSet the recordset to fetch current record from - * @return ADOFetchObj the object containing the fetched information - */ -function rs_fetch_record(&$rs) { - global $CFG; - - if (!$rs) { - debugging('Incorrect $rs used!', DEBUG_DEVELOPER); - return false; - } - - $rec = $rs->FetchObj(); //Retrieve record as object without advance the pointer - - if ($rs->EOF) { //FetchObj requires manual checking of EOF to detect if it's the last record - $rec = false; - } else { - /// DIRTY HACK to retrieve all the ' ' (1 space) fields converted back - /// to '' (empty string) for Oracle. It's the only way to work with - /// all those NOT NULL DEFAULT '' fields until we definetively delete them - if ($CFG->dbfamily == 'oracle') { - $recarr = (array)$rec; /// Cast to array - array_walk($recarr, 'onespace2empty'); - $rec = (object)$recarr;/// Cast back to object - } - /// End DIRTY HACK - } - - return $rec; -} - -/** - * This function is used to advance the pointer of the recordset - * to its next position/record. - * @param ADORecordSet the recordset to be moved to the next record - * @return boolean true if the movement was successful and false if not (end of recordset) - */ -function rs_next_record(&$rs) { - if (!$rs) { - debugging('Incorrect $rs used!', DEBUG_DEVELOPER); - return false; - } - - return $rs->MoveNext(); //Move the pointer to the next record -} - -/** - * This function is used to get the current record from the recordset. It - * does advance the recordset position. - * This is the prefered way to iterate over recordsets with code blocks like this: - * - * $rs = get_recordset('SELECT .....'); - * while ($rec = rs_fetch_next_record($rs)) { - * /// Perform actions with the $rec record here - * } - * rs_close($rs); /// Close the recordset if not used anymore. Saves memory (optional but recommended). - * - * @param ADORecordSet the recordset to fetch current record from - * @return mixed ADOFetchObj the object containing the fetched information or boolean false if no record (end of recordset) - */ -function rs_fetch_next_record(&$rs) { - - global $CFG; - - if (!$rs) { - debugging('Incorrect $rs used!', DEBUG_DEVELOPER); - return false; - } - - $rec = false; - $recarr = $rs->FetchRow(); //Retrieve record as object without advance the pointer. It's quicker that FetchNextObj() - - if ($recarr) { - /// DIRTY HACK to retrieve all the ' ' (1 space) fields converted back - /// to '' (empty string) for Oracle. It's the only way to work with - /// all those NOT NULL DEFAULT '' fields until we definetively delete them - if ($CFG->dbfamily == 'oracle') { - array_walk($recarr, 'onespace2empty'); - } - /// End DIRTY HACK - /// Cast array to object - $rec = (object)$recarr; - } - - return $rec; -} - -/** - * Returns true if no more records found - * @param ADORecordSet the recordset - * @return bool - */ -function rs_EOF($rs) { - if (!$rs) { - debugging('Incorrect $rs used!', DEBUG_DEVELOPER); - return true; - } - return $rs->EOF; -} - -/** - * This function closes the recordset, freeing all the memory and associated resources. - * Note that, once closed, the recordset must not be used anymore along the request. - * Saves memory (optional but recommended). - * @param ADORecordSet the recordset to be closed - * @return void - */ -function rs_close(&$rs) { - if (!$rs) { - debugging('Incorrect $rs used!', DEBUG_DEVELOPER); - return; - } - - $rs->Close(); -} - -/** - * Get a number of records as an array of objects. - * - * Return value as for @see function get_records. - * - * @param string $table the table to query. - * @param string $select A fragment of SQL to be used in a where clause in the SQL call. - * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter). - * @param string $fields a comma separated list of fields to return - * (optional, by default all fields are returned). The first field will be used as key for the - * array so must be a unique field such as 'id'. - * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). - * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). - * @return mixed an array of objects, or false if no records were found or an error occured. - */ -function get_records_select($table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum='') { - $rs = get_recordset_select($table, $select, $sort, $fields, $limitfrom, $limitnum); - return recordset_to_array($rs); -} - -/** - * Get a number of records as an array of objects. - * - * Return value as for @see function get_records. - * - * @param string $sql the SQL select query to execute. The first column of this SELECT statement - * must be a unique value (usually the 'id' field), as it will be used as the key of the - * returned array. - * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set). - * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set). - * @return mixed an array of objects, or false if no records were found or an error occured. - */ -function get_records_sql($sql, $limitfrom='', $limitnum='') { - $rs = get_recordset_sql($sql, $limitfrom, $limitnum); - return recordset_to_array($rs); -} - -/** - * Get a single value from a table row where a particular select clause is true. - * - * @uses $CFG - * @param string $table the table to query. - * @param string $return the field to return the value of. - * @param string $select A fragment of SQL to be used in a where clause in the SQL call. - * @return mixed the specified value, or false if an error occured. - */ -function get_field_select($table, $return, $select) { - global $CFG; - if ($select) { - $select = 'WHERE '. $select; - } - return get_field_sql('SELECT ' . $return . ' FROM ' . $CFG->prefix . $table . ' ' . $select); -} - -/** - * Get a single value from a table. - * - * @param string $sql an SQL statement expected to return a single value. - * @return mixed the specified value, or false if an error occured. - */ -function get_field_sql($sql) { - global $CFG; - -/// Strip potential LIMIT uses arriving here, debugging them (MDL-7173) - $newsql = preg_replace('/ LIMIT [0-9, ]+$/is', '', $sql); - if ($newsql != $sql) { - debugging('Incorrect use of LIMIT clause (not cross-db) in call to get_field_sql(): ' . s($sql), DEBUG_DEVELOPER); - $sql = $newsql; - } - - $rs = get_recordset_sql($sql, 0, 1); - - if ($rs && $rs->RecordCount() == 1) { - /// DIRTY HACK to retrieve all the ' ' (1 space) fields converted back - /// to '' (empty string) for Oracle. It's the only way to work with - /// all those NOT NULL DEFAULT '' fields until we definetively delete them - if ($CFG->dbfamily == 'oracle') { - $value = reset($rs->fields); - onespace2empty($value); - return $value; - } - /// End of DIRTY HACK - return reset($rs->fields); - } else { - return false; - } -} - -/** - * Delete one or more records from a table - * - * @uses $CFG - * @uses $db - * @param string $table The database table to be checked against. - * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria). - * @return object A PHP standard object with the results from the SQL call. - * @todo Verify return type. - */ -function delete_records_select($table, $select='') { - - global $CFG, $db; - - if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; - - if ($select) { - $select = 'WHERE '.$select; - } - - $sql = 'DELETE FROM '. $CFG->prefix . $table .' '. $select; - $rs = $db->Execute($sql); - if (!$rs) { - debugging($db->ErrorMsg() .'

'. s($sql)); - if (!empty($CFG->dblogerror)) { - $debug=array_shift(debug_backtrace()); - error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); - } - return false; - } - return $rs; -} - -/** - * This function is used to convert all the Oracle 1-space defaults to the empty string - * like a really DIRTY HACK to allow it to work better until all those NOT NULL DEFAULT '' - * fields will be out from Moodle. - * @param string the string to be converted to '' (empty string) if it's ' ' (one space) - * @param mixed the key of the array in case we are using this function from array_walk, - * defaults to null for other (direct) uses - * @return boolean always true (the converted variable is returned by reference) - */ - function onespace2empty(&$item, $key=null) { - $item = $item == ' ' ? '' : $item; - return true; - } - -?> diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index 93104ad698..160190e3ef 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -746,7 +746,7 @@ class grade_test extends UnitTestCase { if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/'; - update_record('grade_categories', $grade_category); + $DB->update_record('grade_categories', $grade_category); $this->grade_categories[0] = $grade_category; } @@ -765,7 +765,7 @@ class grade_test extends UnitTestCase { if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/'; - update_record('grade_categories', $grade_category); + $DB->update_record('grade_categories', $grade_category); $this->grade_categories[1] = $grade_category; } @@ -784,7 +784,7 @@ class grade_test extends UnitTestCase { if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = $this->grade_categories[0]->path.$grade_category->id.'/'; - update_record('grade_categories', $grade_category); + $DB->update_record('grade_categories', $grade_category); $this->grade_categories[2] = $grade_category; } @@ -805,7 +805,7 @@ class grade_test extends UnitTestCase { if ($grade_category->id = $DB->insert_record('grade_categories', $grade_category)) { $grade_category->path = '/'.$course_category->id.'/'.$grade_category->id.'/'; - update_record('grade_categories', $grade_category); + $DB->update_record('grade_categories', $grade_category); $this->grade_categories[3] = $grade_category; } } diff --git a/lib/simpletest/testeventslib.php b/lib/simpletest/testeventslib.php index 1c704ae4aa..efb002718f 100755 --- a/lib/simpletest/testeventslib.php +++ b/lib/simpletest/testeventslib.php @@ -116,7 +116,7 @@ class eventslib_test extends UnitTestCase { global $DB; events_uninstall('unittest'); - $this->assertEqual(0, count_records('events_handlers', array('handlermodule'=>'unittest')), 'All handlers should be uninstalled: %s'); + $this->assertEqual(0, $DB->count_records('events_handlers', array('handlermodule'=>'unittest')), 'All handlers should be uninstalled: %s'); } /** diff --git a/lib/simpletestlib.php b/lib/simpletestlib.php index 27345e9492..823b7d46bf 100644 --- a/lib/simpletestlib.php +++ b/lib/simpletestlib.php @@ -149,243 +149,4 @@ class CheckSpecifiedFieldsExpectation extends SimpleExpectation { } } -/** - * Given a table name, a two-dimensional array of data, and a database connection, - * creates a table in the database. The array of data should look something like this. - * - * $testdata = array( - * array('id', 'username', 'firstname', 'lastname', 'email'), - * array(1, 'u1', 'user', 'one', 'u1@example.com'), - * array(2, 'u2', 'user', 'two', 'u2@example.com'), - * array(3, 'u3', 'user', 'three', 'u3@example.com'), - * array(4, 'u4', 'user', 'four', 'u4@example.com'), - * array(5, 'u5', 'user', 'five', 'u5@example.com'), - * ); - * - * The first 'row' of the test data gives the column names. The type of each column - * is set to either INT or VARCHAR($strlen), guessed by inspecting the first row of - * data. Unless the col name is 'id' in which case the col type will be SERIAL. - * The remaining 'rows' of the data array are values loaded into the table. All columns - * are created with a default of 0xdefa or 'Default' as appropriate. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $tablename the name of the table to create. E.g. 'mdl_unittest_user'. - * @param array $data a two-dimensional array of data, in the format described above. - * @param object $db an AdoDB database connection. - * @param int $strlen the width to use for string fields. - */ -function load_test_table($tablename, $data, $db = null, $strlen = 255) { - $colnames = array_shift($data); - $coldefs = array(); - foreach (array_combine($colnames, $data[0]) as $colname => $value) { - if ($colname == 'id') { - $type = 'SERIAL'; - } else if (is_int($value)) { - $type = 'INTEGER DEFAULT 57082'; // 0xdefa - } else { - $type = "VARCHAR($strlen) DEFAULT 'Default'"; - } - $coldefs[] = "$colname $type"; - } - _private_execute_sql("CREATE TABLE $tablename (" . join(',', $coldefs) . ');', $db); - - array_unshift($data, $colnames); - load_test_data($tablename, $data, $db); -} - -/** - * Given a table name, a two-dimensional array of data, and a database connection, - * adds data to the database table. The array should have the same format as for - * load_test_table(), with the first 'row' giving column names. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $tablename the name of the table to populate. E.g. 'mdl_unittest_user'. - * @param array $data a two-dimensional array of data, in the format described. - * @param object $localdb an AdoDB database connection. - */ -function load_test_data($tablename, $data, $localdb = null) { - global $CFG; - - if (null == $localdb) { - global $db; - $localdb = $db; - } - $colnames = array_shift($data); - $idcol = array_search('id', $colnames); - $maxid = -1; - foreach ($data as $row) { - _private_execute_sql($localdb->GetInsertSQL($tablename, array_combine($colnames, $row)), $localdb); - if ($idcol !== false && $row[$idcol] > $maxid) { - $maxid = $row[$idcol]; - } - } - if ($CFG->dbfamily == 'postgres' && $idcol !== false) { - $maxid += 1; - _private_execute_sql("ALTER SEQUENCE {$tablename}_id_seq RESTART WITH $maxid;", $localdb); - } -} - -/** - * Make multiple tables that are the same as a real table but empty. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param mixed $tablename Array of strings containing the names of the table to populate (without prefix). - * @param string $realprefix the prefix used for real tables. E.g. 'mdl_'. - * @param string $testprefix the prefix used for test tables. E.g. 'mdl_unittest_'. - * @param object $db an AdoDB database connection. - */ -function make_test_tables_like_real_one($tablenames, $realprefix, $testprefix, $db,$dropconstraints=false) { - foreach($tablenames as $individual) { - make_test_table_like_real_one($individual,$realprefix,$testprefix,$db,$dropconstraints); - } -} - -/** - * Make a test table that has all the same columns as a real moodle table, - * but which is empty. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $tablename Name of the table to populate. E.g. 'user'. - * @param string $realprefix the prefix used for real tables. E.g. 'mdl_'. - * @param string $testprefix the prefix used for test tables. E.g. 'mdl_unittest_'. - * @param object $db an AdoDB database connection. - */ -function make_test_table_like_real_one($tablename, $realprefix, $testprefix, $db, $dropconstraints=false) { - _private_execute_sql("CREATE TABLE $testprefix$tablename (LIKE $realprefix$tablename INCLUDING DEFAULTS);", $db); - if (_private_has_id_column($testprefix . $tablename, $db)) { - _private_execute_sql("CREATE SEQUENCE $testprefix{$tablename}_id_seq;", $db); - _private_execute_sql("ALTER TABLE $testprefix$tablename ALTER COLUMN id SET DEFAULT nextval('{$testprefix}{$tablename}_id_seq'::regclass);", $db); - _private_execute_sql("ALTER TABLE $testprefix$tablename ADD PRIMARY KEY (id);", $db); - } - if($dropconstraints) { - $cols=$db->MetaColumnNames($testprefix.$tablename); - foreach($cols as $col) { - $rs=_private_execute_sql( - "SELECT constraint_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_name='$testprefix$tablename'",$db); - while(!$rs->EOF) { - $constraintname=$rs->fields['constraint_name']; - _private_execute_sql("ALTER TABLE $testprefix$tablename DROP CONSTRAINT $constraintname",$db); - $rs->MoveNext(); - } - - _private_execute_sql("ALTER TABLE $testprefix$tablename ALTER COLUMN $col DROP NOT NULL",$db); - } - } -} - -/** - * Drops a table from the database pointed to by the database connection. - * This undoes the create performed by load_test_table(). - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $tablename the name of the table to populate. E.g. 'mdl_unittest_user'. - * @param object $db an AdoDB database connection. - * @param bool $cascade If true, also drop tables that depend on this one, e.g. through - * foreign key constraints. - */ -function remove_test_table($tablename, $db, $cascade = false) { - global $CFG; - _private_execute_sql('DROP TABLE ' . $tablename . ($cascade ? ' CASCADE' : '') . ';', $db); - - if ($CFG->dbfamily == 'postgres') { - $rs = $db->Execute("SELECT relname FROM pg_class WHERE relname = '{$tablename}_id_seq' AND relkind = 'S';"); - if ($rs && !rs_EOF($rs)) { - _private_execute_sql("DROP SEQUENCE {$tablename}_id_seq;", $db); - } - } -} - -/** - * Drops all the tables with a particular prefix from the database pointed to by the database connection. - * Useful for cleaning up after a unit test run has crashed leaving the DB full of junk. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $prefix the prfix of tables to drop 'mdl_unittest_'. - * @param object $db an AdoDB database connection. - */ -function wipe_tables($prefix, $db) { - if (strpos($prefix, 'test') === false) { - notice('The wipe_tables function should only be used to wipe test tables.'); - return; - } - $tables = $db->Metatables('TABLES', false, "$prefix%"); - foreach ($tables as $table) { - _private_execute_sql("DROP TABLE $table CASCADE", $db); - } -} - -/** - * Drops all the sequences with a particular prefix from the database pointed to by the database connection. - * Useful for cleaning up after a unit test run has crashed leaving the DB full of junk. - * - * This function should not be used in real code. Only for testing and debugging. - * - * @param string $prefix the prfix of sequences to drop 'mdl_unittest_'. - * @param object $db an AdoDB database connection. - */ -function wipe_sequences($prefix, $db) { - global $CFG; - - if ($CFG->dbfamily == 'postgres') { - $sequences = $db->GetCol("SELECT relname FROM pg_class WHERE relname LIKE '$prefix%_id_seq' AND relkind = 'S';"); - if ($sequences) { - foreach ($sequences as $sequence) { - _private_execute_sql("DROP SEQUENCE $sequence CASCADE", $db); - } - } - } -} - -function _private_has_id_column($table, $db) { - return in_array('id', $db->MetaColumnNames($table)); -} - -function _private_execute_sql($sql, $localdb = null) { - - if (null == $localdb) { - global $db; - $localdb = $db; - } - if (!$rs = $localdb->Execute($sql)) { - echo '

SQL ERROR: ', $localdb->ErrorMsg(), ". STATEMENT: $sql

"; - } - return $rs; -} - -/** - * Base class for testcases that want a different DB prefix. - * - * That is, when you need to load test data into the database for - * unit testing, instead of messing with the real mdl_course table, - * we will temporarily change $CFG->prefix from (say) mdl_ to mdl_unittest_ - * and create a table called mdl_unittest_course to hold the test data. - */ -class prefix_changing_test_case extends UnitTestCase { - var $old_prefix; - - function change_prefix() { - global $CFG; - $this->old_prefix = $CFG->prefix; - $CFG->prefix = $CFG->prefix . 'unittest_'; - } - - function change_prefix_back() { - global $CFG; - $CFG->prefix = $this->old_prefix; - } - - function setUp() { - $this->change_prefix(); - } - - function tearDown() { - $this->change_prefix_back(); - } -} ?> diff --git a/message/lib.php b/message/lib.php index 7d650a5495..421848f1fd 100644 --- a/message/lib.php +++ b/message/lib.php @@ -623,8 +623,8 @@ function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords= function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { global $CFG, $USER, $DB; - $fullname = sql_fullname(); - $LIKE = sql_ilike(); + $fullname = $DB->sql_fullname(); + $LIKE = $DB->sql_ilike(); if (!empty($exceptions)) { $except = ' AND u.id NOT IN ('. $exceptions .') '; diff --git a/mod/data/field/date/field.class.php b/mod/data/field/date/field.class.php index 80b1efb08a..ab438375bc 100755 --- a/mod/data/field/date/field.class.php +++ b/mod/data/field/date/field.class.php @@ -114,8 +114,8 @@ class data_field_date extends data_field_base { } function get_sort_sql($fieldname) { - - return sql_cast_char2int($fieldname, true); + global $DB; + return $DB->sql_cast_char2int($fieldname, true); } diff --git a/mod/data/view.php b/mod/data/view.php index 6b26e1bae2..9a5652fe0a 100755 --- a/mod/data/view.php +++ b/mod/data/view.php @@ -417,7 +417,7 @@ $groupselect = ' '; } - $ilike = sql_ilike(); //Be case-insensitive + $ilike = $DB->sql_ilike(); //Be case-insensitive /// Find the field we are sorting on if ($sort <= 0 or !$sortfield = data_get_field_from_id($sort, $data)) { diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8cb4e9208c..433f3d2445 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -965,7 +965,7 @@ function forum_user_complete($course, $user, $mod, $forum) { */ function forum_print_overview($courses,&$htmlarray) { global $USER, $CFG; - $LIKE = sql_ilike(); + $LIKE = $DB->sql_ilike(); if (empty($courses) || !is_array($courses) || count($courses) == 0) { return array(); @@ -1738,7 +1738,7 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 $selectdiscussion = "(".implode(" OR ", $where).")"; // Some differences SQL - $LIKE = sql_ilike(); + $LIKE = $DB->sql_ilike(); $NOTLIKE = 'NOT ' . $LIKE; if ($CFG->dbfamily == 'postgres') { $REGEXP = '~*'; diff --git a/mod/glossary/editcategories.php b/mod/glossary/editcategories.php index 422638f6c9..b37e837fa1 100644 --- a/mod/glossary/editcategories.php +++ b/mod/glossary/editcategories.php @@ -141,7 +141,7 @@ } elseif ( $action == "add" ) { if ( $confirm ) { - $ILIKE = sql_ilike(); + $ILIKE = $DB->sql_ilike(); $dupcategory = $DB->get_records_sql("SELECT * FROM {glossary_categories} WHERE name $ILIKE ? AND glossaryid=?", array($name, $glossary->id)); if ( $dupcategory ) { echo "

" . get_string("add"). " " . get_string("category","glossary"); diff --git a/mod/hotpot/index.php b/mod/hotpot/index.php index 72ea6b94a7..70934fad3f 100644 --- a/mod/hotpot/index.php +++ b/mod/hotpot/index.php @@ -195,7 +195,7 @@ // - other unidentified situations ?! $regrade_hotpots = array(); - $concat_field = sql_concat('hotpot', "'_'", 'name'); + $concat_field = $DB->sql_concat('hotpot', "'_'", 'name'); if ($concat_field) { $records = $DB->get_records_sql(" SELECT $concat_field, COUNT(*), hotpot, name diff --git a/mod/quiz/backuplib.php b/mod/quiz/backuplib.php index c817c88db8..d691908690 100644 --- a/mod/quiz/backuplib.php +++ b/mod/quiz/backuplib.php @@ -126,7 +126,7 @@ // and their parents will already have been included.) $categorieswithrandom = $DB->get_records_sql(" SELECT question.category AS id, SUM(" . - sql_cast_char2int('questiontext', true) . ") AS numqsusingsubcategories + $DB->sql_cast_char2int('questiontext', true) . ") AS numqsusingsubcategories FROM {quiz_question_instances} qqi, $from {question} question diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 247d2ddae9..0313b76aec 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -196,7 +196,7 @@ "SELECT * FROM {question} q WHERE qtype = '" . RANDOM . "' AND category = ? - AND " . sql_compare_text('questiontext') . " = ? + AND " . $DB->sql_compare_text('questiontext') . " = ? AND NOT EXISTS (SELECT * FROM {quiz_question_instances} WHERE question = q.id) ORDER BY id", array($category->id, $recurse))) { // Take as many of these as needed. diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index c1bad787ee..238c1bed3f 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -443,7 +443,7 @@ function quiz_has_feedback($quizid) { static $cache = array(); if (!array_key_exists($quizid, $cache)) { $cache[$quizid] = $DB->record_exists_select('quiz_feedback', - "quizid = ? AND " . sql_isnotempty('quiz_feedback', 'feedbacktext', false, true), array($quizid)); + "quizid = ? AND " . $DB->sql_isnotempty('quiz_feedback', 'feedbacktext', false, true), array($quizid)); } return $cache[$quizid]; } -- 2.39.5