Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executeMany fails when given empty list of parameters #30

Open
chshersh opened this issue Oct 30, 2018 · 1 comment
Open

executeMany fails when given empty list of parameters #30

chshersh opened this issue Oct 30, 2018 · 1 comment

Comments

@chshersh
Copy link

The type of executeMany function is the following:

executeMany :: QueryParam p => MySQLConn -> Query -> [[p]] -> IO [OK]

But when I'm trying to pass an empty list as an argument of type [[p]] I see the following error:

ERRException (ERR {errCode = 1065, errState = "42000", errMsg = "Query was empty"})

I see two possible solutions to this problem:

  1. Patch implementation of executeMany function to do nothing when empty list is given.
  2. Change type of argument from [[p]] to NonEmpty [p].

Here is the complete code where I observe the error:

#! /usr/bin/env cabal
{- cabal:
build-depends: base >= 4.11 && < 4.12
             , mysql-haskell ^>= 0.8.4.1
             , io-streams
-}

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.String (fromString)
import Database.MySQL.Base
import Database.MySQL.Connection (utf8mb4_unicode_ci)

import qualified System.IO.Streams as Stream


createSchema :: Query
createSchema = fromString $ unlines
    [ "DROP TABLE IF EXISTS users;"
    , "CREATE TABLE users"
    , "    ( id INT AUTO_INCREMENT PRIMARY KEY"
    , "    , name TEXT NOT NULL"
    , "    ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4;"
    ]

insertUser :: Query
insertUser = "INSERT INTO users (name) VALUES (?)"

main :: IO ()
main = do
    (greet, conn) <- connectDetail ConnectInfo
        { ciHost = "127.0.0.1"
        , ciPort = 3306
        , ciUser = "root"
        , ciPassword = "password"
        , ciDatabase = "test_db"
        , ciCharset = utf8mb4_unicode_ci
        }
    print greet

    print =<< mapM (executeMany_ conn) [createSchema]
    print =<< executeMany conn insertUser ([] :: [[MySQLValue]])
    (_defs, is) <- query_ conn "SELECT * FROM `users`"
    print =<< Stream.toList is
@winterland1989
Copy link
Owner

I prefer the first behavior, would you like to send me a patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants