<commit_before><?php

/**
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2013, webtischlerei <http://www.webtischlerei.de>
 */
class Moraso_Eav {

    public static function deleteEntity($id) {

        Moraso_Db::delete('_eav_entity', array('entity_id' => $id));
    }

    public static function setValues($attribute_set, $data) {

        $id = isset($data['id']) && !empty($data['id']) ? $data['id'] : Moraso_Db::query('insert into _eav_entity (entity_id) values (NULL)')->getLastInsertId();

        unset($data['id']);

        foreach ($data as $attribute_alias => $value) {

            $attribute_set_id = self::_getAttributeSetId($attribute_set);

            if (!$attribute_set_id) {
                $attribute_set_id = Moraso_Db::put('_eav_attribute_set', 'attribute_set_id', array(
                            'attribute_set_name' => $attribute_set
                ));
            }

            $attribute_id = Moraso_Db::simpleFetch('attribute_id', '_eav_attribute', array(
                        'attribute_alias' => $attribute_alias,
                        'attribute_set_id' => $attribute_set_id
            ));

            if (!$attribute_id) {
                $attribute_id = Moraso_Db::put('_eav_attribute', 'attribute_id', array(
                            'attribute_alias' => $attribute_alias,
                            'attribute_set_id' => $attribute_set_id
                ));
            }

            if (is_float($value)) {
                $type = 'float';
            } elseif (is_numeric($value)) {
                $type = 'integer';
            } else {
                $type = 'string';
            }

            $entity_attribute_id = Moraso_Db::simpleFetch('entity_attribute_id', '_eav_entity_attribute', array(
                        'entity_id' => $id,
                        'attribute_id' => $attribute_id
            ));

            if (!empty($entity_attribute_id)) {
                $value_id = Moraso_Db::simpleFetch('value_id', '_eav_value', array(
                            'entity_attribute_id' => $entity_attribute_id
                ));
            } else {
                $entity_attribute_id = Moraso_Db::put('_eav_entity_attribute', 'entity_attribute_id', array(
                            'entity_id' => $id,
                            'attribute_id' => $attribute_id
                ));

                $value_id = Moraso_Db::put('_eav_value', 'value_id', array(
                            'entity_attribute_id' => $entity_attribute_id
                ));
            }

            if (is_array($value) || is_object($value)) {
                $value = serialize($value);
            }

            $data = array(
                'value_id' => $value_id,
                'value_' . $type => $value
            );

            Moraso_Db::delete('_eav_value', array('value_id' => $value_id));
            Moraso_Db::put('_eav_value', NULL, $data);
        }
    }

    public static function getData($attribute_set, $entity_id = null) {

        $attribute_set_id = self::_getAttributeSetId($attribute_set);

        $data = array();

        if (!empty($entity_id)) {
            $rows = Moraso_Db::fetchAll('' .
                            'select ' .
                            '   a.attribute_alias, ' .
                            '   coalesce( ' .
                            '       v.value_string, ' .<commit_msg>eav system optimiert<commit_after><?php

/**
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2013, webtischlerei <http://www.webtischlerei.de>
 */
class Moraso_Eav {

    public static function delete($id) {

        Moraso_Db::delete('_eav_entity', array('entity_id' => $id));
    }

    public static function set($attribute_set, $data) {

        $id = isset($data['id']) && !empty($data['id']) ? $data['id'] : Moraso_Db::query('insert into _eav_entity (entity_id) values (NULL)')->getLastInsertId();

        unset($data['id']);

        foreach ($data as $attribute_alias => $value) {

            $attribute_set_id = self::_getAttributeSetId($attribute_set);

            if (!$attribute_set_id) {
                $attribute_set_id = Moraso_Db::put('_eav_attribute_set', 'attribute_set_id', array(
                            'attribute_set_name' => $attribute_set
                ));
            }

            $attribute_id = Moraso_Db::simpleFetch('attribute_id', '_eav_attribute', array(
                        'attribute_alias' => $attribute_alias,
                        'attribute_set_id' => $attribute_set_id
            ));

            if (!$attribute_id) {
                $attribute_id = Moraso_Db::put('_eav_attribute', 'attribute_id', array(
                            'attribute_alias' => $attribute_alias,
                            'attribute_set_id' => $attribute_set_id
                ));
            }

            if (is_float($value)) {
                $type = 'float';
            } elseif (is_numeric($value)) {
                $type = 'integer';
            } else {
                $type = 'string';
            }

            $entity_attribute_id = Moraso_Db::simpleFetch('entity_attribute_id', '_eav_entity_attribute', array(
                        'entity_id' => $id,
                        'attribute_id' => $attribute_id
            ));

            if (!empty($entity_attribute_id)) {
                $value_id = Moraso_Db::simpleFetch('value_id', '_eav_value', array(
                            'entity_attribute_id' => $entity_attribute_id
                ));
            } else {
                $entity_attribute_id = Moraso_Db::put('_eav_entity_attribute', 'entity_attribute_id', array(
                            'entity_id' => $id,
                            'attribute_id' => $attribute_id
                ));

                $value_id = Moraso_Db::put('_eav_value', 'value_id', array(
                            'entity_attribute_id' => $entity_attribute_id
                ));
            }

            if (is_array($value) || is_object($value)) {
                $value = serialize($value);
            }

            $data = array(
                'value_id' => $value_id,
                'value_' . $type => $value
            );

            Moraso_Db::delete('_eav_value', array('value_id' => $value_id));
            Moraso_Db::put('_eav_value', NULL, $data);
        }
    }

    public static function get($attribute_set, $entity_id = null) {

        $attribute_set_id = self::_getAttributeSetId($attribute_set);

        $data = array();

        if (!empty($entity_id)) {
            $rows = Moraso_Db::fetchAll('' .
                            'select ' .
                            '   a.attribute_alias, ' .
                            '   coalesce( ' .
                            '       v.value_string, ' .
{'fraction_non_alphanumeric': 0.13402214022140221, 'fraction_numerical': 0.0011808118081180813, 'mean_word_length': 1.59915611814346, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5487264', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1915, 'n_words': 292}