koriym/dev-pdo-statement

用于开发的PDOStatement

0.1.0 2016-02-24 11:00 UTC

This package is auto-updated.

Last update: 2024-08-31 00:25:39 UTC


README

Continuous Integration

[日语]

koriym/dev-pdo-statement记录以下信息以帮助您的SQL检查。

  • 查询执行时间。
  • 最终SQL查询,参数值已从预定义语句中插入。
  • EXPLAIN查询的结果。
  • SHOW WARNINGS查询的结果。

安装

DevPdoStatement类附加到目标$pdo

use Koriym\DevPdoStatement\DevPdoStatement;
use Koriym\DevPdoStatement\Logger;

$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, [DevPdoStatement::class, [$pdo, new Logger]]);

然后$pdo开始记录以下内容,在每个查询时。

time:0.00035190582275391 query: INSERT INTO user(id, name) VALUES (99, 'koriym99')
time:0.00020503997802734 query: SELECT id, name FROM user where id > 80
warnings:[
    {
        "Level": "Note",
        "Code": "1003",
        "Message": "\/* select#1 *\/ select `tmp`.`user`.`id` AS `id`,`tmp`.`user`.`name` AS `name` from `tmp`.`user` where (`tmp`.`user`.`id` > 80)"
    }
]
explain :[
    {
        "id": "1",
        "select_type": "SIMPLE",
        "table": "user",
        "partitions": null,
        "type": "ALL",
        "possible_keys": null,
        "key": null,
        "key_len": null,
        "ref": null,
        "rows": "100",
        "filtered": "33.33",
        "Extra": "Using where"
    }
]

自定义日志

您可以实现自定义日志记录条件或选择您喜欢的日志记录器。

use Koriym\DevPdoStatement\LoggerInterface;

class MyPsr3Logger implements LoggerInterface
{
    /**
     * {@inheritdoc}
     */
    public function logQuery($query, $time, array $explain, array $warnings)
    {
        // log or throw exception in your custom condition.
    }
}

演示

php doc/demo/run.php