postgresql: enum and character varying, updating

前端 未结 3 1665
孤城傲影
孤城傲影 2021-01-06 17:17

I have the ENUM type in postgresql

CREATE TYPE user_state AS ENUM (\'unconfirmed\', \'locked\', \'active\');
<         


        
3条回答
  •  春和景丽
    2021-01-06 17:37

    Alternatively you may register equal operator instead of define cast one. I did it for Java + MyBatis in similar situation:

    CREATE FUNCTION type_user_state_with_text_equals(_a user_state, _b text)
        RETURNS boolean AS
        $func$
            SELECT _a = _b::user_state;
        $func$
        LANGUAGE SQL IMMUTABLE STRICT;
    
    CREATE OPERATOR = (
        leftarg = user_state,
        rightarg = text,
        procedure = type_user_state_with_text_equals,
        COMMUTATOR = =,
        NEGATOR = !=,
        HASHES, MERGES
    );
    

    You may read about postgres user-defined operations in documentation and do not forget look at optimization hints.

提交回复
热议问题