<commit_before><?php

namespace app\inc;

use Exception;
use PDO;
use PDOStatement;
use app\conf\Connection;

/**
 * Class Model
 * @package app\inc
 */
class Model
{
    public $postgishost;
    public $postgisuser;
    public $postgisdb;
    public $postgispw;
    public $connectString;
    public $PDOerror;
    public $db;
    public $postgisschema;
    public $connectionFailed;

    function __construct()
    {
        $this->postgishost = Connection::$param['postgishost'];
        $this->postgisport = Connection::$param['postgisport'];
        $this->postgisuser = Connection::$param['postgisuser'];
        $this->postgisdb = Connection::$param['postgisdb'];
        $this->postgispw = Connection::$param['postgispw'];
        $this->postgisschema = isset(Connection::$param['postgisschema']) ? Connection::$param['postgisschema'] : null;
    }

    /**
     * @param PDOStatement $result
     * @param string $result_type
     * @return array
     * @throws Exception
     */
    public function fetchRow(PDOStatement $result, $result_type = "assoc")
    {
        $row = [];
        switch ($result_type) {
            case "assoc" :
                try {
                    $row = $result->fetch(PDO::FETCH_ASSOC);
                } catch (\PDOException $e) {
                    throw new \PDOException($e->getMessage());
                }
                break;
            case "both" :
                break;
        }
        return $row;
    }

    /**
     * @param PDOStatement $result
     * @param string $result_type
     * @return array
     * @throws Exception
     */
    public function fetchAll(PDOStatement $result, $result_type = "both")
    {
        $rows = [];
        if ($this->PDOerror) {
            throw new Exception($this->PDOerror[0]);
        }
        switch ($result_type) {
            case "assoc" :
                $rows = $result->fetchAll(PDO::FETCH_ASSOC);
                break;
            case "both" :
                $rows = $result->fetchAll();
                break;
        }
        return $rows;
    }

    /**
     * TODO is it used?
     * @param $result
     * @return int
     */
    public function numRows($result)
    {
        $num = sizeof($result);
        return $num;
    }

    /**
     * @param $result
     */
    public function free($result)
    {
        $result = NULL;
    }

    /**
     * @param string $table
     * @return array|null
     */
    public function getPrimeryKey($table)
    {
        //return array("attname" => "objekt_id");

        unset($this->PDOerror);
        $query = "SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '" . $this->doubleQuoteQualifiedName($table) . "'::REGCLASS AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = ANY(pg_index.indkey) AND indisprimary";
        $result = $this->execQuery($query);

        if (isset($this->PDOerror)) {
            return NULL;
        }
        //if ($featureId = $this->getGeometryColumns($table, "featureid")) {
        //    return array("attname" => $featureId);
        //}

        if (!is_array($row = $this->fetchRow($result))) { // If $table is view we bet on there is a gid field
            return array("attname" => "gid");
        } else {
            return ($row);
        }
    }

