Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support UUID and memoryview types #128

Closed
simonw opened this issue Jul 27, 2020 · 1 comment
Closed

Support UUID and memoryview types #128

simonw opened this issue Jul 27, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Jul 27, 2020

psycopg2 can return data from PostgreSQL as uuid.UUID or memoryview objects. These should to be supported by sqlite-utils - mainly for https://github.com/simonw/db-to-sqlite

@simonw simonw added the enhancement New feature or request label Jul 27, 2020
@simonw
Copy link
Owner Author

simonw commented Jul 27, 2020

This seems to work, but needs more tests:

diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py
index d6b9ecf..ee26433 100644
--- a/sqlite_utils/db.py
+++ b/sqlite_utils/db.py
@@ -7,6 +7,7 @@ import itertools
 import json
 import os
 import pathlib
+import uuid
 
 SQLITE_MAX_VARS = 999
 
@@ -40,11 +41,13 @@ COLUMN_TYPE_MAPPING = {
     str: "TEXT",
     bytes.__class__: "BLOB",
     bytes: "BLOB",
+    memoryview: "BLOB",
     datetime.datetime: "TEXT",
     datetime.date: "TEXT",
     datetime.time: "TEXT",
     decimal.Decimal: "FLOAT",
     None.__class__: "TEXT",
+    uuid.UUID: "TEXT",
     # SQLite explicit types
     "TEXT": "TEXT",
     "INTEGER": "INTEGER",
@@ -1336,6 +1339,8 @@ def jsonify_if_needed(value):
         return json.dumps(value, default=repr)
     elif isinstance(value, (datetime.time, datetime.date, datetime.datetime)):
         return value.isoformat()
+    elif isinstance(value, uuid.UUID):
+        return str(value)
     else:
         return value

@simonw simonw closed this as completed in f804690 Jul 30, 2020
simonw added a commit that referenced this issue Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant