예제 #1
0
/**
 * Returns the next data set from a DB resource or false if there are no more results.
 *
 * @param resource $cursor
 * @param bool $convertNulls	convert all null values to string zeroes
 *
 * @return array|bool
 */
function DBfetch($cursor, $convertNulls = true)
{
    global $DB;
    $result = false;
    if (!isset($DB['DB']) || empty($DB['DB']) || is_bool($cursor)) {
        return $result;
    }
    switch ($DB['TYPE']) {
        case ZBX_DB_MYSQL:
            $result = mysqli_fetch_assoc($cursor);
            if (!$result) {
                mysqli_free_result($cursor);
            }
            break;
        case ZBX_DB_POSTGRESQL:
            if (!($result = pg_fetch_assoc($cursor))) {
                pg_free_result($cursor);
            }
            break;
        case ZBX_DB_ORACLE:
            if ($row = oci_fetch_assoc($cursor)) {
                $result = array();
                foreach ($row as $key => $value) {
                    $field_type = strtolower(oci_field_type($cursor, $key));
                    // Oracle does not support NULL values for string fields, so if the string is empty, it will return NULL
                    // convert it to an empty string to be consistent with other databases
                    $value = str_in_array($field_type, array('varchar', 'varchar2', 'blob', 'clob')) && is_null($value) ? '' : $value;
                    if (is_object($value) && strpos($field_type, 'lob') !== false) {
                        $value = $value->load();
                    }
                    $result[strtolower($key)] = $value;
                }
            }
            break;
        case ZBX_DB_DB2:
            if (!($result = db2_fetch_assoc($cursor))) {
                db2_free_result($cursor);
            } else {
                // cast all of the values to string to be consistent with other DB drivers: all of them return
                // only strings.
                foreach ($result as &$value) {
                    if ($value !== null) {
                        $value = (string) $value;
                    }
                }
                unset($value);
            }
            break;
        case ZBX_DB_SQLITE3:
            if ($DB['TRANSACTIONS'] == 0) {
                lock_sqlite3_access();
            }
            if (!($result = $cursor->fetchArray(SQLITE3_ASSOC))) {
                unset($cursor);
            } else {
                // cast all of the values to string to be consistent with other DB drivers: all of them return
                // only strings.
                foreach ($result as &$value) {
                    $value = (string) $value;
                }
                unset($value);
            }
            if ($DB['TRANSACTIONS'] == 0) {
                unlock_sqlite3_access();
            }
            break;
    }
    if ($result) {
        if ($convertNulls) {
            foreach ($result as $key => $val) {
                if (is_null($val)) {
                    $result[$key] = '0';
                }
            }
        }
        return $result;
    }
    return false;
}
예제 #2
0
function DBfetch(&$cursor, $convertNulls = true)
{
    global $DB;
    $result = false;
    if (!isset($DB['DB']) || empty($DB['DB']) || is_bool($cursor)) {
        return $result;
    }
    switch ($DB['TYPE']) {
        case ZBX_DB_MYSQL:
            if (!($result = mysql_fetch_assoc($cursor))) {
                mysql_free_result($cursor);
            }
            break;
        case ZBX_DB_POSTGRESQL:
            if (!($result = pg_fetch_assoc($cursor))) {
                pg_free_result($cursor);
            }
            break;
        case ZBX_DB_ORACLE:
            if (ocifetchinto($cursor, $row, OCI_ASSOC + OCI_RETURN_NULLS)) {
                $result = array();
                foreach ($row as $key => $value) {
                    $field_type = zbx_strtolower(oci_field_type($cursor, $key));
                    $value = str_in_array($field_type, array('varchar', 'varchar2', 'blob', 'clob')) && is_null($value) ? '' : $value;
                    if (is_object($value) && zbx_stristr($field_type, 'lob') !== false) {
                        $value = $value->load();
                    }
                    $result[zbx_strtolower($key)] = $value;
                }
            }
            break;
        case ZBX_DB_DB2:
            if (!($result = db2_fetch_assoc($cursor))) {
                db2_free_result($cursor);
            } else {
                // cast all of the values to string to be consistent with other DB drivers: all of them return
                // only strings.
                foreach ($result as &$value) {
                    if ($value !== null) {
                        $value = (string) $value;
                    }
                }
                unset($value);
            }
            break;
        case ZBX_DB_SQLITE3:
            if ($DB['TRANSACTIONS'] == 0) {
                lock_sqlite3_access();
            }
            if (!($result = $cursor->fetchArray(SQLITE3_ASSOC))) {
                unset($cursor);
            } else {
                // cast all of the values to string to be consistent with other DB drivers: all of them return
                // only strings.
                foreach ($result as &$value) {
                    $value = (string) $value;
                }
                unset($value);
            }
            if ($DB['TRANSACTIONS'] == 0) {
                unlock_sqlite3_access();
            }
            break;
    }
    if ($convertNulls && $result) {
        foreach ($result as $key => $val) {
            if (is_null($val)) {
                $result[$key] = '0';
            }
        }
    }
    return $result;
}