-
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
f3d5f27
4e979ca
4d4cfe4
9fac0f2
09ae015
c67ad6a
03fda89
661ad6d
883d4c5
f01507a
54eacfb
956e3c3
7e59a66
31b4294
0ded943
c115759
9d82436
dc52ab1
0cc3111
f39a6b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
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. 複数行のテンプレート文字列でインデントすると、以下のようにインデント分も空白として含まれてしまいますよ。
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. 余計な空白を含ませないようにSQLを1行で書くように修正しました! |
||
() => { | ||
let index = 0; | ||
|
||
bookTitles.forEach((title) => { | ||
db.run("INSERT INTO books (title) VALUES (?)", [title], () => { | ||
console.log(`ADD TITLE: ${title}`); | ||
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. 自動採番された ID を出力してください。 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. 修正しました! |
||
|
||
db.get("SELECT * FROM books WHERE title = ?", [title], (_, book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
index++; | ||
|
||
if (index >= bookTitles.length) { | ||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
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. コールバックベースの非同期処理は、ループと組み合わせてその実行順を制御することはできないです。 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. コメントありがとうございます。なるほど、これがコールバック形式の問題点なのですね! 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. ループ処理を除外しました! |
||
}, | ||
); |
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. ファイル名は名詞形になるようにしてください。文のように見えます。 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. 修正しました! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
() => { | ||
let index = 0; | ||
|
||
bookTitles.forEach((title) => { | ||
db.run("INSERT INTO books (title) VALUES (?)", [title], () => { | ||
console.log(`ADD TITLE: ${title}`); | ||
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. エラーメッセージを標準エラー出力に出力してください。 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. 実装漏れ失礼しました🙇♂️ |
||
|
||
db.get("SELECT * FROM books WHERE title = ?", [title], (_, book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
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. エラーメッセージを標準エラー出力に出力してください。 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. 実装漏れ失礼しました🙇♂️ |
||
index++; | ||
|
||
if (index >= bookTitles.length) { | ||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
run( | ||
db, | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
) | ||
.then(() => { | ||
let promise = Promise.resolve(); | ||
|
||
bookTitles.forEach((title) => { | ||
promise = promise | ||
.then(() => | ||
run(db, "INSERT INTO books (title) VALUES (?)", [title]).then(() => | ||
console.log(`ADD TITLE: ${title}`), | ||
), | ||
) | ||
.then(() => | ||
get(db, "SELECT * FROM books WHERE title = ?", [title]).then( | ||
(book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
}, | ||
), | ||
); | ||
}); | ||
|
||
return promise; | ||
}) | ||
.then(() => run(db, "DROP TABLE books")) | ||
.then(() => db.close()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
run( | ||
db, | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
) | ||
.then(() => { | ||
let promise = Promise.resolve(); | ||
|
||
bookTitles.forEach((title) => { | ||
promise = promise | ||
.then(() => | ||
run(db, "INSERT INTO books (title) VALUES (?)", [title]) | ||
.then(() => console.log(`ADD TITLE: ${title}`)) | ||
.catch((err) => { | ||
console.error(`ADD TITLE: ${title}, ${err.message}`); | ||
}), | ||
) | ||
.then(() => | ||
get(db, "SELECT * FROM NotExistTable WHERE title = ?", [title]) | ||
.then((book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
}) | ||
.catch((err) => { | ||
console.error(`GET TITLE: ${title}, ${err.message}`); | ||
}), | ||
); | ||
}); | ||
|
||
return promise; | ||
}) | ||
.then(() => run(db, "DROP TABLE books")) | ||
.then(() => db.close()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
const main = async () => { | ||
await run( | ||
db, | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
); | ||
|
||
for (const title of bookTitles) { | ||
await run(db, "INSERT INTO books (title) VALUES (?)", [title]); | ||
console.log(`ADD TITLE: ${title}`); | ||
|
||
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"); | ||
db.close(); | ||
}; | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
const main = async () => { | ||
await run( | ||
db, | ||
`CREATE TABLE books ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT NOT NULL UNIQUE | ||
)`, | ||
); | ||
|
||
for (const title of bookTitles) { | ||
try { | ||
await run(db, "INSERT INTO books (title) VALUES (?)", [title]); | ||
console.log(`ADD TITLE: ${title}`); | ||
} catch (err) { | ||
if (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.code === "SQLITE_ERROR") { | ||
console.error(`GET TITLE: ${title}, ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
await run(db, "DROP TABLE books"); | ||
db.close(); | ||
}; | ||
|
||
main(); |
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.
順番を管理する必要があるならしょうがないですが、必要がないならファイル名に prefix を付けるのはやめたほうがよいと思います。c1 が何を指しているのかもよく分からないです。
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.
承知しました!