Skip to content

Commit

Permalink
Merge pull request #9444 from nextcloud/techdep/noid/appframework_map…
Browse files Browse the repository at this point in the history
…per_to_qb

Add a QueryBuilder based Mapper
  • Loading branch information
rullzer authored May 14, 2018
2 parents 8657f56 + ed7b483 commit 497a4fa
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'OCP\\AppFramework\\Db\\Entity' => $baseDir . '/lib/public/AppFramework/Db/Entity.php',
'OCP\\AppFramework\\Db\\Mapper' => $baseDir . '/lib/public/AppFramework/Db/Mapper.php',
'OCP\\AppFramework\\Db\\MultipleObjectsReturnedException' => $baseDir . '/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php',
'OCP\\AppFramework\\Db\\QBMapper' => $baseDir . '/lib/public/AppFramework/Db/QBMapper.php',
'OCP\\AppFramework\\Http' => $baseDir . '/lib/public/AppFramework/Http.php',
'OCP\\AppFramework\\Http\\ContentSecurityPolicy' => $baseDir . '/lib/public/AppFramework/Http/ContentSecurityPolicy.php',
'OCP\\AppFramework\\Http\\DataDisplayResponse' => $baseDir . '/lib/public/AppFramework/Http/DataDisplayResponse.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\AppFramework\\Db\\Entity' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/Entity.php',
'OCP\\AppFramework\\Db\\Mapper' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/Mapper.php',
'OCP\\AppFramework\\Db\\MultipleObjectsReturnedException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/MultipleObjectsReturnedException.php',
'OCP\\AppFramework\\Db\\QBMapper' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/QBMapper.php',
'OCP\\AppFramework\\Http' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http.php',
'OCP\\AppFramework\\Http\\ContentSecurityPolicy' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/ContentSecurityPolicy.php',
'OCP\\AppFramework\\Http\\DataDisplayResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/DataDisplayResponse.php',
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Authentication/Token/DefaultTokenMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;

class DefaultTokenMapper extends Mapper {
class DefaultTokenMapper extends QBMapper {

public function __construct(IDBConnection $db) {
parent::__construct($db, 'authtoken');
Expand Down
15 changes: 14 additions & 1 deletion lib/public/AppFramework/Db/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Simple parent class for inheriting your data access layer from. This class
* may be subject to change in the future
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
abstract class Mapper {

Expand All @@ -47,6 +48,7 @@ abstract class Mapper {
* @param string $entityClass the name of the entity that the sql should be
* mapped to queries without using sql
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
public function __construct(IDBConnection $db, $tableName, $entityClass=null){
$this->db = $db;
Expand All @@ -65,6 +67,7 @@ public function __construct(IDBConnection $db, $tableName, $entityClass=null){
/**
* @return string the table name
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
public function getTableName(){
return $this->tableName;
Expand All @@ -76,6 +79,7 @@ public function getTableName(){
* @param Entity $entity the entity that should be deleted
* @return Entity the deleted entity
* @since 7.0.0 - return value added in 8.1.0
* @deprecated 14.0.0 Move over to QBMapper
*/
public function delete(Entity $entity){
$sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?';
Expand All @@ -90,6 +94,7 @@ public function delete(Entity $entity){
* @param Entity $entity the entity that should be created
* @return Entity the saved entity with the set id
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
public function insert(Entity $entity){
// get updated fields to save, fields have to be set using a setter to
Expand Down Expand Up @@ -139,6 +144,7 @@ public function insert(Entity $entity){
* @param Entity $entity the entity that should be created
* @return Entity the saved entity with the set id
* @since 7.0.0 - return value was added in 8.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
public function update(Entity $entity){
// if entity wasn't changed it makes no sense to run a db query
Expand Down Expand Up @@ -195,6 +201,7 @@ public function update(Entity $entity){
* @param array $array
* @return bool true if associative
* @since 8.1.0
* @deprecated 14.0.0 Move over to QBMapper
*/
private function isAssocArray(array $array) {
return array_values($array) !== $array;
Expand All @@ -205,6 +212,7 @@ private function isAssocArray(array $array) {
* @param $value
* @return int PDO constant
* @since 8.1.0
* @deprecated 14.0.0 Move over to QBMapper
*/
private function getPDOType($value) {
switch (gettype($value)) {
Expand All @@ -226,6 +234,7 @@ private function getPDOType($value) {
* @param int $offset from which row we want to start
* @return \PDOStatement the database query result
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
protected function execute($sql, array $params=[], $limit=null, $offset=null){
$query = $this->db->prepare($sql, $limit, $offset);
Expand All @@ -249,7 +258,6 @@ protected function execute($sql, array $params=[], $limit=null, $offset=null){
return $query;
}


/**
* Returns an db result and throws exceptions when there are more or less
* results
Expand All @@ -262,6 +270,7 @@ protected function execute($sql, array $params=[], $limit=null, $offset=null){
* @throws MultipleObjectsReturnedException if more than one item exist
* @return array the result as row
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){
$stmt = $this->execute($sql, $params, $limit, $offset);
Expand Down Expand Up @@ -297,6 +306,7 @@ protected function findOneQuery($sql, array $params=[], $limit=null, $offset=nul
* @param int $offset from which row we want to start
* @return string formatted error message string
* @since 9.1.0
* @deprecated 14.0.0 Move over to QBMapper
*/
private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) {
return $msg .
Expand All @@ -313,6 +323,7 @@ private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $o
* @param array $row the row which should be converted to an entity
* @return Entity the entity
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
protected function mapRowToEntity($row) {
return call_user_func($this->entityClass .'::fromRow', $row);
Expand All @@ -327,6 +338,7 @@ protected function mapRowToEntity($row) {
* @param int $offset from which row we want to start
* @return array all fetched entities
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
protected function findEntities($sql, array $params=[], $limit=null, $offset=null) {
$stmt = $this->execute($sql, $params, $limit, $offset);
Expand Down Expand Up @@ -354,6 +366,7 @@ protected function findEntities($sql, array $params=[], $limit=null, $offset=nul
* @throws MultipleObjectsReturnedException if more than one item exist
* @return Entity the entity
* @since 7.0.0
* @deprecated 14.0.0 Move over to QBMapper
*/
protected function findEntity($sql, array $params=[], $limit=null, $offset=null){
return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset));
Expand Down
Loading

0 comments on commit 497a4fa

Please sign in to comment.