/** * Search and replace strings in the entire database. * Used for xhtml upgrade, but can also be used, for eg, to change links. * This function prints every query * (Adapted from moodle function of same name) * * @param array $replacearray keys = search, values = replacements. */ function db_replace($replacearray) { global $db; /// Turn off time limits, sometimes upgrades can be slow. @set_time_limit(0); @ob_implicit_flush(true); while (@ob_end_flush()) { } if (!($tables = $db->Metatables())) { // No tables yet at all. return false; } foreach ($tables as $table) { if (in_array($table, array('config', 'adodb_logsql'))) { // Don't process these continue; } if ($columns = $db->MetaColumns($table, false)) { $db->debug = true; foreach ($columns as $column => $data) { if (in_array($data->type, array('text', 'mediumtext', 'longtext', 'varchar'))) { // Text stuff only foreach ($replacearray as $s => $r) { $db->execute('UPDATE ' . db_quote_table_placeholders('{' . $table . '}') . ' SET ' . db_quote_identifier($column) . ' = REPLACE(' . db_quote_identifier($column) . ', ?, ?)', array($s, $r)); } } } $db->debug = false; } } }
/** * Get an array of data from one or more fields from a database * use to get a column, or a series of distinct values * * @uses $CFG * @uses $db * @param string $sql The SQL string you wish to be executed. * @return mixed|false Returns the value return from the SQL statment or false if an error occured. * @todo Finish documenting this function */ function get_fieldset_sql($sql) { global $db, $CFG; if (defined('MDL_PERFDB')) { global $PERF; $PERF->dbqueries++; } $sql = db_quote_table_placeholders($sql); $rs = $db->Execute($sql); if (!$rs) { debugging($db->ErrorMsg() . '<br /><br />' . 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; } if (!rs_EOF($rs)) { $keys = array_keys($rs->fields); $key0 = $keys[0]; $results = array(); while (!$rs->EOF) { array_push($results, $rs->fields[$key0]); $rs->MoveNext(); } /// 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($results, 'onespace2empty'); } /// End of DIRTY HACK rs_close($rs); return $results; } else { rs_close($rs); return false; } }