<commit_before><?php

namespace zeus\database\pdo;

use zeus\base\logger\Logger;
use zeus\database\DmlType;
use zeus\database\specification\AbstractSpecification;

/**
 *
 * @author nathena
 *
 */
abstract class AbstractPdoDialect
{
    protected $pdo;
    protected $benchmark = [];
    protected $sql = [];

    /**
     * @param $cfg
     * mysql:host=localhost;port=3306;dbname=testdb
     * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb
     * array(
     * "dsn"=>"",
     * "user"=>"",
     * "pass"=>"",
     * )
     */
    public function __construct($cfg)
    {
        $dsn = $cfg["dsn"];
        $user = isset($cfg["user"]) ? trim($cfg["user"]) : "";
        $pass = isset($cfg["pass"]) ? trim($cfg["pass"]) : "";
        $charset = isset($cfg["charset"]) ? trim($cfg["charset"]) : "utf8";

        /**
         * 'database.pdo.driver_options' => [
         * \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
         * \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
         * ],
         */
        $driver_options = [
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        ];
        if (strpos($dsn, 'mysql') !== FALSE) {
            $driver_options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES {$charset}";
        }

        try {
            $this->pdo = new \PDO($dsn, $user, $pass, $driver_options);
            //取保连接关闭
            register_shutdown_function(array($this, 'close'));
        } catch (\PDOException $e) {
            Logger::error(">> PDO connect Error: " . $e->getCode() . "," . implode(",", array_values($e->errorInfo)));
            throw $e;
        }
    }

    public function close()
    {
        $this->pdo = null;
    }

    public function beginTransaction($nested = false)
    {

    }

    public function commit($nested = false)
    {

    }

    public function rollBack($nested = false)
    {

    }

    /**
     * @return \PDO
     */
    public function getConnection()
    {
        return $this->pdo;
    }

    public function debug()
    {
        return [$this->benchmark, $this->sql];
    }

    public function execute(AbstractSpecification $specification)
    {
        $prepare = $specification->getSql();
        $params = $specification->getParams();
        $dml = $specification->getDml();

        if (DmlType::DML_BATCH == $dml) {
            return $this->_execute_batch($prepare, $params);
        }

        if (DmlType::DML_PAGINATION == $dml && is_array($prepare)) {
            //分页
            $result = $this->_execute($prepare[0], $params, DmlType::DML_SELECT_ONE);
            $total = $result[0];
            $list = $this->_execute($prepare[1], $params, DmlType::DML_SELECT_LIST);

            return [$total, $list];
        }

        return $this->_execute($prepare, $params, $dml);
    }

    protected function _execute($prepare, $params, $dml)
    {
        $_sql = $this->log($prepare, $params);
        list($sm, $ss) = explode(' ', microtime());

        try {
            $sth = $this->pdo->prepare($prepare);
            $sth->execute($params);
            switch ($dml) {
                case DmlType::DML_SELECT_ONE:
                    $result = $sth->fetch();
                    break;
                case DmlType::DML_SELECT_LIST:
                    $result = $sth->fetchAll();
                    break;
                case DmlType::DML_INSERT:
                    $result = $this->pdo->lastInsertId();
                    break;
                case DmlType::DML_UPDATE:
                    $result = $sth->rowCount();
                    break;
                case DmlType::DML_DELETE:
                    $result = $sth->rowCount();
                    break;
                default:
                    $result = $sth->rowCount();
                    break;
            }

            list($em, $es) = explode(' ', microtime());
            $benchmark = ($em + $es) - ($sm + $ss);
            $this->benchmark += $benchmark;
            $this->sql[$benchmark] = $_sql;<commit_msg>fix: bug for pdo log<commit_after><?php

namespace zeus\database\pdo;

use zeus\base\logger\Logger;
use zeus\database\DmlType;
use zeus\database\specification\AbstractSpecification;

/**
 *
 * @author nathena
 *
 */
abstract class AbstractPdoDialect
{
    protected $pdo;
    protected $benchmark = [];
    protected $sql = [];

    /**
     * @param $cfg
     * mysql:host=localhost;port=3306;dbname=testdb
     * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb
     * array(
     * "dsn"=>"",
     * "user"=>"",
     * "pass"=>"",
     * )
     */
    public function __construct($cfg)
    {
        $dsn = $cfg["dsn"];
        $user = isset($cfg["user"]) ? trim($cfg["user"]) : "";
        $pass = isset($cfg["pass"]) ? trim($cfg["pass"]) : "";
        $charset = isset($cfg["charset"]) ? trim($cfg["charset"]) : "utf8";

        /**
         * 'database.pdo.driver_options' => [
         * \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
         * \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
         * ],
         */
        $driver_options = [
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        ];
        if (strpos($dsn, 'mysql') !== FALSE) {
            $driver_options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES {$charset}";
        }

        try {
            $this->pdo = new \PDO($dsn, $user, $pass, $driver_options);
            //取保连接关闭
            register_shutdown_function(array($this, 'close'));
        } catch (\PDOException $e) {
            Logger::error(">> PDO connect Error: " . $e->getCode() . "," . implode(",", array_values($e->errorInfo)));
            throw $e;
        }
    }

    public function close()
    {
        $this->pdo = null;
    }

    public function beginTransaction($nested = false)
    {

    }

    public function commit($nested = false)
    {

    }

    public function rollBack($nested = false)
    {

    }

    /**
     * @return \PDO
     */
    public function getConnection()
    {
        return $this->pdo;
    }

    public function debug()
    {
        return [$this->benchmark, $this->sql];
    }

    public function execute(AbstractSpecification $specification)
    {
        $prepare = $specification->getSql();
        $params = $specification->getParams();
        $dml = $specification->getDml();

        if (DmlType::DML_BATCH == $dml) {
            return $this->_execute_batch($prepare, $params);
        }

        if (DmlType::DML_PAGINATION == $dml && is_array($prepare)) {
            //分页
            $result = $this->_execute($prepare[0], $params, DmlType::DML_SELECT_ONE);
            $total = $result[0];
            $list = $this->_execute($prepare[1], $params, DmlType::DML_SELECT_LIST);

            return [$total, $list];
        }

        return $this->_execute($prepare, $params, $dml);
    }

    protected function _execute($prepare, $params, $dml)
    {
        $_sql = $this->log($prepare, $params);
        list($sm, $ss) = explode(' ', microtime());

        try {
            $sth = $this->pdo->prepare($prepare);
            $sth->execute($params);
            switch ($dml) {
                case DmlType::DML_SELECT_ONE:
                    $result = $sth->fetch();
                    break;
                case DmlType::DML_SELECT_LIST:
                    $result = $sth->fetchAll();
                    break;
                case DmlType::DML_INSERT:
                    $result = $this->pdo->lastInsertId();
                    break;
                case DmlType::DML_UPDATE:
                    $result = $sth->rowCount();
                    break;
                case DmlType::DML_DELETE:
                    $result = $sth->rowCount();
                    break;
                default:
                    $result = $sth->rowCount();
                    break;
            }

            list($em, $es) = explode(' ', microtime());
            $benchmark = ($em + $es) - ($sm + $ss);
            $this->benchmark[] = $benchmark;
            $this->sql[] = $_sql;
{'fraction_non_alphanumeric': 0.16466916354556804, 'fraction_numerical': 0.0022471910112359553, 'mean_word_length': 1.7690978223297615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '2126657', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2473, 'n_words': 425}