Skip to content

Commit d8198ad

Browse files
authored
Merge pull request #12293 from nextcloud/developer/database-types
feat(developer): Document new database field types
2 parents 633d170 + 4545435 commit d8198ad

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

developer_manual/app_publishing_maintenance/app_upgrade_guide/upgrade_to_31.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ Added APIs
7070

7171
- It is now possible to download folders as zip or tar archives using the WebDAV backend using :code:`GET` requests.
7272
See the relevant :ref:`endpoint documentation<webdav-download-folders>`.
73-
- ``OCP\SetupCheck\CheckServerResponseTrait`` was added to ease implementing custom :ref:`setup checks<setup-checks>` which need to check HTTP calls to the the server itself.
73+
- ``OCP\SetupCheck\CheckServerResponseTrait`` was added to ease implementing custom :ref:`setup checks<setup-checks>`
74+
which need to check HTTP calls to the the server itself.
7475

7576
Changed APIs
7677
^^^^^^^^^^^^
@@ -82,12 +83,28 @@ Changed APIs
8283
#. Add all parameter types to your implementation once Nextcloud 31 is the lowest supported version.
8384

8485
- The Nextcloud implementation of the ``log`` method of ``Psr\Log\LoggerInterface`` now supports ``Psr\Log\LogLevel`` as log level parameter.
86+
- The ``OCP\DB\QueryBuilder\IQueryBuilder`` now supports more date / time related parameter types:
87+
88+
- ``PARAM_DATE_MUTABLE`` and ``PARAM_DATE_IMMUTABLE`` for passing a ``\DateTime`` (``\DateTimeImmutable`` respectively) instance when only interested in the date part.
89+
- ``PARAM_TIME_MUTABLE`` and ``PARAM_TIME_IMMUTABLE`` to pass a ``\DateTime`` (``\DateTimeImmutable`` respectively) instance when only interested in the time part.
90+
- ``PARAM_DATETIME_MUTABLE`` and ``PARAM_DATETIME_IMMUTABLE`` to pass a ``\DateTime`` (``\DateTimeImmutable`` respectively) instance without handling of the timezone.
91+
- ``PARAM_DATETIME_TZ_MUTABLE`` and ``PARAM_DATETIME_TZ_IMMUTABLE`` to pass a ``\DateTime`` (``\DateTimeImmutable`` respectively) instance with handling of the timezone.
92+
93+
- The ``OCP\\DB\\Types`` now support more date and time related types for usage with the ``Entity``:
94+
95+
- ``DATE_IMMUTABLE`` for fields that will (de)serialized as ``\DateTimeImmutable`` instances with only the date part set.
96+
- ``TIME_IMMUTABLE`` for fields that will (de)serialized as ``\DateTimeImmutable`` instances with only the time part set.
97+
- ``DATETIME_IMMUTABLE`` for fields that will (de)serialized as ``\DateTimeImmutable`` instances with both the time part set but without timezone information.
98+
- ``DATETIME_TZ`` for fields that will (de)serialized as ``\DateTime`` instances with both the time part set and with timezone information.
99+
- ``DATETIME_TZ_IMMUTABLE`` for fields that will (de)serialized as ``\DateTimeImmutable`` instances with both the time part set and with timezone information.
85100

86101
Deprecated APIs
87102
^^^^^^^^^^^^^^^
88103

89104
- The ``/s/{token}/download`` endpoint for downloading public shares is deprecated.
90105
Instead use the Nextcloud provided :ref:`WebDAV endpoint<webdav-download-folders>`.
106+
- ``OCP\DB\QueryBuilder\IQueryBuilder::PARAM_DATE`` is deprecated in favor of ``PARAM_DATETIME_MUTABLE``
107+
to make clear that this type also includes the time part of a date time instance.
91108

92109
Removed APIs
93110
^^^^^^^^^^^^

developer_manual/basics/storage/database.rst

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ or::
207207
Entities
208208
--------
209209

