]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Db/Sqlite.php
14ee520fd1861a40c1e5d3479512760be9cdf7c7
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Db / Sqlite.php
1 <?php
2
3 /**
4  * Phergie
5  *
6  * PHP version 5
7  *
8  * LICENSE
9  *
10  * This source file is subject to the new BSD license that is bundled
11  * with this package in the file LICENSE.
12  * It is also available through the world-wide-web at this URL:
13  * http://phergie.org/license
14  *
15  * @category  Phergie
16  * @package   Phergie
17  * @author    Phergie Development Team <team@phergie.org>
18  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
19  * @license   http://phergie.org/license New BSD License
20  * @link      http://pear.phergie.org/package/Phergie
21  */
22
23 /**
24  * Provides basic management for SQLite databases
25  *
26  * @category Phergie
27  * @package  Phergie
28  * @author   Phergie Development Team <team@phergie.org>
29  * @license  http://phergie.org/license New BSD License
30  * @link     http://pear.phergie.org/package/Phergie
31  */
32 class Phergie_Db_Sqlite extends Phergie_Db_Manager
33 {
34     /**
35      * Database connection
36      *
37      * @var PDO
38      */
39     protected $db;
40
41     /**
42      * Database file path
43      *
44      * @var string
45      */
46     protected $dbFile;
47
48     /**
49      * Allows setting of the database file path when creating the class.
50      *
51      * @param string $dbFile database file path (optional)
52      *
53      * @return void
54      */
55     public function __construct($dbFile = null)
56     {
57         if ($dbFile != null) {
58             $this->setDbFile($dbFile);
59         }
60     }
61
62     /**
63      * Sets the database file path.
64      *
65      * @param string $dbFile SQLite database file path
66      *
67      * @return null
68      */
69     public function setDbFile($dbFile)
70     {
71         if (is_string($dbFile) && !empty($dbFile)) {
72             $this->dbFile = $dbFile;
73         }
74     }
75
76     /**
77      * Returns a configured database connection.
78      *
79      * @return PDO
80      */
81     public function getDb()
82     {
83         if (!empty($this->db)) {
84             return $this->db;
85         }
86
87         $dir = dirname($this->dbFile);
88         if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
89             throw new Phergie_Db_Exception(
90                 'Unable to create directory',
91                 Phergie_Db_Exception::ERR_UNABLE_TO_CREATE_DIRECTORY
92             );
93         }
94
95         $this->db = new PDO('sqlite:' . $this->dbFile);
96
97         return $this->db;
98     }
99
100
101     /**
102      * Returns whether a given table exists in the database.
103      *
104      * @param string $table Name of the table to check for
105      *
106      * @return boolean TRUE if the table exists, FALSE otherwise
107      */
108     public function hasTable($table)
109     {
110         $db = $this->getDb();
111
112         return (bool) $db->query(
113             'SELECT COUNT(*) FROM sqlite_master WHERE name = '
114             . $db->quote($table)
115         )->fetchColumn();
116     }
117 }