0

What is the proper way to use a single connection and run multiple queries when connecting to MS SQL server from Haskell?

import qualified Database.HDBC as DB
import qualified Database.HDBC.ODBC as DB
import Data.Time.Calendar (fromGregorian)
import Control.Monad (forM_)

testQueries :: String -> IO ()
testQueries connectionString =
  do
    conn <- DB.connectODBC connectionString
    forM_ [startDate..endDate] $ \date -> do
      putStrLn $ "processing date: " ++ show date
      putStrLn . show . length <$> DB.quickQuery' conn q []
    DB.disconnect conn
  where
    q :: String
    q =
      "SELECT TOP 10 * FROM INFORMATION_SCHEMA.COLUMNS"
    startDate =
      fromGregorian 2016 9 14
    endDate =
      fromGregorian 2016 11 29

With both the ODBC and FreeTDS drivers this query fails after processing a nondeterministic number of dates. It also never prints any of the output sizes (10 expected) to stdout. Sample output:

processing date: 2016-09-14
processing date: 2016-09-15
processing date: 2016-09-16
processing date: 2016-09-17
processing date: 2016-09-18
processing date: 2016-09-19
processing date: 2016-09-20
processing date: 2016-09-21
processing date: 2016-09-22
processing date: 2016-09-23
processing date: 2016-09-24
processing date: 2016-09-25
processing date: 2016-09-26
processing date: 2016-09-27
processing date: 2016-09-28
exe: Prelude.chr: bad argument: 5832776

Is this usage pattern incorrect?

System info:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 17.10
Release:    17.10
Codename:   artful
$ cat /etc/odbcinst.ini 
[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1
UsageCount=1

[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
UsageCount = 1
duplode
  • 33,731
  • 7
  • 79
  • 150
bkc
  • 1
  • 2

1 Answers1

0

With both the ODBC and FreeTDS drivers this query fails after processing a nondeterministic number of dates.

This issue appears to have been reported as GitHub issue #28 for HDBC-OBCD. The workaround proposed there is to prepend DSN= to the connection string.

It also never prints any of the output sizes (10 expected) to stdout.

The type of putStrLn . show . length <$> DB.quickQuery' conn q [] is IO (IO ()). You are creating an IO computation that prints the length, but you aren't actually having it executed. To fix that, replace (<$>) with (>>=), or something equivalent to it:

    DB.quickQuery' conn q [] >>= putStrLn . show . length 
    putStrLn . show . length =<< DB.quickQuery' conn q []
    rows <- DB.quickQuery' conn q []
    putStrLn . show . length $ rows

P.S.: putStrLn . show can be more conveniently written as print.

duplode
  • 33,731
  • 7
  • 79
  • 150