プログラム
フォルダー構造
$ tree -a
.
├── .env
└── maria_read.ts
maria_read.ts
// ---------------------------------------------------------------
// maria_read.ts
//
// Feb/27/2023
//
// ---------------------------------------------------------------
import { Client } from "https://deno.land/x/mysql/mod.ts"
import { config } from "https://deno.land/x/dotenv/mod.ts"
// ---------------------------------------------------------------
function sort_by_key_proc(left: {[key: string]: any},right: {[key: string]: any})
{
const aa:string = left.key
const bb:string = right.key
var rvalue = 0
if (aa < bb)
{
rvalue = -1
}
else if (aa > bb)
{
rvalue = 1
}
return rvalue
}
// ---------------------------------------------------------------
function sort_proc (dict_aa: {[key: string]: any})
{
var array = new Array()
for(var it in dict_aa)
{
array.push({'key':String (it), 'value':dict_aa[it]})
}
array.sort (sort_by_key_proc)
return array
}
// ---------------------------------------------------------------
export function dict_display_proc(dict_aa: {[key: string]: any})
{
var array_aa = sort_proc (dict_aa)
for (var it in array_aa)
{
var key:string = array_aa[it].key
var value = array_aa[it].value
var out_str:string = key + "\t"
out_str += value["name"] + "\t"
out_str += value["population"] + "\t"
out_str += value["date_mod"]
console.log (out_str)
}
}
// ---------------------------------------------------------------
function data_process(data:any):{[key: string]: any}
{
var dict_aa : {[key: string]: any} = new Object()
for (var it in data)
{
const unit = data[it]
dict_aa[unit.id] = {"name": unit.name,
"population": unit.population,
"date_mod": unit.date_mod}
}
return dict_aa
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const config_env:any = config()
const client = await new Client().connect({
hostname: config_env.host,
username: config_env.user,
password: config_env.password,
db: config_env.data_base,
})
const data = await client.query(`select * from cities`)
await client.close()
const dict_aa = data_process(data)
dict_display_proc (dict_aa)
console.error ("*** 終了 ***")
// ---------------------------------------------------------------
.env
はこちら
Deno: MariaDB のバージョンを表示
実行コマンド
deno run --allow-read --allow-net maria_read.ts
確認したバージョン
$ deno --version
deno 1.31.0 (release, x86_64-unknown-linux-gnu)
v8 11.0.226.13
typescript 4.9.4