Обсуждение: writing own cast
hi, i wrote my own hexadecimal datatype for postgresql. the input function expects a char* and stores the data as an integer. so i can do something like "SELECT 'ff'::hex;" to turn a string into my datatype. but i would like do be able doing the same with integers: SELECT 255::hex; ... and then getting 'ff'. do you know how i can realize that? i already wrote a function 'to_hex' (int->hex) and 'to_int' (hex->int). thanks, moritz --
Moritz Sinn <moritz@freesources.org> writes: > but i would like do be able doing the same with integers: > SELECT 255::hex; > ... and then getting 'ff'. > do you know how i can realize that? Sure, make a function hex(int) returning hex. The function name must match the datatype name. regards, tom lane
Tom Lane <tgl@sss.pgh.pa.us> writes: > Sure, make a function hex(int) returning hex. The function name > must match the datatype name. thanks, now it works! but i've to type 255::int::hex, because when i type 255::hex it first casts the int to an string and then to hex, so it returns 255 instead of ff. well, that doesn't matter much, but it interests me if this problem could be solved too. i think the reason is that the input function of my hexadecimal datatype expects an string, and so the int is turned into an string and then passed to this input function. regards, moritz -- Try to remove the color-problem by restarting your computer several times. -- Microsoft Internet Explorer README.TXT
Moritz Sinn <moritz@freesources.org> writes: > but i've to type 255::int::hex, because when i type 255::hex it first > casts the int to an string and then to hex, so it returns 255 instead of > ff. I think this means that you need to rethink your I/O definitions for the datatype. 255::hex is not really different from '255'::hex, and should not be because the type of the literal constant is as yet unassigned. The operation invoked is your type's input routine, and nothing else. An operation like integercolumn::hex is a completely different thing. regards, tom lane