client-core 0.1.0

Duck Client 核心库
Documentation
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use anyhow::Result;
use chrono::{DateTime, Utc};
use duckdb::{Connection, params};
use serde_json;
use std::path::PathBuf;
use tokio::sync::mpsc;
use tracing::{debug, info};

use super::messages::{AppStateRecord, DbMessage, DownloadTaskRecord, UserActionRecord};
use super::models::{BackupRecord, ScheduledTask};

/// DuckDB Actor - 确保单线程访问DuckDB
pub struct DuckDbActor {
    connection: Connection,
}

impl DuckDbActor {
    /// 创建新的DuckDB Actor
    pub fn new(db_path: PathBuf) -> Result<Self> {
        let connection = Connection::open(db_path)?;
        Ok(Self { connection })
    }

    /// 创建内存DuckDB Actor
    pub fn new_memory() -> Result<Self> {
        let connection = Connection::open_in_memory()?;
        Ok(Self { connection })
    }

    /// 运行Actor消息循环
    pub async fn run(mut self, mut receiver: mpsc::Receiver<DbMessage>) {
        debug!("DuckDB Actor started");

        while let Some(message) = receiver.recv().await {
            self.handle_message(message).await;
        }

        debug!("DuckDB Actor shut down");
    }

    /// 处理数据库消息
    async fn handle_message(&mut self, message: DbMessage) {
        match message {
            DbMessage::InitTables { respond_to } => {
                let result = self.init_tables();
                let _ = respond_to.send(result);
            }
            DbMessage::GetConfig { key, respond_to } => {
                let result = self.get_config(&key);
                let _ = respond_to.send(result);
            }
            DbMessage::SetConfig {
                key,
                value,
                respond_to,
            } => {
                let result = self.set_config(&key, &value);
                let _ = respond_to.send(result);
            }
            DbMessage::CreateBackupRecord {
                file_path,
                service_version,
                backup_type,
                status,
                respond_to,
            } => {
                let result =
                    self.create_backup_record(&file_path, &service_version, &backup_type, &status);
                let _ = respond_to.send(result);
            }
            DbMessage::GetAllBackups { respond_to } => {
                let result = self.get_all_backups();
                let _ = respond_to.send(result);
            }
            DbMessage::GetBackupById { id, respond_to } => {
                let result = self.get_backup_by_id(id);
                let _ = respond_to.send(result);
            }
            DbMessage::DeleteBackupRecord {
                backup_id,
                respond_to,
            } => {
                let result = self.delete_backup_record(backup_id);
                let _ = respond_to.send(result);
            }
            DbMessage::UpdateBackupFilePath {
                backup_id,
                new_path,
                respond_to,
            } => {
                let result = self.update_backup_file_path(backup_id, &new_path);
                let _ = respond_to.send(result);
            }
            DbMessage::CreateScheduledTask {
                task_type,
                target_version,
                scheduled_at,
                respond_to,
            } => {
                let result = self.create_scheduled_task(
                    &task_type,
                    &target_version,
                    scheduled_at,
                    "PENDING",
                );
                let _ = respond_to.send(result);
            }
            DbMessage::GetPendingTasks { respond_to } => {
                let result = self.get_pending_tasks();
                let _ = respond_to.send(result);
            }
            DbMessage::UpdateTaskStatus {
                task_id,
                status,
                details,
                respond_to,
            } => {
                let result = self.update_task_status(task_id, &status, details.as_deref());
                let _ = respond_to.send(result);
            }
            DbMessage::CancelPendingTasks {
                task_type,
                respond_to,
            } => {
                let result = self.cancel_pending_tasks(&task_type);
                let _ = respond_to.send(result);
            }

            // ========== 下载任务管理 ==========
            DbMessage::CreateDownloadTask {
                task_name,
                download_url,
                total_size,
                target_path,
                file_hash,
                respond_to,
            } => {
                let result = self.create_download_task(
                    &task_name,
                    &download_url,
                    total_size,
                    &target_path,
                    file_hash.as_deref(),
                );
                let _ = respond_to.send(result);
            }
            DbMessage::UpdateDownloadTaskStatus {
                task_id,
                status,
                downloaded_size,
                error_message,
                respond_to,
            } => {
                let result = self.update_download_task_status(
                    task_id,
                    &status,
                    downloaded_size,
                    error_message.as_deref(),
                );
                let _ = respond_to.send(result);
            }
            DbMessage::CompleteDownloadTask {
                task_id,
                average_speed,
                total_duration,
                respond_to,
            } => {
                let result = self.complete_download_task(task_id, average_speed, total_duration);
                let _ = respond_to.send(result);
            }
            DbMessage::GetDownloadTask {
                task_id,
                respond_to,
            } => {
                let result = self.get_download_task(task_id);
                let _ = respond_to.send(result);
            }
            DbMessage::GetActiveDownloadTasks { respond_to } => {
                let result = self.get_active_download_tasks();
                let _ = respond_to.send(result);
            }

            // ========== 应用状态管理 ==========
            DbMessage::UpdateAppState {
                state,
                state_data,
                error_message,
                respond_to,
            } => {
                let result =
                    self.update_app_state(&state, state_data.as_deref(), error_message.as_deref());
                let _ = respond_to.send(result);
            }
            DbMessage::GetAppState { respond_to } => {
                let result = self.get_app_state();
                let _ = respond_to.send(result);
            }

            // ========== 用户操作历史 ==========
            DbMessage::RecordUserAction {
                action_type,
                action_description,
                action_params,
                respond_to,
            } => {
                let result = self.record_user_action(
                    &action_type,
                    &action_description,
                    action_params.as_deref(),
                );
                let _ = respond_to.send(result);
            }
            DbMessage::CompleteUserAction {
                action_id,
                status,
                result_message,
                duration_seconds,
                respond_to,
            } => {
                let result = self.complete_user_action(
                    action_id,
                    &status,
                    result_message.as_deref(),
                    duration_seconds,
                );
                let _ = respond_to.send(result);
            }
            DbMessage::GetUserActions { limit, respond_to } => {
                let result = self.get_user_actions(limit);
                let _ = respond_to.send(result);
            }
        }
    }

    /// 初始化数据库表
    fn init_tables(&mut self) -> Result<()> {
        debug!("Initializing DuckDB tables...");

        // 读取并执行SQL初始化脚本
        let sql_content = include_str!("../../migrations/init_duckdb.sql");

        // 按分号分割SQL语句并执行
        for statement in sql_content.split(';').filter(|s| !s.trim().is_empty()) {
            let trimmed = statement.trim();
            if !trimmed.is_empty() {
                self.connection.execute(trimmed, [])?;
            }
        }

        // 设置数据库初始化标记
        self.connection.execute(
            "INSERT OR REPLACE INTO app_config (config_key, config_value, config_type, category, description, is_system_config, is_user_editable, default_value) VALUES ('db_initialized', '\"true\"', 'BOOLEAN', 'system', '数据库初始化状态标记', TRUE, FALSE, '\"false\"')",
            []
        )?;

        info!("DuckDB tables initialized");
        Ok(())
    }

    /// 获取配置值
    fn get_config(&mut self, key: &str) -> Result<Option<String>> {
        let mut stmt = self
            .connection
            .prepare("SELECT config_value FROM app_config WHERE config_key = ?")?;
        let mut rows = stmt.query(params![key])?;

        if let Some(row) = rows.next()? {
            let json_value: String = row.get(0)?;
            // 尝试解析JSON,如果是字符串则去掉引号
            if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&json_value) {
                match parsed {
                    serde_json::Value::String(s) => Ok(Some(s)),
                    _ => Ok(Some(json_value)), // 非字符串类型直接返回JSON
                }
            } else {
                Ok(Some(json_value)) // JSON解析失败,返回原始值
            }
        } else {
            Ok(None)
        }
    }

    /// 设置配置值
    fn set_config(&mut self, key: &str, value: &str) -> Result<()> {
        // 首先尝试更新现有配置
        let updated = self.connection.execute(
            "UPDATE app_config SET config_value = ?, updated_at = CURRENT_TIMESTAMP WHERE config_key = ?",
            params![format!("\"{}\"", value), key],
        )?;

        // 如果没有更新任何行,则插入新配置
        if updated == 0 {
            self.connection.execute(
                "INSERT INTO app_config (config_key, config_value, config_type, category, is_system_config, is_user_editable) VALUES (?, ?, 'STRING', 'system', TRUE, TRUE)",
                params![key, format!("\"{}\"", value)],
        )?;
        }
        Ok(())
    }

    /// 创建备份记录
    fn create_backup_record(
        &mut self,
        file_path: &str,
        service_version: &str,
        backup_type: &str,
        _status: &str,
    ) -> Result<i64> {
        // 生成唯一的备份名称
        let backup_name = format!("backup_{}", chrono::Utc::now().format("%Y%m%d_%H%M%S"));

        // 插入记录,让数据库自动生成ID
        self.connection.execute(
            "INSERT INTO backup_records (backup_name, backup_type, source_version, backup_path) 
             VALUES (?, ?, ?, ?)",
            params![backup_name, backup_type, service_version, file_path],
        )?;

        // 获取最后插入的ID
        let id: i64 =
            self.connection
                .query_row("SELECT currval('backup_records_seq')", [], |row| row.get(0))?;

        Ok(id)
    }

    /// 获取所有备份记录
    fn get_all_backups(&mut self) -> Result<Vec<BackupRecord>> {
        let mut stmt = self.connection.prepare(
            "SELECT id, backup_path, source_version, backup_type, created_at 
             FROM backup_records ORDER BY created_at DESC",
        )?;

        let backup_iter = stmt.query_map([], |row| {
            Ok(BackupRecord {
                id: row.get(0)?,
                file_path: row.get(1)?,
                service_version: row.get(2)?,
                backup_type: row.get(3)?,
                status: "completed".to_string(), // 新表架构没有status字段,默认为completed
                created_at: row.get(4)?,
            })
        })?;

        let mut backups = Vec::new();
        for backup in backup_iter {
            backups.push(backup?);
        }

        Ok(backups)
    }

    /// 根据ID获取备份记录
    fn get_backup_by_id(&mut self, id: i64) -> Result<Option<BackupRecord>> {
        let mut stmt = self.connection.prepare(
            "SELECT id, backup_path, source_version, backup_type, created_at 
             FROM backup_records WHERE id = ?",
        )?;

        let mut rows = stmt.query(params![id])?;

        if let Some(row) = rows.next()? {
            Ok(Some(BackupRecord {
                id: row.get(0)?,
                file_path: row.get(1)?,
                service_version: row.get(2)?,
                backup_type: row.get(3)?,
                status: "completed".to_string(),
                created_at: row.get(4)?,
            }))
        } else {
            Ok(None)
        }
    }

    /// 删除备份记录
    fn delete_backup_record(&mut self, backup_id: i64) -> Result<()> {
        self.connection.execute(
            "DELETE FROM backup_records WHERE id = ?",
            params![backup_id],
        )?;
        Ok(())
    }

    /// 更新备份文件路径
    fn update_backup_file_path(&mut self, backup_id: i64, new_path: &str) -> Result<()> {
        self.connection.execute(
            "UPDATE backup_records SET backup_path = ? WHERE id = ?",
            params![new_path, backup_id],
        )?;
        Ok(())
    }

    /// 创建计划任务
    fn create_scheduled_task(
        &mut self,
        task_type: &str,
        target_version: &str,
        scheduled_at: DateTime<Utc>,
        status: &str,
    ) -> Result<i64> {
        // 插入记录,让数据库自动生成ID
        self.connection.execute(
            "INSERT INTO scheduled_tasks (task_type, target_version, scheduled_at, status) 
             VALUES (?, ?, ?, ?)",
            params![task_type, target_version, scheduled_at, status],
        )?;

        // 获取最后插入的ID
        let id: i64 = self
            .connection
            .query_row("SELECT currval('task_id_seq')", [], |row| row.get(0))?;

        Ok(id)
    }

    /// 获取待执行任务
    fn get_pending_tasks(&mut self) -> Result<Vec<ScheduledTask>> {
        let mut stmt = self.connection.prepare(
            "SELECT id, task_type, target_version, scheduled_at, status, details, created_at, completed_at 
             FROM scheduled_tasks WHERE status = 'PENDING' ORDER BY scheduled_at"
        )?;

        let task_iter = stmt.query_map([], |row| {
            Ok(ScheduledTask {
                id: row.get(0)?,
                task_type: row.get(1)?,
                target_version: row.get(2)?,
                scheduled_at: row.get(3)?,
                status: row.get(4)?,
                details: row.get(5)?,
                created_at: row.get(6)?,
                completed_at: row.get(7)?,
            })
        })?;

        let mut tasks = Vec::new();
        for task in task_iter {
            tasks.push(task?);
        }

        Ok(tasks)
    }

    /// 更新任务状态
    fn update_task_status(
        &mut self,
        task_id: i64,
        status: &str,
        details: Option<&str>,
    ) -> Result<()> {
        if let Some(details) = details {
            self.connection.execute(
                "UPDATE scheduled_tasks SET status = ?, details = ?, completed_at = NOW() WHERE id = ?",
                params![status, details, task_id],
            )?;
        } else {
            self.connection.execute(
                "UPDATE scheduled_tasks SET status = ?, completed_at = NOW() WHERE id = ?",
                params![status, task_id],
            )?;
        }
        Ok(())
    }

    /// 取消待执行任务
    fn cancel_pending_tasks(&mut self, task_type: &str) -> Result<()> {
        self.connection.execute(
            "UPDATE scheduled_tasks SET status = 'CANCELLED', completed_at = NOW() 
             WHERE task_type = ? AND status = 'PENDING'",
            params![task_type],
        )?;
        Ok(())
    }

    // ========== 下载任务管理方法 ==========

    /// 创建下载任务
    fn create_download_task(
        &mut self,
        task_name: &str,
        download_url: &str,
        total_size: i64,
        target_path: &str,
        file_hash: Option<&str>,
    ) -> Result<i64> {
        // 使用 RETURNING 子句获取插入的ID
        let id: i64 = self
            .connection
            .query_row(
                "INSERT INTO download_tasks (task_name, download_url, total_size, target_path, file_hash) 
                 VALUES (?, ?, ?, ?, ?) RETURNING id",
                params![task_name, download_url, total_size, target_path, file_hash],
                |row| row.get(0)
            )?;

        Ok(id)
    }

    /// 更新下载任务状态
    fn update_download_task_status(
        &mut self,
        task_id: i64,
        status: &str,
        downloaded_size: Option<i64>,
        error_message: Option<&str>,
    ) -> Result<()> {
        if let Some(size) = downloaded_size {
            if let Some(error) = error_message {
                self.connection.execute(
                    "UPDATE download_tasks SET status = ?, downloaded_size = ?, error_message = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
                    params![status, size, error, task_id],
                )?;
            } else {
                self.connection.execute(
                    "UPDATE download_tasks SET status = ?, downloaded_size = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
                    params![status, size, task_id],
                )?;
            }
        } else if let Some(error) = error_message {
            self.connection.execute(
                "UPDATE download_tasks SET status = ?, error_message = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
                params![status, error, task_id],
            )?;
        } else {
            self.connection.execute(
                "UPDATE download_tasks SET status = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
                params![status, task_id],
            )?;
        };
        Ok(())
    }

    /// 完成下载任务
    fn complete_download_task(
        &mut self,
        task_id: i64,
        average_speed: Option<i64>,
        total_duration: Option<i32>,
    ) -> Result<()> {
        self.connection.execute(
            "UPDATE download_tasks SET status = 'COMPLETED', average_speed = ?, total_duration_seconds = ?, 
             completed_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
            params![average_speed.unwrap_or(0), total_duration.unwrap_or(0), task_id],
        )?;
        Ok(())
    }

    /// 获取下载任务
    fn get_download_task(&mut self, task_id: i64) -> Result<Option<DownloadTaskRecord>> {
        let mut stmt = self.connection.prepare(
            "SELECT id, task_name, download_url, total_size, downloaded_size, target_path, file_hash, 
             status, error_message, retry_count, average_speed, total_duration_seconds, 
             created_at, updated_at, completed_at 
             FROM download_tasks WHERE id = ?"
        )?;

        let mut rows = stmt.query(params![task_id])?;

        if let Some(row) = rows.next()? {
            Ok(Some(DownloadTaskRecord {
                id: row.get(0)?,
                task_name: row.get(1)?,
                download_url: row.get(2)?,
                total_size: row.get(3)?,
                downloaded_size: row.get(4)?,
                target_path: row.get(5)?,
                file_hash: row.get(6)?,
                status: row.get(7)?,
                error_message: row.get(8)?,
                retry_count: row.get(9)?,
                average_speed: row.get(10)?,
                total_duration_seconds: row.get(11)?,
                created_at: row.get(12)?,
                updated_at: row.get(13)?,
                completed_at: row.get(14)?,
            }))
        } else {
            Ok(None)
        }
    }

    /// 获取活跃的下载任务
    fn get_active_download_tasks(&mut self) -> Result<Vec<DownloadTaskRecord>> {
        let mut stmt = self.connection.prepare(
            "SELECT id, task_name, download_url, total_size, downloaded_size, target_path, file_hash, 
             status, error_message, retry_count, average_speed, total_duration_seconds, 
             created_at, updated_at, completed_at 
             FROM download_tasks WHERE status IN ('PENDING', 'DOWNLOADING', 'PAUSED') 
             ORDER BY created_at DESC"
        )?;

        let task_iter = stmt.query_map([], |row| {
            Ok(DownloadTaskRecord {
                id: row.get(0)?,
                task_name: row.get(1)?,
                download_url: row.get(2)?,
                total_size: row.get(3)?,
                downloaded_size: row.get(4)?,
                target_path: row.get(5)?,
                file_hash: row.get(6)?,
                status: row.get(7)?,
                error_message: row.get(8)?,
                retry_count: row.get(9)?,
                average_speed: row.get(10)?,
                total_duration_seconds: row.get(11)?,
                created_at: row.get(12)?,
                updated_at: row.get(13)?,
                completed_at: row.get(14)?,
            })
        })?;

        let mut tasks = Vec::new();
        for task in task_iter {
            tasks.push(task?);
        }
        Ok(tasks)
    }

    // ========== 应用状态管理方法 ==========

    /// 更新应用状态
    fn update_app_state(
        &mut self,
        state: &str,
        state_data: Option<&str>,
        error_message: Option<&str>,
    ) -> Result<()> {
        // 使用UPSERT(INSERT OR REPLACE)来更新唯一记录
        self.connection.execute(
            "INSERT OR REPLACE INTO app_state (id, current_state, state_data, last_error, updated_at) 
             VALUES (1, ?, ?, ?, CURRENT_TIMESTAMP)",
            params![state, state_data, error_message],
        )?;
        Ok(())
    }

    /// 获取当前应用状态
    fn get_app_state(&mut self) -> Result<Option<AppStateRecord>> {
        let mut stmt = self.connection.prepare(
            "SELECT current_state, state_data, last_error, 
             created_at, updated_at 
             FROM app_state WHERE id = 1",
        )?;

        let mut rows = stmt.query([])?;

        if let Some(row) = rows.next()? {
            Ok(Some(AppStateRecord {
                current_state: row.get(0)?,
                state_data: row.get(1)?,
                last_error: row.get(2)?,
                created_at: row.get(3)?,
                updated_at: row.get(4)?,
            }))
        } else {
            Ok(None)
        }
    }

    // ========== 用户操作历史方法 ==========

    /// 记录用户操作
    fn record_user_action(
        &mut self,
        action_type: &str,
        action_description: &str,
        action_params: Option<&str>,
    ) -> Result<i64> {
        // 获取客户端版本和平台信息
        let client_version = env!("CARGO_PKG_VERSION");
        let platform_info = format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH);

        // 使用 RETURNING 子句获取插入的ID
        let id: i64 = self
            .connection
            .query_row(
                "INSERT INTO user_actions (action_type, action_description, action_params, status, client_version, platform_info) 
                 VALUES (?, ?, ?, 'SUCCESS', ?, ?) RETURNING id",
                params![action_type, action_description, action_params, client_version, platform_info],
                |row| row.get(0)
            )?;

        Ok(id)
    }

    /// 完成用户操作
    fn complete_user_action(
        &mut self,
        action_id: i64,
        status: &str,
        result_message: Option<&str>,
        duration_seconds: Option<i32>,
    ) -> Result<()> {
        self.connection.execute(
            "UPDATE user_actions SET status = ?, result_message = ?, completed_at = CURRENT_TIMESTAMP, duration_seconds = ? 
             WHERE id = ?",
            params![status, result_message, duration_seconds, action_id],
        )?;
        Ok(())
    }

    /// 获取用户操作历史
    fn get_user_actions(&mut self, limit: Option<i32>) -> Result<Vec<UserActionRecord>> {
        let sql = if let Some(limit) = limit {
            format!(
                "SELECT id, action_type, action_description, action_params, status, result_message, 
                 started_at, completed_at, duration_seconds, client_version, platform_info 
                 FROM user_actions ORDER BY started_at DESC LIMIT {limit}"
            )
        } else {
            "SELECT id, action_type, action_description, action_params, status, result_message, 
             started_at, completed_at, duration_seconds, client_version, platform_info 
             FROM user_actions ORDER BY started_at DESC"
                .to_string()
        };

        let mut stmt = self.connection.prepare(&sql)?;
        let action_iter = stmt.query_map([], |row| {
            Ok(UserActionRecord {
                id: row.get(0)?,
                action_type: row.get(1)?,
                action_description: row.get(2)?,
                action_params: row.get(3)?,
                status: row.get(4)?,
                result_message: row.get(5)?,
                started_at: row.get(6)?,
                completed_at: row.get(7)?,
                duration_seconds: row.get(8)?,
                client_version: row.get(9)?,
                platform_info: row.get(10)?,
            })
        })?;

        let mut actions = Vec::new();
        for action in action_iter {
            actions.push(action?);
        }
        Ok(actions)
    }
}