210-
Entities are data objects that carry all the table's information for one row. Every Entity has an **id** field by default that is set to the integer type. Table rows are mapped from lower case and underscore separated names to *lowerCamelCase* attributes:
210+
Entities are data objects that carry all the table's information for one row.
211+
Every Entity has an **id** field by default that is set to the integer type.
212+
Table rows are mapped from lower case and underscore separated names to *lowerCamelCase* attributes:
211213

212214
* **Table column name**: phone_number
213215
* **Property name**: phoneNumber
@@ -220,6 +222,7 @@ Entities are data objects that carry all the table's information for one row. Ev
220222
namespace OCA\MyApp\Db;
221223
222224
use OCP\AppFramework\Db\Entity;
225+
use OCP\DB\Types;
223226
224227
class Author extends Entity {
225228
@@ -229,24 +232,35 @@ Entities are data objects that carry all the table's information for one row. Ev
229232
230233
public function __construct() {
231234
// add types in constructor
232-
$this->addType('stars', 'integer');
235+
$this->addType('stars', Types::INTEGER);
236+
// other fields are implicitly `Types::STRING`
233237
}
234238
}
235239
236240
Types
237241
^^^^^
238242

239-
The following properties should be annotated by types, to not only assure that the types are converted correctly for storing them in the database (e.g. PHP casts false to the empty string which fails on PostgreSQL) but also for casting them when they are retrieved from the database.
243+
The following properties should be annotated by types, to not only assure that the types are converted correctly for storing them in the database
244+
(e.g. PHP casts false to the empty string which fails on PostgreSQL) but also for casting them when they are retrieved from the database.
240245

241-
The following types can be added for a field:
246+
The following types (as part of ``OCP\DB\Types``) can be added for a field:
242247

243-
* ``integer``
244-
* ``float``
245-
* ``boolean``
246-
* ``string`` - For text and string columns
247-
* ``blob`` - For binary data or strings longer than
248-
* ``json`` - JSON data is automatically decoded on reading
249-
* ``datetime`` - Providing ``\DateTime()`` objects
248+
* ``Types::INTEGER``
249+
* ``Types::FLOAT``
250+
* ``Types::BOOLEAN``
251+
* ``Types::STRING`` - For text and string columns
252+
* ``Types::BLOB`` - For binary data
253+
* ``Types::JSON`` - JSON data is automatically decoded on reading
254+
* For time and/or dates, provided as ``\DateTimeImmutable`` objects, the following types can be used:
255+
256+
* ``Types::DATE_IMMUTABLE`` - only the date is stored (without timezone)
257+
* ``Types::TIME_IMMUTABLE`` - only the time is stored (without timezone)
258+
* ``Types::DATETIME_IMMUTABLE`` - date and time are stored, but without timezone
259+
* ``Types::DATETIME_TZ_IMMUTABLE`` - date and time are stored with timezone information
260+
261+
* ``Types::DATE``, ``Types::TIME``, ``Types::DATETIME``, ``Types::DATETIME_TZ`` - similar as the immutable variants, but these will be provided as ``\DateTime`` objects.
262+
It is recommended to use the immutable variants as the internal state tracking of the ``Entity`` class only work with re-assignments,
263+
so any changes on this mutable types will not be tracked and the update method will not write back the changes to the database.
250264

251265
.. _database-entity-attribute-access:
252266

@@ -346,14 +360,16 @@ You can add attributes to an entity class that do not map to a database column.
346360
}
347361
}
348362
349-
It is important to define getters and setters for any transient attributes. Do not use the :ref:`magic getters and setters<database-entity-attribute-access>` of attributes that map to database columns.
363+
It is important to define getters and setters for any transient attributes.
364+
Do not use the :ref:`magic getters and setters<database-entity-attribute-access>` of attributes that map to database columns.
350365

351366
Slugs
352367
^^^^^
353368

354369
.. deprecated:: 24
355370

356-
Slugs are used to identify resources in the URL by a string rather than integer id. Since the URL allows only certain values, the entity base class provides a slugify method for it:
371+
Slugs are used to identify resources in the URL by a string rather than integer id.
372+
Since the URL allows only certain values, the entity base class provides a slugify method for it:
357373

358374
.. code-block:: php
359375

0 commit comments

Comments
 (0)