Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DbHelper.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use Exception;
  11. use Piwik\Db\Adapter;
  12. use Piwik\Db\Schema;
  13. /**
  14. * Contains database related helper functions.
  15. */
  16. class DbHelper
  17. {
  18. /**
  19. * Get list of tables installed
  20. *
  21. * @param bool $forceReload Invalidate cache
  22. * @return array Tables installed
  23. */
  24. public static function getTablesInstalled($forceReload = true)
  25. {
  26. return Schema::getInstance()->getTablesInstalled($forceReload);
  27. }
  28. /**
  29. * Get list of installed columns in a table
  30. *
  31. * @param string $tableName The name of a table.
  32. *
  33. * @return array Installed columns indexed by the column name.
  34. */
  35. public static function getTableColumns($tableName)
  36. {
  37. return Schema::getInstance()->getTableColumns($tableName);
  38. }
  39. /**
  40. * Creates a new table in the database.
  41. *
  42. * Example:
  43. * ```
  44. * $tableDefinition = "`age` INT(11) NOT NULL AUTO_INCREMENT,
  45. * `name` VARCHAR(255) NOT NULL";
  46. *
  47. * DbHelper::createTable('tablename', $tableDefinition);
  48. * ``
  49. *
  50. * @param string $nameWithoutPrefix The name of the table without any piwik prefix.
  51. * @param string $createDefinition The table create definition
  52. *
  53. * @api
  54. */
  55. public static function createTable($nameWithoutPrefix, $createDefinition)
  56. {
  57. Schema::getInstance()->createTable($nameWithoutPrefix, $createDefinition);
  58. }
  59. /**
  60. * Returns true if Piwik is installed
  61. *
  62. * @since 0.6.3
  63. *
  64. * @return bool True if installed; false otherwise
  65. */
  66. public static function isInstalled()
  67. {
  68. try {
  69. return Schema::getInstance()->hasTables();
  70. } catch (Exception $e) {
  71. return false;
  72. }
  73. }
  74. /**
  75. * Truncate all tables
  76. */
  77. public static function truncateAllTables()
  78. {
  79. Schema::getInstance()->truncateAllTables();
  80. }
  81. /**
  82. * Creates an entry in the User table for the "anonymous" user.
  83. */
  84. public static function createAnonymousUser()
  85. {
  86. Schema::getInstance()->createAnonymousUser();
  87. }
  88. /**
  89. * Create all tables
  90. */
  91. public static function createTables()
  92. {
  93. Schema::getInstance()->createTables();
  94. }
  95. /**
  96. * Drop database, used in tests
  97. */
  98. public static function dropDatabase($dbName = null)
  99. {
  100. if (defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE) {
  101. Schema::getInstance()->dropDatabase($dbName);
  102. }
  103. }
  104. /**
  105. * Check database connection character set is utf8.
  106. *
  107. * @return bool True if it is (or doesn't matter); false otherwise
  108. */
  109. public static function isDatabaseConnectionUTF8()
  110. {
  111. return Db::get()->isConnectionUTF8();
  112. }
  113. /**
  114. * Checks the database server version against the required minimum
  115. * version.
  116. *
  117. * @see config/global.ini.php
  118. * @since 0.4.4
  119. * @throws Exception if server version is less than the required version
  120. */
  121. public static function checkDatabaseVersion()
  122. {
  123. Db::get()->checkServerVersion();
  124. }
  125. /**
  126. * Disconnect from database
  127. */
  128. public static function disconnectDatabase()
  129. {
  130. Db::get()->closeConnection();
  131. }
  132. /**
  133. * Create database
  134. *
  135. * @param string|null $dbName
  136. */
  137. public static function createDatabase($dbName = null)
  138. {
  139. Schema::getInstance()->createDatabase($dbName);
  140. }
  141. /**
  142. * Get the SQL to create Piwik tables
  143. *
  144. * @return array array of strings containing SQL
  145. */
  146. public static function getTablesCreateSql()
  147. {
  148. return Schema::getInstance()->getTablesCreateSql();
  149. }
  150. /**
  151. * Get the SQL to create a specific Piwik table
  152. *
  153. * @param string $tableName
  154. * @return string SQL
  155. */
  156. public static function getTableCreateSql($tableName)
  157. {
  158. return Schema::getInstance()->getTableCreateSql($tableName);
  159. }
  160. }