diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-10-04 13:28:23 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-10-04 13:28:23 +0400 |
commit | f05a25d60acb670d4292c67a10d0b408643ed3df (patch) | |
tree | 19438be0d2c12a71e80c8fe752e77be1f792802d /core/Db | |
parent | 1a7dbc5ff6ab987338fa644f1c4c7a07602c10f6 (diff) |
moved some queries within core to model files
This is only a start. Especially the Tracker model contains quite a bunch of different models. Ideally, one day, the tracker is refactored and all the goal tracker stuff including the queries are in the goal plugins, all the ecommerce stuff is in an ecommerce plugin etc. Haven't moved some of the LogAggregator queries.
Diffstat (limited to 'core/Db')
-rw-r--r-- | core/Db/Adapter.php | 2 | ||||
-rw-r--r-- | core/Db/BatchInsert.php | 19 | ||||
-rw-r--r-- | core/Db/Schema.php | 7 | ||||
-rw-r--r-- | core/Db/Schema/Mysql.php | 437 |
4 files changed, 238 insertions, 227 deletions
diff --git a/core/Db/Adapter.php b/core/Db/Adapter.php index 342a320d72..5ddd529caa 100644 --- a/core/Db/Adapter.php +++ b/core/Db/Adapter.php @@ -38,7 +38,7 @@ class Adapter } $className = self::getAdapterClassName($adapterName); - $adapter = new $className($dbInfos); + $adapter = new $className($dbInfos); if ($connect) { $adapter->getConnection(); diff --git a/core/Db/BatchInsert.php b/core/Db/BatchInsert.php index 1e818c2073..f6e7a223c7 100644 --- a/core/Db/BatchInsert.php +++ b/core/Db/BatchInsert.php @@ -33,13 +33,12 @@ class BatchInsert public static function tableInsertBatchIterate($tableName, $fields, $values, $ignoreWhenDuplicate = true) { $fieldList = '(' . join(',', $fields) . ')'; - $ignore = $ignoreWhenDuplicate ? 'IGNORE' : ''; + $ignore = $ignoreWhenDuplicate ? 'IGNORE' : ''; foreach ($values as $row) { - $query = "INSERT $ignore - INTO " . $tableName . " - $fieldList - VALUES (" . Common::getSqlStringFieldsArray($row) . ")"; + $query = "INSERT $ignore INTO " . $tableName . " + $fieldList + VALUES (" . Common::getSqlStringFieldsArray($row) . ")"; Db::query($query, $row); } } @@ -172,7 +171,8 @@ class BatchInsert * @see http://bugs.php.net/bug.php?id=54158 */ $openBaseDir = ini_get('open_basedir'); - $safeMode = ini_get('safe_mode'); + $safeMode = ini_get('safe_mode'); + if (empty($openBaseDir) && empty($safeMode)) { // php 5.x - LOAD DATA LOCAL INFILE is disabled if open_basedir restrictions or safe_mode enabled $keywords[] = 'LOCAL '; @@ -199,9 +199,11 @@ class BatchInsert $exceptions[] = "\n Try #" . (count($exceptions) + 1) . ': ' . $queryStart . ": " . $message; } } + if (count($exceptions)) { throw new Exception(implode(",", $exceptions)); } + return false; } @@ -218,8 +220,8 @@ class BatchInsert // Set up CSV delimiters, quotes, etc $delim = $fileSpec['delim']; $quote = $fileSpec['quote']; - $eol = $fileSpec['eol']; - $null = $fileSpec['null']; + $eol = $fileSpec['eol']; + $null = $fileSpec['null']; $escapespecial_cb = $fileSpec['escapespecial_cb']; $fp = @fopen($filePath, 'wb'); @@ -246,6 +248,7 @@ class BatchInsert throw new Exception('Error writing to the tmp file ' . $filePath); } } + fclose($fp); @chmod($filePath, 0777); diff --git a/core/Db/Schema.php b/core/Db/Schema.php index c7823cf70a..796e71dca5 100644 --- a/core/Db/Schema.php +++ b/core/Db/Schema.php @@ -116,11 +116,11 @@ class Schema extends Singleton */ private function loadSchema() { - $config = Config::getInstance(); - $dbInfos = $config->database; + $config = Config::getInstance(); + $dbInfos = $config->database; $schemaName = trim($dbInfos['schema']); - $className = self::getSchemaClassName($schemaName); + $className = self::getSchemaClassName($schemaName); $this->schema = new $className(); } @@ -134,6 +134,7 @@ class Schema extends Singleton if ($this->schema === null) { $this->loadSchema(); } + return $this->schema; } diff --git a/core/Db/Schema/Mysql.php b/core/Db/Schema/Mysql.php index 45ff75b685..eb8c1f71f1 100644 --- a/core/Db/Schema/Mysql.php +++ b/core/Db/Schema/Mysql.php @@ -20,6 +20,8 @@ use Piwik\DbHelper; */ class Mysql implements SchemaInterface { + private $tablesInstalled = null; + /** * Is this MySQL storage engine available? * @@ -58,216 +60,213 @@ class Mysql implements SchemaInterface $prefixTables = $this->getTablePrefix(); $tables = array( - 'user' => "CREATE TABLE {$prefixTables}user ( - login VARCHAR(100) NOT NULL, - password CHAR(32) NOT NULL, - alias VARCHAR(45) NOT NULL, - email VARCHAR(100) NOT NULL, - token_auth CHAR(32) NOT NULL, - superuser_access TINYINT(2) unsigned NOT NULL DEFAULT '0', - date_registered TIMESTAMP NULL, - PRIMARY KEY(login), - UNIQUE KEY uniq_keytoken(token_auth) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'access' => "CREATE TABLE {$prefixTables}access ( - login VARCHAR(100) NOT NULL, - idsite INTEGER UNSIGNED NOT NULL, - access VARCHAR(10) NULL, - PRIMARY KEY(login, idsite) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'site' => "CREATE TABLE {$prefixTables}site ( - idsite INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, - name VARCHAR(90) NOT NULL, - main_url VARCHAR(255) NOT NULL, - ts_created TIMESTAMP NULL, - ecommerce TINYINT DEFAULT 0, - sitesearch TINYINT DEFAULT 1, - sitesearch_keyword_parameters TEXT NOT NULL, - sitesearch_category_parameters TEXT NOT NULL, - timezone VARCHAR( 50 ) NOT NULL, - currency CHAR( 3 ) NOT NULL, - excluded_ips TEXT NOT NULL, - excluded_parameters TEXT NOT NULL, - excluded_user_agents TEXT NOT NULL, - `group` VARCHAR(250) NOT NULL, - `type` VARCHAR(255) NOT NULL, - keep_url_fragment TINYINT NOT NULL DEFAULT 0, - PRIMARY KEY(idsite) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'site_url' => "CREATE TABLE {$prefixTables}site_url ( - idsite INTEGER(10) UNSIGNED NOT NULL, - url VARCHAR(255) NOT NULL, - PRIMARY KEY(idsite, url) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'goal' => " CREATE TABLE `{$prefixTables}goal` ( - `idsite` int(11) NOT NULL, - `idgoal` int(11) NOT NULL, - `name` varchar(50) NOT NULL, - `match_attribute` varchar(20) NOT NULL, - `pattern` varchar(255) NOT NULL, - `pattern_type` varchar(10) NOT NULL, - `case_sensitive` tinyint(4) NOT NULL, - `allow_multiple` tinyint(4) NOT NULL, - `revenue` float NOT NULL, - `deleted` tinyint(4) NOT NULL default '0', - PRIMARY KEY (`idsite`,`idgoal`) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'logger_message' => "CREATE TABLE {$prefixTables}logger_message ( - idlogger_message INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, + 'user' => "CREATE TABLE {$prefixTables}user ( + login VARCHAR(100) NOT NULL, + password CHAR(32) NOT NULL, + alias VARCHAR(45) NOT NULL, + email VARCHAR(100) NOT NULL, + token_auth CHAR(32) NOT NULL, + superuser_access TINYINT(2) unsigned NOT NULL DEFAULT '0', + date_registered TIMESTAMP NULL, + PRIMARY KEY(login), + UNIQUE KEY uniq_keytoken(token_auth) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'access' => "CREATE TABLE {$prefixTables}access ( + login VARCHAR(100) NOT NULL, + idsite INTEGER UNSIGNED NOT NULL, + access VARCHAR(10) NULL, + PRIMARY KEY(login, idsite) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'site' => "CREATE TABLE {$prefixTables}site ( + idsite INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, + name VARCHAR(90) NOT NULL, + main_url VARCHAR(255) NOT NULL, + ts_created TIMESTAMP NULL, + ecommerce TINYINT DEFAULT 0, + sitesearch TINYINT DEFAULT 1, + sitesearch_keyword_parameters TEXT NOT NULL, + sitesearch_category_parameters TEXT NOT NULL, + timezone VARCHAR( 50 ) NOT NULL, + currency CHAR( 3 ) NOT NULL, + excluded_ips TEXT NOT NULL, + excluded_parameters TEXT NOT NULL, + excluded_user_agents TEXT NOT NULL, + `group` VARCHAR(250) NOT NULL, + `type` VARCHAR(255) NOT NULL, + keep_url_fragment TINYINT NOT NULL DEFAULT 0, + PRIMARY KEY(idsite) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'site_url' => "CREATE TABLE {$prefixTables}site_url ( + idsite INTEGER(10) UNSIGNED NOT NULL, + url VARCHAR(255) NOT NULL, + PRIMARY KEY(idsite, url) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'goal' => "CREATE TABLE `{$prefixTables}goal` ( + `idsite` int(11) NOT NULL, + `idgoal` int(11) NOT NULL, + `name` varchar(50) NOT NULL, + `match_attribute` varchar(20) NOT NULL, + `pattern` varchar(255) NOT NULL, + `pattern_type` varchar(10) NOT NULL, + `case_sensitive` tinyint(4) NOT NULL, + `allow_multiple` tinyint(4) NOT NULL, + `revenue` float NOT NULL, + `deleted` tinyint(4) NOT NULL default '0', + PRIMARY KEY (`idsite`,`idgoal`) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'logger_message' => "CREATE TABLE {$prefixTables}logger_message ( + idlogger_message INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, tag VARCHAR(50) NULL, - timestamp TIMESTAMP NULL, + timestamp TIMESTAMP NULL, level VARCHAR(16) NULL, - message TEXT NULL, - PRIMARY KEY(idlogger_message) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'log_action' => "CREATE TABLE {$prefixTables}log_action ( - idaction INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, - name TEXT, - hash INTEGER(10) UNSIGNED NOT NULL, - type TINYINT UNSIGNED NULL, - url_prefix TINYINT(2) NULL, - PRIMARY KEY(idaction), - INDEX index_type_hash (type, hash) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'log_visit' => "CREATE TABLE {$prefixTables}log_visit ( - idvisit INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, - idsite INTEGER(10) UNSIGNED NOT NULL, - idvisitor BINARY(8) NOT NULL, - visit_last_action_time DATETIME NOT NULL, - config_id BINARY(8) NOT NULL, - location_ip VARBINARY(16) NOT NULL, - PRIMARY KEY(idvisit), - INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time), - INDEX index_idsite_datetime (idsite, visit_last_action_time), - INDEX index_idsite_idvisitor (idsite, idvisitor) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", + message TEXT NULL, + PRIMARY KEY(idlogger_message) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'log_action' => "CREATE TABLE {$prefixTables}log_action ( + idaction INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, + name TEXT, + hash INTEGER(10) UNSIGNED NOT NULL, + type TINYINT UNSIGNED NULL, + url_prefix TINYINT(2) NULL, + PRIMARY KEY(idaction), + INDEX index_type_hash (type, hash) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'log_visit' => "CREATE TABLE {$prefixTables}log_visit ( + idvisit INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, + idsite INTEGER(10) UNSIGNED NOT NULL, + idvisitor BINARY(8) NOT NULL, + visit_last_action_time DATETIME NOT NULL, + config_id BINARY(8) NOT NULL, + location_ip VARBINARY(16) NOT NULL, + PRIMARY KEY(idvisit), + INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time), + INDEX index_idsite_datetime (idsite, visit_last_action_time), + INDEX index_idsite_idvisitor (idsite, idvisitor) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", 'log_conversion_item' => "CREATE TABLE `{$prefixTables}log_conversion_item` ( - idsite int(10) UNSIGNED NOT NULL, - idvisitor BINARY(8) NOT NULL, - server_time DATETIME NOT NULL, - idvisit INTEGER(10) UNSIGNED NOT NULL, - idorder varchar(100) NOT NULL, - - idaction_sku INTEGER(10) UNSIGNED NOT NULL, - idaction_name INTEGER(10) UNSIGNED NOT NULL, - idaction_category INTEGER(10) UNSIGNED NOT NULL, - idaction_category2 INTEGER(10) UNSIGNED NOT NULL, - idaction_category3 INTEGER(10) UNSIGNED NOT NULL, - idaction_category4 INTEGER(10) UNSIGNED NOT NULL, - idaction_category5 INTEGER(10) UNSIGNED NOT NULL, - price FLOAT NOT NULL, - quantity INTEGER(10) UNSIGNED NOT NULL, - deleted TINYINT(1) UNSIGNED NOT NULL, - - PRIMARY KEY(idvisit, idorder, idaction_sku), - INDEX index_idsite_servertime ( idsite, server_time ) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'log_conversion' => "CREATE TABLE `{$prefixTables}log_conversion` ( - idvisit int(10) unsigned NOT NULL, - idsite int(10) unsigned NOT NULL, - idvisitor BINARY(8) NOT NULL, - server_time datetime NOT NULL, - idaction_url int(11) default NULL, - idlink_va int(11) default NULL, - idgoal int(10) NOT NULL, - buster int unsigned NOT NULL, - idorder varchar(100) default NULL, - items SMALLINT UNSIGNED DEFAULT NULL, - url text NOT NULL, - - PRIMARY KEY (idvisit, idgoal, buster), - UNIQUE KEY unique_idsite_idorder (idsite, idorder), - INDEX index_idsite_datetime ( idsite, server_time ) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", + idsite int(10) UNSIGNED NOT NULL, + idvisitor BINARY(8) NOT NULL, + server_time DATETIME NOT NULL, + idvisit INTEGER(10) UNSIGNED NOT NULL, + idorder varchar(100) NOT NULL, + idaction_sku INTEGER(10) UNSIGNED NOT NULL, + idaction_name INTEGER(10) UNSIGNED NOT NULL, + idaction_category INTEGER(10) UNSIGNED NOT NULL, + idaction_category2 INTEGER(10) UNSIGNED NOT NULL, + idaction_category3 INTEGER(10) UNSIGNED NOT NULL, + idaction_category4 INTEGER(10) UNSIGNED NOT NULL, + idaction_category5 INTEGER(10) UNSIGNED NOT NULL, + price FLOAT NOT NULL, + quantity INTEGER(10) UNSIGNED NOT NULL, + deleted TINYINT(1) UNSIGNED NOT NULL, + PRIMARY KEY(idvisit, idorder, idaction_sku), + INDEX index_idsite_servertime ( idsite, server_time ) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'log_conversion' => "CREATE TABLE `{$prefixTables}log_conversion` ( + idvisit int(10) unsigned NOT NULL, + idsite int(10) unsigned NOT NULL, + idvisitor BINARY(8) NOT NULL, + server_time datetime NOT NULL, + idaction_url int(11) default NULL, + idlink_va int(11) default NULL, + idgoal int(10) NOT NULL, + buster int unsigned NOT NULL, + idorder varchar(100) default NULL, + items SMALLINT UNSIGNED DEFAULT NULL, + url text NOT NULL, + PRIMARY KEY (idvisit, idgoal, buster), + UNIQUE KEY unique_idsite_idorder (idsite, idorder), + INDEX index_idsite_datetime ( idsite, server_time ) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", 'log_link_visit_action' => "CREATE TABLE {$prefixTables}log_link_visit_action ( - idlink_va INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT, - idsite int(10) UNSIGNED NOT NULL, - idvisitor BINARY(8) NOT NULL, - idvisit INTEGER(10) UNSIGNED NOT NULL, - idaction_url_ref INTEGER(10) UNSIGNED NULL DEFAULT 0, - idaction_name_ref INTEGER(10) UNSIGNED NOT NULL, - - custom_float FLOAT NULL DEFAULT NULL, - PRIMARY KEY(idlink_va), - INDEX index_idvisit(idvisit) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'log_profiling' => "CREATE TABLE {$prefixTables}log_profiling ( - query TEXT NOT NULL, - count INTEGER UNSIGNED NULL, - sum_time_ms FLOAT NULL, - UNIQUE KEY query(query(100)) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'option' => "CREATE TABLE `{$prefixTables}option` ( - option_name VARCHAR( 255 ) NOT NULL, - option_value LONGTEXT NOT NULL, - autoload TINYINT NOT NULL DEFAULT '1', - PRIMARY KEY ( option_name ), - INDEX autoload( autoload ) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'session' => "CREATE TABLE {$prefixTables}session ( - id VARCHAR( 255 ) NOT NULL, - modified INTEGER, - lifetime INTEGER, - data TEXT, - PRIMARY KEY ( id ) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'archive_numeric' => "CREATE TABLE {$prefixTables}archive_numeric ( - idarchive INTEGER UNSIGNED NOT NULL, - name VARCHAR(255) NOT NULL, - idsite INTEGER UNSIGNED NULL, - date1 DATE NULL, - date2 DATE NULL, - period TINYINT UNSIGNED NULL, - ts_archived DATETIME NULL, - value DOUBLE NULL, - PRIMARY KEY(idarchive, name), - INDEX index_idsite_dates_period(idsite, date1, date2, period, ts_archived), - INDEX index_period_archived(period, ts_archived) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", - - 'archive_blob' => "CREATE TABLE {$prefixTables}archive_blob ( - idarchive INTEGER UNSIGNED NOT NULL, - name VARCHAR(255) NOT NULL, - idsite INTEGER UNSIGNED NULL, - date1 DATE NULL, - date2 DATE NULL, - period TINYINT UNSIGNED NULL, - ts_archived DATETIME NULL, - value MEDIUMBLOB NULL, - PRIMARY KEY(idarchive, name), - INDEX index_period_archived(period, ts_archived) - ) ENGINE=$engine DEFAULT CHARSET=utf8 - ", + idlink_va INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT, + idsite int(10) UNSIGNED NOT NULL, + idvisitor BINARY(8) NOT NULL, + idvisit INTEGER(10) UNSIGNED NOT NULL, + idaction_url_ref INTEGER(10) UNSIGNED NULL DEFAULT 0, + idaction_name_ref INTEGER(10) UNSIGNED NOT NULL, + custom_float FLOAT NULL DEFAULT NULL, + PRIMARY KEY(idlink_va), + INDEX index_idvisit(idvisit) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'log_profiling' => "CREATE TABLE {$prefixTables}log_profiling ( + query TEXT NOT NULL, + count INTEGER UNSIGNED NULL, + sum_time_ms FLOAT NULL, + UNIQUE KEY query(query(100)) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'option' => "CREATE TABLE `{$prefixTables}option` ( + option_name VARCHAR( 255 ) NOT NULL, + option_value LONGTEXT NOT NULL, + autoload TINYINT NOT NULL DEFAULT '1', + PRIMARY KEY ( option_name ), + INDEX autoload( autoload ) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'session' => "CREATE TABLE {$prefixTables}session ( + id VARCHAR( 255 ) NOT NULL, + modified INTEGER, + lifetime INTEGER, + data TEXT, + PRIMARY KEY ( id ) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'archive_numeric' => "CREATE TABLE {$prefixTables}archive_numeric ( + idarchive INTEGER UNSIGNED NOT NULL, + name VARCHAR(255) NOT NULL, + idsite INTEGER UNSIGNED NULL, + date1 DATE NULL, + date2 DATE NULL, + period TINYINT UNSIGNED NULL, + ts_archived DATETIME NULL, + value DOUBLE NULL, + PRIMARY KEY(idarchive, name), + INDEX index_idsite_dates_period(idsite, date1, date2, period, ts_archived), + INDEX index_period_archived(period, ts_archived) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", + + 'archive_blob' => "CREATE TABLE {$prefixTables}archive_blob ( + idarchive INTEGER UNSIGNED NOT NULL, + name VARCHAR(255) NOT NULL, + idsite INTEGER UNSIGNED NULL, + date1 DATE NULL, + date2 DATE NULL, + period TINYINT UNSIGNED NULL, + ts_archived DATETIME NULL, + value MEDIUMBLOB NULL, + PRIMARY KEY(idarchive, name), + INDEX index_period_archived(period, ts_archived) + ) ENGINE=$engine DEFAULT CHARSET=utf8 + ", ); + return $tables; } @@ -297,17 +296,17 @@ class Mysql implements SchemaInterface */ public function getTablesNames() { - $aTables = array_keys($this->getTablesCreateSql()); + $aTables = array_keys($this->getTablesCreateSql()); $prefixTables = $this->getTablePrefix(); + $return = array(); foreach ($aTables as $table) { $return[] = $prefixTables . $table; } + return $return; } - private $tablesInstalled = null; - /** * Get list of installed columns in a table * @@ -317,7 +316,7 @@ class Mysql implements SchemaInterface */ public function getTableColumns($tableName) { - $db = Db::get(); + $db = $this->getDb(); $allColumns = $db->fetchAll("SHOW COLUMNS FROM . $tableName"); @@ -340,7 +339,8 @@ class Mysql implements SchemaInterface if (is_null($this->tablesInstalled) || $forceReload === true ) { - $db = Db::get(); + + $db = $this->getDb(); $prefixTables = $this->getTablePrefixEscaped(); $allTables = $this->getAllExistingTables($prefixTables); @@ -353,12 +353,13 @@ class Mysql implements SchemaInterface // at this point we have the static list of core tables, but let's add the monthly archive tables $allArchiveNumeric = $db->fetchCol("SHOW TABLES LIKE '" . $prefixTables . "archive_numeric%'"); - $allArchiveBlob = $db->fetchCol("SHOW TABLES LIKE '" . $prefixTables . "archive_blob%'"); + $allArchiveBlob = $db->fetchCol("SHOW TABLES LIKE '" . $prefixTables . "archive_blob%'"); $allTablesReallyInstalled = array_merge($tablesInstalled, $allArchiveNumeric, $allArchiveBlob); $this->tablesInstalled = $allTablesReallyInstalled; } + return $this->tablesInstalled; } @@ -382,6 +383,7 @@ class Mysql implements SchemaInterface if (is_null($dbName)) { $dbName = $this->getDbName(); } + Db::exec("CREATE DATABASE IF NOT EXISTS " . $dbName . " DEFAULT CHARACTER SET utf8"); } @@ -405,7 +407,7 @@ class Mysql implements SchemaInterface } catch (Exception $e) { // mysql code error 1050:table already exists // see bug #153 https://github.com/piwik/piwik/issues/153 - if (!Db::get()->isErrNo($e, '1050')) { + if (!$this->getDb()->isErrNo($e, '1050')) { throw $e; } } @@ -425,7 +427,7 @@ class Mysql implements SchemaInterface */ public function createTables() { - $db = Db::get(); + $db = $this->getDb(); $prefixTables = $this->getTablePrefix(); $tablesAlreadyInstalled = $this->getTablesInstalled(); @@ -448,9 +450,9 @@ class Mysql implements SchemaInterface { // The anonymous user is the user that is assigned by default // note that the token_auth value is anonymous, which is assigned by default as well in the Login plugin - $db = Db::get(); + $db = $this->getDb(); $db->query("INSERT IGNORE INTO " . Common::prefixTable("user") . " - VALUES ( 'anonymous', '', 'anonymous', 'anonymous@example.org', 'anonymous', 0, '" . Date::factory('now')->getDatetime() . "' );"); + VALUES ( 'anonymous', '', 'anonymous', 'anonymous@example.org', 'anonymous', 0, '" . Date::factory('now')->getDatetime() . "' );"); } /** @@ -466,7 +468,7 @@ class Mysql implements SchemaInterface private function getTablePrefix() { - $dbInfos = Db::getDatabaseConfig(); + $dbInfos = Db::getDatabaseConfig(); $prefixTables = $dbInfos['tables_prefix']; return $prefixTables; @@ -475,10 +477,15 @@ class Mysql implements SchemaInterface private function getTableEngine() { $dbInfos = Db::getDatabaseConfig(); - $engine = $dbInfos['type']; + $engine = $dbInfos['type']; + return $engine; } + private function getDb(){ + return Db::get(); + } + private function getDbName() { $dbInfos = Db::getDatabaseConfig(); |