例如,这是 PostgreSQL 中的一个产品表,其状态为枚举:
create type product_status as enum ('InStock', 'OutOfStock');
create table product (
pid int primary key default nextval('product_pid_seq'),
sku text not null unique,
name text not null,
description text not null,
quantity int not null,
cost numeric(10,2) not null,
price numeric(10,2) not null,
weight numeric(10,2),
status product_status not null
);
插入产品的典型 Clojure 代码是:
(def prod-12345 {:sku "12345"
:name "My Product"
:description "yada yada yada"
:quantity 100
:cost 42.00
:price 59.00
:weight 0.3
:status "InStock"})
(sql/with-connection db-spec
(sql/insert-record :product prod-12345))
但是,status
它是一个枚举,因此如果不将其转换为枚举,就不能将其作为普通字符串插入:
'InStock'::product_status
我知道您可以使用准备好的声明来做到这一点,例如:
INSERT INTO product (name, status) VALUES (?, ?::product_status)
但是有没有办法在不使用准备好的语句的情况下做到这一点?