    /**
     * @param string $table
     * @return bool|null
     */
    public function hasPrimeryKey($table)
    {
        unset($this->PDOerror);
        $query = "SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '" . $this->doubleQuoteQualifiedName($table) . "'::REGCLASS AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = ANY(pg_index.indkey) AND indisprimary";
        $result = $this->execQuery($query);

        if (isset($this->PDOerror)) {
            return NULL;
        }
        if (!is_array($row = $this->fetchRow($result))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *
     */
    public function begin()
    {
        $this->db->beginTransaction();
    }

    /**
     *
     */
    public function commit()
    {
        $this->db->commit();
        $this->db = NULL;
    }

    /**
     *
     */
    public function rollback()
    {
        $this->db->rollback();
        $this->db = NULL;
    }

    /**
     * @param $sql
     * @return mixed
     * @throws Exception
     */
    public function prepare($sql)
    {
        if (!$this->db) {
            try {
                $this->connect("PDO");
            } catch (\PDOException $e) {
                throw new \PDOException($e->getMessage());
            }
        }
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {
            $stmt = $this->db->prepare($sql);
        } catch (\PDOException $e) {
            $this->PDOerror[] = $e->getMessage();
            throw new Exception($e->getMessage());
        }
        return $stmt;
    }

    /**
     * @param string $query
     * @param string $conn
     * @param string $queryType
     * @return bool|integer|PDOStatement
     */
    public function execQuery($query, $conn = "PDO", $queryType = "select")
    {
        $result = null;
        switch ($conn) {
            case "PG" :<commit_msg>Catch and throw PDOExceptions<commit_after><?php

namespace app\inc;

use Exception;
use PDO;
use PDOStatement;
use app\conf\Connection;

/**
 * Class Model
 * @package app\inc
 */
class Model
{
    public $postgishost;
    public $postgisport;
    public $postgisuser;
    public $postgisdb;
    public $postgispw;
    public $connectString;
    public $PDOerror;
    public $db;
    public $postgisschema;
    public $connectionFailed;

    function __construct()
    {
        $this->postgishost = Connection::$param['postgishost'];
        $this->postgisport = Connection::$param['postgisport'];
        $this->postgisuser = Connection::$param['postgisuser'];
        $this->postgisdb = Connection::$param['postgisdb'];
        $this->postgispw = Connection::$param['postgispw'];
        $this->postgisschema = isset(Connection::$param['postgisschema']) ? Connection::$param['postgisschema'] : null;
    }

    /**
     * @param PDOStatement $result
     * @param string $result_type
     * @return array
     * @throws \PDOException
     */
    public function fetchRow(PDOStatement $result, $result_type = "assoc")
    {
        $row = [];
        switch ($result_type) {
            case "assoc" :
                try {
                    $row = $result->fetch(PDO::FETCH_ASSOC);
                } catch (\PDOException $e) {
                    throw new \PDOException($e->getMessage());
                }
                break;
            case "both" :
                break;
        }
        return $row;
    }

    /**
     * @param PDOStatement $result
     * @param string $result_type
     * @return array
     * @throws Exception
     */
    public function fetchAll(PDOStatement $result, $result_type = "both")
    {
        $rows = [];
        if ($this->PDOerror) {
            throw new Exception($this->PDOerror[0]);
        }
        switch ($result_type) {
            case "assoc" :
                $rows = $result->fetchAll(PDO::FETCH_ASSOC);
                break;
            case "both" :
                $rows = $result->fetchAll();
                break;
        }
        return $rows;
    }

    /**
     * TODO is it used?
     * @param $result
     * @return int
     */
    public function numRows($result)
    {
        $num = sizeof($result);
        return $num;
    }

    /**
     * @param $result
     */
    public function free($result)
    {
        $result = NULL;
    }

    /**
     * @param string $table
     * @return array|null
     */
    public function getPrimeryKey($table)
    {
        //return array("attname" => "objekt_id");

        unset($this->PDOerror);
        $query = "SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '" . $this->doubleQuoteQualifiedName($table) . "'::REGCLASS AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = ANY(pg_index.indkey) AND indisprimary";
        $result = $this->execQuery($query);

        if (isset($this->PDOerror)) {
            return NULL;
        }
        //if ($featureId = $this->getGeometryColumns($table, "featureid")) {
        //    return array("attname" => $featureId);
        //}

        if (!is_array($row = $this->fetchRow($result))) { // If $table is view we bet on there is a gid field
            return array("attname" => "gid");
        } else {
            return ($row);
        }
    }

    /**
     * @param string $table
     * @return bool|null
     */
    public function hasPrimeryKey($table)
    {
        unset($this->PDOerror);
        $query = "SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE pg_class.oid = '" . $this->doubleQuoteQualifiedName($table) . "'::REGCLASS AND indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = ANY(pg_index.indkey) AND indisprimary";
        $result = $this->execQuery($query);

        if (isset($this->PDOerror)) {
            return NULL;
        }
        if (!is_array($row = $this->fetchRow($result))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *
     */
    public function begin()
    {
        $this->db->beginTransaction();
    }

    /**
     *
     */
    public function commit()
    {
        $this->db->commit();
        $this->db = NULL;
    }

    /**
     *
     */
    public function rollback()
    {
        $this->db->rollback();
        $this->db = NULL;
    }

    /**
     * @param $sql
     * @return mixed
     * @throws \PDOException
     */
    public function prepare($sql)
    {
        if (!$this->db) {
            try {
                $this->connect("PDO");
            } catch (\PDOException $e) {
                throw new \PDOException($e->getMessage());
            }
        }
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {
            $stmt = $this->db->prepare($sql);
        } catch (\PDOException $e) {
            $this->PDOerror[] = $e->getMessage();
            throw new \PDOException($e->getMessage());
        }
        return $stmt;
    }

    /**
     * @param string $query
     * @param string $conn
     * @param string $queryType
     * @return bool|integer|PDOStatement
     */
    public function execQuery($query, $conn = "PDO", $queryType = "select")
    {
        $result = null;
        switch ($conn) {
            case "PG" :
{'fraction_non_alphanumeric': 0.14296114296114296, 'fraction_numerical': 0.000182000182000182, 'mean_word_length': 1.8898238232973967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, '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': '2048459', 'n_tokens_mistral': 3382, 'n_tokens_neox': 3343, 'n_words': 677}