Elixir: MariaDB のデータを削除 (Delete)

Go のプログラムを、ChatGPT と Gemini に渡して得られた結果です。

## Go のプログラム

```go:maria_delete.go
// ----------------------------------------------------------------
//
//	maria_delete.go
//
//					Mar/15/2024
//
// ----------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"gorm.io/gorm"
	"gorm.io/driver/mysql"
)

// ----------------------------------------------------------------
func main() {
	fmt.Fprintln (os.Stderr,"*** 開始 ***")
	key_in := os.Args[1]

	fmt.Printf ("key_in = %s\n" , key_in)

	host := "localhost"
	user := "scott"
	password := "tiger123"
	data_base := "city"

dsn := user + ":" + password + "@tcp(" + host + ":3306)/" + data_base + "?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	db.Exec ("delete from cities WHERE id = ?", key_in)



	fmt.Fprintln (os.Stderr,"*** 終了 ***")
}

// ----------------------------------------------------------------
```

## ChatGPT が作成したプログラム

```exs:lib/maria_delete.ex
# ----------------------------------------------------------------
#
#	maria_delete.exs
#
#					Mar/15/2024
#
# ----------------------------------------------------------------

defmodule MariaDelete do
  def main(args) do
    IO.puts "*** 開始 ***"
    [key_in | _] = args

    IO.puts "key_in = #{key_in}"

    host = "localhost"
    user = "scott"
    password = "tiger123"
    database = "city"

    {:ok, pid} = Mariaex.start_link(username: user, password: password, database: database, hostname: host)

    Mariaex.query!(pid, "DELETE FROM cities WHERE id = $1", [key_in])

    IO.puts "*** 終了 ***"
  end
end

# Run the main function if this script is invoked directly
if System.argv() != [] do
  MariaDelete.main(System.argv())
end
```

```exs:mix.exs
（省略）
	{:mariaex, "~> 0.9.1"},
（省略）
```

実行結果

```text
$ mix run main t3322
Compiling 1 file (.ex)
*** 開始 ***
key_in = t3322

== Compilation error in file lib/maria_delete.ex ==
** (MatchError) no match of right hand side value: {:error, {:noproc, {GenServer, :call, [DBConnection.Watcher, {:watch, DBConnection.ConnectionPool.Supervisor, {DBConnection.ConnectionPool.Pool, {#PID<0.186.0>, #Reference<0.735947604.2707030022.169983>, Mariaex.Protocol, [username: "scott", password: "tiger123", database: "city", hostname: "localhost"]}}}, :infinity]}}}
    lib/maria_delete.ex:21: MariaDelete.main/1
    (elixir 1.14.0) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
```

## Gemini が作成したプログラム

```exs:lib/maria_delete.ex
# -*- coding: utf-8 -*-

defmodule MariaDelete do

  def main(args) do
    IO.puts "*** 開始 ***"
    key_in = args |> hd

    IO.puts "key_in = #{key_in}"

    host = "localhost"
    user = "scott"
    password = "tiger123"
    database = "city"

    dsn = "#{user}:#{password}@tcp(#{host}:3306)/#{database}?charset=utf8mb4&parseTime=True&loc=Local"

    {:ok, db} = Ecto.Adapters.MySQL.start_link(dsn)

    Ecto.Adapters.MySQL.query(db, "delete from cities WHERE id = ?", [key_in])

    IO.puts "*** 終了 ***"
  end
end

MariaDelete.main(System.argv)
```

```exs:mix.exs
（省略）
	{:ecto, "~> 3.11"},
（省略）
```

実行結果

```text
$ mix run  -e MyApp.MariaDelete.main t3323
Compiling 1 file (.ex)
*** 開始 ***
key_in = t3323

== Compilation error in file lib/maria_delete.ex ==
** (UndefinedFunctionError) function Ecto.Adapters.MySQL.start_link/1 is undefined (module Ecto.Adapters.MySQL is not available)
    Ecto.Adapters.MySQL.start_link("scott:tiger123@tcp(localhost:3306)/city?charset=utf8mb4&parseTime=True&loc=Local")
    lib/maria_delete.ex:18: MariaDelete.main/1
    (elixir 1.14.0) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
```

## 私が作成したプログラム

```exs:lib/lib/maria_delete.ex
defmodule MariaDelete do

  def main([]) do
    IO.puts "*** 開始 ***"
    [key_in] = System.argv()
    IO.puts key_in
#
    host = "localhost"
    user = "scott"
    password = "tiger123"
    database = "city"

    {:ok, pid} = Mariaex.start_link(hostname: host, username: user, password: password, database: database)
#
    command = "delete from cities where id = '#{key_in}'"
    IO.puts command
    Mariaex.query!(pid, command, [])
#
    aa = Mariaex.query!(pid, "SELECT * FROM cities", [])

    IO.inspect aa
#
    IO.puts "*** 終了 ***"
  end

end
```

```exs:mix.exs
（省略）
	{:mariaex, "~> 0.9.1"},
（省略）
```

実行結果

```text
$ mix run -e "MariaDelete.main([])" t3322
Compiling 1 file (.ex)
*** 開始 ***
t3322
delete from cities where id = 't3322'
%Mariaex.Result{
  columns: ["id", "name", "population", "date_mod"],
  rows: [
    ["t3321", "岡山", 529176, ~D[2003-09-20]],
    ["t3324", "玉野", 369172, ~D[2003-01-09]],
    ["t3325", "笠岡", 237451, ~D[2003-03-04]],
    ["t3326", "井原", 518397, ~D[2003-05-21]],
    ["t3327", "総社", 248156, ~D[2003-07-23]],
    ["t3328", "高梁", 478294, ~D[2003-10-26]],
    ["t3329", "新見", 863751, ~D[2003-12-15]]
  ],
  last_insert_id: nil,
  num_rows: 7,
  connection_id: #PID<0.202.0>
}
*** 終了 ***
```