@@ -31,47 +31,47 @@ |
||
31 | 31 | */ |
32 | 32 | class Result { |
33 | 33 | |
34 | - /** |
|
35 | - * A unique identifier for the result, usually given as the item ID in its |
|
36 | - * corresponding application. |
|
37 | - * @var string |
|
38 | - * @since 7.0.0 |
|
39 | - */ |
|
40 | - public $id; |
|
34 | + /** |
|
35 | + * A unique identifier for the result, usually given as the item ID in its |
|
36 | + * corresponding application. |
|
37 | + * @var string |
|
38 | + * @since 7.0.0 |
|
39 | + */ |
|
40 | + public $id; |
|
41 | 41 | |
42 | - /** |
|
43 | - * The name of the item returned; this will be displayed in the search |
|
44 | - * results. |
|
45 | - * @var string |
|
46 | - * @since 7.0.0 |
|
47 | - */ |
|
48 | - public $name; |
|
42 | + /** |
|
43 | + * The name of the item returned; this will be displayed in the search |
|
44 | + * results. |
|
45 | + * @var string |
|
46 | + * @since 7.0.0 |
|
47 | + */ |
|
48 | + public $name; |
|
49 | 49 | |
50 | - /** |
|
51 | - * URL to the application item. |
|
52 | - * @var string |
|
53 | - * @since 7.0.0 |
|
54 | - */ |
|
55 | - public $link; |
|
50 | + /** |
|
51 | + * URL to the application item. |
|
52 | + * @var string |
|
53 | + * @since 7.0.0 |
|
54 | + */ |
|
55 | + public $link; |
|
56 | 56 | |
57 | - /** |
|
58 | - * The type of search result returned; for consistency, name this the same |
|
59 | - * as the class name (e.g. \OC\Search\File -> 'file') in lowercase. |
|
60 | - * @var string |
|
61 | - * @since 7.0.0 |
|
62 | - */ |
|
63 | - public $type = 'generic'; |
|
57 | + /** |
|
58 | + * The type of search result returned; for consistency, name this the same |
|
59 | + * as the class name (e.g. \OC\Search\File -> 'file') in lowercase. |
|
60 | + * @var string |
|
61 | + * @since 7.0.0 |
|
62 | + */ |
|
63 | + public $type = 'generic'; |
|
64 | 64 | |
65 | - /** |
|
66 | - * Create a new search result |
|
67 | - * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' |
|
68 | - * @param string $name displayed text of result |
|
69 | - * @param string $link URL to the result within its app |
|
70 | - * @since 7.0.0 |
|
71 | - */ |
|
72 | - public function __construct($id = null, $name = null, $link = null) { |
|
73 | - $this->id = $id; |
|
74 | - $this->name = $name; |
|
75 | - $this->link = $link; |
|
76 | - } |
|
65 | + /** |
|
66 | + * Create a new search result |
|
67 | + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' |
|
68 | + * @param string $name displayed text of result |
|
69 | + * @param string $link URL to the result within its app |
|
70 | + * @since 7.0.0 |
|
71 | + */ |
|
72 | + public function __construct($id = null, $name = null, $link = null) { |
|
73 | + $this->id = $id; |
|
74 | + $this->name = $name; |
|
75 | + $this->link = $link; |
|
76 | + } |
|
77 | 77 | } |
@@ -28,40 +28,40 @@ |
||
28 | 28 | |
29 | 29 | class HomeObjectStoreStorage extends ObjectStoreStorage implements \OCP\Files\IHomeStorage { |
30 | 30 | |
31 | - /** |
|
32 | - * The home user storage requires a user object to create a unique storage id |
|
33 | - * @param array $params |
|
34 | - */ |
|
35 | - public function __construct($params) { |
|
36 | - if (! isset($params['user']) || ! $params['user'] instanceof User) { |
|
37 | - throw new \Exception('missing user object in parameters'); |
|
38 | - } |
|
39 | - $this->user = $params['user']; |
|
40 | - parent::__construct($params); |
|
41 | - } |
|
31 | + /** |
|
32 | + * The home user storage requires a user object to create a unique storage id |
|
33 | + * @param array $params |
|
34 | + */ |
|
35 | + public function __construct($params) { |
|
36 | + if (! isset($params['user']) || ! $params['user'] instanceof User) { |
|
37 | + throw new \Exception('missing user object in parameters'); |
|
38 | + } |
|
39 | + $this->user = $params['user']; |
|
40 | + parent::__construct($params); |
|
41 | + } |
|
42 | 42 | |
43 | - public function getId() { |
|
44 | - return 'object::user:' . $this->user->getUID(); |
|
45 | - } |
|
43 | + public function getId() { |
|
44 | + return 'object::user:' . $this->user->getUID(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * get the owner of a path |
|
49 | - * |
|
50 | - * @param string $path The path to get the owner |
|
51 | - * @return false|string uid |
|
52 | - */ |
|
53 | - public function getOwner($path) { |
|
54 | - if (is_object($this->user)) { |
|
55 | - return $this->user->getUID(); |
|
56 | - } |
|
57 | - return false; |
|
58 | - } |
|
47 | + /** |
|
48 | + * get the owner of a path |
|
49 | + * |
|
50 | + * @param string $path The path to get the owner |
|
51 | + * @return false|string uid |
|
52 | + */ |
|
53 | + public function getOwner($path) { |
|
54 | + if (is_object($this->user)) { |
|
55 | + return $this->user->getUID(); |
|
56 | + } |
|
57 | + return false; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $path, optional |
|
62 | - * @return \OC\User\User |
|
63 | - */ |
|
64 | - public function getUser($path = null) { |
|
65 | - return $this->user; |
|
66 | - } |
|
60 | + /** |
|
61 | + * @param string $path, optional |
|
62 | + * @return \OC\User\User |
|
63 | + */ |
|
64 | + public function getUser($path = null) { |
|
65 | + return $this->user; |
|
66 | + } |
|
67 | 67 | } |
@@ -29,13 +29,13 @@ |
||
29 | 29 | */ |
30 | 30 | class Audio extends File { |
31 | 31 | |
32 | - /** |
|
33 | - * Type name; translated in templates |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $type = 'audio'; |
|
32 | + /** |
|
33 | + * Type name; translated in templates |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $type = 'audio'; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @TODO add ID3 information |
|
40 | - */ |
|
38 | + /** |
|
39 | + * @TODO add ID3 information |
|
40 | + */ |
|
41 | 41 | } |
@@ -29,13 +29,13 @@ |
||
29 | 29 | */ |
30 | 30 | class Image extends File { |
31 | 31 | |
32 | - /** |
|
33 | - * Type name; translated in templates |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $type = 'image'; |
|
32 | + /** |
|
33 | + * Type name; translated in templates |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $type = 'image'; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @TODO add EXIF information |
|
40 | - */ |
|
38 | + /** |
|
39 | + * @TODO add EXIF information |
|
40 | + */ |
|
41 | 41 | } |
@@ -29,9 +29,9 @@ |
||
29 | 29 | */ |
30 | 30 | class Folder extends File { |
31 | 31 | |
32 | - /** |
|
33 | - * Type name; translated in templates |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $type = 'folder'; |
|
32 | + /** |
|
33 | + * Type name; translated in templates |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $type = 'folder'; |
|
37 | 37 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | print_unescaped($l->t("Hey there,\n\nthe admin enabled server-side-encryption. Your files were encrypted using the password '%s'.\n\nPlease login to the web interface, go to the section 'basic encryption module' of your personal settings and update your encryption password by entering this password into the 'old log-in password' field and your current login-password.\n\n", [$_['password']])); |
6 | 6 | if (isset($_['expiration'])) { |
7 | - print_unescaped($l->t("The share will expire on %s.", [$_['expiration']])); |
|
8 | - print_unescaped("\n\n"); |
|
7 | + print_unescaped($l->t("The share will expire on %s.", [$_['expiration']])); |
|
8 | + print_unescaped("\n\n"); |
|
9 | 9 | } |
10 | 10 | // TRANSLATORS term at the end of a mail |
11 | 11 | p($l->t("Cheers!")); |
@@ -48,73 +48,73 @@ |
||
48 | 48 | * @deprecated 14.0.0 |
49 | 49 | */ |
50 | 50 | class Files { |
51 | - /** |
|
52 | - * Recusive deletion of folders |
|
53 | - * @return bool |
|
54 | - * @since 5.0.0 |
|
55 | - * @deprecated 14.0.0 |
|
56 | - */ |
|
57 | - static public function rmdirr($dir) { |
|
58 | - return \OC_Helper::rmdirr($dir); |
|
59 | - } |
|
51 | + /** |
|
52 | + * Recusive deletion of folders |
|
53 | + * @return bool |
|
54 | + * @since 5.0.0 |
|
55 | + * @deprecated 14.0.0 |
|
56 | + */ |
|
57 | + static public function rmdirr($dir) { |
|
58 | + return \OC_Helper::rmdirr($dir); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Get the mimetype form a local file |
|
63 | - * @param string $path |
|
64 | - * @return string |
|
65 | - * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
|
66 | - * @since 5.0.0 |
|
67 | - * @deprecated 14.0.0 |
|
68 | - */ |
|
69 | - static public function getMimeType($path) { |
|
70 | - return \OC::$server->getMimeTypeDetector()->detect($path); |
|
71 | - } |
|
61 | + /** |
|
62 | + * Get the mimetype form a local file |
|
63 | + * @param string $path |
|
64 | + * @return string |
|
65 | + * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead |
|
66 | + * @since 5.0.0 |
|
67 | + * @deprecated 14.0.0 |
|
68 | + */ |
|
69 | + static public function getMimeType($path) { |
|
70 | + return \OC::$server->getMimeTypeDetector()->detect($path); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Search for files by mimetype |
|
75 | - * @param string $mimetype |
|
76 | - * @return array |
|
77 | - * @since 6.0.0 |
|
78 | - * @deprecated 14.0.0 |
|
79 | - */ |
|
80 | - static public function searchByMime($mimetype) { |
|
81 | - return \OC\Files\Filesystem::searchByMime($mimetype); |
|
82 | - } |
|
73 | + /** |
|
74 | + * Search for files by mimetype |
|
75 | + * @param string $mimetype |
|
76 | + * @return array |
|
77 | + * @since 6.0.0 |
|
78 | + * @deprecated 14.0.0 |
|
79 | + */ |
|
80 | + static public function searchByMime($mimetype) { |
|
81 | + return \OC\Files\Filesystem::searchByMime($mimetype); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Copy the contents of one stream to another |
|
86 | - * @param resource $source |
|
87 | - * @param resource $target |
|
88 | - * @return int the number of bytes copied |
|
89 | - * @since 5.0.0 |
|
90 | - * @deprecated 14.0.0 |
|
91 | - */ |
|
92 | - public static function streamCopy($source, $target) { |
|
93 | - list($count, ) = \OC_Helper::streamCopy($source, $target); |
|
94 | - return $count; |
|
95 | - } |
|
84 | + /** |
|
85 | + * Copy the contents of one stream to another |
|
86 | + * @param resource $source |
|
87 | + * @param resource $target |
|
88 | + * @return int the number of bytes copied |
|
89 | + * @since 5.0.0 |
|
90 | + * @deprecated 14.0.0 |
|
91 | + */ |
|
92 | + public static function streamCopy($source, $target) { |
|
93 | + list($count, ) = \OC_Helper::streamCopy($source, $target); |
|
94 | + return $count; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Adds a suffix to the name in case the file exists |
|
99 | - * @param string $path |
|
100 | - * @param string $filename |
|
101 | - * @return string |
|
102 | - * @since 5.0.0 |
|
103 | - * @deprecated 14.0.0 use getNonExistingName of the OCP\Files\Folder object |
|
104 | - */ |
|
105 | - public static function buildNotExistingFileName($path, $filename) { |
|
106 | - return \OC_Helper::buildNotExistingFileName($path, $filename); |
|
107 | - } |
|
97 | + /** |
|
98 | + * Adds a suffix to the name in case the file exists |
|
99 | + * @param string $path |
|
100 | + * @param string $filename |
|
101 | + * @return string |
|
102 | + * @since 5.0.0 |
|
103 | + * @deprecated 14.0.0 use getNonExistingName of the OCP\Files\Folder object |
|
104 | + */ |
|
105 | + public static function buildNotExistingFileName($path, $filename) { |
|
106 | + return \OC_Helper::buildNotExistingFileName($path, $filename); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Gets the Storage for an app - creates the needed folder if they are not |
|
111 | - * existent |
|
112 | - * @param string $app |
|
113 | - * @return \OC\Files\View |
|
114 | - * @since 5.0.0 |
|
115 | - * @deprecated 14.0.0 use IAppData instead |
|
116 | - */ |
|
117 | - public static function getStorage($app) { |
|
118 | - return \OC_App::getStorage($app); |
|
119 | - } |
|
109 | + /** |
|
110 | + * Gets the Storage for an app - creates the needed folder if they are not |
|
111 | + * existent |
|
112 | + * @param string $app |
|
113 | + * @return \OC\Files\View |
|
114 | + * @since 5.0.0 |
|
115 | + * @deprecated 14.0.0 use IAppData instead |
|
116 | + */ |
|
117 | + public static function getStorage($app) { |
|
118 | + return \OC_App::getStorage($app); |
|
119 | + } |
|
120 | 120 | } |
@@ -42,294 +42,294 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class QBMapper { |
44 | 44 | |
45 | - /** @var string */ |
|
46 | - protected $tableName; |
|
47 | - |
|
48 | - /** @var string */ |
|
49 | - protected $entityClass; |
|
50 | - |
|
51 | - /** @var IDBConnection */ |
|
52 | - protected $db; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
56 | - * @param string $tableName the name of the table. set this to allow entity |
|
57 | - * @param string $entityClass the name of the entity that the sql should be |
|
58 | - * mapped to queries without using sql |
|
59 | - * @since 14.0.0 |
|
60 | - */ |
|
61 | - public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) { |
|
62 | - $this->db = $db; |
|
63 | - $this->tableName = $tableName; |
|
64 | - |
|
65 | - // if not given set the entity name to the class without the mapper part |
|
66 | - // cache it here for later use since reflection is slow |
|
67 | - if($entityClass === null) { |
|
68 | - $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
69 | - } else { |
|
70 | - $this->entityClass = $entityClass; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @return string the table name |
|
77 | - * @since 14.0.0 |
|
78 | - */ |
|
79 | - public function getTableName(): string { |
|
80 | - return $this->tableName; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Deletes an entity from the table |
|
86 | - * @param Entity $entity the entity that should be deleted |
|
87 | - * @return Entity the deleted entity |
|
88 | - * @since 14.0.0 |
|
89 | - */ |
|
90 | - public function delete(Entity $entity): Entity { |
|
91 | - $qb = $this->db->getQueryBuilder(); |
|
92 | - |
|
93 | - $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
94 | - |
|
95 | - $qb->delete($this->tableName) |
|
96 | - ->where( |
|
97 | - $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId(), $idType)) |
|
98 | - ); |
|
99 | - $qb->execute(); |
|
100 | - return $entity; |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * Creates a new entry in the db from an entity |
|
106 | - * @param Entity $entity the entity that should be created |
|
107 | - * @return Entity the saved entity with the set id |
|
108 | - * @since 14.0.0 |
|
109 | - * @suppress SqlInjectionChecker |
|
110 | - */ |
|
111 | - public function insert(Entity $entity): Entity { |
|
112 | - // get updated fields to save, fields have to be set using a setter to |
|
113 | - // be saved |
|
114 | - $properties = $entity->getUpdatedFields(); |
|
115 | - |
|
116 | - $qb = $this->db->getQueryBuilder(); |
|
117 | - $qb->insert($this->tableName); |
|
118 | - |
|
119 | - // build the fields |
|
120 | - foreach($properties as $property => $updated) { |
|
121 | - $column = $entity->propertyToColumn($property); |
|
122 | - $getter = 'get' . ucfirst($property); |
|
123 | - $value = $entity->$getter(); |
|
124 | - |
|
125 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
126 | - $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
127 | - } |
|
128 | - |
|
129 | - $qb->execute(); |
|
130 | - |
|
131 | - if($entity->id === null) { |
|
132 | - // When autoincrement is used id is always an int |
|
133 | - $entity->setId((int)$qb->getLastInsertId()); |
|
134 | - } |
|
135 | - |
|
136 | - return $entity; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Tries to creates a new entry in the db from an entity and |
|
141 | - * updates an existing entry if duplicate keys are detected |
|
142 | - * by the database |
|
143 | - * |
|
144 | - * @param Entity $entity the entity that should be created/updated |
|
145 | - * @return Entity the saved entity with the (new) id |
|
146 | - * @throws \InvalidArgumentException if entity has no id |
|
147 | - * @since 15.0.0 |
|
148 | - * @suppress SqlInjectionChecker |
|
149 | - */ |
|
150 | - public function insertOrUpdate(Entity $entity): Entity { |
|
151 | - try { |
|
152 | - return $this->insert($entity); |
|
153 | - } catch (UniqueConstraintViolationException $ex) { |
|
154 | - return $this->update($entity); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Updates an entry in the db from an entity |
|
160 | - * @throws \InvalidArgumentException if entity has no id |
|
161 | - * @param Entity $entity the entity that should be created |
|
162 | - * @return Entity the saved entity with the set id |
|
163 | - * @since 14.0.0 |
|
164 | - * @suppress SqlInjectionChecker |
|
165 | - */ |
|
166 | - public function update(Entity $entity): Entity { |
|
167 | - // if entity wasn't changed it makes no sense to run a db query |
|
168 | - $properties = $entity->getUpdatedFields(); |
|
169 | - if(\count($properties) === 0) { |
|
170 | - return $entity; |
|
171 | - } |
|
172 | - |
|
173 | - // entity needs an id |
|
174 | - $id = $entity->getId(); |
|
175 | - if($id === null){ |
|
176 | - throw new \InvalidArgumentException( |
|
177 | - 'Entity which should be updated has no id'); |
|
178 | - } |
|
179 | - |
|
180 | - // get updated fields to save, fields have to be set using a setter to |
|
181 | - // be saved |
|
182 | - // do not update the id field |
|
183 | - unset($properties['id']); |
|
184 | - |
|
185 | - $qb = $this->db->getQueryBuilder(); |
|
186 | - $qb->update($this->tableName); |
|
187 | - |
|
188 | - // build the fields |
|
189 | - foreach($properties as $property => $updated) { |
|
190 | - $column = $entity->propertyToColumn($property); |
|
191 | - $getter = 'get' . ucfirst($property); |
|
192 | - $value = $entity->$getter(); |
|
193 | - |
|
194 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
195 | - $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
196 | - } |
|
197 | - |
|
198 | - $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
199 | - |
|
200 | - $qb->where( |
|
201 | - $qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)) |
|
202 | - ); |
|
203 | - $qb->execute(); |
|
204 | - |
|
205 | - return $entity; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Returns the type parameter for the QueryBuilder for a specific property |
|
210 | - * of the $entity |
|
211 | - * |
|
212 | - * @param Entity $entity The entity to get the types from |
|
213 | - * @param string $property The property of $entity to get the type for |
|
214 | - * @return int |
|
215 | - * @since 16.0.0 |
|
216 | - */ |
|
217 | - protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
218 | - $types = $entity->getFieldTypes(); |
|
219 | - |
|
220 | - if(!isset($types[ $property ])) { |
|
221 | - return IQueryBuilder::PARAM_STR; |
|
222 | - } |
|
223 | - |
|
224 | - switch($types[ $property ]) { |
|
225 | - case 'int': |
|
226 | - case 'integer': |
|
227 | - return IQueryBuilder::PARAM_INT; |
|
228 | - case 'string': |
|
229 | - return IQueryBuilder::PARAM_STR; |
|
230 | - case 'bool': |
|
231 | - case 'boolean': |
|
232 | - return IQueryBuilder::PARAM_BOOL; |
|
233 | - } |
|
234 | - |
|
235 | - return IQueryBuilder::PARAM_STR; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Returns an db result and throws exceptions when there are more or less |
|
240 | - * results |
|
241 | - * |
|
242 | - * @see findEntity |
|
243 | - * |
|
244 | - * @param IQueryBuilder $query |
|
245 | - * @throws DoesNotExistException if the item does not exist |
|
246 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
247 | - * @return array the result as row |
|
248 | - * @since 14.0.0 |
|
249 | - */ |
|
250 | - protected function findOneQuery(IQueryBuilder $query): array { |
|
251 | - $cursor = $query->execute(); |
|
252 | - |
|
253 | - $row = $cursor->fetch(); |
|
254 | - if($row === false) { |
|
255 | - $cursor->closeCursor(); |
|
256 | - $msg = $this->buildDebugMessage( |
|
257 | - 'Did expect one result but found none when executing', $query |
|
258 | - ); |
|
259 | - throw new DoesNotExistException($msg); |
|
260 | - } |
|
261 | - |
|
262 | - $row2 = $cursor->fetch(); |
|
263 | - $cursor->closeCursor(); |
|
264 | - if($row2 !== false) { |
|
265 | - $msg = $this->buildDebugMessage( |
|
266 | - 'Did not expect more than one result when executing', $query |
|
267 | - ); |
|
268 | - throw new MultipleObjectsReturnedException($msg); |
|
269 | - } |
|
270 | - |
|
271 | - return $row; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @param string $msg |
|
276 | - * @param IQueryBuilder $sql |
|
277 | - * @return string |
|
278 | - * @since 14.0.0 |
|
279 | - */ |
|
280 | - private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
281 | - return $msg . |
|
282 | - ': query "' . $sql->getSQL() . '"; '; |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * Creates an entity from a row. Automatically determines the entity class |
|
288 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
289 | - * |
|
290 | - * @param array $row the row which should be converted to an entity |
|
291 | - * @return Entity the entity |
|
292 | - * @since 14.0.0 |
|
293 | - */ |
|
294 | - protected function mapRowToEntity(array $row): Entity { |
|
295 | - return \call_user_func($this->entityClass .'::fromRow', $row); |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * Runs a sql query and returns an array of entities |
|
301 | - * |
|
302 | - * @param IQueryBuilder $query |
|
303 | - * @return Entity[] all fetched entities |
|
304 | - * @since 14.0.0 |
|
305 | - */ |
|
306 | - protected function findEntities(IQueryBuilder $query): array { |
|
307 | - $cursor = $query->execute(); |
|
308 | - |
|
309 | - $entities = []; |
|
310 | - |
|
311 | - while($row = $cursor->fetch()){ |
|
312 | - $entities[] = $this->mapRowToEntity($row); |
|
313 | - } |
|
314 | - |
|
315 | - $cursor->closeCursor(); |
|
316 | - |
|
317 | - return $entities; |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Returns an db result and throws exceptions when there are more or less |
|
323 | - * results |
|
324 | - * |
|
325 | - * @param IQueryBuilder $query |
|
326 | - * @throws DoesNotExistException if the item does not exist |
|
327 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
328 | - * @return Entity the entity |
|
329 | - * @since 14.0.0 |
|
330 | - */ |
|
331 | - protected function findEntity(IQueryBuilder $query): Entity { |
|
332 | - return $this->mapRowToEntity($this->findOneQuery($query)); |
|
333 | - } |
|
45 | + /** @var string */ |
|
46 | + protected $tableName; |
|
47 | + |
|
48 | + /** @var string */ |
|
49 | + protected $entityClass; |
|
50 | + |
|
51 | + /** @var IDBConnection */ |
|
52 | + protected $db; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
56 | + * @param string $tableName the name of the table. set this to allow entity |
|
57 | + * @param string $entityClass the name of the entity that the sql should be |
|
58 | + * mapped to queries without using sql |
|
59 | + * @since 14.0.0 |
|
60 | + */ |
|
61 | + public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) { |
|
62 | + $this->db = $db; |
|
63 | + $this->tableName = $tableName; |
|
64 | + |
|
65 | + // if not given set the entity name to the class without the mapper part |
|
66 | + // cache it here for later use since reflection is slow |
|
67 | + if($entityClass === null) { |
|
68 | + $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
69 | + } else { |
|
70 | + $this->entityClass = $entityClass; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @return string the table name |
|
77 | + * @since 14.0.0 |
|
78 | + */ |
|
79 | + public function getTableName(): string { |
|
80 | + return $this->tableName; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Deletes an entity from the table |
|
86 | + * @param Entity $entity the entity that should be deleted |
|
87 | + * @return Entity the deleted entity |
|
88 | + * @since 14.0.0 |
|
89 | + */ |
|
90 | + public function delete(Entity $entity): Entity { |
|
91 | + $qb = $this->db->getQueryBuilder(); |
|
92 | + |
|
93 | + $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
94 | + |
|
95 | + $qb->delete($this->tableName) |
|
96 | + ->where( |
|
97 | + $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId(), $idType)) |
|
98 | + ); |
|
99 | + $qb->execute(); |
|
100 | + return $entity; |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * Creates a new entry in the db from an entity |
|
106 | + * @param Entity $entity the entity that should be created |
|
107 | + * @return Entity the saved entity with the set id |
|
108 | + * @since 14.0.0 |
|
109 | + * @suppress SqlInjectionChecker |
|
110 | + */ |
|
111 | + public function insert(Entity $entity): Entity { |
|
112 | + // get updated fields to save, fields have to be set using a setter to |
|
113 | + // be saved |
|
114 | + $properties = $entity->getUpdatedFields(); |
|
115 | + |
|
116 | + $qb = $this->db->getQueryBuilder(); |
|
117 | + $qb->insert($this->tableName); |
|
118 | + |
|
119 | + // build the fields |
|
120 | + foreach($properties as $property => $updated) { |
|
121 | + $column = $entity->propertyToColumn($property); |
|
122 | + $getter = 'get' . ucfirst($property); |
|
123 | + $value = $entity->$getter(); |
|
124 | + |
|
125 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
126 | + $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
127 | + } |
|
128 | + |
|
129 | + $qb->execute(); |
|
130 | + |
|
131 | + if($entity->id === null) { |
|
132 | + // When autoincrement is used id is always an int |
|
133 | + $entity->setId((int)$qb->getLastInsertId()); |
|
134 | + } |
|
135 | + |
|
136 | + return $entity; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Tries to creates a new entry in the db from an entity and |
|
141 | + * updates an existing entry if duplicate keys are detected |
|
142 | + * by the database |
|
143 | + * |
|
144 | + * @param Entity $entity the entity that should be created/updated |
|
145 | + * @return Entity the saved entity with the (new) id |
|
146 | + * @throws \InvalidArgumentException if entity has no id |
|
147 | + * @since 15.0.0 |
|
148 | + * @suppress SqlInjectionChecker |
|
149 | + */ |
|
150 | + public function insertOrUpdate(Entity $entity): Entity { |
|
151 | + try { |
|
152 | + return $this->insert($entity); |
|
153 | + } catch (UniqueConstraintViolationException $ex) { |
|
154 | + return $this->update($entity); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Updates an entry in the db from an entity |
|
160 | + * @throws \InvalidArgumentException if entity has no id |
|
161 | + * @param Entity $entity the entity that should be created |
|
162 | + * @return Entity the saved entity with the set id |
|
163 | + * @since 14.0.0 |
|
164 | + * @suppress SqlInjectionChecker |
|
165 | + */ |
|
166 | + public function update(Entity $entity): Entity { |
|
167 | + // if entity wasn't changed it makes no sense to run a db query |
|
168 | + $properties = $entity->getUpdatedFields(); |
|
169 | + if(\count($properties) === 0) { |
|
170 | + return $entity; |
|
171 | + } |
|
172 | + |
|
173 | + // entity needs an id |
|
174 | + $id = $entity->getId(); |
|
175 | + if($id === null){ |
|
176 | + throw new \InvalidArgumentException( |
|
177 | + 'Entity which should be updated has no id'); |
|
178 | + } |
|
179 | + |
|
180 | + // get updated fields to save, fields have to be set using a setter to |
|
181 | + // be saved |
|
182 | + // do not update the id field |
|
183 | + unset($properties['id']); |
|
184 | + |
|
185 | + $qb = $this->db->getQueryBuilder(); |
|
186 | + $qb->update($this->tableName); |
|
187 | + |
|
188 | + // build the fields |
|
189 | + foreach($properties as $property => $updated) { |
|
190 | + $column = $entity->propertyToColumn($property); |
|
191 | + $getter = 'get' . ucfirst($property); |
|
192 | + $value = $entity->$getter(); |
|
193 | + |
|
194 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
195 | + $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
196 | + } |
|
197 | + |
|
198 | + $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
199 | + |
|
200 | + $qb->where( |
|
201 | + $qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)) |
|
202 | + ); |
|
203 | + $qb->execute(); |
|
204 | + |
|
205 | + return $entity; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Returns the type parameter for the QueryBuilder for a specific property |
|
210 | + * of the $entity |
|
211 | + * |
|
212 | + * @param Entity $entity The entity to get the types from |
|
213 | + * @param string $property The property of $entity to get the type for |
|
214 | + * @return int |
|
215 | + * @since 16.0.0 |
|
216 | + */ |
|
217 | + protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
218 | + $types = $entity->getFieldTypes(); |
|
219 | + |
|
220 | + if(!isset($types[ $property ])) { |
|
221 | + return IQueryBuilder::PARAM_STR; |
|
222 | + } |
|
223 | + |
|
224 | + switch($types[ $property ]) { |
|
225 | + case 'int': |
|
226 | + case 'integer': |
|
227 | + return IQueryBuilder::PARAM_INT; |
|
228 | + case 'string': |
|
229 | + return IQueryBuilder::PARAM_STR; |
|
230 | + case 'bool': |
|
231 | + case 'boolean': |
|
232 | + return IQueryBuilder::PARAM_BOOL; |
|
233 | + } |
|
234 | + |
|
235 | + return IQueryBuilder::PARAM_STR; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Returns an db result and throws exceptions when there are more or less |
|
240 | + * results |
|
241 | + * |
|
242 | + * @see findEntity |
|
243 | + * |
|
244 | + * @param IQueryBuilder $query |
|
245 | + * @throws DoesNotExistException if the item does not exist |
|
246 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
247 | + * @return array the result as row |
|
248 | + * @since 14.0.0 |
|
249 | + */ |
|
250 | + protected function findOneQuery(IQueryBuilder $query): array { |
|
251 | + $cursor = $query->execute(); |
|
252 | + |
|
253 | + $row = $cursor->fetch(); |
|
254 | + if($row === false) { |
|
255 | + $cursor->closeCursor(); |
|
256 | + $msg = $this->buildDebugMessage( |
|
257 | + 'Did expect one result but found none when executing', $query |
|
258 | + ); |
|
259 | + throw new DoesNotExistException($msg); |
|
260 | + } |
|
261 | + |
|
262 | + $row2 = $cursor->fetch(); |
|
263 | + $cursor->closeCursor(); |
|
264 | + if($row2 !== false) { |
|
265 | + $msg = $this->buildDebugMessage( |
|
266 | + 'Did not expect more than one result when executing', $query |
|
267 | + ); |
|
268 | + throw new MultipleObjectsReturnedException($msg); |
|
269 | + } |
|
270 | + |
|
271 | + return $row; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @param string $msg |
|
276 | + * @param IQueryBuilder $sql |
|
277 | + * @return string |
|
278 | + * @since 14.0.0 |
|
279 | + */ |
|
280 | + private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
281 | + return $msg . |
|
282 | + ': query "' . $sql->getSQL() . '"; '; |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * Creates an entity from a row. Automatically determines the entity class |
|
288 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
289 | + * |
|
290 | + * @param array $row the row which should be converted to an entity |
|
291 | + * @return Entity the entity |
|
292 | + * @since 14.0.0 |
|
293 | + */ |
|
294 | + protected function mapRowToEntity(array $row): Entity { |
|
295 | + return \call_user_func($this->entityClass .'::fromRow', $row); |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * Runs a sql query and returns an array of entities |
|
301 | + * |
|
302 | + * @param IQueryBuilder $query |
|
303 | + * @return Entity[] all fetched entities |
|
304 | + * @since 14.0.0 |
|
305 | + */ |
|
306 | + protected function findEntities(IQueryBuilder $query): array { |
|
307 | + $cursor = $query->execute(); |
|
308 | + |
|
309 | + $entities = []; |
|
310 | + |
|
311 | + while($row = $cursor->fetch()){ |
|
312 | + $entities[] = $this->mapRowToEntity($row); |
|
313 | + } |
|
314 | + |
|
315 | + $cursor->closeCursor(); |
|
316 | + |
|
317 | + return $entities; |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Returns an db result and throws exceptions when there are more or less |
|
323 | + * results |
|
324 | + * |
|
325 | + * @param IQueryBuilder $query |
|
326 | + * @throws DoesNotExistException if the item does not exist |
|
327 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
328 | + * @return Entity the entity |
|
329 | + * @since 14.0.0 |
|
330 | + */ |
|
331 | + protected function findEntity(IQueryBuilder $query): Entity { |
|
332 | + return $this->mapRowToEntity($this->findOneQuery($query)); |
|
333 | + } |
|
334 | 334 | |
335 | 335 | } |
@@ -37,339 +37,339 @@ |
||
37 | 37 | */ |
38 | 38 | abstract class Mapper { |
39 | 39 | |
40 | - protected $tableName; |
|
41 | - protected $entityClass; |
|
42 | - protected $db; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
46 | - * @param string $tableName the name of the table. set this to allow entity |
|
47 | - * @param string $entityClass the name of the entity that the sql should be |
|
48 | - * mapped to queries without using sql |
|
49 | - * @since 7.0.0 |
|
50 | - * @deprecated 14.0.0 Move over to QBMapper |
|
51 | - */ |
|
52 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null) { |
|
53 | - $this->db = $db; |
|
54 | - $this->tableName = '*PREFIX*' . $tableName; |
|
55 | - |
|
56 | - // if not given set the entity name to the class without the mapper part |
|
57 | - // cache it here for later use since reflection is slow |
|
58 | - if($entityClass === null) { |
|
59 | - $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | - } else { |
|
61 | - $this->entityClass = $entityClass; |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @return string the table name |
|
68 | - * @since 7.0.0 |
|
69 | - * @deprecated 14.0.0 Move over to QBMapper |
|
70 | - */ |
|
71 | - public function getTableName() { |
|
72 | - return $this->tableName; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Deletes an entity from the table |
|
78 | - * @param Entity $entity the entity that should be deleted |
|
79 | - * @return Entity the deleted entity |
|
80 | - * @since 7.0.0 - return value added in 8.1.0 |
|
81 | - * @deprecated 14.0.0 Move over to QBMapper |
|
82 | - */ |
|
83 | - public function delete(Entity $entity) { |
|
84 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
85 | - $stmt = $this->execute($sql, [$entity->getId()]); |
|
86 | - $stmt->closeCursor(); |
|
87 | - return $entity; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Creates a new entry in the db from an entity |
|
93 | - * @param Entity $entity the entity that should be created |
|
94 | - * @return Entity the saved entity with the set id |
|
95 | - * @since 7.0.0 |
|
96 | - * @deprecated 14.0.0 Move over to QBMapper |
|
97 | - */ |
|
98 | - public function insert(Entity $entity) { |
|
99 | - // get updated fields to save, fields have to be set using a setter to |
|
100 | - // be saved |
|
101 | - $properties = $entity->getUpdatedFields(); |
|
102 | - $values = ''; |
|
103 | - $columns = ''; |
|
104 | - $params = []; |
|
105 | - |
|
106 | - // build the fields |
|
107 | - $i = 0; |
|
108 | - foreach($properties as $property => $updated) { |
|
109 | - $column = $entity->propertyToColumn($property); |
|
110 | - $getter = 'get' . ucfirst($property); |
|
111 | - |
|
112 | - $columns .= '`' . $column . '`'; |
|
113 | - $values .= '?'; |
|
114 | - |
|
115 | - // only append colon if there are more entries |
|
116 | - if($i < count($properties)-1){ |
|
117 | - $columns .= ','; |
|
118 | - $values .= ','; |
|
119 | - } |
|
120 | - |
|
121 | - $params[] = $entity->$getter(); |
|
122 | - $i++; |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
127 | - $columns . ') VALUES(' . $values . ')'; |
|
128 | - |
|
129 | - $stmt = $this->execute($sql, $params); |
|
130 | - |
|
131 | - $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
132 | - |
|
133 | - $stmt->closeCursor(); |
|
134 | - |
|
135 | - return $entity; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * Updates an entry in the db from an entity |
|
142 | - * @throws \InvalidArgumentException if entity has no id |
|
143 | - * @param Entity $entity the entity that should be created |
|
144 | - * @return Entity the saved entity with the set id |
|
145 | - * @since 7.0.0 - return value was added in 8.0.0 |
|
146 | - * @deprecated 14.0.0 Move over to QBMapper |
|
147 | - */ |
|
148 | - public function update(Entity $entity) { |
|
149 | - // if entity wasn't changed it makes no sense to run a db query |
|
150 | - $properties = $entity->getUpdatedFields(); |
|
151 | - if(count($properties) === 0) { |
|
152 | - return $entity; |
|
153 | - } |
|
154 | - |
|
155 | - // entity needs an id |
|
156 | - $id = $entity->getId(); |
|
157 | - if($id === null){ |
|
158 | - throw new \InvalidArgumentException( |
|
159 | - 'Entity which should be updated has no id'); |
|
160 | - } |
|
161 | - |
|
162 | - // get updated fields to save, fields have to be set using a setter to |
|
163 | - // be saved |
|
164 | - // do not update the id field |
|
165 | - unset($properties['id']); |
|
166 | - |
|
167 | - $columns = ''; |
|
168 | - $params = []; |
|
169 | - |
|
170 | - // build the fields |
|
171 | - $i = 0; |
|
172 | - foreach($properties as $property => $updated) { |
|
173 | - |
|
174 | - $column = $entity->propertyToColumn($property); |
|
175 | - $getter = 'get' . ucfirst($property); |
|
176 | - |
|
177 | - $columns .= '`' . $column . '` = ?'; |
|
178 | - |
|
179 | - // only append colon if there are more entries |
|
180 | - if($i < count($properties)-1){ |
|
181 | - $columns .= ','; |
|
182 | - } |
|
183 | - |
|
184 | - $params[] = $entity->$getter(); |
|
185 | - $i++; |
|
186 | - } |
|
187 | - |
|
188 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
189 | - $columns . ' WHERE `id` = ?'; |
|
190 | - $params[] = $id; |
|
191 | - |
|
192 | - $stmt = $this->execute($sql, $params); |
|
193 | - $stmt->closeCursor(); |
|
194 | - |
|
195 | - return $entity; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Checks if an array is associative |
|
200 | - * @param array $array |
|
201 | - * @return bool true if associative |
|
202 | - * @since 8.1.0 |
|
203 | - * @deprecated 14.0.0 Move over to QBMapper |
|
204 | - */ |
|
205 | - private function isAssocArray(array $array) { |
|
206 | - return array_values($array) !== $array; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns the correct PDO constant based on the value type |
|
211 | - * @param $value |
|
212 | - * @return int PDO constant |
|
213 | - * @since 8.1.0 |
|
214 | - * @deprecated 14.0.0 Move over to QBMapper |
|
215 | - */ |
|
216 | - private function getPDOType($value) { |
|
217 | - switch (gettype($value)) { |
|
218 | - case 'integer': |
|
219 | - return \PDO::PARAM_INT; |
|
220 | - case 'boolean': |
|
221 | - return \PDO::PARAM_BOOL; |
|
222 | - default: |
|
223 | - return \PDO::PARAM_STR; |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Runs an sql query |
|
230 | - * @param string $sql the prepare string |
|
231 | - * @param array $params the params which should replace the ? in the sql query |
|
232 | - * @param int $limit the maximum number of rows |
|
233 | - * @param int $offset from which row we want to start |
|
234 | - * @return \PDOStatement the database query result |
|
235 | - * @since 7.0.0 |
|
236 | - * @deprecated 14.0.0 Move over to QBMapper |
|
237 | - */ |
|
238 | - protected function execute($sql, array $params=[], $limit=null, $offset=null) { |
|
239 | - $query = $this->db->prepare($sql, $limit, $offset); |
|
240 | - |
|
241 | - if ($this->isAssocArray($params)) { |
|
242 | - foreach ($params as $key => $param) { |
|
243 | - $pdoConstant = $this->getPDOType($param); |
|
244 | - $query->bindValue($key, $param, $pdoConstant); |
|
245 | - } |
|
246 | - } else { |
|
247 | - $index = 1; // bindParam is 1 indexed |
|
248 | - foreach ($params as $param) { |
|
249 | - $pdoConstant = $this->getPDOType($param); |
|
250 | - $query->bindValue($index, $param, $pdoConstant); |
|
251 | - $index++; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - $query->execute(); |
|
256 | - |
|
257 | - return $query; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Returns an db result and throws exceptions when there are more or less |
|
262 | - * results |
|
263 | - * @see findEntity |
|
264 | - * @param string $sql the sql query |
|
265 | - * @param array $params the parameters of the sql query |
|
266 | - * @param int $limit the maximum number of rows |
|
267 | - * @param int $offset from which row we want to start |
|
268 | - * @throws DoesNotExistException if the item does not exist |
|
269 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
270 | - * @return array the result as row |
|
271 | - * @since 7.0.0 |
|
272 | - * @deprecated 14.0.0 Move over to QBMapper |
|
273 | - */ |
|
274 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) { |
|
275 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
276 | - $row = $stmt->fetch(); |
|
277 | - |
|
278 | - if($row === false || $row === null){ |
|
279 | - $stmt->closeCursor(); |
|
280 | - $msg = $this->buildDebugMessage( |
|
281 | - 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
282 | - ); |
|
283 | - throw new DoesNotExistException($msg); |
|
284 | - } |
|
285 | - $row2 = $stmt->fetch(); |
|
286 | - $stmt->closeCursor(); |
|
287 | - //MDB2 returns null, PDO and doctrine false when no row is available |
|
288 | - if(! ($row2 === false || $row2 === null)) { |
|
289 | - $msg = $this->buildDebugMessage( |
|
290 | - 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
291 | - ); |
|
292 | - throw new MultipleObjectsReturnedException($msg); |
|
293 | - } else { |
|
294 | - return $row; |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Builds an error message by prepending the $msg to an error message which |
|
300 | - * has the parameters |
|
301 | - * @see findEntity |
|
302 | - * @param string $sql the sql query |
|
303 | - * @param array $params the parameters of the sql query |
|
304 | - * @param int $limit the maximum number of rows |
|
305 | - * @param int $offset from which row we want to start |
|
306 | - * @return string formatted error message string |
|
307 | - * @since 9.1.0 |
|
308 | - * @deprecated 14.0.0 Move over to QBMapper |
|
309 | - */ |
|
310 | - private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
311 | - return $msg . |
|
312 | - ': query "' . $sql . '"; ' . |
|
313 | - 'parameters ' . print_r($params, true) . '; ' . |
|
314 | - 'limit "' . $limit . '"; '. |
|
315 | - 'offset "' . $offset . '"'; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Creates an entity from a row. Automatically determines the entity class |
|
321 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
322 | - * @param array $row the row which should be converted to an entity |
|
323 | - * @return Entity the entity |
|
324 | - * @since 7.0.0 |
|
325 | - * @deprecated 14.0.0 Move over to QBMapper |
|
326 | - */ |
|
327 | - protected function mapRowToEntity($row) { |
|
328 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Runs a sql query and returns an array of entities |
|
334 | - * @param string $sql the prepare string |
|
335 | - * @param array $params the params which should replace the ? in the sql query |
|
336 | - * @param int $limit the maximum number of rows |
|
337 | - * @param int $offset from which row we want to start |
|
338 | - * @return array all fetched entities |
|
339 | - * @since 7.0.0 |
|
340 | - * @deprecated 14.0.0 Move over to QBMapper |
|
341 | - */ |
|
342 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
343 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
344 | - |
|
345 | - $entities = []; |
|
346 | - |
|
347 | - while($row = $stmt->fetch()){ |
|
348 | - $entities[] = $this->mapRowToEntity($row); |
|
349 | - } |
|
350 | - |
|
351 | - $stmt->closeCursor(); |
|
352 | - |
|
353 | - return $entities; |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns an db result and throws exceptions when there are more or less |
|
359 | - * results |
|
360 | - * @param string $sql the sql query |
|
361 | - * @param array $params the parameters of the sql query |
|
362 | - * @param int $limit the maximum number of rows |
|
363 | - * @param int $offset from which row we want to start |
|
364 | - * @throws DoesNotExistException if the item does not exist |
|
365 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
366 | - * @return Entity the entity |
|
367 | - * @since 7.0.0 |
|
368 | - * @deprecated 14.0.0 Move over to QBMapper |
|
369 | - */ |
|
370 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { |
|
371 | - return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
372 | - } |
|
40 | + protected $tableName; |
|
41 | + protected $entityClass; |
|
42 | + protected $db; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
46 | + * @param string $tableName the name of the table. set this to allow entity |
|
47 | + * @param string $entityClass the name of the entity that the sql should be |
|
48 | + * mapped to queries without using sql |
|
49 | + * @since 7.0.0 |
|
50 | + * @deprecated 14.0.0 Move over to QBMapper |
|
51 | + */ |
|
52 | + public function __construct(IDBConnection $db, $tableName, $entityClass=null) { |
|
53 | + $this->db = $db; |
|
54 | + $this->tableName = '*PREFIX*' . $tableName; |
|
55 | + |
|
56 | + // if not given set the entity name to the class without the mapper part |
|
57 | + // cache it here for later use since reflection is slow |
|
58 | + if($entityClass === null) { |
|
59 | + $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | + } else { |
|
61 | + $this->entityClass = $entityClass; |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @return string the table name |
|
68 | + * @since 7.0.0 |
|
69 | + * @deprecated 14.0.0 Move over to QBMapper |
|
70 | + */ |
|
71 | + public function getTableName() { |
|
72 | + return $this->tableName; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Deletes an entity from the table |
|
78 | + * @param Entity $entity the entity that should be deleted |
|
79 | + * @return Entity the deleted entity |
|
80 | + * @since 7.0.0 - return value added in 8.1.0 |
|
81 | + * @deprecated 14.0.0 Move over to QBMapper |
|
82 | + */ |
|
83 | + public function delete(Entity $entity) { |
|
84 | + $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
85 | + $stmt = $this->execute($sql, [$entity->getId()]); |
|
86 | + $stmt->closeCursor(); |
|
87 | + return $entity; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Creates a new entry in the db from an entity |
|
93 | + * @param Entity $entity the entity that should be created |
|
94 | + * @return Entity the saved entity with the set id |
|
95 | + * @since 7.0.0 |
|
96 | + * @deprecated 14.0.0 Move over to QBMapper |
|
97 | + */ |
|
98 | + public function insert(Entity $entity) { |
|
99 | + // get updated fields to save, fields have to be set using a setter to |
|
100 | + // be saved |
|
101 | + $properties = $entity->getUpdatedFields(); |
|
102 | + $values = ''; |
|
103 | + $columns = ''; |
|
104 | + $params = []; |
|
105 | + |
|
106 | + // build the fields |
|
107 | + $i = 0; |
|
108 | + foreach($properties as $property => $updated) { |
|
109 | + $column = $entity->propertyToColumn($property); |
|
110 | + $getter = 'get' . ucfirst($property); |
|
111 | + |
|
112 | + $columns .= '`' . $column . '`'; |
|
113 | + $values .= '?'; |
|
114 | + |
|
115 | + // only append colon if there are more entries |
|
116 | + if($i < count($properties)-1){ |
|
117 | + $columns .= ','; |
|
118 | + $values .= ','; |
|
119 | + } |
|
120 | + |
|
121 | + $params[] = $entity->$getter(); |
|
122 | + $i++; |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
127 | + $columns . ') VALUES(' . $values . ')'; |
|
128 | + |
|
129 | + $stmt = $this->execute($sql, $params); |
|
130 | + |
|
131 | + $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
132 | + |
|
133 | + $stmt->closeCursor(); |
|
134 | + |
|
135 | + return $entity; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * Updates an entry in the db from an entity |
|
142 | + * @throws \InvalidArgumentException if entity has no id |
|
143 | + * @param Entity $entity the entity that should be created |
|
144 | + * @return Entity the saved entity with the set id |
|
145 | + * @since 7.0.0 - return value was added in 8.0.0 |
|
146 | + * @deprecated 14.0.0 Move over to QBMapper |
|
147 | + */ |
|
148 | + public function update(Entity $entity) { |
|
149 | + // if entity wasn't changed it makes no sense to run a db query |
|
150 | + $properties = $entity->getUpdatedFields(); |
|
151 | + if(count($properties) === 0) { |
|
152 | + return $entity; |
|
153 | + } |
|
154 | + |
|
155 | + // entity needs an id |
|
156 | + $id = $entity->getId(); |
|
157 | + if($id === null){ |
|
158 | + throw new \InvalidArgumentException( |
|
159 | + 'Entity which should be updated has no id'); |
|
160 | + } |
|
161 | + |
|
162 | + // get updated fields to save, fields have to be set using a setter to |
|
163 | + // be saved |
|
164 | + // do not update the id field |
|
165 | + unset($properties['id']); |
|
166 | + |
|
167 | + $columns = ''; |
|
168 | + $params = []; |
|
169 | + |
|
170 | + // build the fields |
|
171 | + $i = 0; |
|
172 | + foreach($properties as $property => $updated) { |
|
173 | + |
|
174 | + $column = $entity->propertyToColumn($property); |
|
175 | + $getter = 'get' . ucfirst($property); |
|
176 | + |
|
177 | + $columns .= '`' . $column . '` = ?'; |
|
178 | + |
|
179 | + // only append colon if there are more entries |
|
180 | + if($i < count($properties)-1){ |
|
181 | + $columns .= ','; |
|
182 | + } |
|
183 | + |
|
184 | + $params[] = $entity->$getter(); |
|
185 | + $i++; |
|
186 | + } |
|
187 | + |
|
188 | + $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
189 | + $columns . ' WHERE `id` = ?'; |
|
190 | + $params[] = $id; |
|
191 | + |
|
192 | + $stmt = $this->execute($sql, $params); |
|
193 | + $stmt->closeCursor(); |
|
194 | + |
|
195 | + return $entity; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Checks if an array is associative |
|
200 | + * @param array $array |
|
201 | + * @return bool true if associative |
|
202 | + * @since 8.1.0 |
|
203 | + * @deprecated 14.0.0 Move over to QBMapper |
|
204 | + */ |
|
205 | + private function isAssocArray(array $array) { |
|
206 | + return array_values($array) !== $array; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns the correct PDO constant based on the value type |
|
211 | + * @param $value |
|
212 | + * @return int PDO constant |
|
213 | + * @since 8.1.0 |
|
214 | + * @deprecated 14.0.0 Move over to QBMapper |
|
215 | + */ |
|
216 | + private function getPDOType($value) { |
|
217 | + switch (gettype($value)) { |
|
218 | + case 'integer': |
|
219 | + return \PDO::PARAM_INT; |
|
220 | + case 'boolean': |
|
221 | + return \PDO::PARAM_BOOL; |
|
222 | + default: |
|
223 | + return \PDO::PARAM_STR; |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Runs an sql query |
|
230 | + * @param string $sql the prepare string |
|
231 | + * @param array $params the params which should replace the ? in the sql query |
|
232 | + * @param int $limit the maximum number of rows |
|
233 | + * @param int $offset from which row we want to start |
|
234 | + * @return \PDOStatement the database query result |
|
235 | + * @since 7.0.0 |
|
236 | + * @deprecated 14.0.0 Move over to QBMapper |
|
237 | + */ |
|
238 | + protected function execute($sql, array $params=[], $limit=null, $offset=null) { |
|
239 | + $query = $this->db->prepare($sql, $limit, $offset); |
|
240 | + |
|
241 | + if ($this->isAssocArray($params)) { |
|
242 | + foreach ($params as $key => $param) { |
|
243 | + $pdoConstant = $this->getPDOType($param); |
|
244 | + $query->bindValue($key, $param, $pdoConstant); |
|
245 | + } |
|
246 | + } else { |
|
247 | + $index = 1; // bindParam is 1 indexed |
|
248 | + foreach ($params as $param) { |
|
249 | + $pdoConstant = $this->getPDOType($param); |
|
250 | + $query->bindValue($index, $param, $pdoConstant); |
|
251 | + $index++; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + $query->execute(); |
|
256 | + |
|
257 | + return $query; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Returns an db result and throws exceptions when there are more or less |
|
262 | + * results |
|
263 | + * @see findEntity |
|
264 | + * @param string $sql the sql query |
|
265 | + * @param array $params the parameters of the sql query |
|
266 | + * @param int $limit the maximum number of rows |
|
267 | + * @param int $offset from which row we want to start |
|
268 | + * @throws DoesNotExistException if the item does not exist |
|
269 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
270 | + * @return array the result as row |
|
271 | + * @since 7.0.0 |
|
272 | + * @deprecated 14.0.0 Move over to QBMapper |
|
273 | + */ |
|
274 | + protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) { |
|
275 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
276 | + $row = $stmt->fetch(); |
|
277 | + |
|
278 | + if($row === false || $row === null){ |
|
279 | + $stmt->closeCursor(); |
|
280 | + $msg = $this->buildDebugMessage( |
|
281 | + 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
282 | + ); |
|
283 | + throw new DoesNotExistException($msg); |
|
284 | + } |
|
285 | + $row2 = $stmt->fetch(); |
|
286 | + $stmt->closeCursor(); |
|
287 | + //MDB2 returns null, PDO and doctrine false when no row is available |
|
288 | + if(! ($row2 === false || $row2 === null)) { |
|
289 | + $msg = $this->buildDebugMessage( |
|
290 | + 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
291 | + ); |
|
292 | + throw new MultipleObjectsReturnedException($msg); |
|
293 | + } else { |
|
294 | + return $row; |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Builds an error message by prepending the $msg to an error message which |
|
300 | + * has the parameters |
|
301 | + * @see findEntity |
|
302 | + * @param string $sql the sql query |
|
303 | + * @param array $params the parameters of the sql query |
|
304 | + * @param int $limit the maximum number of rows |
|
305 | + * @param int $offset from which row we want to start |
|
306 | + * @return string formatted error message string |
|
307 | + * @since 9.1.0 |
|
308 | + * @deprecated 14.0.0 Move over to QBMapper |
|
309 | + */ |
|
310 | + private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
311 | + return $msg . |
|
312 | + ': query "' . $sql . '"; ' . |
|
313 | + 'parameters ' . print_r($params, true) . '; ' . |
|
314 | + 'limit "' . $limit . '"; '. |
|
315 | + 'offset "' . $offset . '"'; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Creates an entity from a row. Automatically determines the entity class |
|
321 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
322 | + * @param array $row the row which should be converted to an entity |
|
323 | + * @return Entity the entity |
|
324 | + * @since 7.0.0 |
|
325 | + * @deprecated 14.0.0 Move over to QBMapper |
|
326 | + */ |
|
327 | + protected function mapRowToEntity($row) { |
|
328 | + return call_user_func($this->entityClass .'::fromRow', $row); |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Runs a sql query and returns an array of entities |
|
334 | + * @param string $sql the prepare string |
|
335 | + * @param array $params the params which should replace the ? in the sql query |
|
336 | + * @param int $limit the maximum number of rows |
|
337 | + * @param int $offset from which row we want to start |
|
338 | + * @return array all fetched entities |
|
339 | + * @since 7.0.0 |
|
340 | + * @deprecated 14.0.0 Move over to QBMapper |
|
341 | + */ |
|
342 | + protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
343 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
344 | + |
|
345 | + $entities = []; |
|
346 | + |
|
347 | + while($row = $stmt->fetch()){ |
|
348 | + $entities[] = $this->mapRowToEntity($row); |
|
349 | + } |
|
350 | + |
|
351 | + $stmt->closeCursor(); |
|
352 | + |
|
353 | + return $entities; |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns an db result and throws exceptions when there are more or less |
|
359 | + * results |
|
360 | + * @param string $sql the sql query |
|
361 | + * @param array $params the parameters of the sql query |
|
362 | + * @param int $limit the maximum number of rows |
|
363 | + * @param int $offset from which row we want to start |
|
364 | + * @throws DoesNotExistException if the item does not exist |
|
365 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
366 | + * @return Entity the entity |
|
367 | + * @since 7.0.0 |
|
368 | + * @deprecated 14.0.0 Move over to QBMapper |
|
369 | + */ |
|
370 | + protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { |
|
371 | + return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
372 | + } |
|
373 | 373 | |
374 | 374 | |
375 | 375 | } |