1<?php
2
3namespace Doctrine\DBAL\Event\Listeners;
4
5use Doctrine\Common\EventSubscriber;
6use Doctrine\DBAL\Event\ConnectionEventArgs;
7use Doctrine\DBAL\Events;
8use Doctrine\DBAL\Exception;
9
10/**
11 * Session init listener for executing a single SQL statement right after a connection is opened.
12 */
13class SQLSessionInit implements EventSubscriber
14{
15    /** @var string */
16    protected $sql;
17
18    /**
19     * @param string $sql
20     */
21    public function __construct($sql)
22    {
23        $this->sql = $sql;
24    }
25
26    /**
27     * @return void
28     *
29     * @throws Exception
30     */
31    public function postConnect(ConnectionEventArgs $args)
32    {
33        $args->getConnection()->executeStatement($this->sql);
34    }
35
36    /**
37     * {@inheritdoc}
38     */
39    public function getSubscribedEvents()
40    {
41        return [Events::postConnect];
42    }
43}
44