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