Skip to content

Commit

Permalink
MDL-14679 removed some ora hack
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 1, 2008
1 parent c72ff23 commit 95ab92b
Showing 1 changed file with 0 additions and 89 deletions.
89 changes: 0 additions & 89 deletions lib/dmllib_todo.php
Expand Up @@ -695,95 +695,6 @@ function delete_records_select($table, $select='') {
return $rs;
}

/**
* 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 dmllib at all
*
* @param $table string the table where the record is going to be inserted/updated (without prefix)
* @param $dataobject object the object to be inserted/updated
* @param $usecache boolean flag to determinate if we must use the per request cache of metadata
* true to use it, false to ignore and delete it
*/
function oracle_dirty_hack ($table, &$dataobject, $usecache = true) {

global $CFG, $db, $metadata_cache;

/// Init and delete metadata cache
if (!isset($metadata_cache) || !$usecache) {
$metadata_cache = array();
}

/// 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 db isn't Oracle, return without modif
if ( $CFG->dbfamily != 'oracle') {
return;
}

/// Get Meta info to know what to change, using the cached meta if exists
if (!isset($metadata_cache[$table])) {
$metadata_cache[$table] = array_change_key_case($db->MetaColumns($CFG->prefix . $table), CASE_LOWER);
}
$columns = $metadata_cache[$table];
/// Iterate over all the fields in the insert, transforming values
/// in the best possible form
foreach ($dataobject as $fieldname => $fieldvalue) {
/// If the field doesn't exist in metadata, skip
if (!isset($columns[strtolower($fieldname)])) {
continue;
}
/// If the field ins't VARCHAR or CLOB, skip
if ($columns[strtolower($fieldname)]->type != 'VARCHAR2' && $columns[strtolower($fieldname)]->type != 'CLOB') {
continue;
}
/// If the field isn't NOT NULL, skip (it's nullable, so accept empty values)
if (!$columns[strtolower($fieldname)]->not_null) {
continue;
}
/// If the value isn't empty, skip
if (!empty($fieldvalue)) {
continue;
}
/// 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 ($fieldvalue === '0') {
continue;
}

/// Transformations start
if (gettype($fieldvalue) == 'boolean') {
$dataobject->$fieldname = '0'; /// Transform false to '0' that evaluates the same for PHP
} else if (gettype($fieldvalue) == 'integer') {
$dataobject->$fieldname = '0'; /// Transform 0 to '0' that evaluates the same for PHP
} else if (gettype($fieldvalue) == 'NULL') {
$dataobject->$fieldname = '0'; /// Transform NULL to '0' that evaluates the same for PHP
} else if ($fieldvalue === '') {
$dataobject->$fieldname = ' '; /// Transform '' to ' ' that DONT'T EVALUATE THE SAME
/// (we'll transform back again on get_records_XXX functions and others)!!
}
}
}
/// End of DIRTY HACK

/**
* 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 ''
Expand Down

0 comments on commit 95ab92b

Please sign in to comment.