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