Search Postgresql Archives

Re: How to check whether a data type can be cast to another

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you Merlin and Tom. I appreciate your answers.

I have tried to follow that approach before I go this way. So, I checked the appendix  of the documentation for error conditions. Searching for cast-related conditions, I found INVALID_CHARACTER_VALUE_FOR_CAST. Would that be the exception to catch? It doesn't sound right.

The problem I faced is that the type 'any' is not permitted in procedural languages. If it were permitted, then we could have a function like is_castable( value ANY, target_type TEXT) and then we would catch exceptions if they are thrown. We didn't want to begin working on C functions before the prototype is complete.  However, if this is the only way, C is the way to go for now then.

If we can,
programmatically, figure out whether a type can be cast to another, we can then check each type's limits or valid values to consider the valid.

Here is what we have, there is a field in which we should store different data types and another field in the same row to store the identifier of the original data type of the first field. The first field should be polymorphic. Since I couldn't find how to make a field polymorphic, I resorted to a 'text' field so that different data types can be stored in the field after being cast to 'text'. When retrieving a field, its original state should be cast back from 'text'.

Do you think of a better strategy?

Thank you, Merlin and Tom. I benefited from your answers. I appreciate your help.

Regards,
Kareem

On 10/23/2009 04:00 PM, Tom Lane wrote:
Kareem Sedki <isiscreation@xxxxxxxxx> writes:
  
I am trying to write a function that takes one source and one target
data type identifiers, each as a '/text/' variable, as arguments and
return true if the cast can be made, false otherwise.
    
I think this is fundamentally the wrong approach, because it ignores the
question of whether a specific value can be cast.  For example, there is
a cast from int4 to int2, but that doesn't mean that 999999::int2 will
succeed.  What I think you should do is just try the cast and catch any
error that occurs.

If you really want to try to do what you're saying, look into
parser/parse_coerce.c.  In particular, can_coerce_type() is the
authoritative answer.

			regards, tom lane
  

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux