Thread: inet value validation
hello, I want to write a function to validate an inet data type, but I figure that postgres should already have such a function to use before inserting values in inet type columns. is it possible to access postgres's validation function for inet types ? I have snooped through the catalogs but I can not find such a function. thanks, Razvan Radu
pgsql-general@list.coretech.ro <pgsql-general@list.coretech.ro> schrieb: > > hello, > > I want to write a function to validate an inet data type, but I figure that > postgres should already have such a function to use before inserting values > in inet type columns. > is it possible to access postgres's validation function for inet types ? I > have snooped through the catalogs but I can not find such a function. You can try to cast a string into inet like this: test=# select '127.0.0.255'::inet; inet ------------- 127.0.0.255 (1 row) test=# select '127.0.0.256'::inet; ERROR: invalid input syntax for type inet: "127.0.0.256" Now you can write a little function to do this: ,----[ a little function ] | create or replace function check_ip(varchar) returns bool as $$ | declare | i inet; | begin | i := $1::inet; | return 't'::bool; | EXCEPTION WHEN invalid_text_representation then | return 'f'::bool; | end; | $$ language plpgsql immutable strict; `---- You can use this function now inside a transaction. HTH, Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknow) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
yes, this is a good example, but I do not want to use an exception block because of the warning present on documentation regarding exception blocks "*Tip: * A block containing an EXCEPTION clause is significantly more expensive to enter and exit than a block without one. Therefore, don't use EXCEPTION without need." I intend to use this function heavily. Razvan Radu Andreas Kretschmer wrote: > pgsql-general@list.coretech.ro <pgsql-general@list.coretech.ro> schrieb: > > >> hello, >> >> I want to write a function to validate an inet data type, but I figure that >> postgres should already have such a function to use before inserting values >> in inet type columns. >> is it possible to access postgres's validation function for inet types ? I >> have snooped through the catalogs but I can not find such a function. >> > > You can try to cast a string into inet like this: > > test=# select '127.0.0.255'::inet; > inet > ------------- > 127.0.0.255 > (1 row) > > test=# select '127.0.0.256'::inet; > ERROR: invalid input syntax for type inet: "127.0.0.256" > > > Now you can write a little function to do this: > > ,----[ a little function ] > | create or replace function check_ip(varchar) returns bool as $$ > | declare > | i inet; > | begin > | i := $1::inet; > | return 't'::bool; > | EXCEPTION WHEN invalid_text_representation then > | return 'f'::bool; > | end; > | $$ language plpgsql immutable strict; > `---- > > You can use this function now inside a transaction. > > > > HTH, Andreas >
pgsql-general@list.coretech.ro <pgsql-general@list.coretech.ro> schrieb: > > yes, this is a good example, but I do not want to use an exception block > because of the warning present on documentation regarding exception blocks No problem, i want to show a transaction-secure solution. You can use this without the exception block, but if you got a error, a transaction will be failed. > Andreas Kretschmer wrote: > >pgsql-general@list.coretech.ro <pgsql-general@list.coretech.ro> schrieb: > > > >>hello, Argh. Please no silly fullquote below the text. Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknow) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°