forked from fjordllc/js-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
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
非同期処理プログラムの提出 #3
Open
miyanew
wants to merge
20
commits into
main
Choose a base branch
from
my-asynchronous
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f3d5f27
splite3をインストール
miyanew 4e979ca
エラーなしエラーありをファイル分割して作成する
miyanew 4d4cfe4
Promiseベースなsqlite3の関数を切り出す
miyanew 9fac0f2
async版にもヘルパー関数を適用する
miyanew 09ae015
捕捉したい例外のみを捕捉する
miyanew c67ad6a
エラーメッセージを標準エラー出力に出力する
miyanew 03fda89
余計な空白を含まないようにSQLを1行でかく
miyanew 661ad6d
自動採番された ID を出力する
miyanew 883d4c5
ファイル名をプレフィックスのない名詞形にする
miyanew f01507a
実行順を制御できてないためコールバックベースの処理からループを除外する
miyanew 54eacfb
DBクローズをPromiseとして扱えるようにする
miyanew 956e3c3
不要なデフォルト引数を削除する
miyanew 7e59a66
待機できてないのでpromiseベースの処理からループを除外する
miyanew 31b4294
課題要件をみたすため参照エラーが起きるようにする
miyanew 0ded943
不要なネストを修正する
miyanew c115759
アロー関数の簡易文体でそろえる
miyanew 9d82436
Promiseの渡し漏れを直す
miyanew dc52ab1
async関数を定義せずTop-level awaitでシンプルに書く
miyanew 0cc3111
catchで捕捉したオブジェクトがErrorであり指定プロパティをもつことを確認する
miyanew f39a6b9
フォーマッタの修正を反映する
miyanew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get, close } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
await run( | ||
db, | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
); | ||
|
||
for (const title of bookTitles) { | ||
const result = await run(db, "INSERT INTO books (title) VALUES (?)", [title]); | ||
console.log(`ADD TITLE: ${title}, ID: ${result.lastID}`); | ||
|
||
const book = await get(db, "SELECT * FROM books WHERE title = ?", [title]); | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} | ||
|
||
await run(db, "DROP TABLE books"); | ||
|
||
await close(db); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get, close } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
await run( | ||
db, | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
); | ||
|
||
for (const title of bookTitles) { | ||
try { | ||
const result = await run(db, "INSERT INTO books (title) VALUES (?)", [ | ||
title, | ||
]); | ||
console.log(`ADD TITLE: ${title}, ID: ${result.lastID}`); | ||
} catch (err) { | ||
if ( | ||
err instanceof Error && | ||
"code" in err && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 質問ですが、 |
||
err.code === "SQLITE_CONSTRAINT" | ||
) { | ||
console.error(`ADD TITLE: ${title}, ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
|
||
try { | ||
const book = await get(db, "SELECT * FROM NotExistTable WHERE title = ?", [ | ||
title, | ||
]); | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} catch (err) { | ||
if (err instanceof Error && "code" in err && err.code === "SQLITE_ERROR") { | ||
console.error(`GET TITLE: ${title}, ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
await run(db, "DROP TABLE books"); | ||
|
||
await close(db); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitle = "booktitle_01"; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
() => { | ||
db.run("INSERT INTO books (title) VALUES (?)", [bookTitle], function () { | ||
console.log(`ADD TITLE: ${bookTitle}, ID: ${this.lastID}`); | ||
|
||
db.get("SELECT * FROM books WHERE title = ?", [bookTitle], (_, book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
|
||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
}); | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitle = "booktitle_01"; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
() => { | ||
db.run( | ||
"INSERT INTO NotExistTable (title) VALUES (?)", | ||
[bookTitle], | ||
function (err) { | ||
if (err) { | ||
console.error(`ADD TITLE: ${bookTitle}, ${err.message}`); | ||
} else { | ||
console.log(`ADD TITLE: ${bookTitle}, ID: ${this.lastID}`); | ||
} | ||
|
||
db.get( | ||
"SELECT * FROM NotExistTable WHERE title = ?", | ||
[bookTitle], | ||
(err, book) => { | ||
if (err) { | ||
console.error(`GET TITLE: ${bookTitle}, ${err.message}`); | ||
} else { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} | ||
|
||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
}, | ||
); | ||
}, | ||
); | ||
}, | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コールバックや Promise のプログラムと処理内容が異なっています。前のプログラムを順に書き換えていく課題なので、処理内容はプログラム間で統一してください。