1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <database/etcbc4.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/date.h>
#include <config/globals.h>
#include <database/sqlite.h>
// Database resilience:
// The database is normally only read from.
namespace database::etcbc4 {
constexpr const auto etcb4 {"etcb4"};
static int get_id (SqliteDatabase& sql, const char* table_row, const std::string& item)
{
int id {0};
// Two iterations to be sure a rowid can be returned.
for (unsigned int i = 0; i < 2; i++) {
// Save the existing SQL just now because the code below puts new SQL into the object.
sql.push_sql();
{
// Check on the rowid and fetch it if it's there.
sql.clear();
sql.add ("SELECT rowid FROM");
sql.add (table_row);
sql.add ("WHERE");
sql.add (table_row);
sql.add ("=");
sql.add (item);
sql.add (";");
const std::vector <std::string> result = sql.query () ["rowid"];
if (!result.empty ())
id = filter::strings::convert_to_int (result [0]);
}
if (!id) {
// The rowid was not found: Insert the word into the table.
// The rowid will now be found during the second iteration.
sql.clear();
sql.add ("INSERT INTO");
sql.add (table_row);
sql.add ("VALUES (");
sql.add (item);
sql.add (");");
sql.execute ();
}
// Restore the previously saved SQL so the caller can again use it.
sql.pop_sql();
if (id)
return id;
}
return id;
}
static std::string get_item (const char * item, int rowid)
{
SqliteDatabase sql (etcb4);
// The $rowid refers to the main table.
// Update it so it refers to the sub table.
{
sql.clear();
sql.add ("SELECT");
sql.add (item);
sql.add ("FROM data WHERE rowid =");
sql.add (rowid);
sql.add (";");
const std::vector <std::string> result = sql.query () [item];
rowid = 0;
if (!result.empty ())
rowid = filter::strings::convert_to_int (result.at(0));
}
// Retrieve the requested value from the sub table.
std::string value {};
{
sql.clear();
sql.add ("SELECT");
sql.add (item);
sql.add ("FROM");
sql.add (item);
sql.add ("WHERE rowid =");
sql.add (rowid);
sql.add (";");
const std::vector <std::string> result = sql.query () [item];
if (!result.empty ())
value = result.at(0);
}
// Done.
return value;
}
void create ()
{
SqliteDatabase sql (etcb4);
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS rawdata (book int, chapter int, verse int, data text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS data;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS data (book int, chapter int, verse int, "
"word int, vocalized_lexeme int, consonantal_lexeme int, "
"gloss int, pos int, subpos int, "
"gender int, number int, person int, "
"state int, tense int, stem int, "
"phrase_function int, phrase_type int, phrase_relation int, "
"phrase_a_relation int, clause_text_type int, clause_type int, clause_relation int"
");");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS word;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS word (word text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS vocalized_lexeme;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS vocalized_lexeme (vocalized_lexeme text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS consonantal_lexeme;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS consonantal_lexeme (consonantal_lexeme text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS gloss;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS gloss (gloss text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS pos;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS pos (pos text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS subpos;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS subpos (subpos text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS gender;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS gender (gender text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS number;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS number (number text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS person;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS person (person text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS state;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS state (state text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS tense;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS tense (tense text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS stem;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS stem (stem text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS phrase_function;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS phrase_function (phrase_function text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS phrase_type;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS phrase_type (phrase_type text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS phrase_relation;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS phrase_relation (phrase_relation text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS phrase_a_relation;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS phrase_a_relation (phrase_a_relation text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS clause_text_type;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS clause_text_type (clause_text_type text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS clause_type;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS clause_type (clause_type text);");
sql.execute();
sql.clear();
sql.add ("DROP TABLE IF EXISTS clause_relation;");
sql.execute();
sql.clear();
sql.add ("CREATE TABLE IF NOT EXISTS clause_relation (clause_relation text);");
sql.execute();
}
std::string raw (int book, int chapter, int verse)
{
SqliteDatabase sql (etcb4);
sql.add ("SELECT data FROM rawdata WHERE book =");
sql.add (book);
sql.add ("AND chapter =");
sql.add (chapter);
sql.add ("AND verse =");
sql.add (verse);
sql.add (";");
const std::vector <std::string> result = sql.query () ["data"];
if (!result.empty ())
return result.at(0);
return std::string();
}
void store (int book, int chapter, int verse, const std::string& data)
{
SqliteDatabase sql (etcb4);
sql.clear();
sql.add ("DELETE FROM rawdata WHERE book =");
sql.add (book);
sql.add ("AND chapter =");
sql.add (chapter);
sql.add ("AND verse =");
sql.add (verse);
sql.add (";");
sql.execute();
sql.clear();
sql.add ("INSERT INTO rawdata VALUES (");
sql.add (book);
sql.add (",");
sql.add (chapter);
sql.add (",");
sql.add (verse);
sql.add (",");
sql.add (data);
sql.add (");");
sql.execute();
}
void store (int book, int chapter, int verse,
std::string word, std::string vocalized_lexeme, std::string consonantal_lexeme,
std::string gloss, std::string pos, std::string subpos,
std::string gender, std::string number, std::string person,
std::string state, std::string tense, std::string stem,
std::string phrase_function, std::string phrase_type, std::string phrase_relation,
std::string phrase_a_relation, std::string clause_text_type, std::string clause_type, std::string clause_relation)
{
SqliteDatabase sql (etcb4);
sql.add ("INSERT INTO data VALUES (");
sql.add (book);
sql.add (",");
sql.add (chapter);
sql.add (",");
sql.add (verse);
sql.add (",");
sql.add (get_id (sql, "word", word));
sql.add (",");
sql.add (get_id (sql, "vocalized_lexeme", vocalized_lexeme));
sql.add (",");
sql.add (get_id (sql, "consonantal_lexeme", consonantal_lexeme));
sql.add (",");
sql.add (get_id (sql, "gloss", gloss));
sql.add (",");
sql.add (get_id (sql, "pos", pos));
sql.add (",");
sql.add (get_id (sql, "subpos", subpos));
sql.add (",");
sql.add (get_id (sql, "gender", gender));
sql.add (",");
sql.add (get_id (sql, "number", number));
sql.add (",");
sql.add (get_id (sql, "person", person));
sql.add (",");
sql.add (get_id (sql, "state", state));
sql.add (",");
sql.add (get_id (sql, "tense", tense));
sql.add (",");
sql.add (get_id (sql, "stem", stem));
sql.add (",");
sql.add (get_id (sql, "phrase_function", phrase_function));
sql.add (",");
sql.add (get_id (sql, "phrase_type", phrase_type));
sql.add (",");
sql.add (get_id (sql, "phrase_relation", phrase_relation));
sql.add (",");
sql.add (get_id (sql, "phrase_a_relation", phrase_a_relation));
sql.add (",");
sql.add (get_id (sql, "clause_text_type", clause_text_type));
sql.add (",");
sql.add (get_id (sql, "clause_type", clause_type));
sql.add (",");
sql.add (get_id (sql, "clause_relation", clause_relation));
sql.add (");");
sql.execute();
}
std::vector <int> books ()
{
SqliteDatabase sql (etcb4);
sql.add ("SELECT DISTINCT book FROM rawdata ORDER BY book;");
const std::vector <std::string> result = sql.query () ["book"];
std::vector <int> books;
for (const auto& b : result)
books.push_back (filter::strings::convert_to_int (b));
return books;
}
std::vector <int> chapters (const int book)
{
SqliteDatabase sql (etcb4);
sql.add ("SELECT DISTINCT chapter FROM rawdata WHERE book =");
sql.add (book);
sql.add ("ORDER BY chapter;");
const std::vector <std::string> result = sql.query () ["chapter"];
std::vector <int> chapters;
for (const auto& c : result)
chapters.push_back (filter::strings::convert_to_int (c));
return chapters;
}
std::vector <int> verses (const int book, const int chapter)
{
SqliteDatabase sql (etcb4);
sql.add ("SELECT DISTINCT verse FROM rawdata WHERE book =");
sql.add (book);
sql.add ("AND chapter =");
sql.add (chapter);
sql.add ("ORDER BY verse;");
const std::vector <std::string> result = sql.query () ["verse"];
std::vector <int> verses;
for (const auto& v : result)
verses.push_back (filter::strings::convert_to_int (v));
return verses;
}
std::vector <int> rowids (const int book, const int chapter, const int verse)
{
SqliteDatabase sql (etcb4);
sql.add ("SELECT rowid FROM data WHERE book =");
sql.add (book);
sql.add ("AND chapter =");
sql.add (chapter);
sql.add ("AND verse =");
sql.add (verse);
sql.add (";");
const std::vector <std::string> result = sql.query () ["rowid"];
std::vector <int> rowids;
for (const auto& rowid : result)
rowids.push_back (filter::strings::convert_to_int (rowid));
return rowids;
}
std::string word (const int rowid)
{
return get_item ("word", rowid);
}
std::string vocalized_lexeme (const int rowid)
{
return get_item ("vocalized_lexeme", rowid);
}
std::string consonantal_lexeme (const int rowid)
{
return get_item ("consonantal_lexeme", rowid);
}
std::string gloss (const int rowid)
{
return get_item ("gloss", rowid);
}
std::string pos (const int rowid)
{
return get_item ("pos", rowid);
}
std::string subpos (const int rowid)
{
return get_item ("subpos", rowid);
}
std::string gender (const int rowid)
{
return get_item ("gender", rowid);
}
std::string number (const int rowid)
{
return get_item ("number", rowid);
}
std::string person (const int rowid)
{
return get_item ("person", rowid);
}
std::string state (const int rowid)
{
return get_item ("state", rowid);
}
std::string tense (const int rowid)
{
return get_item ("tense", rowid);
}
std::string stem (const int rowid)
{
return get_item ("stem", rowid);
}
std::string phrase_function (const int rowid)
{
return get_item ("phrase_function", rowid);
}
std::string phrase_type (const int rowid)
{
return get_item ("phrase_type", rowid);
}
std::string phrase_relation (const int rowid)
{
return get_item ("phrase_relation", rowid);
}
std::string phrase_a_relation (const int rowid)
{
return get_item ("phrase_a_relation", rowid);
}
std::string clause_text_type (const int rowid)
{
return get_item ("clause_text_type", rowid);
}
std::string clause_type (const int rowid)
{
return get_item ("clause_type", rowid);
}
std::string clause_relation (const int rowid)
{
return get_item ("clause_relation", rowid);
}
} // Namespace.
|