Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | const Database = require('better-sqlite3'); const config = require('../config'); /** * 数据库管理 - 统一数据库连接 */ class DatabaseManager { constructor(dbPath = null) { // 从配置文件读取数据库路径 this.dbPath = dbPath || config.database.path; this.db = null; } /** * 初始化数据库连接 */ init() { try { this.db = new Database(this.dbPath); this.db.pragma('journal_mode = WAL'); console.log(`✅ 数据库已连接:${this.dbPath}`); return true; } catch (error) { console.error('❌ 数据库连接失败:', error.message); return false; } } /** * 执行 SQL 查询 */ query(sql, params = []) { if (!this.db) { throw new Error('数据库未初始化'); } return this.db.prepare(sql).run(...params); } /** * 执行 SQL 查询并获取结果 */ getAll(sql, params = []) { if (!this.db) { throw new Error('数据库未初始化'); } return this.db.prepare(sql).all(...params); } /** * 执行 SQL 查询并获取单条结果 */ getOne(sql, params = []) { if (!this.db) { throw new Error('数据库未初始化'); } return this.db.prepare(sql).get(...params); } /** * 关闭数据库连接 */ close() { if (this.db) { this.db.close(); console.log('👋 数据库已关闭'); } } /** * 获取数据库路径 */ getDbPath() { return this.dbPath; } } module.exports = { DatabaseManager }; |