mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 07:37:18 +01:00
Convert TagMapper to QBMapper
This commit is contained in:
@@ -2,56 +2,61 @@
|
|||||||
namespace OCA\QuickNotes\Db;
|
namespace OCA\QuickNotes\Db;
|
||||||
|
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
use OCP\AppFramework\Db\Mapper;
|
use OCP\AppFramework\Db\QBMapper;
|
||||||
use OCP\AppFramework\Db\DoesNotExistException;
|
use OCP\AppFramework\Db\DoesNotExistException;
|
||||||
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||||
|
|
||||||
use OCA\QuickNotes\Db\Tag;
|
use OCA\QuickNotes\Db\Tag;
|
||||||
|
|
||||||
class TagMapper extends Mapper {
|
class TagMapper extends QBMapper {
|
||||||
|
|
||||||
public function __construct(IDBConnection $db) {
|
public function __construct(IDBConnection $db) {
|
||||||
parent::__construct($db, 'quicknotes_tags', '\OCA\QuickNotes\Db\Tag');
|
parent::__construct($db, 'quicknotes_tags', Tag::class);
|
||||||
}
|
|
||||||
|
|
||||||
public function find($id, $userId): Tag {
|
|
||||||
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE id = ? AND user_id = ?';
|
|
||||||
return $this->findEntity($sql, [$id, $userId]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function findAll($userId): array {
|
|
||||||
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ?';
|
|
||||||
return $this->findEntities($sql, [$userId]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTagsForNote(string $userId, int $noteId): array {
|
public function getTagsForNote(string $userId, int $noteId): array {
|
||||||
$sql = 'SELECT T.id, T.name FROM *PREFIX*quicknotes_tags T ';
|
$qb = $this->db->getQueryBuilder();
|
||||||
$sql.= 'INNER JOIN *PREFIX*quicknotes_note_tags NT ';
|
$qb->select('T.id', 'T.name')
|
||||||
$sql.= 'ON T.id = NT.tag_id ';
|
->from($this->getTableName(), 'T')
|
||||||
$sql.= 'WHERE NT.user_id = ? AND NT.note_id = ?';
|
->innerJoin('T', 'quicknotes_note_tags' ,'NT', $qb->expr()->eq('T.id', 'NT.tag_id'))
|
||||||
return $this->findEntities($sql, [$userId, $noteId]);
|
->where($qb->expr()->eq('NT.user_id', $qb->createParameter('user_id')))
|
||||||
|
->andWhere($qb->expr()->eq('NT.note_id', $qb->createParameter('note_id')))
|
||||||
|
->setParameter('user_id', $userId)
|
||||||
|
->setParameter('note_id', $noteId);
|
||||||
|
return $this->findEntities($qb);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTag(string $userId, $name): Tag {
|
public function getTag(string $userId, string $name): Tag {
|
||||||
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?';
|
$qb = $this->db->getQueryBuilder();
|
||||||
return $this->findEntity($sql, [$userId, $name]);
|
$qb->select('*')
|
||||||
|
->from($this->getTableName())
|
||||||
|
->where(
|
||||||
|
$qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)),
|
||||||
|
$qb->expr()->eq('name', $qb->createNamedParameter($id, IQueryBuilder::PARAM_STR))
|
||||||
|
);
|
||||||
|
return $this->findEntity($qb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function tagExists(string $userId, $name): bool {
|
public function tagExists(string $userId, string $name): bool {
|
||||||
$sql = 'SELECT * FROM *PREFIX*quicknotes_tags WHERE user_id = ? AND name = ?';
|
|
||||||
try {
|
try {
|
||||||
$this->findEntities($sql, [$userId, $name]);
|
$this->getTag($userId, $name);
|
||||||
} catch (DoesNotExistException $e) {
|
} catch (DoesNotExistException $e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dropOld () {
|
public function dropOld (): void {
|
||||||
$sql = 'DELETE FROM *PREFIX*quicknotes_tags WHERE ';
|
$sub = $this->db->getQueryBuilder();
|
||||||
$sql.= 'id NOT IN (SELECT tag_id FROM *PREFIX*quicknotes_note_tags)';
|
$sub->select('tag_id')
|
||||||
$this->execute($sql, []);
|
->from('quicknotes_note_tags');
|
||||||
|
|
||||||
|
$qb = $this->db->getQueryBuilder();
|
||||||
|
$qb->delete($this->getTableName())
|
||||||
|
->where('id NOT IN (' . $sub->getSQL() . ')')
|
||||||
|
->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user