libdir.'/dml/moodle_database.php'); require_once($CFG->libdir.'/dml/adodb_moodle_database.php'); require_once($CFG->libdir.'/dml/oci8po_adodb_moodle_recordset.php'); /** * Oracle database class using adodb backend * @package dml */ class oci8po_adodb_moodle_database extends adodb_moodle_database { public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) { if ($prefix == '' and !$this->external) { //Enforce prefixes for everybody but mysql throw new dml_exception('prefixcannotbeempty', $this->get_dbfamily()); } if (!$this->external and strlen($prefix) > 2) { //Max prefix length for Oracle is 2cc $a = (object)array('dbfamily'=>'oracle', 'maxlength'=>2); throw new dml_exception('prefixtoolong', $a); } return parent::connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions); } /** * Detects if all needed PHP stuff installed. * Do not connect to connect to db if this test fails. * @return mixed true if ok, string if something */ public function driver_installed() { if (!extension_loaded('oci8')) { return get_string('ociextensionisnotpresentinphp', 'install'); } return true; } protected function preconfigure_dbconnection() { if (!defined('ADODB_ASSOC_CASE')) { define ('ADODB_ASSOC_CASE', 0); /// Use lowercase fieldnames for ADODB_FETCH_ASSOC /// (only meaningful for oci8po, it's the default /// for other DB drivers so this won't affect them) } /// Row prefetching uses a bit of memory but saves a ton /// of network latency. With current AdoDB and PHP, only /// Oracle uses this setting. if (!defined('ADODB_PREFETCH_ROWS')) { define ('ADODB_PREFETCH_ROWS', 1000); } } protected function configure_dbconnection() { $this->adodb->SetFetchMode(ADODB_FETCH_ASSOC); /// No need to set charset. It must be specified by the NLS_LANG env. variable /// Now set the decimal separator to DOT, Moodle & PHP will always send floats to /// DB using DOTS. Manually introduced floats (if using other characters) must be /// converted back to DOTs (like gradebook does) $sql = "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'"; $this->query_start($sql, null, SQL_QUERY_AUX); $rs = $this->adodb->Execute($sql); $this->query_end($rs); return true; } /** * Returns database family type * @return string db family name (mysql, postgres, mssql, oracle, etc.) */ public function get_dbfamily() { return 'oracle'; } /** * Returns database type * @return string db type mysql, mysqli, postgres7 */ protected function get_dbtype() { return 'oci8po'; } /** * Returns localised database description * Note: can be used before connect() * @return string */ public function get_configuration_hints() { $str = get_string('databasesettingssub_oci8po', 'install'); $str .= "

'; $str .= get_string('moodledocslink', 'install') . '

'; return $str; } /** * Returns supported query parameter types * @return bitmask */ protected function allowed_param_types() { return SQL_PARAMS_QM; } /** * This method will introspect inside DB to detect it it's a UTF-8 DB or no * Used from setup.php to set correctly "set names" when the installation * process is performed without the initial and beautiful installer * @return bool true if db in unicode mode */ function setup_is_unicodedb() { $sql = "SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'"; $this->query_start($sql, null, SQL_QUERY_AUX); $rs = $this->adodb->Execute($sql); $this->query_end($rs); if ($rs && !$rs->EOF) { $encoding = $rs->fields['value']; if (strtoupper($encoding) == 'AL32UTF8') { return true; } } return false; } /** * Selects rows and return values of first column as array. * * @param string $sql The SQL query * @param array $params array of sql parameters * @return mixed array of values * @throws dml_exception if error */ public function get_fieldset_sql($sql, array $params=null) { if ($result = parent::get_fieldset_sql($sql, $params)) { array_walk($result, array('oci8po_adodb_moodle_database', 'onespace2empty')); } return $result; } protected function create_recordset($rs) { return new oci8po_adodb_moodle_recordset($rs); } protected function adodb_recordset_to_array($rs) { /// Really DIRTY HACK for Oracle - needed because it can not see difference from NULL and '' /// this can not be removed even if we chane db defaults :-( if ($result = parent::adodb_recordset_to_array($rs)) { foreach ($result as $key=>$row) { $row = (array)$row; array_walk($row, array('oci8po_adodb_moodle_database', 'onespace2empty')); $result[$key] = (object)$row; } } return $result; } public function sql_bitand($int1, $int2) { return 'bitand((' . $int1 . '), (' . $int2 . '))'; } public function sql_bitnot($int1) { return '((0 - (' . $int1 . ')) - 1)'; } public function sql_bitor($int1, $int2) { return '((' . $int1 . ') + (' . $int2 . ') - ' . $this->sql_bitand($int1, $int2) . ')'; } public function sql_bitxor($int1, $int2) { return '(' . $this->sql_bitor($int1, $int2) . ' - ' . $this->sql_bitand($int1, $int2) . ')'; } /** * Returns the SQL text to be used in order to perform module '%' * opration - remainder after division * * @param integer int1 first integer in the operation * @param integer int2 second integer in the operation * @return string the piece of SQL code to be used in your statement. */ public function sql_modulo($int1, $int2) { return 'MOD(' . $int1 . ', ' . $int2 . ')'; } function sql_null_from_clause() { return ' FROM dual'; } public function sql_cast_char2int($fieldname, $text=false) { if (!$text) { return ' CAST(' . $fieldname . ' AS INT) '; } else { return ' CAST(' . $this->sql_compare_text($fieldname) . ' AS INT) '; } } public function sql_order_by_text($fieldname, $numchars=32) { return 'dbms_lob.substr(' . $fieldname . ', ' . $numchars . ',1)'; } /** * Returns the SQL for returning searching one string for the location of another. */ public function sql_position($needle, $haystack) { return "INSTR(($haystack), ($needle))"; } public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) { if ($nullablefield) { return " $fieldname IS NULL "; /// empties in nullable fields are stored as } else { /// NULLs if ($textfield) { return " ".$this->sql_compare_text($fieldname)." = ' ' "; /// oracle_dirty_hack inserts 1-whitespace } else { /// in NOT NULL varchar and text columns so return " $fieldname = ' ' "; /// we need to look for that in any situation } } } function sql_empty() { return ' '; } /** * Update a record in a table * * $dataobject is an object containing needed data * Relies on $dataobject having a variable "id" to * specify the record to update * * @param string $table The database table to be checked against. * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified. * @param bool true means repeated updates expected * @return bool true * @throws dml_exception if error */ public function update_record($table, $dataobject, $bulk=false) { if (!is_object($dataobject)) { $dataobject = (object)$dataobject; } $columns = $this->get_columns($table); $cleaned = array(); $clobs = array(); $blobs = array(); foreach ($dataobject as $field=>$value) { if (!isset($columns[$field])) { /// Non-existing table field, skip it continue; } /// Apply Oracle dirty hack to value, to have "correct" empty values for Oracle $value = $this->oracle_dirty_hack($table, $field, $value); /// Get column metadata $column = $columns[$field]; if ($column->meta_type == 'B') { /// BLOB columns need to be updated apart if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $blobs[$field] = $value; continue; /// We don't want this column to be processed by update_record_raw() at all. /// $value = 'empty_blob()'; /// Set the default value to be inserted in first instance. Not needed to initialize lob storage in updates } } else if ($column->meta_type == 'X' && strlen($value) > 4000) { /// CLOB columns need to be updated apart (if lenght > 4000) if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $blobs[$field] = $value; continue; /// We don't want this column to be processed by update_record_raw() at all. /// $value = 'empty_clob()'; /// Set the default value to be inserted in first instance. Not needed to initialize lob storage in updates } } else if (is_bool($value)) { $value = (int)$value; // prevent "false" problems } else if ($value === '' || $value === ' ') { if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') { $value = 0; // prevent '' problems in numeric fields } } $cleaned[$field] = $value; } if (empty($blobs) && empty($clobs)) { /// Without BLOBs and CLOBs, execute the raw update and return return $this->update_record_raw($table, $cleaned, $bulk); } /// We have BLOBs or CLOBs to postprocess, execute the raw update and then update blobs $this->update_record_raw($table, $cleaned, $bulk); foreach ($blobs as $key=>$value) { $this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = {$dataobject->id}"); $this->query_end($result); } foreach ($clobs as $key=>$value) { $this->query_start('--adodb-UpdateClob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateClob($this->prefix.$table, $key, $value, "id = {$dataobject->id}"); $this->query_end($result); } return true; } /** * Insert a record into a table and return the "id" field if required, * Some conversions and safety checks are carried out. Lobs are supported. * If the return ID isn't required, then this just reports success as true/false. * $data is an object containing needed data * @param string $table The database table to be inserted into * @param object $data A data object with values for one or more fields in the record * @param bool $returnid Should the id of the newly created record entry be returned? If this option is not requested then true/false is returned. * @param bool $bulk true means repeated inserts expected * @return true or new id * @throws dml_exception if error */ public function insert_record($table, $dataobject, $returnid=true, $bulk=false) { if (!is_object($dataobject)) { $dataobject = (object)$dataobject; } unset($dataobject->id); $columns = $this->get_columns($table); $cleaned = array(); $blobs = array(); $clobs = array(); foreach ($dataobject as $field=>$value) { if (!isset($columns[$field])) { /// Non-existing table field, skip it continue; } /// Apply Oracle dirty hack to value, to have "correct" empty values for Oracle $value = $this->oracle_dirty_hack($table, $field, $value); /// Get column metadata $column = $columns[$field]; if ($column->meta_type == 'B') { /// BLOBs columns need to be updated apart if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $blobs[$field] = $value; $value = 'empty_blob()'; /// Set the default value to be inserted (preparing lob storage for next update) } } else if ($column->meta_type == 'X' && strlen($value) > 4000) { /// CLOB columns need to be updated apart (if lenght > 4000) if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $clobs[$field] = $value; $value = 'empty_clob()'; /// Set the default value to be inserted (preparing lob storage for next update) } } else if (is_bool($value)) { $value = (int)$value; // prevent "false" problems } else if ($value === '' || $value === ' ') { if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') { $value = 0; // prevent '' problems in numeric fields } } $cleaned[$field] = $value; } if (empty($blobs) && empty($clobs)) { /// Without BLOBs and CLOBs, execute the raw insert and return return $this->insert_record_raw($table, $cleaned, $returnid, $bulk); } /// We have BLOBs or CLOBs to postprocess, insert the raw record fetching the id to be used later $id = $this->insert_record_raw($table, $cleaned, true, $bulk); foreach ($blobs as $key=>$value) { $this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id"); $this->query_end($result); } foreach ($clobs as $key=>$value) { $this->query_start('--adodb-UpdateClob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateClob($this->prefix.$table, $key, $value, "id = $id"); $this->query_end($result); } return ($returnid ? $id : true); } /** * Set a single field in every table row where the select statement evaluates to true. * * @param string $table The database table to be checked against. * @param string $newfield the field to set. * @param string $newvalue the value to set the field to. * @param string $select A fragment of SQL to be used in a where clause in the SQL call. * @param array $params array of sql parameters * @return bool true * @throws dml_exception if error */ public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) { if (is_null($params)) { $params = array(); } list($select, $params, $type) = $this->fix_sql_params($select, $params); /// Apply Oracle dirty hack to value, to have "correct" empty values for Oracle $newvalue = $this->oracle_dirty_hack($table, $newfield, $newvalue); /// Get column metadata $columns = $this->get_columns($table); $column = $columns[$newfield]; if ($column->meta_type == 'B') { /// If the column is a BLOB /// Update BLOB column and return $select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-( $this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateBlob($this->prefix.$table, $newfield, $newvalue, $select); $this->query_end($result); return true; } if ($column->meta_type == 'X' && strlen($newvalue) > 4000) { /// If the column is a CLOB with lenght > 4000 /// Update BLOB column and return $select = $this->emulate_bound_params($select, $params); // adodb does not use bound parameters for blob updates :-( $this->query_start('--adodb-UpdateClob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateClob($this->prefix.$table, $newfield, $newvalue, $select); $this->query_end($result); return true; } /// Arrived here, normal update (without BLOBs) if (is_null($newvalue)) { $newfield = "$newfield = NULL"; } else { if (is_bool($newvalue)) { $newvalue = (int)$newvalue; // prevent "false" problems } else if ($newvalue === '' || $newvalue === ' ') { if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') { $newvalue = 0; // prevent '' problems in numeric fields } } $newfield = "$newfield = ?"; array_unshift($params, $newvalue); // add as first param } $sql = "UPDATE {$this->prefix}$table SET $newfield WHERE $select"; $this->query_start($sql, $params, SQL_QUERY_UPDATE); $rs = $rs = $this->adodb->Execute($sql, $params); $this->query_end($rs); return true; } /** * Insert new record into database, as fast as possible, no safety checks, lobs not supported. * (overloaded from adodb_moodle_database because of sequence numbers and empty_blob()/empty_clob()) * @param string $table name * @param mixed $params data record as object or array * @param bool $returnit return it of inserted record * @param bool $bulk true means repeated inserts expected * @param bool $customsequence true if 'id' included in $params, disables $returnid * @return true or new id * @throws dml_exception if error */ public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) { if (!is_array($params)) { $params = (array)$params; } if ($customsequence) { if (!isset($params['id'])) { throw new coding_exception('moodle_database::insert_record_raw() id field must be specified if custom sequences used.'); } $returnid = false; } else { unset($params['id']); } if ($returnid) { $dbman = $this->get_manager(); $xmldb_table = new xmldb_table($table); $this->query_start('--find_sequence_name', null, SQL_QUERY_AUX); $seqname = $dbman->find_sequence_name($xmldb_table); $this->query_end(true); if (!$seqname) { /// Fallback, seqname not found, something is wrong. Inform and use the alternative getNameForObject() method $generator = $this->get_dbman()->generator; $generator->setPrefix($this->getPrefix()); $seqname = $generator->getNameForObject($table, 'id', 'seq'); } $xmldb_table = new xmldb_table($table); $this->query_start('--adodb-GenID', null, SQL_QUERY_AUX); $nextval = $this->adodb->GenID($seqname); $this->query_end(true); if ($nextval) { $params['id'] = (int)$nextval; } } if (empty($params)) { throw new coding_exception('moodle_database::insert_record_raw() no fields found.'); } $fields = implode(',', array_keys($params)); $qms = array(); /// Look for 'empty_clob() and empty_blob() params to replace question marks properly /// Oracle requires those function calls on insert to prepare blob/clob storage, so we /// specify them as SQL, deleting them from parameters foreach ($params as $key=>$param) { if ($param === 'empty_blob()') { $qms[] = 'empty_blob()'; unset($params[$key]); } else if ($param === 'empty_clob()') { $qms[] = 'empty_clob()'; unset($params[$key]); } else { $qms[] = '?'; } } $qms = implode(',', $qms); $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)"; $this->query_start($sql, $params, SQL_QUERY_INSERT); $rs = $rs = $this->adodb->Execute($sql, $params); $this->query_end($rs); if (!$returnid) { return true; } if (!empty($params['id'])) { return (int)$params['id']; } throw new dml_write_exception('unknown error fetching inserted id'); } /** * 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) */ static function onespace2empty(&$item, $key=null) { $item = $item == ' ' ? '' : $item; return true; } /** * This function will handle all the records before being inserted/updated to DB for Oracle * installations. This is because the "special feature" of Oracle where the empty string is * equal to NULL and this presents a problem with all our currently NOT NULL default '' fields. * * Once Moodle DB will be free of this sort of false NOT NULLS, this hack could be removed safely * * Note that this function is 100% private and should be used, exclusively by DML functions * in this file. Also, this is considered a DIRTY HACK to be removed when possible. (stronk7) * * This function is private and must not be used outside this driver at all * * @param $table string the table where the record is going to be inserted/updated (without prefix) * @param $field string the field where the record is going to be inserted/updated * @param $value mixed the value to be inserted/updated */ private function oracle_dirty_hack ($table, $field, $value) { /// Get metadata $columns = $this->get_columns($table); if (!isset($columns[$field])) { return $value; } $column = $columns[$field]; /// For Oracle DB, empty strings are converted to NULLs in DB /// and this breaks a lot of NOT NULL columns currenty Moodle. In the future it's /// planned to move some of them to NULL, if they must accept empty values and this /// piece of code will become less and less used. But, for now, we need it. /// What we are going to do is to examine all the data being inserted and if it's /// an empty string (NULL for Oracle) and the field is defined as NOT NULL, we'll modify /// such data in the best form possible ("0" for booleans and numbers and " " for the /// rest of strings. It isn't optimal, but the only way to do so. /// In the oppsite, when retrieving records from Oracle, we'll decode " " back to /// empty strings to allow everything to work properly. DIRTY HACK. /// If the field ins't VARCHAR or CLOB, skip if ($column->meta_type != 'C' and $column->meta_type != 'X') { return $value; } /// If the field isn't NOT NULL, skip (it's nullable, so accept empty-null values) if (!$column->not_null) { return $value; } /// If the value isn't empty, skip if (!empty($value)) { return $value; } /// Now, we have one empty value, going to be inserted to one NOT NULL, VARCHAR2 or CLOB field /// Try to get the best value to be inserted /// The '0' string doesn't need any transformation, skip if ($value === '0') { return $value; } /// Transformations start if (gettype($value) == 'boolean') { return '0'; /// Transform false to '0' that evaluates the same for PHP } else if (gettype($value) == 'integer') { return '0'; /// Transform 0 to '0' that evaluates the same for PHP } else if (gettype($value) == 'NULL') { return '0'; /// Transform NULL to '0' that evaluates the same for PHP } else if ($value === '') { return ' '; /// Transform '' to ' ' that DONT'T EVALUATE THE SAME /// (we'll transform back again on get_records_XXX functions and others)!! } /// Fail safe to original value return $value; } /** * Import a record into a table, id field is required. * Basic safety checks only. Lobs are supported. * @param string $table name of database table to be inserted into * @param mixed $dataobject object or array with fields in the record * @return bool success */ public function import_record($table, $dataobject) { $dataobject = (object)$dataobject; $columns = $this->get_columns($table); $cleaned = array(); $blobs = array(); $clobs = array(); foreach ($dataobject as $field=>$value) { if (!isset($columns[$field])) { /// Non-existing table field, skip it continue; } /// Apply Oracle dirty hack to value, to have "correct" empty values for Oracle $value = $this->oracle_dirty_hack($table, $field, $value); /// Get column metadata $column = $columns[$field]; if ($column->meta_type == 'B') { /// BLOBs columns need to be updated apart if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $blobs[$field] = $value; $value = 'empty_blob()'; /// Set the default value to be inserted (preparing lob storage for next update) } } else if ($column->meta_type == 'X' && strlen($value) > 4000) { /// CLOB columns need to be updated apart (if lenght > 4000) if (!is_null($value)) { /// If value not null, add it to the list of BLOBs to update later $clobs[$field] = $value; $value = 'empty_clob()'; /// Set the default value to be inserted (preparing lob storage for next update) } } $cleaned[$field] = $value; } $this->insert_record_raw($table, $cleaned, false, true, true); if (empty($blobs) and empty($clobs)) { return true; } /// We have BLOBs or CLOBs to postprocess foreach ($blobs as $key=>$value) { $this->query_start('--adodb-UpdateBlob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateBlob($this->prefix.$table, $key, $value, "id = $id"); $this->query_end($result); } foreach ($clobs as $key=>$value) { $this->query_start('--adodb-UpdateClob', null, SQL_QUERY_UPDATE); $result = $this->adodb->UpdateClob($this->prefix.$table, $key, $value, "id = $id"); $this->query_end($result); } return true; } }