Elixir: MariaDB のデータを更新 (Update)

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

## Ruby のプログラム

```rb:maria_update.rb
#! /usr/bin/ruby
#
#	maria_update.rb
#
#					Mar/15/2024
#
require 'mysql2'
require 'date'
#
# ------------------------------------------------------------
def	update_proc (client,id,population)
	date_mod=Date.today
	sql_str="UPDATE cities SET population='#{population}', DATE_MOD='#{date_mod}' where ID = '#{id}'"
	client.query(sql_str)
end
# ------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
id_in = ARGV[0]
population_in = ARGV[1].to_i
#
puts id_in,population_in
#
host = "localhost"
user = "scott"
password = "tiger123"
data_base = 'city'
client = Mysql2::Client.new(host: host, username: user, password: password, database: data_base)
#
update_proc(client,id_in,population_in)
#
client.close
#
STDERR.puts "*** 終了 ***"
# ------------------------------------------------------------
```

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

```exs:lib/maria_update.ex
defmodule MariaUpdate do
  def update_proc(id, population) do
    date_mod = Date.utc_today()
    sql_str = "UPDATE cities SET population='#{population}', DATE_MOD='#{date_mod}' WHERE ID = '#{id}'"
    {:ok, conn} = Mariaex.start_link(username: "scott", password: "tiger123", database: "city", hostname: "localhost")
    {:ok, _result} = Mariaex.query(conn, sql_str)
    Mariaex.stop(conn)
  end
end

IO.puts("*** 開始 ***")

[id_in, population_in] = System.argv()[1..2]
|> Enum.map(&String.to_integer/1)

MariaUpdate.update_proc(id_in, population_in)

IO.puts("*** 終了 ***")
```

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


実行結果

```text
$ mix run t3321 789
Compiling 1 file (.ex)
*** 開始 ***

== Compilation error in file lib/maria_update.ex ==
** (ArgumentError) the Access calls for keywords expect the key to be an atom, got: 1..2
    (elixir 1.14.0) lib/access.ex:313: Access.get/3
    lib/maria_update.ex:13: (file)
    (elixir 1.14.0) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
```

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

```exs:lib/maria_update.ex
# Import necessary libraries
defmodule MyApp.MariaUpdate do
  import Ecto.Query

  # Schema representing the "cities" table (assuming it exists)
  @schema_def %{
    __meta__: %{source: :cities},
    id: :string, primary_key: true,
    name: :string,
    population: :integer,
    date_mod: :date
  }

  # Function to update a city's population
  def update_city(repo, id, population) do
    date_mod = Date.today()

    query = quote do
      update c in City, set: [population: ^population, date_mod: ^date_mod],
        where: [id: ^id]
    end

    Ecto.execute(repo, query)
  end

  # Main function to process command-line arguments and update data
  def main do
    id = System.argv[1]
    population = String.to_integer(System.argv[2])

    IO.puts("ID: #{id}, Population: #{population}")

    repo = Ecto.Repo.new(MyApp.Repo, adapter: Ecto.Adapters.MySQL)
    update_city(repo, id, population)

    IO.puts("*** 終了 ***")
  end
end
```

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

実行結果

```text
$ mix run  -e MyApp.MariaUpdate.main t3325 89
** (ArgumentError) the Access calls for keywords expect the key to be an atom, got: 1
    (elixir 1.14.0) lib/access.ex:313: Access.get/3
    (maria_update 0.1.0) lib/maria_update.ex:28: MyApp.MariaUpdate.main/0
    (stdlib 4.2) erl_eval.erl:744: :erl_eval.do_apply/7
    (elixir 1.14.0) lib/code.ex:422: Code.validated_eval_string/3
    (elixir 1.14.0) lib/enum.ex:975: Enum."-each/2-lists^foreach/1-0-"/2
    (mix 1.14.0) lib/mix/tasks/run.ex:140: Mix.Tasks.Run.run/5
```

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

```exs:lib/maria_update.ex
defmodule MariaUpdate do

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

    {:ok, pid} = Mariaex.start_link(hostname: host, username: user, password: password, database: database)
#
    date_mod = Date.utc_today()
    command = "UPDATE cities SET population='#{population_in}', DATE_MOD='#{date_mod}' WHERE ID = '#{id_in}'"
    IO.puts command
    Mariaex.query!(pid, command, [])
#
    aa = Mariaex.query!(pid, "SELECT * FROM cities", [])

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

end
```

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

実行結果

```text
$ mix run -e "MariaUpdate.main([])" t3322 892000
*** 開始 ***
t3322
892000
UPDATE cities SET population='892000', DATE_MOD='2024-03-20' WHERE ID = 't3322'
%Mariaex.Result{
  columns: ["id", "name", "population", "date_mod"],
  rows: [
    ["t3321", "岡山", 529176, ~D[2003-09-20]],
    ["t3322", "倉敷", 892000, ~D[2024-03-20]],
    ["t3323", "津山", 163754, ~D[2003-08-18]],
    ["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: 9,
  connection_id: #PID<0.190.0>
}
*** 終了 ***
```