<commit_before><?php
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Hydrate');
/**
 * Doctrine_RawSql
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      <NAME> <<EMAIL>>
 */
class Doctrine_RawSql extends Doctrine_Hydrate {
    /**
     * @var array $fields
     */
    private $fields;
    /**
     * __call
     * method overloader
     *
     * @param string $name
     * @param array $args
     * @return Doctrine_RawSql
     */
    public function __call($name, $args) {
        if( ! isset($this->parts[$name]))
            throw new Doctrine_RawSql_Exception("Unknown overload method $name. Availible overload methods are ".implode(" ",array_keys($this->parts)));

        if($name == 'select') {
            preg_match_all('/{([^}{]*)}/U', $args[0], $m);

            $this->fields = $m[1];
            $this->parts["select"] = array();
        } else
            $this->parts[$name][] = $args[0];

        return $this;
    }
    /**
     * get
     */
    public function get($name) {
        if( ! isset($this->parts[$name]))
            throw new Doctrine_RawSql_Exception('Unknown query part '.$name);
            
        return $this->parts[$name];
    }
    /**
     * parseQuery
     *
     * @param string $query
     * @return Doctrine_RawSql
     */
    public function parseQuery($query) {
        preg_match_all('/{([^}{]*)}/U', $query, $m);

        $this->fields = $m[1];
        $this->clear();

        $e = Doctrine_Query::sqlExplode($query,' ');

        foreach($e as $k => $part):
            $low = strtolower($part);
            switch(strtolower($part)):
                case "select":
                case "from":
                case "where":
                case "limit":
                case "offset":
                case "having":
                    $p = $low;
                    if( ! isset($parts[$low]))
                        $parts[$low] = array();

                break;
                case "order":
                case "group":
                    $i = ($k + 1);
                    if(isset($e[$i]) && strtolower($e[$i]) === "by") {
                        $p = $low;
                        $p .= "by";
                        $parts[$low."by"] = array();

                    } else
                        $parts[$p][] = $part;
                break;
                case "by":
                    continue;
                default:
                    if( ! isset($parts[$p][0])) 
                        $parts[$p][0] = $part;
                    else
                        $parts[$p][0] .= ' '.$part;
            endswitch;
        endforeach;

        $this->parts = $parts;
        $this->parts["select"] = array();
        
        return $this;
    }
    /**
     * getQuery
     *
     *
     * @return string
     */
    public function getQuery() {
        foreach($this->fields as $field) {
            $e = explode(".", $field);
            if( ! isset($e[1]))
                throw new Doctrine_RawSql_Exception("All selected fields in Sql query must be in format tableAlias.fieldName");

            if( ! isset($this->tables[$e[0]])) {
                try {
                    $this->addComponent($e[0], ucwords($e[0]));
                } catch(Doctrine_Exception $exception) {
                    throw new Doctrine_RawSql_Exception("The associated component for table alias $e[0] couldn't be found.");
                }
            }

            if($e[1] == '*') {
                foreach($this->tables[$e[0]]->getColumnNames() as $name) {
                    $field = $e[0].".".$name;
                    $this->parts["select"][$field] = $field." AS ".$e[0]."__".$name;
                }
            } else {
                $field = $e[0].".".$e[1];
                $this->parts["select"][$field] = $field." AS ".$e[0]."__".$e[1];
            }
        }

        // force-add all primary key fields

        foreach($this->tableAliases as $alias) {
            foreach($this->tables[$alias]->getPrimaryKeys() as $key) {
                $field = $alias.".".$key;
                if( ! isset($this->parts["select"][$field]))
                    $this->parts["select"][$field] = $field." AS ".$alias."__".$key;
            }
        }

        $q = 'SELECT '.implode(', ', $this->parts['select']);

        $string = $this->applyInheritance();
        if( ! empty($string))
            $this->parts['where'][] = $string;

        $copy = $this->parts;
        unset($copy['select']);

        $q .= ( ! empty($this->parts['from']))?    ' FROM '     . implode(' ', $this->parts['from']) : '';
        $q .= ( ! empty($this->parts['where']))?   ' WHERE '    . implode(' AND ', $this->parts['where']) : '';
        $q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
        $q .= ( ! empty($this->parts['having']))?  ' HAVING '   . implode(' ', $this->parts['having']) : '';
        $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(' ', $this->parts['orderby']) : '';

        if( ! empty($string))
            array_pop($this->parts['where']);

        return $q;<commit_msg>limit offset support for rawsql<commit_after><?php
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
Doctrine::autoload('Doctrine_Hydrate');
/**
 * Doctrine_RawSql
 *
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      <NAME> <<EMAIL>>
 */
class Doctrine_RawSql extends Doctrine_Hydrate {
    /**
     * @var array $fields
     */
    private $fields;
    /**
     * __call
     * method overloader
     *
     * @param string $name
     * @param array $args
     * @return Doctrine_RawSql
     */
    public function __call($name, $args) {
        if( ! isset($this->parts[$name]))
            throw new Doctrine_RawSql_Exception("Unknown overload method $name. Availible overload methods are ".implode(" ",array_keys($this->parts)));

        if($name == 'select') {
            preg_match_all('/{([^}{]*)}/U', $args[0], $m);

            $this->fields = $m[1];
            $this->parts["select"] = array();
        } else
            $this->parts[$name][] = $args[0];

        return $this;
    }
    /**
     * get
     */
    public function get($name) {
        if( ! isset($this->parts[$name]))
            throw new Doctrine_RawSql_Exception('Unknown query part '.$name);
            
        return $this->parts[$name];
    }
    /**
     * parseQuery
     *
     * @param string $query
     * @return Doctrine_RawSql
     */
    public function parseQuery($query) {
        preg_match_all('/{([^}{]*)}/U', $query, $m);

        $this->fields = $m[1];
        $this->clear();

        $e = Doctrine_Query::sqlExplode($query,' ');

        foreach($e as $k => $part):
            $low = strtolower($part);
            switch(strtolower($part)):
                case "select":
                case "from":
                case "where":
                case "limit":
                case "offset":
                case "having":
                    $p = $low;
                    if( ! isset($parts[$low]))
                        $parts[$low] = array();

                break;
                case "order":
                case "group":
                    $i = ($k + 1);
                    if(isset($e[$i]) && strtolower($e[$i]) === "by") {
                        $p = $low;
                        $p .= "by";
                        $parts[$low."by"] = array();

                    } else
                        $parts[$p][] = $part;
                break;
                case "by":
                    continue;
                default:
                    if( ! isset($parts[$p][0])) 
                        $parts[$p][0] = $part;
                    else
                        $parts[$p][0] .= ' '.$part;
            endswitch;
        endforeach;

        $this->parts = $parts;
        $this->parts["select"] = array();
        
        return $this;
    }
    /**
     * getQuery
     *
     *
     * @return string
     */
    public function getQuery() {
        foreach($this->fields as $field) {
            $e = explode(".", $field);
            if( ! isset($e[1]))
                throw new Doctrine_RawSql_Exception("All selected fields in Sql query must be in format tableAlias.fieldName");

            if( ! isset($this->tables[$e[0]])) {
                try {
                    $this->addComponent($e[0], ucwords($e[0]));
                } catch(Doctrine_Exception $exception) {
                    throw new Doctrine_RawSql_Exception("The associated component for table alias $e[0] couldn't be found.");
                }
            }

            if($e[1] == '*') {
                foreach($this->tables[$e[0]]->getColumnNames() as $name) {
                    $field = $e[0].".".$name;
                    $this->parts["select"][$field] = $field." AS ".$e[0]."__".$name;
                }
            } else {
                $field = $e[0].".".$e[1];
                $this->parts["select"][$field] = $field." AS ".$e[0]."__".$e[1];
            }
        }

        // force-add all primary key fields

        foreach($this->tableAliases as $alias) {
            foreach($this->tables[$alias]->getPrimaryKeys() as $key) {
                $field = $alias . '.' . $key;
                if( ! isset($this->parts["select"][$field]))
                    $this->parts["select"][$field] = $field." AS ".$alias."__".$key;
            }
        }

        $q = 'SELECT '.implode(', ', $this->parts['select']);

        $string = $this->applyInheritance();
        if( ! empty($string))
            $this->parts['where'][] = $string;

        $copy = $this->parts;
        unset($copy['select']);

        $q .= ( ! empty($this->parts['from']))?    ' FROM '     . implode(' ', $this->parts['from']) : '';
        $q .= ( ! empty($this->parts['where']))?   ' WHERE '    . implode(' AND ', $this->parts['where']) : '';
        $q .= ( ! empty($this->parts['groupby']))? ' GROUP BY ' . implode(', ', $this->parts['groupby']) : '';
        $q .= ( ! empty($this->parts['having']))?  ' HAVING '   . implode(' ', $this->parts['having']) : '';
        $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(' ', $this->parts['orderby']) : '';
        $q .= ( ! empty($this->parts['limit']))?   ' LIMIT ' . implode(' ', $this->parts['limit']) : '';
        $q .= ( ! empty($this->parts['offset']))?  ' OFFSET ' . implode(' ', $this->parts['offset']) : '';

        if( ! empty($string))
            array_pop($this->parts['where']);

        return $q;
{'fraction_non_alphanumeric': 0.1703958691910499, 'fraction_numerical': 0.0035988108277264902, 'mean_word_length': 1.7573339085418465, 'pattern_counts': {'":': 18, '<': 13, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2096249', 'n_tokens_mistral': 3831, 'n_tokens_neox': 3595, 'n_words': 884}