Adapter
6 years ago
Schema
6 years ago
Adapter.php
6 years ago
AdapterInterface.php
6 years ago
BatchInsert.php
6 years ago
Schema.php
6 years ago
SchemaInterface.php
6 years ago
Settings.php
6 years ago
TransactionLevel.php
6 years ago
Schema.php
212 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Db; |
| 10 | |
| 11 | use Piwik\Config; |
| 12 | use Piwik\Singleton; |
| 13 | |
| 14 | /** |
| 15 | * Schema abstraction |
| 16 | * |
| 17 | * Note: no relation to the ZF proposals for Zend_Db_Schema_Manager |
| 18 | * |
| 19 | * @method static \Piwik\Db\Schema getInstance() |
| 20 | */ |
| 21 | class Schema extends Singleton |
| 22 | { |
| 23 | const DEFAULT_SCHEMA = 'Mysql'; |
| 24 | |
| 25 | /** |
| 26 | * Type of database schema |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $schema = null; |
| 31 | |
| 32 | /** |
| 33 | * Get schema class name |
| 34 | * |
| 35 | * @param string $schemaName |
| 36 | * @return string |
| 37 | */ |
| 38 | private static function getSchemaClassName($schemaName) |
| 39 | { |
| 40 | // Upgrade from pre 2.0.4 |
| 41 | if (strtolower($schemaName) == 'myisam' |
| 42 | || empty($schemaName)) { |
| 43 | $schemaName = self::DEFAULT_SCHEMA; |
| 44 | } |
| 45 | |
| 46 | $class = str_replace(' ', '\\', ucwords(str_replace('_', ' ', strtolower($schemaName)))); |
| 47 | return '\Piwik\Db\Schema\\' . $class; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Load schema |
| 53 | */ |
| 54 | private function loadSchema() |
| 55 | { |
| 56 | $config = Config::getInstance(); |
| 57 | $dbInfos = $config->database; |
| 58 | $schemaName = trim($dbInfos['schema']); |
| 59 | |
| 60 | $className = self::getSchemaClassName($schemaName); |
| 61 | $this->schema = new $className(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns an instance that subclasses Schema |
| 66 | * |
| 67 | * @return \Piwik\Db\SchemaInterface |
| 68 | */ |
| 69 | private function getSchema() |
| 70 | { |
| 71 | if ($this->schema === null) { |
| 72 | $this->loadSchema(); |
| 73 | } |
| 74 | |
| 75 | return $this->schema; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the SQL to create a specific Piwik table |
| 80 | * |
| 81 | * @param string $tableName name of the table to create |
| 82 | * @return string SQL |
| 83 | */ |
| 84 | public function getTableCreateSql($tableName) |
| 85 | { |
| 86 | return $this->getSchema()->getTableCreateSql($tableName); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the SQL to create Piwik tables |
| 91 | * |
| 92 | * @return array array of strings containing SQL |
| 93 | */ |
| 94 | public function getTablesCreateSql() |
| 95 | { |
| 96 | return $this->getSchema()->getTablesCreateSql(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a new table in the database. |
| 101 | * |
| 102 | * @param string $nameWithoutPrefix The name of the table without any piwik prefix. |
| 103 | * @param string $createDefinition The table create definition |
| 104 | */ |
| 105 | public function createTable($nameWithoutPrefix, $createDefinition) |
| 106 | { |
| 107 | $this->getSchema()->createTable($nameWithoutPrefix, $createDefinition); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Create database |
| 112 | * |
| 113 | * @param null|string $dbName database name to create |
| 114 | */ |
| 115 | public function createDatabase($dbName = null) |
| 116 | { |
| 117 | $this->getSchema()->createDatabase($dbName); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Drop database |
| 122 | */ |
| 123 | public function dropDatabase($dbName = null) |
| 124 | { |
| 125 | $this->getSchema()->dropDatabase($dbName); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Create all tables |
| 130 | */ |
| 131 | public function createTables() |
| 132 | { |
| 133 | $this->getSchema()->createTables(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates an entry in the User table for the "anonymous" user. |
| 138 | */ |
| 139 | public function createAnonymousUser() |
| 140 | { |
| 141 | $this->getSchema()->createAnonymousUser(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Records the Matomo version a user used when installing this Matomo for the first time |
| 146 | */ |
| 147 | public function recordInstallVersion() |
| 148 | { |
| 149 | $this->getSchema()->recordInstallVersion(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns which Matomo version was used to install this Matomo for the first time. |
| 154 | */ |
| 155 | public function getInstallVersion() |
| 156 | { |
| 157 | return $this->getSchema()->getInstallVersion(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Truncate all tables |
| 162 | */ |
| 163 | public function truncateAllTables() |
| 164 | { |
| 165 | $this->getSchema()->truncateAllTables(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Names of all the prefixed tables in piwik |
| 170 | * Doesn't use the DB |
| 171 | * |
| 172 | * @return array Table names |
| 173 | */ |
| 174 | public function getTablesNames() |
| 175 | { |
| 176 | return $this->getSchema()->getTablesNames(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get list of tables installed |
| 181 | * |
| 182 | * @param bool $forceReload Invalidate cache |
| 183 | * @return array installed tables |
| 184 | */ |
| 185 | public function getTablesInstalled($forceReload = true) |
| 186 | { |
| 187 | return $this->getSchema()->getTablesInstalled($forceReload); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get list of installed columns in a table |
| 192 | * |
| 193 | * @param string $tableName The name of a table. |
| 194 | * |
| 195 | * @return array Installed columns indexed by the column name. |
| 196 | */ |
| 197 | public function getTableColumns($tableName) |
| 198 | { |
| 199 | return $this->getSchema()->getTableColumns($tableName); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns true if Piwik tables exist |
| 204 | * |
| 205 | * @return bool True if tables exist; false otherwise |
| 206 | */ |
| 207 | public function hasTables() |
| 208 | { |
| 209 | return $this->getSchema()->hasTables(); |
| 210 | } |
| 211 | } |
| 212 |