<commit_before><?php

namespace FTB\core\db;

class PostgreSQLQuery extends SQLQuery {

	protected $pdo;
	protected static $pg_connections = [];

	public function __construct($table, $db = '', array $allowed_ops = []) {
		parent::__construct($table, $db, $allowed_ops);
		if (FTB_USE_PDO_PGSQL) {
			$this->pdo = get_pdo($this->db);
		} else {
			if (!isset(static::$pg_connections[$this->db])) {
				global $db_url;
				$info = parse_url($db_url[$this->db]);
				$pdo_url = "host={$info['host']} dbname={$this->db} user={$info['user']} password={$info['pass']}";
				static::$pg_connections[$this->db] = pg_connect($pdo_url);
			}
		}
	}

	/**
	 * http://php.net/manual/en/pdo.errorinfo.php
	 * @return array
	 * [SQLSTATE error code, Driver-specific error code, Driver-specific error message]
	 */
	public function errorInfo() {
		if (FTB_USE_PDO_PGSQL) {
			return $this->pdo->errorInfo();
		} else {
			return [
				-1, //TODO
				-1, //TODO
				pg_last_error(static::$pg_connections[$this->db])
			];
		}
	}

	protected function build_insert() {
		$sql = parent::build_insert();
		if ($this->return_id) {
			$sql .= " RETURNING \"id\"";
		}
		return $sql;
	}

	public function query($query, array $args = []) {
		static::$last_insert_id = null;
		if (static::$debug === true) {
			$t = microtime(true);
			if (FTB_USE_PDO_PGSQL) {
				$pdo_result = $this->pdo->query($query);
			} else {
				$pdo_result = pg_query(static::$pg_connections[$this->db], $query);
			}
			$this->logQuery('postgresql', $query, (bool)$pdo_result, microtime(true) - $t);
		} else {
			if (FTB_USE_PDO_PGSQL) {
				$pdo_result = $this->pdo->query($query);<commit_msg>PostgresSQLQuery: added upsert method and have it throw exception since its not implemented yet<commit_after><?php

namespace FTB\core\db;

class PostgreSQLQuery extends SQLQuery {

	protected $pdo;
	protected static $pg_connections = [];

	public function __construct($table, $db = '', array $allowed_ops = []) {
		parent::__construct($table, $db, $allowed_ops);
		if (FTB_USE_PDO_PGSQL) {
			$this->pdo = get_pdo($this->db);
		} else {
			if (!isset(static::$pg_connections[$this->db])) {
				global $db_url;
				$info = parse_url($db_url[$this->db]);
				$pdo_url = "host={$info['host']} dbname={$this->db} user={$info['user']} password={$info['<PASSWORD>']}";
				static::$pg_connections[$this->db] = pg_connect($pdo_url);
			}
		}
	}

	/**
	 * http://php.net/manual/en/pdo.errorinfo.php
	 * @return array
	 * [SQLSTATE error code, Driver-specific error code, Driver-specific error message]
	 */
	public function errorInfo() {
		if (FTB_USE_PDO_PGSQL) {
			return $this->pdo->errorInfo();
		} else {
			return [
				-1, //TODO
				-1, //TODO
				pg_last_error(static::$pg_connections[$this->db])
			];
		}
	}

	protected function build_insert() {
		$sql = parent::build_insert();
		if ($this->return_id) {
			$sql .= " RETURNING \"id\"";
		}
		return $sql;
	}

	public function upsert(array $data = [], array $skip_on_update = [], $no_escape = false) {
		throw new Exception("UPSERT (INSERT INTO ON DUPLICATE KEY) method is NOT implemented for PostgreSQL");
	}

	public function query($query, array $args = []) {
		static::$last_insert_id = null;
		if (static::$debug === true) {
			$t = microtime(true);
			if (FTB_USE_PDO_PGSQL) {
				$pdo_result = $this->pdo->query($query);
			} else {
				$pdo_result = pg_query(static::$pg_connections[$this->db], $query);
			}
			$this->logQuery('postgresql', $query, (bool)$pdo_result, microtime(true) - $t);
		} else {
			if (FTB_USE_PDO_PGSQL) {
				$pdo_result = $this->pdo->query($query);
{'fraction_non_alphanumeric': 0.18633193863319386, 'fraction_numerical': 0.0011157601115760112, 'mean_word_length': 3.9944289693593316, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 36, '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': '764874', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1339, 'n_words': 248}