69 lines
2.0 KiB
D
69 lines
2.0 KiB
D
module source.sql;
|
||
|
||
import singlog;
|
||
import std.conv;
|
||
import arsd.sqlite;
|
||
|
||
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,
|
||
bool question_has_picture,
|
||
ubyte[] question_picture,
|
||
string question_picture_name
|
||
) {
|
||
SqliteResult queryResult;
|
||
try {
|
||
if (question_has_picture)
|
||
queryResult = dblite.sql(
|
||
"insert into questions (topic_id, number, text, picture_name, picture)
|
||
values (?, ?, ?, ?, ?) returning id",
|
||
topic_id, question_number, question_text, question_picture_name, question_picture
|
||
);
|
||
else
|
||
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;
|
||
}
|