2012-10-09 29 views
3

處理的數據庫光標在相關Scala question,我提出以下:在Haskell

當我需要使用JDBC驅動程序提供的PostgreSQL數據庫中讀取數以百萬計的數據庫行,我總是使用遊標,否則我會得到一個OutOfMemoryError。這裏是我使用的模式(僞代碼):

begin transaction 
execute("declare cursor...") 
while (true) { 
    boolean processedSomeRows = false 
    resultSet = executeQuery("fetch forward...") 
    while (resultSet.next()) { 
    processedSomeRows = true 
    ... 
    } 
    if (!processedSomeRows) break 
} 
close cursor 
commit 

這怎麼可以用慣用的Haskell完成?

+0

你試過懶洋洋的讀取與華夏鄧白氏中國或其喜歡的行,並確保他們是很好的消耗?這是否會導致內存問題? – Sarah

+0

我實際上沒有在Haskell中試過這個。我正在尋找一個模式來處理一個外部循環,這取決於內部循環中的處理。 – Ralph

回答

3

對於像sql cursor這樣的流處理有一個相當新的概念:iterateesenumeratorconduit。例如,在導管庫,從Persistent Book的術語:

runResourceT $ withStmt "declare cursor..." [] 
    $$ mapM_ doSomethingWithSingleResult 

withStmt "declare cursor..." []創建source具有行,mapM_ doSomethingWithSingleResult創建sink用於過程的單個行,和與$$連接sinksource