Skip to content

Commit 1acc44e

Browse files
committed
Data type converter: Improve documentation
1 parent 944e427 commit 1acc44e

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

docs/query.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,54 @@ You can turn this into something more manageable with a `list comprehension`_::
199199
>>> [column[0] for column in cursor.description]
200200
['date', 'datetime_tz', 'datetime_notz', ..., 'nullable_datetime', 'position']
201201

202+
203+
Data type conversion
204+
====================
205+
206+
The cursor object can optionally convert database types to native Python data
207+
types. There is a default implementation for the CrateDB data types ``IP`` and
208+
``TIMESTAMP`` on behalf of the ``DefaultTypeConverter``.
209+
210+
::
211+
212+
>>> from crate.client.cursor import Cursor
213+
>>> ccursor = connection.cursor(converter=Cursor.get_default_converter())
214+
215+
>>> ccursor.execute("SELECT datetime_tz, datetime_notz FROM locations ORDER BY name")
216+
217+
>>> ccursor.fetchone()
218+
[datetime.datetime(2022, 7, 18, 18, 10, 36, 758000), datetime.datetime(2022, 7, 18, 18, 10, 36, 758000)]
219+
220+
221+
Custom data type conversion
222+
===========================
223+
224+
By providing a custom converter instance, you can define your own data type
225+
conversions. For investigating the list of available data types, please either
226+
inspect the ``DataType`` enum, or the documentation about the list of available
227+
`CrateDB data type identifiers for the HTTP interface`_.
228+
229+
This example creates and applies a simple custom converter for converging
230+
CrateDB's ``BOOLEAN`` type to Python's ``str`` type. It is using a simple
231+
converter function defined as ``lambda``, which assigns ``yes`` for boolean
232+
``True``, and ``no`` otherwise.
233+
234+
::
235+
236+
>>> from crate.client.converter import Converter, DataType
237+
238+
>>> converter = Converter()
239+
>>> converter.set(DataType.BOOLEAN, lambda value: value is True and "yes" or "no")
240+
>>> ccursor = connection.cursor(converter=converter)
241+
242+
>>> ccursor.execute("SELECT flag FROM locations ORDER BY name")
243+
244+
>>> ccursor.fetchone()
245+
['no']
246+
247+
202248
.. _Bulk inserts: https://crate.io/docs/crate/reference/en/latest/interfaces/http.html#bulk-operations
249+
.. _CrateDB data type identifiers for the HTTP interface: https://crate.io/docs/crate/reference/en/latest/interfaces/http.html#column-types
203250
.. _Database API: http://www.python.org/dev/peps/pep-0249/
204251
.. _database cursor: https://en.wikipedia.org/wiki/Cursor_(databases)
205252
.. _DB API 2.0: http://www.python.org/dev/peps/pep-0249/

src/crate/client/doctests/cursor.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,9 @@ Custom data type conversion
334334
===========================
335335

336336
By providing a custom converter instance, you can define your own data type
337-
conversions. Please inspect the list of available `CrateDB HTTP interface data
338-
type identifiers`_.
337+
conversions. For investigating the list of available data types, please either
338+
inspect the ``DataType`` enum, or the documentation about the list of available
339+
`CrateDB data type identifiers for the HTTP interface`_.
339340

340341
To create a simple converter for converging CrateDB's ``BIT`` type to Python's
341342
``int`` type::
@@ -368,4 +369,4 @@ Proof that the converter works correctly, ``B\'0110\'`` should be converted to
368369
>>> connection.close()
369370

370371

371-
.. _CrateDB HTTP interface data type identifiers: https://crate.io/docs/crate/reference/en/latest/interfaces/http.html#column-types
372+
.. _CrateDB data type identifiers for the HTTP interface: https://crate.io/docs/crate/reference/en/latest/interfaces/http.html#column-types

0 commit comments

Comments
 (0)