@@ -13,6 +13,7 @@ import io.github.kdroidfilter.seforimlibrary.core.models.Author
1313import io.github.kdroidfilter.seforimlibrary.core.models.Book
1414import io.github.kdroidfilter.seforimlibrary.core.models.Category
1515import io.github.kdroidfilter.seforimlibrary.core.models.ConnectionType
16+ import io.github.kdroidfilter.seforimlibrary.core.models.Generation
1617import io.github.kdroidfilter.seforimlibrary.core.models.Line
1718import io.github.kdroidfilter.seforimlibrary.core.models.LineAltTocMapping
1819import io.github.kdroidfilter.seforimlibrary.core.models.LineTocMapping
@@ -577,19 +578,76 @@ class SeforimRepository(databasePath: String, private val driver: SqlDriver) {
577578 return @withContext author?.toModel()
578579 }
579580
581+ // ── Generation helpers ───────────────────────────────────────────
582+
583+ /* *
584+ * Insert a generation (era) and return its ID.
585+ * If [explicitId] is provided, it is used as the ID (for stable ID resolution).
586+ * If the generation already exists, returns its existing ID.
587+ */
588+ suspend fun insertGeneration (name : String , explicitId : Long? = null): Long = withContext(Dispatchers .IO ) {
589+ logger.d { " Inserting generation: $name " + (explicitId?.let { " with explicit ID $it " } ? : " " ) }
590+ val existing = database.generationQueriesQueries.selectByName(name).executeAsOneOrNull()
591+ if (existing != null ) return @withContext existing.id
592+
593+ if (explicitId != null ) {
594+ database.generationQueriesQueries.insertWithId(explicitId, name)
595+ return @withContext explicitId
596+ }
597+
598+ database.generationQueriesQueries.insert(name)
599+ val id = database.generationQueriesQueries.lastInsertRowId().executeAsOne()
600+ if (id != 0L ) return @withContext id
601+
602+ // fallback
603+ val retry = database.generationQueriesQueries.selectByName(name).executeAsOneOrNull()
604+ return @withContext retry?.id ? : 0L
605+ }
606+
607+ /* *
608+ * Resolve a generation name to its ID, or null if not found.
609+ */
610+ suspend fun getGenerationIdByName (name : String ): Long? = withContext(Dispatchers .IO ) {
611+ database.generationQueriesQueries.selectIdByName(name).executeAsOneOrNull()
612+ }
613+
614+ /* *
615+ * Return all generations.
616+ */
617+ suspend fun getAllGenerations (): List <Generation > = withContext(Dispatchers .IO ) {
618+ database.generationQueriesQueries.selectAll().executeAsList().map { it.toModel() }
619+ }
620+
621+ /* *
622+ * Update the generation for a given author ID.
623+ */
624+ suspend fun updateAuthorGeneration (authorId : Long , generationId : Long? ) = withContext(Dispatchers .IO ) {
625+ database.authorQueriesQueries.updateGeneration(generationId, authorId)
626+ }
627+
628+ // ── Author insertion ─────────────────────────────────────────────
629+
580630 // Insert an author and return its ID
581- suspend fun insertAuthor (name : String ): Long = withContext(Dispatchers .IO ) {
631+ suspend fun insertAuthor (name : String , generationId : Long? = null ): Long = withContext(Dispatchers .IO ) {
582632 logger.d{" Inserting author: $name " }
583633
584634 // Check if author already exists
585635 val existingAuthor = database.authorQueriesQueries.selectByName(name).executeAsOneOrNull()
586636 if (existingAuthor != null ) {
637+ // If a generationId is supplied and the existing author doesn't have one, update it
638+ if (generationId != null && existingAuthor.generationId == null ) {
639+ database.authorQueriesQueries.updateGeneration(generationId, existingAuthor.id)
640+ }
587641 logger.d{" Author already exists with ID: ${existingAuthor.id} " }
588642 return @withContext existingAuthor.id
589643 }
590644
591- // Insert the author
592- database.authorQueriesQueries.insert(name)
645+ // Insert the author (with optional generationId)
646+ if (generationId != null ) {
647+ database.authorQueriesQueries.insertWithGeneration(name, generationId)
648+ } else {
649+ database.authorQueriesQueries.insert(name)
650+ }
593651
594652 // Get the ID of the inserted author
595653 val authorId = database.authorQueriesQueries.lastInsertRowId().executeAsOne()
0 commit comments