-- | Internal function that opens a connection to the MySQL
-- server.
open' :: MySQL.ConnectInfo -> LogFunc -> IO SqlBackend
open' ci logFunc = do
    conn <- MySQL.connect ci
    MySQLBase.autocommit conn False -- disable autocommit!
    smap <- newIORef $ Map.empty
    return SqlBackend
        { connPrepare    = prepare' conn
        , connStmtMap    = smap
        , connInsertSql  = insertSql'
        , connInsertManySql = Nothing
        , connClose      = MySQL.close conn
        , connMigrateSql = migrate' ci
        , connBegin      = const $ MySQL.execute_ conn "start transaction" >> return ()
        , connCommit     = const $ MySQL.commit   conn
        , connRollback   = const $ MySQL.rollback conn
        , connEscapeName = pack . escapeDBName
        , connNoLimit    = "LIMIT 18446744073709551615"
        -- This noLimit is suggested by MySQL's own docs, see
        -- <http://dev.mysql.com/doc/refman/5.5/en/select.html>
        , connRDBMS      = "mysql"
        , connLimitOffset = decorateSQLWithLimitOffset "LIMIT 18446744073709551615"
        , connLogFunc    = logFunc
        }

-- | Prepare a query.  We don't support prepared statements, but
-- we'll do some client-side preprocessing here.
open' :: MySQL.ConnectInfo -> LogFunc -> IO SqlBackend
open' ci logFunc = do
    conn <- MySQL.connect ci
    MySQLBase.autocommit conn False -- disable autocommit!
    smap <- newIORef $ Map.empty
    return SqlBackend
        { connPrepare    = prepare' conn
        , connStmtMap    = smap
        , connInsertSql  = insertSql'
        , connInsertManySql = Nothing
        , connClose      = MySQL.close conn
        , connMigrateSql = migrate' ci
        , connBegin      = const $ MySQL.execute_ conn "start transaction" >> return ()
        , connCommit     = const $ MySQL.commit   conn
        , connRollback   = const $ MySQL.rollback conn
        , connEscapeName = pack . escapeDBName
        , connNoLimit    = "LIMIT 18446744073709551615"
        -- This noLimit is suggested by MySQL's own docs, see
        -- <http://dev.mysql.com/doc/refman/5.5/en/select.html>
        , connRDBMS      = "mysql"
        , connLimitOffset = decorateSQLWithLimitOffset "LIMIT 18446744073709551615"
        , connLogFunc    = logFunc
        }

-- | Prepare a query.  We don't support prepared statements, but
-- we'll do some client-side preprocessing here.
open' ci logFunc = do
    conn <- MySQL.connect ci
    MySQLBase.autocommit conn False -- disable autocommit!
    smap <- newIORef $ Map.empty
    return SqlBackend
        { connPrepare    = prepare' conn
        , connStmtMap    = smap
        , connInsertSql  = insertSql'
        , connInsertManySql = Nothing
        , connClose      = MySQL.close conn
        , connMigrateSql = migrate' ci
        , connBegin      = const $ MySQL.execute_ conn "start transaction" >> return ()
        , connCommit     = const $ MySQL.commit   conn
        , connRollback   = const $ MySQL.rollback conn
        , connEscapeName = pack . escapeDBName
        , connNoLimit    = "LIMIT 18446744073709551615"
        -- This noLimit is suggested by MySQL's own docs, see
        -- <http://dev.mysql.com/doc/refman/5.5/en/select.html>
        , connRDBMS      = "mysql"
        , connLimitOffset = decorateSQLWithLimitOffset "LIMIT 18446744073709551615"
        , connLogFunc    = logFunc
        }

-- | Prepare a query.  We don't support prepared statements, but
-- we'll do some client-side preprocessing here.