Обсуждение: array manipulations
Hi all again, Tom, _thanks_ for information! I really appreciated it. I managed to create a function which can be used to append or delete elems in an integer array. I needed it because I wanted to somehow solve the user-group relationship problems with Postgres. If you are interested, I could extend its functionality to other types as well and send this small pack to you. For this, however I should know more about how Postgres manages types internally... Regards, Peter Blazso
Peter Blazso <blazso@deltav.hu> writes:
> If you are interested, I could extend its functionality to other types
> as well and send this small pack to you. For this, however I should know
> more about how Postgres manages types internally...
I think it should be possible to make a type-independent version of that
code, and if you want to do so it'd be a great extension.
How does it look to the user? Something like
UPDATE table SET arrayfield = arrayInsert(arrayfield, index, newval)
UPDATE table SET arrayfield = arrayDelete(arrayfield, index)
I suppose? What do you do about multi-dimensional arrays?
One thing a number of people have complained about is that the natural
way to extend an array is
UPDATE table SET arrayfield[n+1] = newval
if arrayfield currently has n entries. The array assignment code ought
to handle this case but doesn't. I don't think it would be a huge fix
but I haven't looked at the code enough to understand what would need
to change.
regards, tom lane
Tom Lane wrote:
> I think it should be possible to make a type-independent version of that
> code, and if you want to do so it'd be a great extension.
I should have much more spare time but I'll try... :-)
> How does it look to the user? Something like
>
> UPDATE table SET arrayfield = arrayInsert(arrayfield, index, newval)
> UPDATE table SET arrayfield = arrayDelete(arrayfield, index)
Not exactly. My functions don't use indices yet and they still work only on one
dimensional 'int' arrays. You can insert a new value only as the last elem and
delete all values from the array that match a given integer. They can be used
like below:
UPDATE table SET arrayfield = array_app_int(arrayfield, newval); UPDATE table SET arrayfield =
array_del_int(arrayfield,matchval);
This time I focused only on user additions/deletions to/from a group in
'pg_group', however the code can be extended easily to do other things as well.
> What do you do about multi-dimensional arrays?
Unfortunately nothing, yet. This code is still an intro into array
manipulations.
> One thing a number of people have complained about is that the natural
> way to extend an array is
>
> UPDATE table SET arrayfield[n+1] = newval
>
> if arrayfield currently has n entries. The array assignment code ought
> to handle this case but doesn't. I don't think it would be a huge fix
> but I haven't looked at the code enough to understand what would need
> to change.
If you want I can send you these functions accompanied by some notes and
comments I just did ...
Peter Blazso