Adapter
1 year ago
Schema
1 year ago
Adapter.php
1 year ago
AdapterInterface.php
1 year ago
BatchInsert.php
1 year ago
Schema.php
1 year ago
SchemaInterface.php
1 year ago
Settings.php
1 year ago
TransactionLevel.php
1 year ago
TransactionalDatabaseDynamicTrait.php
1 year ago
TransactionalDatabaseInterface.php
1 year ago
TransactionalDatabaseStaticTrait.php
1 year ago
Schema.php
283 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Db; |
| 10 | |
| 11 | use Piwik\Config; |
| 12 | use Piwik\Singleton; |
| 13 | /** |
| 14 | * Schema abstraction |
| 15 | * |
| 16 | * @method static \Piwik\Db\Schema getInstance() |
| 17 | */ |
| 18 | class Schema extends Singleton |
| 19 | { |
| 20 | public const DEFAULT_SCHEMA = 'Mysql'; |
| 21 | /** |
| 22 | * Type of database schema |
| 23 | * |
| 24 | * @var SchemaInterface |
| 25 | */ |
| 26 | private $schema = null; |
| 27 | /** |
| 28 | * Get schema class name |
| 29 | * |
| 30 | * @param string $schemaName |
| 31 | * @return string |
| 32 | */ |
| 33 | private static function getSchemaClassName($schemaName) : string |
| 34 | { |
| 35 | // Upgrade from pre 2.0.4 |
| 36 | if (strtolower($schemaName) == 'myisam' || empty($schemaName)) { |
| 37 | $schemaName = self::DEFAULT_SCHEMA; |
| 38 | } |
| 39 | $class = str_replace(' ', '\\', ucwords(str_replace('_', ' ', strtolower($schemaName)))); |
| 40 | return '\\Piwik\\Db\\Schema\\' . $class; |
| 41 | } |
| 42 | /** |
| 43 | * Return the default port for the provided database schema |
| 44 | * |
| 45 | * @param string $schemaName |
| 46 | * @return int |
| 47 | */ |
| 48 | public static function getDefaultPortForSchema(string $schemaName) : int |
| 49 | { |
| 50 | $schemaClassName = self::getSchemaClassName($schemaName); |
| 51 | /** @var SchemaInterface $schemaClass */ |
| 52 | $schemaClass = new $schemaClassName(); |
| 53 | return $schemaClass->getDefaultPort(); |
| 54 | } |
| 55 | /** |
| 56 | * Load schema |
| 57 | */ |
| 58 | private function loadSchema() : void |
| 59 | { |
| 60 | $config = Config::getInstance(); |
| 61 | $dbInfos = $config->database; |
| 62 | $schemaName = trim($dbInfos['schema']); |
| 63 | $className = self::getSchemaClassName($schemaName); |
| 64 | $this->schema = new $className(); |
| 65 | } |
| 66 | /** |
| 67 | * Returns an instance that subclasses Schema |
| 68 | * |
| 69 | * @return SchemaInterface |
| 70 | */ |
| 71 | private function getSchema() : \Piwik\Db\SchemaInterface |
| 72 | { |
| 73 | if ($this->schema === null) { |
| 74 | $this->loadSchema(); |
| 75 | } |
| 76 | return $this->schema; |
| 77 | } |
| 78 | /** |
| 79 | * Returns the default collation for a charset. |
| 80 | * |
| 81 | * @param string $charset |
| 82 | * @return string |
| 83 | */ |
| 84 | public function getDefaultCollationForCharset(string $charset) : string |
| 85 | { |
| 86 | return $this->getSchema()->getDefaultCollationForCharset($charset); |
| 87 | } |
| 88 | /** |
| 89 | * Get the table options to use for a CREATE TABLE statement. |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function getTableCreateOptions() : string |
| 94 | { |
| 95 | return $this->getSchema()->getTableCreateOptions(); |
| 96 | } |
| 97 | /** |
| 98 | * Get the SQL to create a specific Piwik table |
| 99 | * |
| 100 | * @param string $tableName name of the table to create |
| 101 | * @return string SQL |
| 102 | */ |
| 103 | public function getTableCreateSql($tableName) |
| 104 | { |
| 105 | return $this->getSchema()->getTableCreateSql($tableName); |
| 106 | } |
| 107 | /** |
| 108 | * Get the SQL to create Piwik tables |
| 109 | * |
| 110 | * @return array array of strings containing SQL |
| 111 | */ |
| 112 | public function getTablesCreateSql() |
| 113 | { |
| 114 | return $this->getSchema()->getTablesCreateSql(); |
| 115 | } |
| 116 | /** |
| 117 | * Creates a new table in the database. |
| 118 | * |
| 119 | * @param string $nameWithoutPrefix The name of the table without any prefix. |
| 120 | * @param string $createDefinition The table create definition |
| 121 | */ |
| 122 | public function createTable($nameWithoutPrefix, $createDefinition) |
| 123 | { |
| 124 | $this->getSchema()->createTable($nameWithoutPrefix, $createDefinition); |
| 125 | } |
| 126 | /** |
| 127 | * Create database |
| 128 | * |
| 129 | * @param null|string $dbName database name to create |
| 130 | */ |
| 131 | public function createDatabase($dbName = null) |
| 132 | { |
| 133 | $this->getSchema()->createDatabase($dbName); |
| 134 | } |
| 135 | /** |
| 136 | * Drop database |
| 137 | */ |
| 138 | public function dropDatabase($dbName = null) |
| 139 | { |
| 140 | $this->getSchema()->dropDatabase($dbName); |
| 141 | } |
| 142 | /** |
| 143 | * Create all tables |
| 144 | */ |
| 145 | public function createTables() : void |
| 146 | { |
| 147 | $this->getSchema()->createTables(); |
| 148 | } |
| 149 | /** |
| 150 | * Creates an entry in the User table for the "anonymous" user. |
| 151 | */ |
| 152 | public function createAnonymousUser() : void |
| 153 | { |
| 154 | $this->getSchema()->createAnonymousUser(); |
| 155 | } |
| 156 | /** |
| 157 | * Records the Matomo version a user used when installing this Matomo for the first time |
| 158 | */ |
| 159 | public function recordInstallVersion() : void |
| 160 | { |
| 161 | $this->getSchema()->recordInstallVersion(); |
| 162 | } |
| 163 | /** |
| 164 | * Returns which Matomo version was used to install this Matomo for the first time. |
| 165 | */ |
| 166 | public function getInstallVersion() |
| 167 | { |
| 168 | return $this->getSchema()->getInstallVersion(); |
| 169 | } |
| 170 | /** |
| 171 | * Truncate all tables |
| 172 | */ |
| 173 | public function truncateAllTables() : void |
| 174 | { |
| 175 | $this->getSchema()->truncateAllTables(); |
| 176 | } |
| 177 | /** |
| 178 | * Names of all the prefixed tables in Matomo |
| 179 | * Doesn't use the DB |
| 180 | * |
| 181 | * @return array Table names |
| 182 | */ |
| 183 | public function getTablesNames() |
| 184 | { |
| 185 | return $this->getSchema()->getTablesNames(); |
| 186 | } |
| 187 | /** |
| 188 | * Get list of tables installed |
| 189 | * |
| 190 | * @param bool $forceReload Invalidate cache |
| 191 | * @return array installed tables |
| 192 | */ |
| 193 | public function getTablesInstalled($forceReload = \true) |
| 194 | { |
| 195 | return $this->getSchema()->getTablesInstalled($forceReload); |
| 196 | } |
| 197 | /** |
| 198 | * Get list of installed columns in a table |
| 199 | * |
| 200 | * @param string $tableName The name of a table. |
| 201 | * |
| 202 | * @return array Installed columns indexed by the column name. |
| 203 | */ |
| 204 | public function getTableColumns($tableName) |
| 205 | { |
| 206 | return $this->getSchema()->getTableColumns($tableName); |
| 207 | } |
| 208 | /** |
| 209 | * Returns true if Matomo tables exist |
| 210 | * |
| 211 | * @return bool True if tables exist; false otherwise |
| 212 | */ |
| 213 | public function hasTables() |
| 214 | { |
| 215 | return $this->getSchema()->hasTables(); |
| 216 | } |
| 217 | /** |
| 218 | * Adds a MAX_EXECUTION_TIME hint into a SELECT query if $limit is bigger than 0 |
| 219 | * |
| 220 | * @param string $sql query to add hint to |
| 221 | * @param float $limit time limit in seconds |
| 222 | * @return string |
| 223 | */ |
| 224 | public function addMaxExecutionTimeHintToQuery(string $sql, float $limit) : string |
| 225 | { |
| 226 | return $this->getSchema()->addMaxExecutionTimeHintToQuery($sql, $limit); |
| 227 | } |
| 228 | /** |
| 229 | * Returns if the schema support complex column updates |
| 230 | * |
| 231 | * @return bool |
| 232 | */ |
| 233 | public function supportsComplexColumnUpdates() : bool |
| 234 | { |
| 235 | return $this->getSchema()->supportsComplexColumnUpdates(); |
| 236 | } |
| 237 | /** |
| 238 | * Returns if the schema supports `OPTIMIZE TABLE` statements for innodb tables |
| 239 | * |
| 240 | * @return bool |
| 241 | */ |
| 242 | public function isOptimizeInnoDBSupported() : bool |
| 243 | { |
| 244 | return $this->getSchema()->isOptimizeInnoDBSupported(); |
| 245 | } |
| 246 | /** |
| 247 | * Runs an `OPTIMIZE TABLE` query on the supplied table or tables. |
| 248 | * |
| 249 | * Tables will only be optimized if the `[General] enable_sql_optimize_queries` INI config option is |
| 250 | * set to **1**. |
| 251 | * |
| 252 | * @param string|array $tables The name of the table to optimize or an array of tables to optimize. |
| 253 | * Table names must be prefixed (see {@link Piwik\Common::prefixTable()}). |
| 254 | * @param bool $force If true, the `OPTIMIZE TABLE` query will be run even if InnoDB tables are being used. |
| 255 | * @return bool |
| 256 | */ |
| 257 | public function optimizeTables(array $tables, bool $force = \false) : bool |
| 258 | { |
| 259 | return $this->getSchema()->optimizeTables($tables, $force); |
| 260 | } |
| 261 | /** |
| 262 | * Returns if the database engine is able to use sorted subqueries |
| 263 | * |
| 264 | * @return bool |
| 265 | */ |
| 266 | public function supportsSortingInSubquery() : bool |
| 267 | { |
| 268 | return $this->getSchema()->supportsSortingInSubquery(); |
| 269 | } |
| 270 | /** |
| 271 | * Returns the supported read isolation transaction level |
| 272 | * |
| 273 | * For example: |
| 274 | * READ COMMITTED |
| 275 | * or |
| 276 | * READ UNCOMMITTED |
| 277 | */ |
| 278 | public function getSupportedReadIsolationTransactionLevel() : string |
| 279 | { |
| 280 | return $this->getSchema()->getSupportedReadIsolationTransactionLevel(); |
| 281 | } |
| 282 | } |
| 283 |