Move to QBMapper

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma
2018-05-14 21:32:07 +02:00
parent 4828552200
commit 771761edd2

View File

@@ -26,7 +26,7 @@ Inside your database layer class you can now start running queries like:
$this->db = $db;
}
public function find($id) {
public function find(int $id) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
@@ -66,10 +66,11 @@ To create a mapper, inherit from the mapper base class and call the parent const
namespace OCA\MyApp\Db;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
class AuthorMapper extends Mapper {
class AuthorMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'myapp_authors');
@@ -80,26 +81,44 @@ To create a mapper, inherit from the mapper base class and call the parent const
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
*/
public function find($id) {
$sql = 'SELECT * FROM `*PREFIX*myapp_authors` ' .
'WHERE `id` = ?';
return $this->findEntity($sql, [$id]);
public function find(int $id) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('myapp_authors')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);
return $this->findEntity($qb);
}
public function findAll($limit=null, $offset=null) {
$sql = 'SELECT * FROM `*PREFIX*myapp_authors`';
return $this->findEntities($sql, $limit, $offset);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('myapp_authors')
->setMaxResults($limit)
->setFirstResult($offset);
return $this->findEntities($sql);
}
public function authorNameCount($name) {
$sql = 'SELECT COUNT(*) AS `count` FROM `*PREFIX*myapp_authors` ' .
'WHERE `name` = ?';
$stmt = $this->execute($sql, [$name]);
$qb = $this->db->getQueryBuilder();
$qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
->from('myapp_authors')
->where(
$qb->expr()->eq('name', $qb->createNamedParameter($name, IQueryBuilder::PARAM_STR))
);
$cursor = $qb->execute();
$row = $cursor->fetch();
$cursor->closeCursor();
$row = $stmt->fetch();
$stmt->closeCursor();
return $row['count'];
}