Driver.php 3.07 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18
 * <http://www.doctrine-project.org>.
 */
19

20
namespace Doctrine\DBAL\Driver\PDOSqlite;
21

22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
Steve Müller's avatar
Steve Müller committed
24
use Doctrine\DBAL\Driver\PDOConnection;
25
use PDOException;
26

27 28 29 30 31
/**
 * The PDO Sqlite driver.
 *
 * @since 2.0
 */
32
class Driver extends AbstractSQLiteDriver
33
{
34 35 36 37 38 39
    /**
     * @var array
     */
    protected $_userDefinedFunctions = array(
        'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1),
        'mod'  => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'), 'numArgs' => 2),
40
        'locate'  => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'), 'numArgs' => -1),
41 42
    );

43
    /**
Benjamin Morel's avatar
Benjamin Morel committed
44
     * {@inheritdoc}
45
     */
46 47
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
48 49 50 51 52 53
        if (isset($driverOptions['userDefinedFunctions'])) {
            $this->_userDefinedFunctions = array_merge(
                $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
            unset($driverOptions['userDefinedFunctions']);
        }

54
        try {
Steve Müller's avatar
Steve Müller committed
55
            $pdo = new PDOConnection(
56 57 58 59 60 61 62 63
                $this->_constructPdoDsn($params),
                $username,
                $password,
                $driverOptions
            );
        } catch (PDOException $ex) {
            throw DBALException::driverException($this, $ex);
        }
64

65
        foreach ($this->_userDefinedFunctions as $fn => $data) {
66 67 68 69
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
        }

        return $pdo;
70
    }
71

72 73 74
    /**
     * Constructs the Sqlite PDO DSN.
     *
Benjamin Morel's avatar
Benjamin Morel committed
75 76 77
     * @param array $params
     *
     * @return string The DSN.
78 79 80 81 82 83
     */
    protected function _constructPdoDsn(array $params)
    {
        $dsn = 'sqlite:';
        if (isset($params['path'])) {
            $dsn .= $params['path'];
Steve Müller's avatar
Steve Müller committed
84
        } elseif (isset($params['memory'])) {
85 86
            $dsn .= ':memory:';
        }
87

88 89
        return $dsn;
    }
90

Benjamin Morel's avatar
Benjamin Morel committed
91 92 93
    /**
     * {@inheritdoc}
     */
94 95 96 97
    public function getName()
    {
        return 'pdo_sqlite';
    }
98
}