Rev 68 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
51 | PointedEar | 3 | namespace PointedEars\PHPX\Db; |
4 | |||
27 | PointedEar | 5 | class MySQLDB extends Database |
6 | { |
||
7 | /** |
||
8 | * Database host |
||
9 | * @var string |
||
10 | */ |
||
11 | protected $_host; |
||
41 | PointedEar | 12 | |
27 | PointedEar | 13 | /** |
68 | PointedEar | 14 | * Database port on the host |
15 | * @var int |
||
16 | */ |
||
17 | protected $_port; |
||
18 | |||
19 | /** |
||
20 | * MySQL Unix socket (shouldn't be used with host or port). |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $_unix_socket; |
||
24 | |||
25 | /** |
||
27 | PointedEar | 26 | * Database name |
27 | * @var string |
||
28 | */ |
||
29 | protected $_dbname; |
||
41 | PointedEar | 30 | |
27 | PointedEar | 31 | /** |
32 | * Username to access the database |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $_username; |
||
41 | PointedEar | 36 | |
27 | PointedEar | 37 | /** |
38 | * Password to access the database |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $_password; |
||
41 | PointedEar | 42 | |
43 | /** |
||
27 | PointedEar | 44 | * Optional charset parameter value |
41 | PointedEar | 45 | * @var string |
27 | PointedEar | 46 | */ |
47 | protected $_charset = null; |
||
41 | PointedEar | 48 | |
32 | PointedEar | 49 | /** |
50 | * (non-PHPdoc) |
||
51 | * @see Database::_leftQuote |
||
52 | */ |
||
53 | protected $_leftQuote = '`'; |
||
41 | PointedEar | 54 | |
32 | PointedEar | 55 | /** |
56 | * (non-PHPdoc) |
||
57 | * @see Database::_rightQuote |
||
58 | */ |
||
59 | protected $_rightQuote = '`'; |
||
41 | PointedEar | 60 | |
27 | PointedEar | 61 | public function __construct() |
62 | { |
||
68 | PointedEar | 63 | $dbconfig = $this->readConfig(); |
64 | if ($dbconfig === false) |
||
65 | { |
||
66 | return; |
||
67 | } |
||
51 | PointedEar | 68 | |
68 | PointedEar | 69 | if (isset($dbconfig['unix_socket'])) |
70 | { |
||
71 | $this->_unix_socket = $dbconfig['unix_socket']; |
||
72 | } |
||
73 | |||
27 | PointedEar | 74 | $this->_dsn = "mysql:host={$this->_host}" |
68 | PointedEar | 75 | . (!is_null($this->_port) ? ";port={$this->_port}" : '') |
76 | . (!is_null($this->_unix_socket) ? ";unix_socket={$this->_unix_socket}" : '') |
||
27 | PointedEar | 77 | . (!is_null($this->_dbname) ? ";dbname={$this->_dbname}" : '') |
78 | . (!is_null($this->_charset) ? ";charset={$this->_charset}" : ''); |
||
41 | PointedEar | 79 | |
27 | PointedEar | 80 | if (!is_null($this->_charset)) |
81 | { |
||
51 | PointedEar | 82 | $this->_options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->_charset; |
27 | PointedEar | 83 | } |
41 | PointedEar | 84 | |
27 | PointedEar | 85 | parent::__construct(); |
86 | } |
||
44 | PointedEar | 87 | |
88 | /** |
||
58 | PointedEar | 89 | * (non-PHPdoc) |
90 | * @see \PointedEars\PHPX\Db\Database::_columnDef() |
||
91 | */ |
||
92 | protected function _columnDef (array $data, $columnName) |
||
93 | { |
||
94 | $standard_Def = parent::_columnDef($data, $columnName); |
||
95 | $mySQL_def = $standardDef |
||
96 | . (isset($data['format']) && $data['format'] ? " COLUMN_FORMAT {$data['format']}" : '') |
||
97 | . (isset($data['storage']) && $data['storage'] ? " STORAGE {$data['storage']}" : ''); |
||
98 | |||
99 | return $mySQL_def; |
||
100 | } |
||
101 | |||
102 | /** |
||
44 | PointedEar | 103 | * Creates this MySQL database |
104 | * |
||
46 | PointedEar | 105 | * @param string $dsn |
106 | * Ignored. Required for strict compatibility only. |
||
44 | PointedEar | 107 | * @param string $username = null |
108 | * @param string $password = null |
||
46 | PointedEar | 109 | * @param array? $options = null |
110 | * Ignored. Required for strict compatibility only. |
||
111 | * @param string $dbspec = null |
||
112 | * Currently ignored. |
||
44 | PointedEar | 113 | * @param boolean $force = false |
114 | * @see Database::create() |
||
115 | */ |
||
46 | PointedEar | 116 | public function create ($dsn, $username = null, $password = null, |
117 | array $options = null, $dbspec = null, $force = false) |
||
44 | PointedEar | 118 | { |
119 | return parent::create( |
||
120 | "mysql:host={$this->_host}" |
||
121 | . (!is_null($this->_charset) ? ";charset={$this->_charset}" : ''), |
||
122 | $username, $password, null, $force); |
||
123 | } |
||
73 | PointedEar | 124 | |
125 | /** |
||
126 | * Returns the date of last modification of this database or |
||
127 | * one of its tables. |
||
128 | * |
||
129 | * @param string $table (optional) |
||
130 | * Table name. If not provided, all tables of this database |
||
131 | * are considered. |
||
132 | * @return int|null |
||
133 | * Timestamp of last modification, or <code>null</code> if |
||
134 | * unavailable. |
||
135 | */ |
||
136 | public function getLastModified ($table = null) |
||
137 | { |
||
138 | $filter = array('TABLE_SCHEMA' => $this->_dbname); |
||
139 | |||
140 | if ($table !== null) |
||
141 | { |
||
142 | $filter['TABLE_NAME'] = $table; |
||
143 | } |
||
144 | |||
145 | $times = $this->select( |
||
146 | '`information_schema`.`tables`', |
||
147 | array('CREATE_TIME', 'UPDATE_TIME'), |
||
148 | $filter, |
||
149 | array('UPDATE_TIME DESC', 'CREATE_TIME DESC'), |
||
150 | 1 |
||
151 | ); |
||
152 | |||
153 | $modi = ($times ? $times[0] : null); |
||
154 | |||
155 | if ($modi) |
||
156 | { |
||
157 | $modi = $modi['UPDATE_TIME'] ?: $modi['CREATE_TIME']; |
||
158 | |||
159 | if ($modi) |
||
160 | { |
||
161 | $modi = strtotime($modi . ' GMT'); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $modi; |
||
166 | } |
||
27 | PointedEar | 167 | } |