forked from ezsystems/ezpublish-kernel
/
DoctrineDatabase.php
171 lines (146 loc) · 5.16 KB
/
DoctrineDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway;
use Doctrine\DBAL\Connection;
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway;
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
use PDO;
class DoctrineDatabase extends Gateway
{
const TABLE_BOOKMARKS = 'ezcontentbrowsebookmark';
const COLUMN_ID = 'id';
const COLUMN_USER_ID = 'user_id';
const COLUMN_LOCATION_ID = 'node_id';
const COLUMN_NAME = 'name';
/**
* @var \Doctrine\DBAL\Connection
*/
protected $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function insertBookmark(Bookmark $bookmark): int
{
$query = $this->connection->createQueryBuilder();
$query
->insert(self::TABLE_BOOKMARKS)
->values([
self::COLUMN_NAME => ':name',
self::COLUMN_USER_ID => ':user_id',
self::COLUMN_LOCATION_ID => ':location_id',
])
->setParameter(':name', $bookmark->name, PDO::PARAM_STR)
->setParameter(':user_id', $bookmark->userId, PDO::PARAM_INT)
->setParameter(':location_id', $bookmark->locationId, PDO::PARAM_INT);
$query->execute();
return (int) $this->connection->lastInsertId();
}
/**
* {@inheritdoc}
*/
public function deleteBookmark(int $id): void
{
$query = $this->connection->createQueryBuilder();
$query
->delete(self::TABLE_BOOKMARKS)
->where($query->expr()->eq(self::COLUMN_ID, ':id'))
->setParameter(':id', $id, PDO::PARAM_INT);
$query->execute();
}
/**
* {@inheritdoc}
*/
public function loadBookmarkDataById(int $id): array
{
$query = $this->connection->createQueryBuilder();
$query
->select(...$this->getColumns())
->from(self::TABLE_BOOKMARKS)
->where($query->expr()->eq(self::COLUMN_ID, ':id'))
->setParameter(':id', $id, PDO::PARAM_INT);
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locationIds): array
{
$query = $this->connection->createQueryBuilder();
$query
->select(...$this->getColumns())
->from(self::TABLE_BOOKMARKS)
->where($query->expr()->andX(
$query->expr()->eq(self::COLUMN_USER_ID, ':user_id'),
$query->expr()->in(self::COLUMN_LOCATION_ID, ':location_id')
))
->setParameter(':user_id', $userId, PDO::PARAM_INT)
->setParameter(':location_id', $locationIds, Connection::PARAM_INT_ARRAY);
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
{
$query = $this->connection->createQueryBuilder();
$query
->select(...$this->getColumns())
->from(self::TABLE_BOOKMARKS)
->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id'))
->setFirstResult($offset);
if ($limit > 0) {
$query->setMaxResults($limit);
}
$query->orderBy(self::COLUMN_ID, 'DESC');
$query->setParameter(':user_id', $userId, PDO::PARAM_INT);
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
{
$query = $this->connection->createQueryBuilder();
$query
->select('COUNT(' . self::COLUMN_ID . ')')
->from(self::TABLE_BOOKMARKS)
->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id'))
->setParameter(':user_id', $userId, PDO::PARAM_INT);
return (int) $query->execute()->fetchColumn();
}
/**
* {@inheritdoc}
*/
public function locationSwapped(int $location1Id, int $location2Id): void
{
$query = $this->connection->createQueryBuilder();
$query
->update(self::TABLE_BOOKMARKS)
->set(self::COLUMN_LOCATION_ID, '(CASE WHEN node_id = :source_id THEN :target_id ELSE :source_id END)')
->where($query->expr()->orX(
$query->expr()->eq(self::COLUMN_LOCATION_ID, ':source_id'),
$query->expr()->eq(self::COLUMN_LOCATION_ID, ':target_id')
));
$stmt = $this->connection->prepare($query->getSQL());
$stmt->bindValue('source_id', $location1Id, PDO::PARAM_INT);
$stmt->bindValue('target_id', $location2Id, PDO::PARAM_INT);
$stmt->execute();
}
private function getColumns(): array
{
return [
self::COLUMN_ID,
self::COLUMN_NAME,
self::COLUMN_USER_ID,
self::COLUMN_LOCATION_ID,
];
}
}