cache = new DbStatementCache($dbConn); } public function countNotes( UserInfo|string|null $userInfo = null, UserInfo|string|null $authorInfo = null ): int { if($userInfo instanceof UserInfo) $userInfo = $userInfo->id; if($authorInfo instanceof UserInfo) $authorInfo = $authorInfo->id; $hasUserInfo = $userInfo !== null; $hasAuthorInfo = $authorInfo !== null; $args = 0; $query = 'SELECT COUNT(*) FROM msz_users_modnotes'; if($hasUserInfo) { ++$args; $query .= ' WHERE user_id = ?'; } if($hasAuthorInfo) $query .= sprintf(' %s author_id = ?', ++$args > 1 ? 'AND' : 'WHERE'); $stmt = $this->cache->get($query); if($hasUserInfo) $stmt->nextParameter($userInfo); if($hasAuthorInfo) $stmt->nextParameter($authorInfo); $stmt->execute(); $result = $stmt->getResult(); $count = 0; if($result->next()) $count = $result->getInteger(0); return $count; } /** @return \Iterator */ public function getNotes( UserInfo|string|null $userInfo = null, UserInfo|string|null $authorInfo = null, ?Pagination $pagination = null ): iterable { if($userInfo instanceof UserInfo) $userInfo = $userInfo->id; if($authorInfo instanceof UserInfo) $authorInfo = $authorInfo->id; $hasUserInfo = $userInfo !== null; $hasAuthorInfo = $authorInfo !== null; $hasPagination = $pagination !== null; $args = 0; $query = 'SELECT note_id, user_id, author_id, UNIX_TIMESTAMP(note_created), note_title, note_body FROM msz_users_modnotes'; if($hasUserInfo) { ++$args; $query .= ' WHERE user_id = ?'; } if($hasAuthorInfo) $query .= sprintf(' %s author_id = ?', ++$args > 1 ? 'AND' : 'WHERE'); $query .= ' ORDER BY note_created DESC'; if($hasPagination) $query .= ' LIMIT ? OFFSET ?'; $stmt = $this->cache->get($query); if($hasUserInfo) $stmt->nextParameter($userInfo); if($hasAuthorInfo) $stmt->nextParameter($authorInfo); if($hasPagination) $pagination->addToStatement($stmt); $stmt->execute(); return $stmt->getResult()->getIterator(ModNoteInfo::fromResult(...)); } public function getNote(string $noteId): ModNoteInfo { $stmt = $this->cache->get('SELECT note_id, user_id, author_id, UNIX_TIMESTAMP(note_created), note_title, note_body FROM msz_users_modnotes WHERE note_id = ?'); $stmt->nextParameter($noteId); $stmt->execute(); $result = $stmt->getResult(); if(!$result->next()) throw new RuntimeException('No note with ID $noteId found.'); return ModNoteInfo::fromResult($result); } public function createNote( UserInfo|string $userInfo, string $title, string $body, UserInfo|string|null $authorInfo = null ): ModNoteInfo { if($userInfo instanceof UserInfo) $userInfo = $userInfo->id; if($authorInfo instanceof UserInfo) $authorInfo = $authorInfo->id; $stmt = $this->cache->get('INSERT INTO msz_users_modnotes (user_id, author_id, note_title, note_body) VALUES (?, ?, ?, ?)'); $stmt->nextParameter($userInfo); $stmt->nextParameter($authorInfo); $stmt->nextParameter($title); $stmt->nextParameter($body); $stmt->execute(); return $this->getNote((string)$this->dbConn->getLastInsertId()); } /** @param ModNoteInfo|string|array $noteInfos */ public function deleteNotes(ModNoteInfo|string|array $noteInfos): void { if(!is_array($noteInfos)) $noteInfos = [$noteInfos]; elseif(empty($noteInfos)) return; $stmt = $this->cache->get(sprintf( 'DELETE FROM msz_users_modnotes WHERE note_id IN (%s)', DbTools::prepareListString($noteInfos) )); foreach($noteInfos as $noteInfo) { if($noteInfo instanceof ModNoteInfo) $noteInfo = $noteInfo->id; elseif(!is_string($noteInfo)) throw new InvalidArgumentException('$noteInfos must be strings of instances of ModNoteInfo.'); $stmt->nextParameter($noteInfo); } $stmt->execute(); } public function updateNote( ModNoteInfo|string $noteInfo, ?string $title = null, ?string $body = null ): void { if($noteInfo instanceof ModNoteInfo) $noteInfo = $noteInfo->id; $stmt = $this->cache->get('UPDATE msz_users_modnotes SET note_title = COALESCE(?, note_title), note_body = COALESCE(?, note_body) WHERE note_id = ?'); $stmt->nextParameter($title); $stmt->nextParameter($body); $stmt->nextParameter($noteInfo); $stmt->execute(); } }