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

Add function Base64 (base64_encode, base64_decode) #2423

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

a-polyakov
Copy link

No description provided.

Copy link
Contributor

@katzyn katzyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

A good encoding function should have configuration options, because there are two character sets (regular and URL-safe) and both of them are very common, padding may be needed or not, and somebody way want to wrap lines in RFC2045-compatible way.

https://tools.ietf.org/html/rfc4648
https://tools.ietf.org/html/rfc2045

New functions need some documentation in h2/src/docsrc/help/help.csv.

h2/src/main/org/h2/expression/function/Function.java Outdated Show resolved Hide resolved
h2/src/test/org/h2/test/db/TestFunctions.java Outdated Show resolved Hide resolved
@a-polyakov
Copy link
Author

Initially I wanted to do as in postgresql
https://postgrespro.ru/docs/postgrespro/11/functions-binarystring
encode('my string'::bytea, 'base64')
decode('bXkgc3RyaW5n', 'base64')
this function has the ability to adjust
but "decode" is already implemented https://www.h2database.com/html/functions.html#decode
for this reason I chose the implementation as in oracle

I think creating a unique function (not the same as others) is bad practice

as an option, I can add BASE64_URL_DECODE/ENCODE, it is not difficult, but I did not want to do garbage

@katzyn
Copy link
Contributor

katzyn commented Feb 3, 2020

I meant that you can add some optional parameter to BASE64_ENCODE function to control its output.

…t, change type: encode return string; decode input string.
@a-polyakov
Copy link
Author

Add optional parameter to BASE64_ENCODE function to control its output,
example:
base64_encode(stringtoutf8('<>')) ->PDw/Pz4+
base64_encode(stringtoutf8('<>'), 'URL') -> PDw_Pz4-

change type: encode return string; decode input string.

@a-polyakov
Copy link
Author

Add functions documentation in help.csv

h2/src/main/org/h2/api/ErrorCode.java Outdated Show resolved Hide resolved
h2/src/main/org/h2/expression/function/Function.java Outdated Show resolved Hide resolved
h2/src/main/org/h2/expression/function/Function.java Outdated Show resolved Hide resolved
h2/src/docsrc/help/help.csv Outdated Show resolved Hide resolved
h2/src/docsrc/help/help.csv Outdated Show resolved Hide resolved
… edit help.csv, move test from TestFunctions to scripts
@a-polyakov
Copy link
Author

Del error code, del EncodingFactory, decode auto detection algorithm, edit help.csv, move test from TestFunctions to scripts

Copy link
Contributor

@katzyn katzyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, resolve minor remaining problems and send a license statement as described here
https://h2database.com/html/build.html#providing_patches
to our mailing list
https://groups.google.com/forum/#!forum/h2-database

h2/src/docsrc/help/help.csv Outdated Show resolved Hide resolved
h2/src/main/org/h2/expression/function/Function.java Outdated Show resolved Hide resolved
…th unsupported algorithm, call base64_decode with error base64 string, edit help, use only ASCII characters in test
@a-polyakov
Copy link
Author

call base64_decode and base64_encode with null, call base64_encode with unsupported algorithm, call base64_decode with error base64 string, edit help, use only ASCII characters in test

@@ -2291,6 +2300,41 @@ private static Value oraHash(Value value, long bucket, long seed) {
return ValueBigint.get((hc & Long.MAX_VALUE) % (bucket + 1));
}

private static Value base64encode(byte[] data, String algorithm) {
if (data!=null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data is not expected to be null here, if it can, something else is going wrong. If function does not declare NULL support, getValueWithArgs() returns NULL if any of arguments is NULL by itself at the beginning of the method.

}

private static Value base64decode(String data) {
if (data!=null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants