Skip to content

executeMany fails when given empty list of parameters #30

@chshersh

Description

@chshersh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions