@@ -199,7 +199,54 @@ You can turn this into something more manageable with a `list comprehension`_::
199
199
>>> [column[0] for column in cursor.description]
200
200
['date', 'datetime_tz', 'datetime_notz', ..., 'nullable_datetime', 'position']
201
201
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
+
202
248
.. _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
203
250
.. _Database API : http://www.python.org/dev/peps/pep-0249/
204
251
.. _database cursor : https://en.wikipedia.org/wiki/Cursor_(databases)
205
252
.. _DB API 2.0 : http://www.python.org/dev/peps/pep-0249/
0 commit comments