我希望在可能存在或不存在该列的表上运行drop列CQL语句。理想情况下,我希望做一个if-存在然后删除列,但我不相信CQL支持这一点。
我的工作是捕获一个无效的查询异常,但这不起作用,我不知道原因。在下面的代码中,泛型"com.datastax.driver.core.exceptions.InvalidQueryException:将命中给出在表my_table中找不到的消息my_col列my_table“。我不知道为什么我的InvalidQueryException没有击中。
(ns my-namespace
(:require [qbits
[alia :as alia]
[hayt :as hayt]]))
(defn prepared-statement
[session]
(try
(alia/execute session
(hayt/alter-table :my_table
(hayt/drop-column :my_col)))
(catch com.datastax.driver.core.exceptions.InvalidQueryException ie
(prn "your column doesn't exist"))
(catch Exception e
(prn (ex-data e)))))
发布于 2022-11-26 17:53:21
看看来源 for alia/execute
。如您所见,它已经包含了只包含区块的Exception
。
因此,您必须调用CqlSession
的CqlSession
方法(并使用hayt/->raw
创建查询字符串):
(:import (com.datastax.oss.driver.api.core.servererrors InvalidQueryException))
(defn prepared-statement
[session]
(try (.execute session
(hayt/->raw (hayt/alter-table :my_table
(hayt/drop-column :my_col))))
(catch InvalidQueryException ie
(prn "your column doesn't exist"))
(catch Exception e
(prn (ex-data e)))))
测试:
(prepared-statement s)
"your column doesn't exist"
=> nil
https://stackoverflow.com/questions/74578173
复制相似问题