I have an Informix database that I really would have liked an ‘enum’ field for. Being that Informix (At least in the version I am constrained to) has no built-in enum type I used some Google-Foo to find out that I could constrain a VARCHAR field to only allow certain values like so:
ALTER TABLE table ADD CONSTRAINT CHECK (type IN ('type1', 'type2', 'type3'));
This seems to work well. Now I need to connect to the database from a Perl script that checks user input against those valid values. I can of course check them in code and make sure my code knows what the values are that the database requires, but I wondered if there is any way I could have my script query the constraint on the database therefore if I need to Add/Remove a valid input at a later time I could just alter the constraint and the processing code would adapt.
Any suggestions would be greatly appreciated.
If you encapsulate you database access in a DBIx::Class schema, you gain this kind of validation “as a bonus”.
A simple way of generating the schema from a pre-existing database is by
dbicdump.