-
Notifications
You must be signed in to change notification settings - Fork 3
Types: Add support for BINARY columns #12
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
base: main
Are you sure you want to change the base?
Changes from all commits
b504343
576a0ca
4dafb65
08157ed
dcdd4a9
5d7e2ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import base64 | ||
|
|
||
| from sqlalchemy import String | ||
|
|
||
|
|
||
| class LargeBinary(String): | ||
| """A type for large binary byte data. | ||
|
|
||
| The :class:`.LargeBinary` type corresponds to a large and/or unlengthed | ||
| binary type for the target platform, such as BLOB on MySQL and BYTEA for | ||
| PostgreSQL. It also handles the necessary conversions for the DBAPI. | ||
|
|
||
| """ | ||
|
|
||
| __visit_name__ = "large_binary" | ||
|
|
||
| def bind_processor(self, dialect): | ||
| if dialect.dbapi is None: | ||
| return None | ||
|
|
||
| def process(value): | ||
| if value is not None: | ||
| return base64.b64encode(value).decode() | ||
| else: | ||
| return None | ||
|
|
||
| return process | ||
|
Comment on lines
+17
to
+27
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self, or others who want to pick this up: Review that detail about returning a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amotl Did you review it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. I just staged the patch directly from its origin, where the code is in use and works well. However, it can still be wrong from a generic perspective. Let's toggle this patch back into draft mode: Better safe than sorry, thanks! The other commit 465b55b has been removed here and diverged to a separate patch.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @bgunebakan,
No matter how the downstream supertask package currently consumes this API, I think we should aim for making it standards-compliant and then adjust supertask correspondingly, when possible. In this spirit, I think this comment is the most prominent example what we are referring to: # TODO: return DBAPIBinary(value)Only because we "unlocked" this feature by harmonizing sqlalchemy-cratedb with supertask in a bare-bones way to satisfy some basic explorations the other day doesn't automatically mean that the currently implemented interface is correct yet. With kind regards,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi. I can see that this comment was removed from this patch. Unless this has been verified positively, I can't recommend to merge the patch. # TODO: return DBAPIBinary(value) |
||
|
|
||
| def result_processor(self, dialect, coltype): | ||
| def process(value): | ||
| if value is not None: | ||
| return base64.b64decode(value) | ||
| return value | ||
|
|
||
| return process | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # -*- coding: utf-8; -*- | ||
| # | ||
| # Licensed to CRATE Technology GmbH ("Crate") under one or more contributor | ||
| # license agreements. See the NOTICE file distributed with this work for | ||
| # additional information regarding copyright ownership. Crate licenses | ||
| # this file to you under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. You may | ||
| # obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
| # | ||
| # However, if you have executed another commercial license agreement | ||
| # with Crate these terms will supersede the license and you may use the | ||
| # software solely pursuant to the terms of the relevant commercial agreement. | ||
|
|
||
| import base64 | ||
| from unittest import TestCase | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from sqlalchemy_cratedb.type.binary import LargeBinary | ||
|
|
||
|
|
||
| class LargeBinaryBindProcessorTest(TestCase): | ||
| def setUp(self): | ||
| self.type = LargeBinary() | ||
| self.dialect = MagicMock() | ||
| self.dialect.dbapi = MagicMock() | ||
|
|
||
| def test_encodes_bytes_to_base64_string(self): | ||
| process = self.type.bind_processor(self.dialect) | ||
| result = process(b"hello world") | ||
| self.assertEqual(result, base64.b64encode(b"hello world").decode()) | ||
|
|
||
| def test_returns_none_for_none_input(self): | ||
| process = self.type.bind_processor(self.dialect) | ||
| self.assertIsNone(process(None)) | ||
|
|
||
| def test_returns_none_processor_when_dbapi_is_none(self): | ||
| self.dialect.dbapi = None | ||
| processor = self.type.bind_processor(self.dialect) | ||
| self.assertIsNone(processor) | ||
|
|
||
| def test_encodes_arbitrary_binary_data(self): | ||
| process = self.type.bind_processor(self.dialect) | ||
| data = bytes(range(256)) | ||
| result = process(data) | ||
| self.assertEqual(result, base64.b64encode(data).decode()) | ||
|
|
||
|
|
||
| class LargeBinaryResultProcessorTest(TestCase): | ||
| def setUp(self): | ||
| self.type = LargeBinary() | ||
| self.dialect = MagicMock() | ||
|
|
||
| def test_decodes_base64_string_to_bytes(self): | ||
| process = self.type.result_processor(self.dialect, None) | ||
| encoded = base64.b64encode(b"hello world").decode() | ||
| result = process(encoded) | ||
| self.assertEqual(result, b"hello world") | ||
|
|
||
| def test_returns_none_for_none_input(self): | ||
| process = self.type.result_processor(self.dialect, None) | ||
| self.assertIsNone(process(None)) | ||
|
|
||
| def test_round_trip(self): | ||
| bind = self.type.bind_processor(self.dialect) | ||
| result = self.type.result_processor(self.dialect, None) | ||
| data = b"\x00\x01\x02\xff\xfe\xfd" | ||
| self.assertEqual(result(bind(data)), data) |
Uh oh!
There was an error while loading. Please reload this page.