database¶
database
¶
Core database functionality for the AI Translate application.
get_db_path
¶
Returns the full path to the SQLite database file.
| 戻り値 | デスクリプション |
|---|---|
str
|
Path to the database file.
タイプ:
|
ソースコード位置: src/core/database.py
create_connection
¶
Creates a database connection to the SQLite database.
| 戻り値 | デスクリプション |
|---|---|
Connection | None
|
Optional[sqlite3.Connection]: Connection object or None. |
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
func
|
The database operation function to wrap.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
Callable
|
The wrapped function.
タイプ:
|
ソースコード位置: src/core/database.py
init_db
¶
Initializes the database tables if they do not exist.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
file_name
|
Original file name.
タイプ:
|
src
|
Source language label.
タイプ:
|
target
|
Target language label.
タイプ:
|
status
|
Initial status string (e.g. STATUS_PENDING).
タイプ:
|
source_path
|
Absolute path to the original file.
タイプ:
|
storage_path
|
Path to the cloned file in app storage.
タイプ:
|
file_size
|
Size of the original file in bytes.
タイプ:
|
ソースコード位置: 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.
ソースコード位置: src/core/database.py
update_history_progress
¶
Updates the progress of a history entry (monotonic — never decreases).
ソースコード位置: src/core/database.py
update_history_file_name
¶
Updates the original file_name of a history entry.
ソースコード位置: src/core/database.py
batch_pause_history_entries
¶
Pauses multiple history entries in a single transaction.
ソースコード位置: src/core/database.py
batch_resume_history_entries
¶
Resumes multiple paused or failed history entries.
ソースコード位置: src/core/database.py
batch_retranslate_history_entries
¶
Prepares multiple history entries for retranslation.
ソースコード位置: src/core/database.py
batch_mark_deleting_history_entries
¶
Marks multiple history entries as 'Deleting'.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: src/core/database.py
get_history_entry_status
¶
Returns the current status of a history entry.
ソースコード位置: src/core/database.py
get_history_entry_detail
¶
Returns a full detail dict for a history entry, or None if not found.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by
タイプ:
|
entry_id
|
The history entry ID to look up.
タイプ:
|
| 戻り値 | デスクリプション |
|---|---|
dict[str, Any] | None
|
A dict with keys |
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
ソースコード位置: 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).
ソースコード位置: src/core/database.py
delete_history_entry
¶
Deletes a history entry and returns its storage path.
ソースコード位置: src/core/database.py
is_any_translating
¶
Checks if there are any files currently in 'Translating' status.
ソースコード位置: src/core/database.py
is_any_paused
¶
Checks if there are any files currently in 'Paused' status.
ソースコード位置: src/core/database.py
is_any_extracting
¶
Checks if any extraction entries are currently active.
ソースコード位置: src/core/database.py
is_any_subtitle_generating
¶
Checks if any subtitle entries are currently active.
ソースコード位置: src/core/database.py
is_any_voice_generating
¶
Checks if any voice entries are currently active.
ソースコード位置: src/core/database.py
is_any_dubbing_generating
¶
Checks if any dubbing entries are currently active.
ソースコード位置: 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.
ソースコード位置: src/core/database.py
clear_history
¶
create_glossary_set
¶
Creates a new glossary set.
ソースコード位置: 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.
ソースコード位置: src/core/database.py
get_active_glossary_sets
¶
Returns all active glossary sets.
update_glossary_set_name
¶
Updates the name of a glossary set.
ソースコード位置: src/core/database.py
delete_glossary_set
¶
add_glossary_entry
¶
Adds a translation entry to a set.
ソースコード位置: src/core/database.py
get_glossary_entries
¶
Returns all entries for a given set.
ソースコード位置: src/core/database.py
get_glossary_entry_count
¶
Returns the number of entries in a glossary set.
ソースコード位置: 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.
ソースコード位置: src/core/database.py
delete_glossary_entry
¶
update_glossary_entry
¶
Updates an existing glossary entry.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
file_name
|
Original file name.
タイプ:
|
file_size
|
Size of the file in bytes.
タイプ:
|
source_path
|
Absolute path to the source image.
タイプ:
|
output_path
|
Path where extracted text will be written.
タイプ:
|
status
|
Initial status string.
タイプ:
|
error_message
|
Optional error description if the entry starts failed.
タイプ:
|
ソースコード位置: src/core/database.py
get_extraction_history
¶
Returns the most recent extraction history entries.
ソースコード位置: src/core/database.py
get_extraction_fingerprint
¶
Returns a lightweight fingerprint of extraction history state.
ソースコード位置: src/core/database.py
update_extraction_status
¶
Updates the status and output of an extraction history entry.
ソースコード位置: src/core/database.py
delete_extraction_entry
¶
Deletes an extraction history entry and returns its output path.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
file_name
|
Original media file name.
タイプ:
|
file_size
|
Size of the media file in bytes.
タイプ:
|
source_path
|
Absolute path to the source media file.
タイプ:
|
output_path
|
Path where the generated subtitle will be written.
タイプ:
|
src_lang
|
Source language label for STT.
タイプ:
|
status
|
Initial status string.
タイプ:
|
error_message
|
Optional error description if the entry starts failed.
タイプ:
|
ソースコード位置: src/core/database.py
get_subtitle_history
¶
Returns the most recent subtitle history entries.
ソースコード位置: src/core/database.py
get_subtitle_fingerprint
¶
Returns a lightweight fingerprint of subtitle history state.
ソースコード位置: src/core/database.py
update_subtitle_status
¶
Updates the status of a subtitle history entry.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
entry_id
|
Entry ID to update.
タイプ:
|
status
|
New status string.
タイプ:
|
output_path
|
If not None, also update output_path.
タイプ:
|
error_message
|
If not None, also update error_message.
タイプ:
|
ソースコード位置: src/core/database.py
delete_subtitle_entry
¶
Deletes a subtitle history entry and returns its output path.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
file_name
|
Original subtitle file name.
タイプ:
|
file_size
|
Size of the subtitle file in bytes.
タイプ:
|
source_path
|
Absolute path to the source subtitle file.
タイプ:
|
output_path
|
Path where the generated audio will be written.
タイプ:
|
status
|
Initial status string.
タイプ:
|
error_message
|
Optional error description if the entry starts failed.
タイプ:
|
ソースコード位置: src/core/database.py
get_voice_history
¶
Returns the most recent voice history entries.
ソースコード位置: src/core/database.py
get_voice_fingerprint
¶
Returns a lightweight fingerprint of voice history state.
ソースコード位置: src/core/database.py
update_voice_status
¶
Updates the status of a voice history entry.
ソースコード位置: src/core/database.py
delete_voice_entry
¶
Deletes a voice history entry and returns its output path.
ソースコード位置: 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.
ソースコード位置: 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.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
file_name
|
Original video file name.
タイプ:
|
file_size
|
Size of the video file in bytes.
タイプ:
|
source_path
|
Absolute path to the source video file.
タイプ:
|
output_path
|
Path where the dubbed video will be written.
タイプ:
|
status
|
Initial status string.
タイプ:
|
src_lang
|
Source language label (empty string if unknown).
タイプ:
|
target_lang
|
Target language label (empty string if unknown).
タイプ:
|
error_message
|
Optional error description if the entry starts failed.
タイプ:
|
ソースコード位置: src/core/database.py
get_dubbing_history
¶
Returns the most recent dubbing history entries.
ソースコード位置: src/core/database.py
get_dubbing_fingerprint
¶
Returns a lightweight fingerprint of dubbing history state.
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
entry_id
|
Entry ID to update.
タイプ:
|
status
|
New status string.
タイプ:
|
output_path
|
If not None, also update the output file path.
タイプ:
|
progress
|
If not None, also update the dubbing step progress.
タイプ:
|
error_message
|
If not None, also update the error description.
タイプ:
|
subtitle_path
|
If not None, also update the generated subtitle path.
タイプ:
|
translated_subtitle_path
|
If not None, also update the translated subtitle path.
タイプ:
|
voice_path
|
If not None, also update the synthesized voice path.
タイプ:
|
ソースコード位置: src/core/database.py
get_dubbing_entry_status
¶
Returns the current status of a dubbing history entry.
ソースコード位置: src/core/database.py
update_dubbing_progress
¶
Updates the progress percentage of a dubbing entry (monotonic).
ソースコード位置: 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.
| 戻り値 | デスクリプション |
|---|---|
list[tuple]
|
List of (id, source_path, src_lang, target_lang) tuples. |
ソースコード位置: src/core/database.py
batch_pause_dubbing_entries
¶
Pauses multiple dubbing entries in a single transaction.
ソースコード位置: src/core/database.py
batch_resume_dubbing_entries
¶
Resumes multiple paused or failed dubbing entries.
ソースコード位置: src/core/database.py
delete_dubbing_entry
¶
Deletes a dubbing history entry and returns its file paths.
| 戻り値 | デスクリプション |
|---|---|
str
|
Tuple of (output_path, subtitle_path, translated_subtitle_path, |
...
|
voice_path). Missing values are empty strings. |
ソースコード位置: 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.
| 引数 | デスクリプション |
|---|---|
cursor
|
Database cursor (injected by decorator).
タイプ:
|
source_text
|
Original text before translation.
タイプ:
|
translated_text
|
Translated text from the LLM.
タイプ:
|
src_lang
|
Source language label.
タイプ:
|
target_lang
|
Target language label.
タイプ:
|
char_count
|
Character count of the source text.
タイプ:
|
ソースコード位置: src/core/database.py
get_text_translation_history
¶
Returns the most recent text translation history entries.
ソースコード位置: src/core/database.py
get_text_translation_fingerprint
¶
Returns a lightweight fingerprint of text translation history state.
ソースコード位置: src/core/database.py
update_text_translation_entry
¶
Updates the translated text of a text translation history entry.
ソースコード位置: src/core/database.py
delete_text_translation_entry
¶
Deletes a text translation history entry.