chapter.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import connection from "./base.js";
  2. /*
  3. * `name` VARCHAR(255) NOT NULL, -- Chapter name with a maximum length of 255 characters
  4. `book_id` VARCHAR(100) NOT NULL, -- Book ID with a maximum length of 100 characters
  5. `author_id` VARCHAR(100) NOT NULL, -- Author ID with a maximum length of 100 characters
  6. `content` LONGTEXT DEFAULT NULL, -- Chapter content
  7. `level` INT NULL, -- Level of the chapter, can be NULL
  8. `order_index` INT NULL, -- Order index for sorting chapters, can be NULL
  9. `order_id` VARCHAR(255) NULL, -- Order ID, can be NULL
  10. `old_path` VARCHAR(255) NULL, -- Old path, can be NULL
  11. `path` VARCHAR(255) NULL, -- Current path, can be NULL
  12. * */
  13. export async function chapter_insert({
  14. name = "",
  15. book_id = "",
  16. author_id = "",
  17. content = "",
  18. level = '',
  19. order_index = "",
  20. order_id = "",
  21. old_path = "",
  22. path = "",
  23. }) {
  24. const sql = `
  25. INSERT INTO chapter (name, book_id, author_id, content, level, order_index, order_id, old_path, path)
  26. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
  27. `;
  28. const values = [
  29. name, book_id, author_id, content, level, order_index, order_id, old_path, path,
  30. ];
  31. return new Promise((resolve, reject) => {
  32. connection.execute(sql, values, (error, result) => {
  33. if (error) {
  34. console.error('Database error:', error);
  35. return reject(error); // 使用 reject 处理错误
  36. }
  37. resolve(result); // 返回查询结果
  38. });
  39. });
  40. }
  41. /*根据文件路径查询章节数据*/
  42. // select * from files where source_id like '%part0042%'
  43. export async function searchChapterInfoForPath(path, book_id) {
  44. return new Promise((resolve, reject) => {
  45. const query = `
  46. SELECT files.file_id
  47. FROM files
  48. INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
  49. WHERE book_link_file.book_id = ?
  50. AND files.source_id LIKE ?;`; // 确保 `source_id` 上有索引
  51. // 调整参数顺序以匹配 SQL 中的占位符顺序
  52. const queryParams = [book_id, `%${path}%`];
  53. connection.query(query, queryParams, (err, rows) => {
  54. if (err) {
  55. return reject(err);
  56. }
  57. resolve(rows.length > 0 ? rows[0] : false);
  58. });
  59. });
  60. }