Using both TypesConverterCursorFactory and DataClassRowFactory at the same time

15 views
Skip to first unread message

Thomas Teixeira

unread,
Jul 3, 2025, 7:43:35 PMJul 3
to python...@googlegroups.com
Hi ! Loving apsw so far, thanks !

I'm trying to use `TypesConverterCursorFactory` to auto transform `list`s and `dict`s
as json representation so I can work with them in SQLite using builtin json support.
The issue is that I cannot seem to be able to use the `cursor_factory` and `row_trace`
properties at the same time.
It would seem that when a `cursor_factory`, other than the default `Cursor` value,
is set, the `row_trace` is not called ?

A minimal example to illustrate what I'm saying :)
```py
import dataclasses
import json

import apsw
from apsw.ext import DataClassRowFactory, TypesConverterCursorFactory

conn = apsw.Connection(":memory:")
converter = TypesConverterCursorFactory()

converter.register_adapter(list, lambda _list: json.dumps(_list or []))

converter.register_converter(
"LIST",
lambda _list: json.loads(_list or [])
)

conn.row_trace = DataClassRowFactory(dataclass_kwargs={"frozen": True})
conn.cursor_factory = converter

conn.execute("CREATE TABLE mvt(column1 LIST)")
l = [1, 2, 3, 4]
conn.execute("INSERT INTO mvt VALUES(?)", (l, ))

rows = conn.execute("select * from mvt limit 1")
for row in rows:
# This should print `True` but prints `False`
print(dataclasses.is_dataclass(row))
```

Roger Binns

unread,
Jul 3, 2025, 8:00:07 PMJul 3
to Python-SQLite group
> The issue is that I cannot seem to be able to use the `cursor_factory`
> and `row_trace` properties at the same time.

TypesConverterCursorFactory internally sets its own row tracer in order to adapt the values coming out.

Look at the source for TypeConverterCursor (inside TypesConverterCursorFactory) for the _rowtracer method to see it doing that.

I suggest writing your own code based on the above that does exactly what you want. You'll notice it isn't very much code. For anything coming out of SQLite, it requires heuristics based on how you name and use things in order to map back to other types.

There is currently no SQLite C API where I can ask if something is really a JSON value under the hood. I've [made a request on the forum](https://sqlite.org/forum/forumpost/6bcef1c62c).

Roger

Roger Binns

unread,
Jul 4, 2025, 10:29:20 AMJul 4
to Python-SQLite group
> ... auto transform `list`s and `dict`s as json representation so
> I can work with them in SQLite using builtin json support.

I think I have solution and will have to write some code to make this seamless and easy.

https://github.com/rogerbinns/apsw/issues/563

Roger
Reply all
Reply to author
Forward
0 new messages