更新DB函数clojure

时间:2013-02-23 23:46:34

标签: clojure functional-programming

我正在尝试构建一个类似于SQL的函数:

UPDATE Persons SET Address=Martin20, City=Miami WHERE LastName=Darmon AND FirstName=Gilad

我已经解析了SET和Where到地图和列表。
现在我想更新我的桌子。但是我收到了错误

Exception in thread "main" setMap #<Ref@6bad186f: [:Address Martin20 :City Miami]>
whereList #{0}
java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to java.util.Map$Entry
    at clojure.lang.APersistentMap$KeySeq.first(APersistentMap.java:132)
    at clojure.lang.RT.first(RT.java:559)
    at clojure.core$first.invoke(core.clj:55)
    at ClojureSQL$UpdateRecord.invoke(project.clj:205)
    at ClojureSQL$UpdateTable.invoke(project.clj:253)
    at ClojureSQL$Execute.invoke(project.clj:285)
    at ClojureSQL$eval1340.invoke(project.clj:312)

这是为Update功能设置参数的功能:

;UpdateTable function
(defn UpdateTable [updateQuery]
  "The UPDATE statement is used to update existing records in a table"
  (println "UPDATE ")
  (let [SpecificGetTableName (CreateGetTableNameFunc #"UPDATE ([_0-9a-zA-Z]+)") 
        tableName (SpecificGetTableName updateQuery)
        whereList (ListRecordsMatchConditions updateQuery tableName)        ; return the row numbers in which we need to update 
        setMap    (GetValuesFromSetCommand updateQuery)]
    (UpdateRecord tableName whereList setMap)
  )
)

错误在doseq函数中:

;update specific table records (from whereList) with new values (from setMap)
(defn UpdateRecord [tableName whereList setMap]
  "Insert values into table in order of keys / columns"
  (println "setMap" setMap)
  (println "whereList" whereList)
  (let [tableRef (get (deref dataBase) tableName)]        
    (doseq [curKey (keys @setMap) i whereList]         ; <-here is the BUG

       (dosync (alter (tableRef curKey) assoc i val))  ; perfrom thread safe insert of value to column
    )
    (println tableRef) 
  )
)

1 个答案:

答案 0 :(得分:1)

你的setMap ref包装了一个矢量,而不是一个地图。 keys函数从地图返回一系列键,而不是向量。您需要更改setMap以包装地图:

(ref {:Address "Martin20" :City "Miami"})