<commit_before><?php
/**
 * Cloudlib
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   Copyright (c) 2013 <NAME> <<EMAIL>>
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

namespace cloudlib\helpers;

use PDO;
use PDOException;
use RuntimeException;

/**
 * The Database class
 *
 * @copyright   Copyright (c) 2013 <NAME> <<EMAIL>>
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class Database
{
    /**
     * The Database connection
     *
     * @access  public
     * @var     object
     */
    public $connection = null;

    /**
     * Database connection settings
     *
     * @access  public
     * @var     array
     */
    public $settings = array();

    /**
     * Current Database statement (query)
     *
     * @access  public
     * @var     string
     */
    public $statement = '';

    /**
     * PDO Bindings for prepared statements
     *
     * @access  public
     * @var     array
     */
    public $bindings = array();

    /**
     * The current statement handler
     *
     * @access  public
     * @var     object
     */
    public $statementHandler = null;

    /**
     * Array of error information
     *
     * @access  public
     * @var     array
     */
    public $errorInfo = null;

    /**
     * Error code of database exception
     *
     * @access  public
     * @var     int
     */
    public $errorCode = null;

    /**
     * PDO Exception
     *
     * @access  public
     * @var     object
     */
    public $exception = null;

    /**
     * Method to be used when executing a database statement
     *
     * @access  public
     * @var     string
     */
    public $method = false;

    /**
     * Array of columns for the current query
     *
     * @access  public
     * @var     array
     */
    public $columns = array();

    /**
     * Initialize the database connection
     *
     * @access  public
     * @param   mixed   $settings   Array or String to be used to connect to the database
     * @param   bool    $wait       If we should wait with connecting to the database
     * @return  void
     */
    public function __construct($settings, $wait = false)
    {
        if(is_string($settings))
        {
            try
            {
                extract(parse_url($settings));

                $settings = array(
                    'dsn' => sprintf('%s:host=%s;port=%s;dbname=%s',
                        $scheme, $host, $port, trim($path, '/')),
                    'username' => $user,
                    'password' => $<PASSWORD>
                );
            }
            catch(Exception $e)
            {
                throw new InvalidArgumentException(
                    sprintf('Invalid Database URL [%]', $settings)
                );
            }
        }

        $this->settings = $settings;

        if( ! $wait)
        {
            $this->connect();
        }
    }

    /**
     * Initialize a connection to a database
     *
     * @access  public
     * @param   mixed   $settings   Array or String to be used to connect to the database
     * @return  object              Returns the database connection
     */
    public function connect($settings = null)
    {
        $settings = $settings ? $settings : $this->settings;

        $driverOptions = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        );

        try
        {
            $this->connection = new PDO(
                $settings['dsn'],
                $settings['username'],
                $settings['password'],
                $driverOptions
            );
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            return $this->connection;
        }
        catch(PDOException $e)
        {
            throw new RuntimeException('Unable to establish database connection');
        }
    }

    /**
     * Shorthand method for beginTransaction()
     *
     * @access  public
     * @return  bool
     */
    public function beginTransaction()
    {
        return $this->connection->beginTransaction();
    }

    /**
     * Shorthand method for rollBack()
     *
     * @access  public
     * @return  bool
     */
    public function rollBack()
    {
        return $this->connection->rollBack();
    }

    /**
     * Shorthand method for commit()
     *
     * @access  public
     * @return  bool
     */
    public function commit()
    {
        return $this->connection->commit();
    }

    /**
     * Shorthand method for isTransaction()
     *
     * @access  public
     * @return  bool
     */
    public function inTransaction()
    {
        return $this->connection->inTransaction();
    }

    /**
     * Shorthand method for lastInsertId()
     *
     * @access  public
     * @return  bool
     */
    public function lastInsertId()
    {
        return $this->connection->lastInsertId();
    }

    /**
     * Shorthand method for errorInfo()
     *
     * @access  public
     * @return  array
     */
    public function errorInfo()
    {
        return $this->connection->errorInfo();
    }

    /**
     * Shorthand method for errorCode()
     *
     * @access  public
     * @return  int
     */
    public function errorCode()
    {
        return $this->connection->errorCode();
    }

    /**
     * Fetch all results from a query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function fetchAll($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        $this->method = 'fetchAll';
        return $this;
    }

    /**
     * Fetch the first result from a query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function fetchFirst($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        $this->method = 'fetch';
        return $this;
    }

    /**
     * Perform a custom query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function query($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        return $this;
    }

    /**
     * Determine if we are performing an INSERT query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @return  object
     */
    public function create($object, $table)
    {
        $this->setColumns($object);

        if(is_object($object))
        {
            $this->insertObject($object, $table);
        }
        elseif(is_array($object))
        {
            $this->insertObjects($object, $table);
        }

        return $this;
    }

    /**
     * Determine if we are performing an UPDATE query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function update($object, $table, $id = 'id')
    {
        $this->setColumns($object);
    
        if(is_object($object))
        {
            $this->updateObject($object, $table, $id);
        }
        elseif(is_array($object))
        {
            $this->updateObjects($object, $table, $id);
        }

        return $this;
    }

    /**
     * Determine if we are performing an DELETE query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function remove($object, $table, $id = 'id')
    {
        $this->setColumns($object);

        if(is_object($object))
        {
            $this->deleteObject($object, $table, $id);
        }
        elseif(is_array($object))
        {
            $this->deleteObjects($object, $table, $id);
        }

        return $this;
    }

    /**
     * Perform an INSERT query on a single object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @return  object
     */
    public function insertObject($object, $table)
    {
        $columns = implode(', ', $this->columns);
        $placeholders = str_repeat('?, ', (count($this->columns) - 1)) . '?';

        $statement = sprintf('INSERT INTO %s (%s) VALUES (%s)',
            $table, $columns, $placeholders);

        $this->statement = $statement;
        $this->bindings = array_values(get_object_vars($object));
        $this->prepare($statement);
        
        return $this;
    }

    /**
     * Perform an INSERT query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @return  object
     */
    public function insertObjects(array $objects, $table)
    {
        $columns = implode(', ', $this->columns);   

        $bindings = $placeholders = array();

        foreach($objects as $object)
        {
            $placeholders[] = sprintf('(%s?)',
                str_repeat('?, ', (count($this->columns) - 1))
            );

            $bindings = array_merge($bindings,
                array_values(get_object_vars($object))
            );
        }

        $placeholders = implode(', ', $placeholders);

        $statement = sprintf('INSERT INTO %s (%s) VALUES %s',
            $table, $columns, $placeholders);

        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an UPDATE query on an object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function updateObject($object, $table, $id = 'id')
    {
        $columns = $this->columns;
        $key = array_search($id, $columns);
        unset($columns[$key]);

        $placeholders = implode(' = ?, ', $columns) . ' = ?';

        $statement = sprintf('UPDATE %s SET %s WHERE %s = ?',
            $table, $placeholders, $id);

        $values = get_object_vars($object);
        $tmp = $values[$id];
        unset($values[$id]);
        $values[$id] = $tmp;

        $this->statement = $statement;
        $this->bindings = array_values($values);
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an UPDATE query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @param   string  $id         Object identifier
     * @return  object
     */
    public function updateObjects(array $objects, $table, $id = 'id')
    {
        $columns = $this->columns;
        $key = array_search($id, $columns);
        unset($columns[$key]);

        $bindings = array();
        $cases = array();
        $ids = array();

        foreach($objects as $object)
        {
            array_push($ids, $object->id);
        }

        foreach($columns as $column)
        {
            $case = sprintf('%s = CASE %s ', $column, $id);

            foreach($objects as $object)
            {
                if(property_exists($object, $column))
                {
                    $case .= 'WHEN ? THEN ? ';
                    array_push($bindings, $object->{$id}, $object->{$column});
                }
            }

            $case .= 'END';
            array_push($cases, $case);
        }

        $cases = implode(', ', $cases);
        $ids = implode(', ', $ids);
        
        $statement = sprintf('UPDATE %s SET %s WHERE %s IN (%s)',
            $table, $cases, $id, $ids);

        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an DELETE query on an object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function deleteObject($object, $table, $id = 'id')
    {
        $statement = sprintf('DELETE FROM %s WHERE %s = ?', $table, $id);

        $this->statement = $statement;
        $this->bindings = array($object->{$id});
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an DELETE query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @param   string  $id         Object identifier
     * @return  object
     */
    public function deleteObjects(array $objects, $table, $id = 'id')
    {
        $placeholders = array();
        $bindings = array();

        foreach($objects as $object)
        {
            array_push($placeholders, '?');
            array_push($bindings, $object->{$id});
        }

        $placeholders = implode(', ', $placeholders);

        $statement = sprintf('DELETE FROM %s WHERE %s IN (%s)',
            $table, $id, $placeholders);<commit_msg>single line array assignment<commit_after><?php
/**
 * Cloudlib
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   Copyright (c) 2013 <NAME> <<EMAIL>>
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

namespace cloudlib\helpers;

use PDO;
use PDOException;
use RuntimeException;

/**
 * The Database class
 *
 * @copyright   Copyright (c) 2013 <NAME> <<EMAIL>>
 * @license     MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class Database
{
    /**
     * The Database connection
     *
     * @access  public
     * @var     object
     */
    public $connection = null;

    /**
     * Database connection settings
     *
     * @access  public
     * @var     array
     */
    public $settings = array();

    /**
     * Current Database statement (query)
     *
     * @access  public
     * @var     string
     */
    public $statement = '';

    /**
     * PDO Bindings for prepared statements
     *
     * @access  public
     * @var     array
     */
    public $bindings = array();

    /**
     * The current statement handler
     *
     * @access  public
     * @var     object
     */
    public $statementHandler = null;

    /**
     * Array of error information
     *
     * @access  public
     * @var     array
     */
    public $errorInfo = null;

    /**
     * Error code of database exception
     *
     * @access  public
     * @var     int
     */
    public $errorCode = null;

    /**
     * PDO Exception
     *
     * @access  public
     * @var     object
     */
    public $exception = null;

    /**
     * Method to be used when executing a database statement
     *
     * @access  public
     * @var     string
     */
    public $method = false;

    /**
     * Array of columns for the current query
     *
     * @access  public
     * @var     array
     */
    public $columns = array();

    /**
     * Initialize the database connection
     *
     * @access  public
     * @param   mixed   $settings   Array or String to be used to connect to the database
     * @param   bool    $wait       If we should wait with connecting to the database
     * @return  void
     */
    public function __construct($settings, $wait = false)
    {
        if(is_string($settings))
        {
            try
            {
                extract(parse_url($settings));

                $settings = array(
                    'dsn' => sprintf('%s:host=%s;port=%s;dbname=%s',
                        $scheme, $host, $port, trim($path, '/')),
                    'username' => $user,
                    'password' => <PASSWORD>
                );
            }
            catch(Exception $e)
            {
                throw new InvalidArgumentException(
                    sprintf('Invalid Database URL [%]', $settings)
                );
            }
        }

        $this->settings = $settings;

        if( ! $wait)
        {
            $this->connect();
        }
    }

    /**
     * Initialize a connection to a database
     *
     * @access  public
     * @param   mixed   $settings   Array or String to be used to connect to the database
     * @return  object              Returns the database connection
     */
    public function connect($settings = null)
    {
        $settings = $settings ? $settings : $this->settings;

        $driverOptions = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        );

        try
        {
            $this->connection = new PDO(
                $settings['dsn'],
                $settings['username'],
                $settings['password'],
                $driverOptions
            );
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            return $this->connection;
        }
        catch(PDOException $e)
        {
            throw new RuntimeException('Unable to establish database connection');
        }
    }

    /**
     * Shorthand method for beginTransaction()
     *
     * @access  public
     * @return  bool
     */
    public function beginTransaction()
    {
        return $this->connection->beginTransaction();
    }

    /**
     * Shorthand method for rollBack()
     *
     * @access  public
     * @return  bool
     */
    public function rollBack()
    {
        return $this->connection->rollBack();
    }

    /**
     * Shorthand method for commit()
     *
     * @access  public
     * @return  bool
     */
    public function commit()
    {
        return $this->connection->commit();
    }

    /**
     * Shorthand method for isTransaction()
     *
     * @access  public
     * @return  bool
     */
    public function inTransaction()
    {
        return $this->connection->inTransaction();
    }

    /**
     * Shorthand method for lastInsertId()
     *
     * @access  public
     * @return  bool
     */
    public function lastInsertId()
    {
        return $this->connection->lastInsertId();
    }

    /**
     * Shorthand method for errorInfo()
     *
     * @access  public
     * @return  array
     */
    public function errorInfo()
    {
        return $this->connection->errorInfo();
    }

    /**
     * Shorthand method for errorCode()
     *
     * @access  public
     * @return  int
     */
    public function errorCode()
    {
        return $this->connection->errorCode();
    }

    /**
     * Fetch all results from a query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function fetchAll($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        $this->method = 'fetchAll';
        return $this;
    }

    /**
     * Fetch the first result from a query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function fetchFirst($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        $this->method = 'fetch';
        return $this;
    }

    /**
     * Perform a custom query
     *
     * @access  public
     * @param   string  $statement  Database query
     * @param   array   $bindings   Bindings used for prepared statements
     * @return  object
     */
    public function query($statement, array $bindings = array())
    {
        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);
        return $this;
    }

    /**
     * Determine if we are performing an INSERT query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @return  object
     */
    public function create($object, $table)
    {
        $this->setColumns($object);

        if(is_object($object))
        {
            $this->insertObject($object, $table);
        }
        elseif(is_array($object))
        {
            $this->insertObjects($object, $table);
        }

        return $this;
    }

    /**
     * Determine if we are performing an UPDATE query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function update($object, $table, $id = 'id')
    {
        $this->setColumns($object);
    
        if(is_object($object))
        {
            $this->updateObject($object, $table, $id);
        }
        elseif(is_array($object))
        {
            $this->updateObjects($object, $table, $id);
        }

        return $this;
    }

    /**
     * Determine if we are performing an DELETE query on a single object or multiple
     *
     * @access  public
     * @param   mixed   $object The object(s)
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function remove($object, $table, $id = 'id')
    {
        $this->setColumns($object);

        if(is_object($object))
        {
            $this->deleteObject($object, $table, $id);
        }
        elseif(is_array($object))
        {
            $this->deleteObjects($object, $table, $id);
        }

        return $this;
    }

    /**
     * Perform an INSERT query on a single object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @return  object
     */
    public function insertObject($object, $table)
    {
        $columns = implode(', ', $this->columns);
        $placeholders = str_repeat('?, ', (count($this->columns) - 1)) . '?';

        $statement = sprintf('INSERT INTO %s (%s) VALUES (%s)',
            $table, $columns, $placeholders);

        $this->statement = $statement;
        $this->bindings = array_values(get_object_vars($object));
        $this->prepare($statement);
        
        return $this;
    }

    /**
     * Perform an INSERT query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @return  object
     */
    public function insertObjects(array $objects, $table)
    {
        $columns = implode(', ', $this->columns);   

        $bindings = $placeholders = array();

        foreach($objects as $object)
        {
            $placeholders[] = sprintf('(%s?)',
                str_repeat('?, ', (count($this->columns) - 1))
            );

            $bindings = array_merge($bindings,
                array_values(get_object_vars($object))
            );
        }

        $placeholders = implode(', ', $placeholders);

        $statement = sprintf('INSERT INTO %s (%s) VALUES %s',
            $table, $columns, $placeholders);

        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an UPDATE query on an object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function updateObject($object, $table, $id = 'id')
    {
        $columns = $this->columns;
        $key = array_search($id, $columns);
        unset($columns[$key]);

        $placeholders = implode(' = ?, ', $columns) . ' = ?';

        $statement = sprintf('UPDATE %s SET %s WHERE %s = ?',
            $table, $placeholders, $id);

        $values = get_object_vars($object);
        $tmp = $values[$id];
        unset($values[$id]);
        $values[$id] = $tmp;

        $this->statement = $statement;
        $this->bindings = array_values($values);
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an UPDATE query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @param   string  $id         Object identifier
     * @return  object
     */
    public function updateObjects(array $objects, $table, $id = 'id')
    {
        $columns = $this->columns;
        $key = array_search($id, $columns);
        unset($columns[$key]);

        $bindings = $cases = $ids = array();

        foreach($objects as $object)
        {
            array_push($ids, $object->id);
        }

        foreach($columns as $column)
        {
            $case = sprintf('%s = CASE %s ', $column, $id);

            foreach($objects as $object)
            {
                if(property_exists($object, $column))
                {
                    $case .= 'WHEN ? THEN ? ';
                    array_push($bindings, $object->{$id}, $object->{$column});
                }
            }

            $case .= 'END';
            array_push($cases, $case);
        }

        $cases = implode(', ', $cases);
        $ids = implode(', ', $ids);
        
        $statement = sprintf('UPDATE %s SET %s WHERE %s IN (%s)',
            $table, $cases, $id, $ids);

        $this->statement = $statement;
        $this->bindings = $bindings;
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an DELETE query on an object
     *
     * @access  public
     * @param   object  $object The object
     * @param   string  $table  The database table
     * @param   string  $id     Object identifier
     * @return  object
     */
    public function deleteObject($object, $table, $id = 'id')
    {
        $statement = sprintf('DELETE FROM %s WHERE %s = ?', $table, $id);

        $this->statement = $statement;
        $this->bindings = array($object->{$id});
        $this->prepare($statement);

        return $this;
    }

    /**
     * Perform an DELETE query on multiple objects
     *
     * @access  public
     * @param   object  $objects    The objects
     * @param   string  $table      The database table
     * @param   string  $id         Object identifier
     * @return  object
     */
    public function deleteObjects(array $objects, $table, $id = 'id')
    {
        $placeholders = $bindings = array();

        foreach($objects as $object)
        {
            array_push($placeholders, '?');
            array_push($bindings, $object->{$id});
        }

        $placeholders = implode(', ', $placeholders);

        $statement = sprintf('DELETE FROM %s WHERE %s IN (%s)',
            $table, $id, $placeholders);
{'fraction_non_alphanumeric': 0.12719629190471057, 'fraction_numerical': 0.0007904854299162804, 'mean_word_length': 1.695593220338983, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 165, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3686196', 'n_tokens_mistral': 8001, 'n_tokens_neox': 7785, 'n_words': 2035}