Driver.php 3.66 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */
19

20
namespace Doctrine\DBAL\Driver\PDOSqlite;
21

22 23 24 25 26
/**
 * The PDO Sqlite driver.
 *
 * @since 2.0
 */
27
class Driver implements \Doctrine\DBAL\Driver
28
{
29 30 31 32 33 34
    /**
     * @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),
35
        'locate'  => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'), 'numArgs' => -1),
36 37
    );

38 39 40 41
    /**
     * Tries to establish a database connection to SQLite.
     *
     * @param array $params
42 43
     * @param string $username
     * @param string $password
44
     * @param array $driverOptions
45
     * @return Connection
46
     */
47 48
    public function connect(array $params, $username = null, $password = null, array $driverOptions = array())
    {
49 50 51 52 53 54 55
        if (isset($driverOptions['userDefinedFunctions'])) {
            $this->_userDefinedFunctions = array_merge(
                $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
            unset($driverOptions['userDefinedFunctions']);
        }

        $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
56 57 58 59 60
            $this->_constructPdoDsn($params),
            $username,
            $password,
            $driverOptions
        );
61 62 63 64 65 66

        foreach ($this->_userDefinedFunctions AS $fn => $data) {
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
        }

        return $pdo;
67
    }
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    /**
     * Constructs the Sqlite PDO DSN.
     *
     * @return string  The DSN.
     * @override
     */
    protected function _constructPdoDsn(array $params)
    {
        $dsn = 'sqlite:';
        if (isset($params['path'])) {
            $dsn .= $params['path'];
        } else if (isset($params['memory'])) {
            $dsn .= ':memory:';
        }
        
        return $dsn;
    }
86

87 88 89
    /**
     * Gets the database platform that is relevant for this driver.
     */
90 91
    public function getDatabasePlatform()
    {
92
        return new \Doctrine\DBAL\Platforms\SqlitePlatform();
93
    }
94

95 96 97
    /**
     * Gets the schema manager that is relevant for this driver.
     *
98
     * @param Doctrine\DBAL\Connection $conn
99
     * @return Doctrine\DBAL\Schema\SqliteSchemaManager
100
     */
101
    public function getSchemaManager(\Doctrine\DBAL\Connection $conn)
102
    {
103
        return new \Doctrine\DBAL\Schema\SqliteSchemaManager($conn);
104
    }
105 106 107 108 109

    public function getName()
    {
        return 'pdo_sqlite';
    }
110 111 112 113 114 115

    public function getDatabase(\Doctrine\DBAL\Connection $conn)
    {
        $params = $conn->getParams();
        return isset($params['path']) ? $params['path'] : null;
    }
116
}