Пример #1
0
function fetchAttrsForObjects($object_set = array())
{
    $ret = array();
    $query = "select AM.attr_id, A.name as attr_name, A.type as attr_type, C.name as chapter_name, " . "C.id as chapter_id, AV.uint_value, AV.float_value, AV.string_value, D.dict_value, O.id as object_id from " . "Object as O left join AttributeMap as AM on O.objtype_id = AM.objtype_id " . "left join Attribute as A on AM.attr_id = A.id " . "left join AttributeValue as AV on AV.attr_id = AM.attr_id and AV.object_id = O.id " . "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_id = D.chapter_id " . "left join Chapter as C on AM.chapter_id = C.id";
    if (count($object_set)) {
        $query .= ' WHERE O.id IN (' . implode(', ', $object_set) . ')';
    }
    $query .= " ORDER BY A.name, A.type";
    $result = usePreparedSelectBlade($query);
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $object_id = $row['object_id'];
        if (!array_key_exists($object_id, $ret)) {
            $ret[$object_id] = array();
        }
        # Objects with zero attributes also matter due to the LEFT JOIN. Create
        # keys for them too to enable negative caching.
        if ($row['attr_id'] == NULL) {
            continue;
        }
        $record = array();
        $record['id'] = $row['attr_id'];
        $record['name'] = $row['attr_name'];
        $record['type'] = $row['attr_type'];
        switch ($row['attr_type']) {
            case 'dict':
                $record['chapter_id'] = $row['chapter_id'];
                $record['chapter_name'] = $row['chapter_name'];
                $record['key'] = $row['uint_value'];
                // fall through
            // fall through
            case 'uint':
            case 'float':
            case 'string':
                $record['value'] = $row[$row['attr_type'] . '_value'];
                parseWikiLink($record);
                break;
            case 'date':
                $record['value'] = $row['uint_value'];
                break;
            default:
                $record['value'] = NULL;
                break;
        }
        $ret[$object_id][$row['attr_id']] = $record;
    }
    return $ret;
}
Пример #2
0
function getAttrValuesForNetwork($network)
{
    global $netobject_type_id;
    switch ($network['realm']) {
        case 'ipv4net':
            $av_table = 'AttributeValue_IPv4';
            $o_table = 'IPv4Network';
            break;
        case 'ipv6net':
            $av_table = 'AttributeValue_IPv6';
            $o_table = 'IPv6Network';
            break;
        default:
            throw new InvalidArgException('realm', $network['realm'], "Unknown realm");
    }
    $ret = array();
    $query = "select AM.attr_id, A.name as attr_name, A.type as attr_type, C.name as chapter_name, " . "C.id as chapter_id, AV.uint_value, AV.float_value, AV.string_value, D.dict_value, O.id as object_id from " . "{$o_table} as O left join AttributeMap as AM on AM.objtype_id = ? " . "left join Attribute as A on AM.attr_id = A.id " . "left join {$av_table} as AV on AV.attr_id = AM.attr_id and AV.net_id = O.id " . "left join Dictionary as D on D.dict_key = AV.uint_value and AM.chapter_id = D.chapter_id " . "left join Chapter as C on AM.chapter_id = C.id " . " WHERE O.id = ?";
    $query .= " order by O.ip, O.mask";
    $result = usePreparedSelectBlade($query, array($netobject_type_id[$network['realm']], $network['id']));
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $object_id = $row['object_id'];
        # Objects with zero attributes also matter due to the LEFT JOIN. Create
        # keys for them too to enable negative caching.
        if ($row['attr_id'] == NULL) {
            continue;
        }
        $record = array();
        $record['id'] = $row['attr_id'];
        $record['name'] = $row['attr_name'];
        $record['type'] = $row['attr_type'];
        switch ($row['attr_type']) {
            case 'dict':
                $record['chapter_id'] = $row['chapter_id'];
                $record['chapter_name'] = $row['chapter_name'];
                $record['key'] = $row['uint_value'];
                // fall through
            // fall through
            case 'uint':
            case 'float':
            case 'string':
                $record['value'] = $row[$row['attr_type'] . '_value'];
                parseWikiLink($record);
                break;
            case 'date':
                $record['value'] = $row['uint_value'];
                break;
            default:
                $record['value'] = NULL;
                break;
        }
        $ret[$row['attr_id']] = $record;
    }
    return $ret;
}