API
6 years ago
Access
6 years ago
Application
6 years ago
Archive
6 years ago
ArchiveProcessor
6 years ago
Archiver
6 years ago
AssetManager
6 years ago
Auth
6 years ago
Category
6 years ago
CliMulti
6 years ago
Columns
6 years ago
Composer
6 years ago
Concurrency
6 years ago
Config
6 years ago
Container
6 years ago
CronArchive
6 years ago
DataAccess
5 years ago
DataFiles
6 years ago
DataTable
6 years ago
Db
6 years ago
DeviceDetector
5 years ago
Email
6 years ago
Exception
6 years ago
Http
6 years ago
Intl
6 years ago
Mail
6 years ago
Measurable
6 years ago
Menu
6 years ago
Metrics
6 years ago
Notification
6 years ago
Period
6 years ago
Plugin
6 years ago
ProfessionalServices
6 years ago
Report
6 years ago
ReportRenderer
6 years ago
Scheduler
6 years ago
Segment
6 years ago
Session
6 years ago
Settings
6 years ago
Tracker
5 years ago
Translation
6 years ago
UpdateCheck
6 years ago
Updater
6 years ago
Updates
6 years ago
Validators
6 years ago
View
6 years ago
ViewDataTable
6 years ago
Visualization
6 years ago
Widget
6 years ago
.htaccess
6 years ago
Access.php
6 years ago
Archive.php
6 years ago
ArchiveProcessor.php
6 years ago
AssetManager.php
6 years ago
Auth.php
6 years ago
BaseFactory.php
6 years ago
Cache.php
6 years ago
CacheId.php
6 years ago
CliMulti.php
6 years ago
Common.php
6 years ago
Config.php
6 years ago
Console.php
6 years ago
Context.php
6 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
6 years ago
DataTable.php
6 years ago
Date.php
6 years ago
Db.php
6 years ago
DbHelper.php
6 years ago
Development.php
6 years ago
DeviceDetectorFactory.php
6 years ago
ErrorHandler.php
6 years ago
EventDispatcher.php
6 years ago
ExceptionHandler.php
6 years ago
FileIntegrity.php
6 years ago
Filechecks.php
6 years ago
Filesystem.php
6 years ago
FrontController.php
6 years ago
Http.php
6 years ago
IP.php
6 years ago
Log.php
6 years ago
LogDeleter.php
6 years ago
Mail.php
6 years ago
Metrics.php
6 years ago
MetricsFormatter.php
6 years ago
Nonce.php
5 years ago
Notification.php
6 years ago
NumberFormatter.php
6 years ago
Option.php
5 years ago
Period.php
6 years ago
Piwik.php
6 years ago
Plugin.php
6 years ago
Profiler.php
6 years ago
ProxyHeaders.php
6 years ago
ProxyHttp.php
6 years ago
QuickForm2.php
6 years ago
RankingQuery.php
6 years ago
Registry.php
6 years ago
ReportRenderer.php
6 years ago
ScheduledTask.php
6 years ago
Segment.php
6 years ago
Sequence.php
6 years ago
Session.php
6 years ago
SettingsPiwik.php
6 years ago
SettingsServer.php
6 years ago
Singleton.php
6 years ago
Site.php
6 years ago
TCPDF.php
6 years ago
TaskScheduler.php
6 years ago
Theme.php
6 years ago
Timer.php
6 years ago
Tracker.php
6 years ago
Translate.php
6 years ago
Twig.php
6 years ago
Unzip.php
6 years ago
UpdateCheck.php
6 years ago
Updater.php
6 years ago
Updates.php
6 years ago
Url.php
6 years ago
UrlHelper.php
6 years ago
Version.php
5 years ago
View.php
6 years ago
bootstrap.php
6 years ago
dispatch.php
6 years ago
testMinimumPhpVersion.php
6 years ago
Sequence.php
128 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; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Db\AdapterInterface; |
| 13 | |
| 14 | /** |
| 15 | * Used for generating auto increment ids. |
| 16 | * |
| 17 | * Example: |
| 18 | * |
| 19 | * $sequence = new Sequence('my_sequence_name'); |
| 20 | * $id = $sequence->getNextId(); |
| 21 | * $db->insert('anytable', array('id' => $id, '...' => '...')); |
| 22 | */ |
| 23 | class Sequence |
| 24 | { |
| 25 | const TABLE_NAME = 'sequence'; |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $name; |
| 30 | |
| 31 | /** |
| 32 | * @var AdapterInterface |
| 33 | */ |
| 34 | private $db; |
| 35 | |
| 36 | /** |
| 37 | * @var string |
| 38 | */ |
| 39 | private $table; |
| 40 | |
| 41 | /** |
| 42 | * The name of the table or sequence you want to get an id for. |
| 43 | * |
| 44 | * @param string $name eg 'archive_numeric_2014_11' |
| 45 | * @param AdapterInterface $db You can optionally pass a DB adapter to make it work against another database. |
| 46 | * @param string|null $tablePrefix |
| 47 | */ |
| 48 | public function __construct($name, $db = null, $tablePrefix = null) |
| 49 | { |
| 50 | $this->name = $name; |
| 51 | $this->db = $db ?: Db::get(); |
| 52 | $this->table = $this->getTableName($tablePrefix); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates / initializes a new sequence. |
| 57 | * |
| 58 | * @param int $initialValue |
| 59 | * @return int The actually used value to initialize the table. |
| 60 | * |
| 61 | * @throws \Exception in case a sequence having this name already exists. |
| 62 | */ |
| 63 | public function create($initialValue = 0) |
| 64 | { |
| 65 | $initialValue = (int) $initialValue; |
| 66 | |
| 67 | $this->db->insert($this->table, array('name' => $this->name, 'value' => $initialValue)); |
| 68 | |
| 69 | return $initialValue; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns true if the sequence exist. |
| 74 | * |
| 75 | * @return bool |
| 76 | */ |
| 77 | public function exists() |
| 78 | { |
| 79 | $query = $this->db->query('SELECT * FROM ' . $this->table . ' WHERE name = ?', $this->name); |
| 80 | |
| 81 | return $query->rowCount() > 0; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get / allocate / reserve a new id for the current sequence. Important: Getting the next id will fail in case |
| 86 | * no such sequence exists. Make sure to create one if needed, see {@link create()}. |
| 87 | * |
| 88 | * @return int |
| 89 | * @throws Exception |
| 90 | */ |
| 91 | public function getNextId() |
| 92 | { |
| 93 | $sql = 'UPDATE ' . $this->table . ' SET value = LAST_INSERT_ID(value + 1) WHERE name = ?'; |
| 94 | |
| 95 | $result = $this->db->query($sql, array($this->name)); |
| 96 | $rowCount = $result->rowCount(); |
| 97 | |
| 98 | if (1 !== $rowCount) { |
| 99 | throw new Exception("Sequence '" . $this->name . "' not found."); |
| 100 | } |
| 101 | |
| 102 | $createdId = $this->db->lastInsertId(); |
| 103 | |
| 104 | return (int) $createdId; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the current max id. |
| 109 | * @return int |
| 110 | * @internal |
| 111 | */ |
| 112 | public function getCurrentId() |
| 113 | { |
| 114 | $sql = 'SELECT value FROM ' . $this->table . ' WHERE name = ?'; |
| 115 | |
| 116 | $id = $this->db->fetchOne($sql, array($this->name)); |
| 117 | |
| 118 | if (!empty($id) || '0' === $id || 0 === $id) { |
| 119 | return (int) $id; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private function getTableName($prefix) |
| 124 | { |
| 125 | return ($prefix !== null) ? $prefix . self::TABLE_NAME : Common::prefixTable(self::TABLE_NAME); |
| 126 | } |
| 127 | } |
| 128 |