<commit_before><?php

    class entity_link_driver_mysql implements entity_link_driver {

        /**
         * Parse the table name into a properly escaped table string
         *
         * @param string $table
         *
         * @return string
         */
        protected static function table($table) {
            if (strpos($table, '.') !== false) {
                $table = explode('.', $table);
                return "{$table[0]}`.`{$table[1]}";
            } else {
                return $table;
            }
        }

        /**
         * Get specific fields from a record, by keys
         *
         * @param entity_link_dao $self the name of the DAO
         * @param string          $pool which source engine pool to use
         * @param array           $select_fields
         * @param array           $fieldvals
         *
         * @return array
         */
        public static function by_fields(entity_link_dao $self, $pool, array $select_fields, array $fieldvals) {

            $where = [];
            $vals  = [];

            if ($fieldvals) {
                foreach ($fieldvals as $k => $v) {
                    if ($v === null) {
                        $where[] = "`{$k}` IS NULL";
                    } else {
                        $vals[]  = $v;
                        $where[] = "`{$k}` = ?";
                    }
                }
            }

            $rs = core::sql($pool)->prepare("
                SELECT " . join(',', $select_fields) . "
                FROM `" . self::table($self::TABLE) . "`
                " . (count($where) ? "WHERE " . join(" AND ", $where) : "") . "
            ");

            $rs->execute($vals);

            if (count($select_fields) === 1) {
                return $rs->fetchAll(PDO::FETCH_COLUMN, 0);
            } else {
                return $rs->fetchAll();
            }
        }

        /**
         * Get specific fields from multiple records, by keys
         *
         * @param entity_link_dao $self the name of the DAO
         * @param string          $pool which source engine pool to use
         * @param array           $select_fields
         * @param array           $fieldvals_arr
         *
         * @return array
         */
        public static function by_fields_multi(entity_link_dao $self, $pool, array $select_fields, array $fieldvals_arr) {
            $fieldvals_fields     = array_keys(reset($fieldvals_arr));
            $quoted_select_fields = [];
            $reverse_lookup       = [];
            $return               = [];
            $vals                 = [];
            $where                = [];

            foreach (array_unique(array_merge($select_fields, $fieldvals_fields)) as $field) {
                $quoted_select_fields[] = "`{$field}`";
            }

            foreach ($fieldvals_arr as $k => $fieldvals) {
                $w             = [];
                $return[$k]    = [];
                $hashed_valued = [];
                foreach ($fieldvals as $field => $val) {
                    if ($val === null) {
                        $w[]             = "`{$field}` IS NULL";
                        $hashed_valued[] = '';
                    } else {
                        $vals[]          = $val;
                        $w[]             = "`{$field}` = ?";
                        $hashed_valued[] = md5($val);
                    }
                }
                $reverse_lookup[join(':', $hashed_valued)] = $k;
                $where[] = '(' . join(" AND ", $w) . ')';
            }

            $rs = core::sql($pool)->prepare("
                SELECT " . join(',', $quoted_select_fields) . "
                FROM `" . self::table($self::TABLE) . "`
                WHERE " . join(' OR ', $where) . "
            ");
            $rs->execute($vals);

            if (count($select_fields) === 1) {
                $field = reset($select_fields);
                foreach ($rs->fetchAll() as $row) {
                    $hashed = [];
                    foreach ($fieldvals_fields as $field) {
                        $hashed[$row[$field]] = md5($row[$field]);
                    }
                    $return[$reverse_lookup[join(':', $hashed)]][] = $row[$field];
                }
            } else {
                // If the selected field count is different than the requested fields, only return the requested fields
                if (count($select_fields) !== count($quoted_select_fields)) {
                    $select_fields = array_keys($select_fields);
                    foreach ($rs->fetchAll() as $row) {
                        $hashed = [];
                        foreach ($fieldvals_fields as $field) {
                            $hashed[$row[$field]] = md5($row[$field]);
                        }
                        $return[$reverse_lookup[join(':', $hashed)]][] = array_intersect_key($row, $select_fields);
                    }
                } else {
                    foreach ($rs->fetchAll() as $row) {
                        $hashed = [];
                        foreach ($fieldvals_fields as $field) {
                            $hashed[$row[$field]] = md5($row[$field]);
                        }
                        $return[$reverse_lookup[join(':', $hashed)]][] = $row;
                    }
                }
            }

            return $return;
        }

        /**<commit_msg>Fixed a variable collision in the entity_link mysql driver causing the wrong data to be returned.<commit_after><?php

    class entity_link_driver_mysql implements entity_link_driver {

        /**
         * Parse the table name into a properly escaped table string
         *
         * @param string $table
         *
         * @return string
         */
        protected static function table($table) {
            if (strpos($table, '.') !== false) {
                $table = explode('.', $table);
                return "{$table[0]}`.`{$table[1]}";
            } else {
                return $table;
            }
        }

        /**
         * Get specific fields from a record, by keys
         *
         * @param entity_link_dao $self the name of the DAO
         * @param string          $pool which source engine pool to use
         * @param array           $select_fields
         * @param array           $fieldvals
         *
         * @return array
         */
        public static function by_fields(entity_link_dao $self, $pool, array $select_fields, array $fieldvals) {

            $where = [];
            $vals  = [];

            if ($fieldvals) {
                foreach ($fieldvals as $k => $v) {
                    if ($v === null) {
                        $where[] = "`{$k}` IS NULL";
                    } else {
                        $vals[]  = $v;
                        $where[] = "`{$k}` = ?";
                    }
                }
            }

            $rs = core::sql($pool)->prepare("
                SELECT " . join(',', $select_fields) . "
                FROM `" . self::table($self::TABLE) . "`
                " . (count($where) ? "WHERE " . join(" AND ", $where) : "") . "
            ");

            $rs->execute($vals);

            if (count($select_fields) === 1) {
                return $rs->fetchAll(PDO::FETCH_COLUMN, 0);
            } else {
                return $rs->fetchAll();
            }
        }

        /**
         * Get specific fields from multiple records, by keys
         *
         * @param entity_link_dao $self the name of the DAO
         * @param string          $pool which source engine pool to use
         * @param array           $select_fields
         * @param array           $fieldvals_arr
         *
         * @return array
         */
        public static function by_fields_multi(entity_link_dao $self, $pool, array $select_fields, array $fieldvals_arr) {
            $fieldvals_fields     = array_keys(reset($fieldvals_arr));
            $quoted_select_fields = [];
            $reverse_lookup       = [];
            $return               = [];
            $vals                 = [];
            $where                = [];

            foreach (array_unique(array_merge($select_fields, $fieldvals_fields)) as $field) {
                $quoted_select_fields[] = "`{$field}`";
            }

            foreach ($fieldvals_arr as $k => $fieldvals) {
                $w             = [];
                $return[$k]    = [];
                $hashed_valued = [];
                foreach ($fieldvals as $field => $val) {
                    if ($val === null) {
                        $w[]             = "`{$field}` IS NULL";
                        $hashed_valued[] = '';
                    } else {
                        $vals[]          = $val;
                        $w[]             = "`{$field}` = ?";
                        $hashed_valued[] = md5($val);
                    }
                }
                $reverse_lookup[join(':', $hashed_valued)] = $k;
                $where[] = '(' . join(" AND ", $w) . ')';
            }

            $rs = core::sql($pool)->prepare("
                SELECT " . join(',', $quoted_select_fields) . "
                FROM `" . self::table($self::TABLE) . "`
                WHERE " . join(' OR ', $where) . "
            ");
            $rs->execute($vals);

            if (count($select_fields) === 1) {
                $field = reset($select_fields);
                foreach ($rs->fetchAll() as $row) {
                    $hashed = [];
                    foreach ($fieldvals_fields as $k) {
                        $hashed[$row[$field]] = md5($row[$k]);
                    }
                    $return[$reverse_lookup[join(':', $hashed)]][] = $row[$field];
                }
            } else {
                // If the selected field count is different than the requested fields, only return the requested fields
                if (count($select_fields) !== count($quoted_select_fields)) {
                    $select_fields = array_keys($select_fields);
                    foreach ($rs->fetchAll() as $row) {
                        $hashed = [];
                        foreach ($fieldvals_fields as $field) {
                            $hashed[$row[$field]] = md5($row[$field]);
                        }
                        $return[$reverse_lookup[join(':', $hashed)]][] = array_intersect_key($row, $select_fields);
                    }
                } else {
                    foreach ($rs->fetchAll() as $row) {
                        $hashed = [];
                        foreach ($fieldvals_fields as $field) {
                            $hashed[$row[$field]] = md5($row[$field]);
                        }
                        $return[$reverse_lookup[join(':', $hashed)]][] = $row;
                    }
                }
            }

            return $return;
        }

        /**
{'fraction_non_alphanumeric': 0.1375734214390602, 'fraction_numerical': 0.0016519823788546256, 'mean_word_length': 1.1155115511551155, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1349358', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2707, 'n_words': 611}