-
Notifications
You must be signed in to change notification settings - Fork 7
SQL Array methods to support multi choice fields #7239
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
Changes from all commits
6b26a9b
47f43aa
6775328
2a0007c
fd14ba1
bd43085
6994b54
239b3d8
98554f5
4c94952
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 |
|---|---|---|
|
|
@@ -280,4 +280,106 @@ AND d.deptype IN ('a', 'i') -- Automatic dependency for DEFAULT or index-related | |
| ); | ||
| return new SqlSelector(table.getSchema(), sql).getCollection(Sequence.class); | ||
| } | ||
|
|
||
|
|
||
| // | ||
| // ARRAY and SET syntax | ||
| // | ||
|
|
||
| // NOTE LabKey currently does not support ARRAY[VARCHAR], use ARRAY[text] instead | ||
| // | ||
| // Postgres string literals can be auto-cast to both VARCHAR and TEXT. These all work | ||
| // 'color' = 'color'::varchar | ||
| // 'color' = 'color'::text | ||
| // ARRAY['color'] = ARRAY['color'::text]; | ||
| // However, ARRAY[text] cannot be auto cast to ARRAY[varchar] | ||
| // ARRAY['color'] = ARRAY['color'::varchar]; -- ERROR! | ||
| // | ||
|
|
||
|
|
||
| @Override | ||
| public boolean supportsArrays() | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_construct(SQLFragment[] elements) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("ARRAY["); | ||
| String separator = ""; | ||
| for (SQLFragment element : elements) | ||
| { | ||
| ret.append(separator); | ||
| ret.append(element); | ||
|
Contributor
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. Is there special encoding needed? For example a'b needs to be encoded into a''b?
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. Any encoding should be handled by whoever generated the elements. Those should be correct SQL already. |
||
| separator = ", "; | ||
| } | ||
| ret.append("]"); | ||
|
Contributor
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. Do we always assume text[] type? I don't think this works for varchar[]. If the column is of type varchar[], the column needs to be casted to text[] first for the comparators to work:
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. Good question... We assume that any array we create would be text, and I think we an require that for module created tables as well. It would be tricky for us to handle varchar (say for attached schemas). Currently we don't track varchar and text separately they tend to just get merged as JdbcType.VARCHAR. Worth adding a comment.
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. I wonder what happens when using VARCHAR columns e.g
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. Maybe we need a TEXTARRAY() method? I'll think about it. |
||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_all_in_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("(").append(a).append(") <@ (").append(b).append(")"); | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_some_in_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("(").append(a).append(") && (").append(b).append(")"); | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_none_in_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| return new SQLFragment(" NOT (").append(array_some_in_array(a, b)).append(")"); | ||
|
Contributor
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. Should array_none_in_array also return empty values? |
||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_same_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append(array_all_in_array(a, b)).append(" AND ").append(array_all_in_array(b, a)); | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment array_not_same_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("NOT (").append(array_all_in_array(a, b)).append(") OR NOT (").append(array_all_in_array(b, a)).append(")"); | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment element_in_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("(").append(a).append(")"); | ||
| // DOCs imply that IS NOT DISTINCT FROM ANY should work, but it doesn't??? | ||
| // ret.append(" IS NOT DISTINCT FROM ANY("); | ||
| ret.append(" = ANY("); | ||
| ret.append(b); | ||
| ret.append(")"); | ||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public SQLFragment element_not_in_array(SQLFragment a, SQLFragment b) | ||
| { | ||
| SQLFragment ret = new SQLFragment(); | ||
| ret.append("(").append(a).append(")"); | ||
| // DOCs imply that IS NOT DISTINCT FROM ANY should work, but it doesn't??? | ||
| // ret.append(" IS DISTINCT FROM ALL("); | ||
| ret.append(" <> ALL("); | ||
| ret.append(b); | ||
| ret.append(")"); | ||
| return ret; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments for these public API methods should use proper Javadoc format with
/**instead of//for better documentation generation. Consider using proper Javadoc like: