<commit_before><?php
# namespace fboes\SmallPhpHelpers;
# use \PDO;

/**
 * @class SuperPDO
 * Extends functionality of PDO
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   MIT License (MIT)
 */
class SuperPDO extends PDO
{
	public $lastCmd = '';
	public $lastData = array();

	/**
	 * Open Mysql-PDO
	 *
	 * @param   string  $host   like 'localhost'
	 * @param   string  $db like 'test'
	 * @param   string  $usr    MySQL-Username. Optional, defaults to NULL
	 * @param   string  $pwd    MySQL-Password. Optional, defaults to NULL
	 * @return  SuperPDO
	 */
	public static function openMysql ($host, $db, $usr = NULL, $pwd = <PASSWORD>)
	{
		$dsn = 'mysql:host='.$host.';dbname='.$db;
		return new self($dsn, $usr, $pwd);
	}

	/**
	 * Open Sqlite-PDO
	 *
	 * @param   string  $file   Absolute or relative filename
	 * @return  SuperPDO
	 */
	public static function openSqlite ($file)
	{
		if (!preg_match('#^/#',$file))
		{
			$file = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$file;
		}
		return new self('sqlite:'.$file);
	}

	/**
	 * Prepare DB for communication in UTF8
	 * @return [type] [description]
	 */
	public function useUtf8 () {
		if ($this->getAttribute(self::ATTR_DRIVER_NAME) === 'mysql') {
			return $this->exec('SET NAMES utf8');
		}
	}

	/**
	 * Quotes an array for inserting / updating operations
	 *
	 * @param   array   $data
	 * @param   string  $separator  Optional, defaults to ','
	 * @param   bool    $withKey    Optional, defaults to TRUE
	 * @return  string  SQL
	 */
	public function quoteArray (array $data, $separator = ',', $withKey = TRUE)
	{
		$set = array();
		foreach ($data as $key => $value)
		{
			$set[] = ($withKey)
				? addslashes($key).'='.$this->quote($value)
				: $this->quote($value)
			;
		}
		return (implode($separator,$set));
	}

	/**
	 * Convert keys of an array to SQL usable for prepared statements
	 * @param  array  $data  FIELDNAME => VALUE
	 * @param  string $table Optional
	 * @return string        TABLE.KEY = :KEY
	 */
	public function buildPreparedArray (array $data, $table = '') {
		$preparedArray = array();
		foreach ($data as $key => $value) {
			$preparedArray[] = (!empty($table) ? $table.'.' : '').$key.' = :'.$key;
		}
		return $preparedArray;
	}

	/**
	 * Insert array into table. Will do proper quoting.
	 *
	 * @param   string  $table  Name of the table
	 * @param   array   $data   associative array with FIELDNAME => FIELDVALUE
	 * @param   string  $options    Like 'DELAYED'. Optional, defaults to NULL
	 * @return  bool
	 */
	public function insert ($table, array $data, $options = NULL)
	{
		$this->lastCmd =
			'INSERT'
			.(!empty($options) ? ' '.addslashes($options) : '')
			.' INTO '.addslashes($table)
			.'('.implode(',',array_keys($data)).')'
			.' VALUES(:'.implode(',:',array_keys($data)).')'
		;
		$this->lastData = $data;
		$sth = $this->prepare($this->lastCmd);
		return $sth->execute($this->lastData);
	}

	/**
	 * Select from table. Will do proper quoting, but for order
	 *
	 * @param   string  $table  Name of the table
	 * @param   string  $where  SQL-statement for WHERE-clause; you may want to use $this->quoteArray
	 * @param   string  $order  Optional
	 * @param   int     $count  Optional
	 * @param   int     $offset Optional
	 * @return  PDOStatement
	 */
	public function select ($table, $where = '1', array $order = array(), $count = NULL, $offset = 0)
	{
		return $this->selectJoin($table, $where, NULL, $order, $count, $offset);
	}

	/**
	 * Select from table. Will do proper quoting, but for order
	 *
	 * @param   string  $table  Name of the table
	 * @param   string  $where  SQL-statement for WHERE-clause; you may want to use $this->quoteArray
	 * @param   string  $join   some unquoted SQL
	 * @param   string  $order  Optional
	 * @param   int     $count  Optional
	 * @param   int     $offset Optional
	 * @param   string  $values Optional, defaults to '*'
	 * @return  PDOStatement
	 */
	public function selectJoin ($table, $where = '1', $join = '', array $order = array(), $count = NULL, $offset = 0, $values = '*')
	{
		$this->lastCmd =
			'SELECT '.$values
			.' FROM '.addslashes($table)
		;
		if (!empty($join)) {
			$this->lastCmd .= ' '.$join;
		}
		if (!empty($where)) {
			$this->lastCmd .= ' WHERE '.$where;
		}
		if (!empty($order)) {
			$this->lastCmd .= ' ORDER BY '.$table.'.'.implode(', '.$table.'.', $order);
		}
		if (!empty($count)) {
			$this->lastCmd .= ' LIMIT '.(int)$offset.','.(int)$count;
		}
		$this->lastData = NULL;
		$sth = $this->prepare($this->lastCmd);
		$sth->execute();
		return $sth->fetchAll(self::FETCH_ASSOC);
	}

	/**<commit_msg>Better UTF-8 support for SuperPDO<commit_after><?php
# namespace fboes\SmallPhpHelpers;
# use \PDO;

/**
 * @class SuperPDO
 * Extends functionality of PDO
 *
 * $pdo = new SuperPDO('mysql:host=localhost;dbname=testdb;charset=utf8','usr','pwd');
 * $pdo->useUtf8();
 * $pdo->setAttribute(SuperPDO::ATTR_DEFAULT_FETCH_MODE, SuperPDO::FETCH_OBJ);
 *
 *
 * @author      <NAME> <<EMAIL>>
 * @copyright   MIT License (MIT)
 */
class SuperPDO extends PDO
{
	public $lastCmd = '';
	public $lastData = array();

	/**
	 * Open Mysql-PDO
	 *
	 * @param   string  $host   like 'localhost'
	 * @param   string  $db like 'test'
	 * @param   string  $usr    MySQL-Username. Optional, defaults to NULL
	 * @param   string  $pwd    MySQL-Password. Optional, defaults to NULL
	 * @return  SuperPDO
	 */
	public static function openMysql ($host, $db, $usr = NULL, $pwd = NULL)
	{
		$dsn = 'mysql:host='.$host.';dbname='.$db.';charset=utf8'; # PHP 5.3.6
		return new self($dsn, $usr, $pwd);
	}

	/**
	 * Open Sqlite-PDO
	 *
	 * @param   string  $file   Absolute or relative filename
	 * @return  SuperPDO
	 */
	public static function openSqlite ($file)
	{
		if (!preg_match('#^/#',$file))
		{
			$file = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$file;
		}
		return new self('sqlite:'.$file);
	}

	/**
	 * Prepare DB for communication in UTF8
	 * @return [type] [description]
	 */
	public function useUtf8 () {
		if ($this->getAttribute(self::ATTR_DRIVER_NAME) === 'mysql') {
			return $this->exec('SET NAMES utf8');
		}
	}

	/**
	 * Quotes an array for inserting / updating operations
	 *
	 * @param   array   $data
	 * @param   string  $separator  Optional, defaults to ','
	 * @param   bool    $withKey    Optional, defaults to TRUE
	 * @return  string  SQL
	 */
	public function quoteArray (array $data, $separator = ',', $withKey = TRUE)
	{
		$set = array();
		foreach ($data as $key => $value)
		{
			$set[] = ($withKey)
				? addslashes($key).'='.$this->quote($value)
				: $this->quote($value)
			;
		}
		return (implode($separator,$set));
	}

	/**
	 * Convert keys of an array to SQL usable for prepared statements
	 * @param  array  $data  FIELDNAME => VALUE
	 * @param  string $table Optional
	 * @return string        TABLE.KEY = :KEY
	 */
	public function buildPreparedArray (array $data, $table = '') {
		$preparedArray = array();
		foreach ($data as $key => $value) {
			$preparedArray[] = (!empty($table) ? $table.'.' : '').$key.' = :'.$key;
		}
		return $preparedArray;
	}

	/**
	 * Insert array into table. Will do proper quoting.
	 *
	 * @param   string  $table  Name of the table
	 * @param   array   $data   associative array with FIELDNAME => FIELDVALUE
	 * @param   string  $options    Like 'DELAYED'. Optional, defaults to NULL
	 * @return  bool
	 */
	public function insert ($table, array $data, $options = NULL)
	{
		$this->lastCmd =
			'INSERT'
			.(!empty($options) ? ' '.addslashes($options) : '')
			.' INTO '.addslashes($table)
			.'('.implode(',',array_keys($data)).')'
			.' VALUES(:'.implode(',:',array_keys($data)).')'
		;
		$this->lastData = $data;
		$sth = $this->prepare($this->lastCmd);
		return $sth->execute($this->lastData);
	}

	/**
	 * Select from table. Will do proper quoting, but for order
	 *
	 * @param   string  $table  Name of the table
	 * @param   string  $where  SQL-statement for WHERE-clause; you may want to use $this->quoteArray
	 * @param   string  $order  Optional
	 * @param   int     $count  Optional
	 * @param   int     $offset Optional
	 * @return  PDOStatement
	 */
	public function select ($table, $where = '1', array $order = array(), $count = NULL, $offset = 0)
	{
		return $this->selectJoin($table, $where, NULL, $order, $count, $offset);
	}

	/**
	 * Select from table. Will do proper quoting, but for order
	 *
	 * @param   string  $table  Name of the table
	 * @param   string  $where  SQL-statement for WHERE-clause; you may want to use $this->quoteArray
	 * @param   string  $join   some unquoted SQL
	 * @param   string  $order  Optional
	 * @param   int     $count  Optional
	 * @param   int     $offset Optional
	 * @param   string  $values Optional, defaults to '*'
	 * @return  PDOStatement
	 */
	public function selectJoin ($table, $where = '1', $join = '', array $order = array(), $count = NULL, $offset = 0, $values = '*')
	{
		$this->lastCmd =
			'SELECT '.$values
			.' FROM '.addslashes($table)
		;
		if (!empty($join)) {
			$this->lastCmd .= ' '.$join;
		}
		if (!empty($where)) {
			$this->lastCmd .= ' WHERE '.$where;
		}
		if (!empty($order)) {
			$this->lastCmd .= ' ORDER BY '.$table.'.'.implode(', '.$table.'.', $order);
		}
		if (!empty($count)) {
			$this->lastCmd .= ' LIMIT '.(int)$offset.','.(int)$count;
		}
		$this->lastData = NULL;
		$sth = $this->prepare($this->lastCmd);
		$sth->execute();
		return $sth->fetchAll();
	}

	/**
{'fraction_non_alphanumeric': 0.17469943611022448, 'fraction_numerical': 0.0022342802425789976, 'mean_word_length': 3.0780911062906724, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 66, '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': '2298414', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3186, 'n_words': 872}