database¶
database
¶
Core database functionality for the AI Translate application.
get_db_path
¶
Returns the full path to the SQLite database file.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Path to the database file.
TYPE:
|
Source code in src/core/database.py
create_connection
¶
Creates a database connection to the SQLite database.
| RETURNS | DESCRIPTION |
|---|---|
Connection | None
|
Optional[sqlite3.Connection]: Connection object or None. |
Source code in src/core/database.py
db_transaction
¶
Decorator to handle database connections and transactions.
If the first argument is already a sqlite3.Cursor, it uses it without creating a new connection, allowing for nested calls or shared transactions.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The database operation function to wrap.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
The wrapped function.
TYPE:
|
Source code in src/core/database.py
init_db
¶
Initializes the database tables if they do not exist.
Source code in src/core/database.py
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 | |
add_history_entry
¶
add_history_entry(
cursor,
file_name,
src,
target,
status,
source_path="",
storage_path="",
file_size=0,
)
Adds a new entry to the translation history and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
file_name
|
Original file name.
TYPE:
|
src
|
Source language label.
TYPE:
|
target
|
Target language label.
TYPE:
|
status
|
Initial status string (e.g. STATUS_PENDING).
TYPE:
|
source_path
|
Absolute path to the original file.
TYPE:
|
storage_path
|
Path to the cloned file in app storage.
TYPE:
|
file_size
|
Size of the original file in bytes.
TYPE:
|
Source code in src/core/database.py
update_history_status
¶
Updates the status of a history entry and refreshes timestamp if starting.
error_message is the raw error tag string (e.g.
"AUTH_ERROR:Gemini") — preserved so the UI can render
service-specific copy via :func:display_error_message. Passed
independently from error_code (the numeric code) so the
persisted columns stay in sync: code drives the localised
template, message drives the {service} substitution.
Source code in src/core/database.py
update_history_progress
¶
Updates the progress of a history entry (monotonic — never decreases).
Source code in src/core/database.py
update_history_file_name
¶
Updates the original file_name of a history entry.
Source code in src/core/database.py
batch_pause_history_entries
¶
Pauses multiple history entries in a single transaction.
Source code in src/core/database.py
batch_resume_history_entries
¶
Resumes multiple paused or failed history entries.
Source code in src/core/database.py
batch_retranslate_history_entries
¶
Prepares multiple history entries for retranslation.
Source code in src/core/database.py
batch_mark_deleting_history_entries
¶
Marks multiple history entries as 'Deleting'.
Source code in src/core/database.py
get_history
¶
Returns the most recent translation history.
Tuple shape: (id, file_name, source_lang, target_lang, status,
progress, created_at, file_size, storage_path, error_code,
error_message). error_message is the raw error tag string
(may include :Service suffix) — UI passes it through
:func:display_error_message for service-specific copy.
Source code in src/core/database.py
get_history_fingerprint
¶
Returns a lightweight fingerprint of current history state.
Used by the UI to skip full table rebuilds when nothing has changed.
Source code in src/core/database.py
get_history_entry_status
¶
Returns the current status of a history entry.
Source code in src/core/database.py
get_history_entry_detail
¶
Returns a full detail dict for a history entry, or None if not found.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by
TYPE:
|
entry_id
|
The history entry ID to look up.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | None
|
A dict with keys |
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
Source code in src/core/database.py
get_history_entry_details
¶
Returns a {id: detail} map for multiple history entries.
Batch variant of :func:get_history_entry_detail — a single
WHERE id IN (?, ?, …) query replaces the per-id loop +
round-trip pattern callers used to write. Missing IDs are
simply absent from the result dict; the caller decides how to
surface "task auto-removed" status.
Driver: the MCP server's get_task_status polls in batches
of arbitrary size (clients can ask for every queued task at
once), where the prior 1-query-per-id pattern was a textbook
N+1. Internal callers that already work with a single id
should keep using :func:get_history_entry_detail.
Returns an empty dict when entry_ids is empty (avoids
generating WHERE id IN (), which SQLite rejects).
Source code in src/core/database.py
delete_history_entry
¶
Deletes a history entry and returns its storage path.
Source code in src/core/database.py
is_any_translating
¶
Checks if there are any files currently in 'Translating' status.
Source code in src/core/database.py
is_any_paused
¶
Checks if there are any files currently in 'Paused' status.
Source code in src/core/database.py
is_any_extracting
¶
Checks if any extraction entries are currently active.
Source code in src/core/database.py
is_any_subtitle_generating
¶
Checks if any subtitle entries are currently active.
Source code in src/core/database.py
is_any_voice_generating
¶
Checks if any voice entries are currently active.
Source code in src/core/database.py
is_any_dubbing_generating
¶
Checks if any dubbing entries are currently active.
Source code in src/core/database.py
get_unfinished_history
¶
Returns unfinished translation tasks filtered by status.
Results are ordered so that 'Translating' entries come first (interrupted tasks should be resumed before starting new ones), then by ascending id for deterministic processing order.
Source code in src/core/database.py
clear_history
¶
create_glossary_set
¶
Creates a new glossary set.
Source code in src/core/database.py
get_glossary_sets
¶
update_all_glossary_sets_active
¶
Updates the active status of all glossary sets at once.
update_glossary_set_active
¶
Updates the active status of a glossary set.
Source code in src/core/database.py
get_active_glossary_sets
¶
Returns all active glossary sets.
update_glossary_set_name
¶
Updates the name of a glossary set.
Source code in src/core/database.py
delete_glossary_set
¶
add_glossary_entry
¶
Adds a translation entry to a set.
Source code in src/core/database.py
get_glossary_entries
¶
Returns all entries for a given set.
Source code in src/core/database.py
get_glossary_entry_count
¶
Returns the number of entries in a glossary set.
Source code in src/core/database.py
find_glossary_entry_by_source
¶
Returns (entry_id, target_text) for a case-insensitive match on source.
Used to detect duplicates before adding a new entry. Returns None when
no matching row exists.
Source code in src/core/database.py
delete_glossary_entry
¶
update_glossary_entry
¶
Updates an existing glossary entry.
Source code in src/core/database.py
add_extraction_entry
¶
add_extraction_entry(
cursor,
file_name,
file_size,
source_path,
output_path,
status,
error_message=None,
)
Adds a new extraction history entry and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
file_name
|
Original file name.
TYPE:
|
file_size
|
Size of the file in bytes.
TYPE:
|
source_path
|
Absolute path to the source image.
TYPE:
|
output_path
|
Path where extracted text will be written.
TYPE:
|
status
|
Initial status string.
TYPE:
|
error_message
|
Optional error description if the entry starts failed.
TYPE:
|
Source code in src/core/database.py
get_extraction_history
¶
Returns the most recent extraction history entries.
Source code in src/core/database.py
get_extraction_fingerprint
¶
Returns a lightweight fingerprint of extraction history state.
Source code in src/core/database.py
update_extraction_status
¶
Updates the status and output of an extraction history entry.
Source code in src/core/database.py
delete_extraction_entry
¶
Deletes an extraction history entry and returns its output path.
Source code in src/core/database.py
add_subtitle_entry
¶
add_subtitle_entry(
cursor,
file_name,
file_size,
source_path,
output_path,
src_lang,
status,
error_message=None,
)
Adds a new subtitle history entry and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
file_name
|
Original media file name.
TYPE:
|
file_size
|
Size of the media file in bytes.
TYPE:
|
source_path
|
Absolute path to the source media file.
TYPE:
|
output_path
|
Path where the generated subtitle will be written.
TYPE:
|
src_lang
|
Source language label for STT.
TYPE:
|
status
|
Initial status string.
TYPE:
|
error_message
|
Optional error description if the entry starts failed.
TYPE:
|
Source code in src/core/database.py
get_subtitle_history
¶
Returns the most recent subtitle history entries.
Source code in src/core/database.py
get_subtitle_fingerprint
¶
Returns a lightweight fingerprint of subtitle history state.
Source code in src/core/database.py
update_subtitle_status
¶
Updates the status of a subtitle history entry.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
entry_id
|
Entry ID to update.
TYPE:
|
status
|
New status string.
TYPE:
|
output_path
|
If not None, also update output_path.
TYPE:
|
error_message
|
If not None, also update error_message.
TYPE:
|
Source code in src/core/database.py
delete_subtitle_entry
¶
Deletes a subtitle history entry and returns its output path.
Source code in src/core/database.py
add_voice_entry
¶
add_voice_entry(
cursor,
file_name,
file_size,
source_path,
output_path,
status,
error_message=None,
)
Adds a new voice history entry and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
file_name
|
Original subtitle file name.
TYPE:
|
file_size
|
Size of the subtitle file in bytes.
TYPE:
|
source_path
|
Absolute path to the source subtitle file.
TYPE:
|
output_path
|
Path where the generated audio will be written.
TYPE:
|
status
|
Initial status string.
TYPE:
|
error_message
|
Optional error description if the entry starts failed.
TYPE:
|
Source code in src/core/database.py
get_voice_history
¶
Returns the most recent voice history entries.
Source code in src/core/database.py
get_voice_fingerprint
¶
Returns a lightweight fingerprint of voice history state.
Source code in src/core/database.py
update_voice_status
¶
Updates the status of a voice history entry.
Source code in src/core/database.py
delete_voice_entry
¶
Deletes a voice history entry and returns its output path.
Source code in src/core/database.py
reset_stuck_subtitle_entries
¶
Resets any subtitle entries stuck in 'Generating' to 'Failed'.
Called at app startup to clean up interrupted entries from a previous crash. Returns the number of rows updated.
Source code in src/core/database.py
reset_stuck_voice_entries
¶
Resets any voice entries stuck in 'Generating' to 'Failed'.
Called at app startup to clean up interrupted entries from a previous crash. Returns the number of rows updated.
Source code in src/core/database.py
add_dubbing_entry
¶
add_dubbing_entry(
cursor,
file_name,
file_size,
source_path,
output_path,
status,
src_lang="",
target_lang="",
error_message=None,
)
Adds a new dubbing history entry and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
file_name
|
Original video file name.
TYPE:
|
file_size
|
Size of the video file in bytes.
TYPE:
|
source_path
|
Absolute path to the source video file.
TYPE:
|
output_path
|
Path where the dubbed video will be written.
TYPE:
|
status
|
Initial status string.
TYPE:
|
src_lang
|
Source language label (empty string if unknown).
TYPE:
|
target_lang
|
Target language label (empty string if unknown).
TYPE:
|
error_message
|
Optional error description if the entry starts failed.
TYPE:
|
Source code in src/core/database.py
get_dubbing_history
¶
Returns the most recent dubbing history entries.
Source code in src/core/database.py
get_dubbing_fingerprint
¶
Returns a lightweight fingerprint of dubbing history state.
Source code in src/core/database.py
update_dubbing_status
¶
update_dubbing_status(
cursor,
entry_id,
status,
output_path=None,
progress=None,
error_message=None,
subtitle_path=None,
translated_subtitle_path=None,
voice_path=None,
)
Updates the status of a dubbing history entry.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
entry_id
|
Entry ID to update.
TYPE:
|
status
|
New status string.
TYPE:
|
output_path
|
If not None, also update the output file path.
TYPE:
|
progress
|
If not None, also update the dubbing step progress.
TYPE:
|
error_message
|
If not None, also update the error description.
TYPE:
|
subtitle_path
|
If not None, also update the generated subtitle path.
TYPE:
|
translated_subtitle_path
|
If not None, also update the translated subtitle path.
TYPE:
|
voice_path
|
If not None, also update the synthesized voice path.
TYPE:
|
Source code in src/core/database.py
get_dubbing_entry_status
¶
Returns the current status of a dubbing history entry.
Source code in src/core/database.py
update_dubbing_progress
¶
Updates the progress percentage of a dubbing entry (monotonic).
Source code in src/core/database.py
get_unfinished_dubbing
¶
Returns unfinished dubbing tasks (Pending or Generating).
Results are ordered so that 'Generating' entries come first (interrupted tasks resume before new ones), then by ascending id.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple]
|
List of (id, source_path, src_lang, target_lang) tuples. |
Source code in src/core/database.py
batch_pause_dubbing_entries
¶
Pauses multiple dubbing entries in a single transaction.
Source code in src/core/database.py
batch_resume_dubbing_entries
¶
Resumes multiple paused or failed dubbing entries.
Source code in src/core/database.py
delete_dubbing_entry
¶
Deletes a dubbing history entry and returns its file paths.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Tuple of (output_path, subtitle_path, translated_subtitle_path, |
...
|
voice_path). Missing values are empty strings. |
Source code in src/core/database.py
add_text_translation_entry
¶
add_text_translation_entry(
cursor, source_text, translated_text, src_lang, target_lang, char_count
)
Adds a new text translation history entry and returns its ID.
| PARAMETER | DESCRIPTION |
|---|---|
cursor
|
Database cursor (injected by decorator).
TYPE:
|
source_text
|
Original text before translation.
TYPE:
|
translated_text
|
Translated text from the LLM.
TYPE:
|
src_lang
|
Source language label.
TYPE:
|
target_lang
|
Target language label.
TYPE:
|
char_count
|
Character count of the source text.
TYPE:
|
Source code in src/core/database.py
get_text_translation_history
¶
Returns the most recent text translation history entries.
Source code in src/core/database.py
get_text_translation_fingerprint
¶
Returns a lightweight fingerprint of text translation history state.
Source code in src/core/database.py
update_text_translation_entry
¶
Updates the translated text of a text translation history entry.
Source code in src/core/database.py
delete_text_translation_entry
¶
Deletes a text translation history entry.