1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\DBAL\Driver\PDOSqlite; |
---|
21 | |
---|
22 | /** |
---|
23 | * The PDO Sqlite driver. |
---|
24 | * |
---|
25 | * @since 2.0 |
---|
26 | */ |
---|
27 | class Driver implements \Doctrine\DBAL\Driver |
---|
28 | { |
---|
29 | /** |
---|
30 | * @var array |
---|
31 | */ |
---|
32 | protected $_userDefinedFunctions = array( |
---|
33 | 'sqrt' => array('callback' => array('Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'), 'numArgs' => 1), |
---|
34 | '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 | * Tries to establish a database connection to SQLite. |
---|
40 | * |
---|
41 | * @param array $params |
---|
42 | * @param string $username |
---|
43 | * @param string $password |
---|
44 | * @param array $driverOptions |
---|
45 | * @return Connection |
---|
46 | */ |
---|
47 | public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) |
---|
48 | { |
---|
49 | if (isset($driverOptions['userDefinedFunctions'])) { |
---|
50 | $this->_userDefinedFunctions = array_merge( |
---|
51 | $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']); |
---|
52 | unset($driverOptions['userDefinedFunctions']); |
---|
53 | } |
---|
54 | |
---|
55 | $pdo = new \Doctrine\DBAL\Driver\PDOConnection( |
---|
56 | $this->_constructPdoDsn($params), |
---|
57 | $username, |
---|
58 | $password, |
---|
59 | $driverOptions |
---|
60 | ); |
---|
61 | |
---|
62 | foreach ($this->_userDefinedFunctions AS $fn => $data) { |
---|
63 | $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); |
---|
64 | } |
---|
65 | |
---|
66 | return $pdo; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Constructs the Sqlite PDO DSN. |
---|
71 | * |
---|
72 | * @return string The DSN. |
---|
73 | * @override |
---|
74 | */ |
---|
75 | protected function _constructPdoDsn(array $params) |
---|
76 | { |
---|
77 | $dsn = 'sqlite:'; |
---|
78 | if (isset($params['path'])) { |
---|
79 | $dsn .= $params['path']; |
---|
80 | } else if (isset($params['memory'])) { |
---|
81 | $dsn .= ':memory:'; |
---|
82 | } |
---|
83 | |
---|
84 | return $dsn; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Gets the database platform that is relevant for this driver. |
---|
89 | */ |
---|
90 | public function getDatabasePlatform() |
---|
91 | { |
---|
92 | return new \Doctrine\DBAL\Platforms\SqlitePlatform(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Gets the schema manager that is relevant for this driver. |
---|
97 | * |
---|
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 | public function getName() |
---|
107 | { |
---|
108 | return 'pdo_sqlite'; |
---|
109 | } |
---|
110 | |
---|
111 | public function getDatabase(\Doctrine\DBAL\Connection $conn) |
---|
112 | { |
---|
113 | $params = $conn->getParams(); |
---|
114 | return isset($params['path']) ? $params['path'] : null; |
---|
115 | } |
---|
116 | } |
---|