• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / caching / 6311829630

26 Sep 2023 11:12AM UTC coverage: 88.889%. Remained the same
6311829630

push

github

dg
removed Nette\SmartObject

536 of 603 relevant lines covered (88.89%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.55
/src/Caching/Storages/SQLiteJournal.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Caching\Storages;
11

12
use Nette;
13
use Nette\Caching\Cache;
14

15

16
/**
17
 * SQLite based journal.
18
 */
19
class SQLiteJournal implements Journal
20
{
21
        /** @string */
22
        private $path;
23
        private \PDO $pdo;
24

25

26
        public function __construct(string $path)
1✔
27
        {
28
                if (!extension_loaded('pdo_sqlite')) {
1✔
29
                        throw new Nette\NotSupportedException('SQLiteJournal requires PHP extension pdo_sqlite which is not loaded.');
×
30
                }
31

32
                $this->path = $path;
1✔
33
        }
1✔
34

35

36
        private function open(): void
37
        {
38
                if ($this->path !== ':memory:' && !is_file($this->path)) {
1✔
39
                        touch($this->path); // ensures ordinary file permissions
1✔
40
                }
41

42
                $this->pdo = new \PDO('sqlite:' . $this->path);
1✔
43
                $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
1✔
44
                $this->pdo->exec('
1✔
45
                        PRAGMA foreign_keys = OFF;
46
                        PRAGMA journal_mode = WAL;
47
                        CREATE TABLE IF NOT EXISTS tags (
48
                                key BLOB NOT NULL,
49
                                tag BLOB NOT NULL
50
                        );
51
                        CREATE TABLE IF NOT EXISTS priorities (
52
                                key BLOB NOT NULL,
53
                                priority INT NOT NULL
54
                        );
55
                        CREATE INDEX IF NOT EXISTS idx_tags_tag ON tags(tag);
56
                        CREATE UNIQUE INDEX IF NOT EXISTS idx_tags_key_tag ON tags(key, tag);
57
                        CREATE UNIQUE INDEX IF NOT EXISTS idx_priorities_key ON priorities(key);
58
                        CREATE INDEX IF NOT EXISTS idx_priorities_priority ON priorities(priority);
59
                        PRAGMA synchronous = NORMAL;
60
                ');
61
        }
1✔
62

63

64
        public function write(string $key, array $dependencies): void
1✔
65
        {
66
                if (!isset($this->pdo)) {
1✔
67
                        $this->open();
1✔
68
                }
69

70
                $this->pdo->exec('BEGIN');
1✔
71

72
                if (!empty($dependencies[Cache::Tags])) {
1✔
73
                        $this->pdo->prepare('DELETE FROM tags WHERE key = ?')->execute([$key]);
1✔
74

75
                        foreach ($dependencies[Cache::Tags] as $tag) {
1✔
76
                                $arr[] = $key;
1✔
77
                                $arr[] = $tag;
1✔
78
                        }
79

80
                        $this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
1✔
81
                                ->execute($arr);
1✔
82
                }
83

84
                if (!empty($dependencies[Cache::Priority])) {
1✔
85
                        $this->pdo->prepare('REPLACE INTO priorities (key, priority) VALUES (?, ?)')
1✔
86
                                ->execute([$key, (int) $dependencies[Cache::Priority]]);
1✔
87
                }
88

89
                $this->pdo->exec('COMMIT');
1✔
90
        }
1✔
91

92

93
        public function clean(array $conditions): ?array
1✔
94
        {
95
                if (!isset($this->pdo)) {
1✔
96
                        $this->open();
×
97
                }
98

99
                if (!empty($conditions[Cache::All])) {
1✔
100
                        $this->pdo->exec('
1✔
101
                                BEGIN;
102
                                DELETE FROM tags;
103
                                DELETE FROM priorities;
104
                                COMMIT;
105
                        ');
106

107
                        return null;
1✔
108
                }
109

110
                $unions = $args = [];
1✔
111
                if (!empty($conditions[Cache::Tags])) {
1✔
112
                        $tags = (array) $conditions[Cache::Tags];
1✔
113
                        $unions[] = 'SELECT DISTINCT key FROM tags WHERE tag IN (?' . str_repeat(', ?', count($tags) - 1) . ')';
1✔
114
                        $args = $tags;
1✔
115
                }
116

117
                if (!empty($conditions[Cache::Priority])) {
1✔
118
                        $unions[] = 'SELECT DISTINCT key FROM priorities WHERE priority <= ?';
1✔
119
                        $args[] = (int) $conditions[Cache::Priority];
1✔
120
                }
121

122
                if (empty($unions)) {
1✔
123
                        return [];
×
124
                }
125

126
                $unionSql = implode(' UNION ', $unions);
1✔
127

128
                $this->pdo->exec('BEGIN IMMEDIATE');
1✔
129

130
                $stmt = $this->pdo->prepare($unionSql);
1✔
131
                $stmt->execute($args);
1✔
132
                $keys = $stmt->fetchAll(\PDO::FETCH_COLUMN, 0);
1✔
133

134
                if (empty($keys)) {
1✔
135
                        $this->pdo->exec('COMMIT');
1✔
136
                        return [];
1✔
137
                }
138

139
                $this->pdo->prepare("DELETE FROM tags WHERE key IN ($unionSql)")->execute($args);
1✔
140
                $this->pdo->prepare("DELETE FROM priorities WHERE key IN ($unionSql)")->execute($args);
1✔
141
                $this->pdo->exec('COMMIT');
1✔
142

143
                return $keys;
1✔
144
        }
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc