readtest/source/sql.d

52 lines
1.5 KiB
D
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module source.sql;
import singlog;
import std.conv;
import source.dblite;
int sqlAddNewTheme(string topic) {
try {
auto queryResult = dblite.sql(
"insert into topics (name) values (?) returning id",
topic
);
if (!queryResult.empty())
return queryResult.front()["id"].to!int;
} catch (Exception e) {
log.e("Не удалось выполнить запрос к БД. " ~ e.msg);
}
return 0;
}
int sqlAddNewQuestion(int topic_id, int question_number, string question_text) {
try {
auto queryResult = dblite.sql(
"insert into questions (topic_id, number, text) values (?, ?, ?) returning id",
topic_id, question_number, question_text
);
if (!queryResult.empty())
return queryResult.front()["id"].to!int;
} catch (Exception e) {
log.e("Не удалось выполнить запрос к БД. " ~ e.msg);
}
return 0;
}
int sqlAddNewAnswer(int question_id, int answer_number, string answer_text, int answer_truth) {
try {
auto queryResult = dblite.sql(
"insert into answers (question_id, number, text, truth) values (?, ?, ?, ?) returning id",
question_id, answer_number, answer_text, answer_truth
);
if (!queryResult.empty())
return queryResult.front()["id"].to!int;
} catch (Exception e) {
log.e("Не удалось выполнить запрос к БД. " ~ e.msg);
}
return 0;
}