chapter.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // 假设最大长度为255,您可能需要根据实际数据库定义调整
  25. const maxLength = 255;
  26. const sql = `
  27. INSERT INTO chapter (name, book_id, author_id, content, level, order_index, order_id, old_path, path)
  28. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
  29. `;
  30. const values = [
  31. name,
  32. book_id,
  33. author_id,
  34. content,
  35. level,
  36. order_index,
  37. order_id,
  38. old_path,
  39. path,
  40. ];
  41. return new Promise((resolve, reject) => {
  42. connection.execute(sql, values, (error, result) => {
  43. if (error) {
  44. console.error("Database error:", error);
  45. return reject(error); // 使用 reject 处理错误
  46. }
  47. resolve(result); // 返回查询结果
  48. });
  49. });
  50. }
  51. /*根据文件路径查询章节数据*/
  52. // select * from files where source_id like '%part0042%'
  53. export async function searchChapterInfoForPath(path, book_id) {
  54. return new Promise((resolve, reject) => {
  55. const query = `
  56. SELECT files.file_id
  57. FROM files
  58. INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
  59. WHERE book_link_file.book_id = ?
  60. AND files.path LIKE ?;`; // 确保 `source_id` 上有索引
  61. // 调整参数顺序以匹配 SQL 中的占位符顺序
  62. const queryParams = [book_id, `%${path}%`];
  63. connection.query(query, queryParams, (err, rows) => {
  64. if (err) {
  65. return reject(err);
  66. }
  67. resolve(rows.length > 0 ? rows[0] : false);
  68. });
  69. });
  70. }
  71. /*获取当前书籍的所有数据*/
  72. export async function searchChapterForBookId({book_id, level = 10}) {
  73. console.log(8282, level);
  74. return new Promise((resolve, reject) => {
  75. /*
  76. SELECT files.*, chapter.*
  77. FROM files
  78. INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
  79. INNER JOIN chapter ON files.path like CONCAT('%', chapter.order_id, '%')
  80. WHERE book_link_file.book_id = '28b639bf9362ad3b2cd5a24cb6d811c0'
  81. AND files.mimetype = 'text/html' AND chapter.level = '1';
  82. */
  83. const query = `
  84. SELECT files.*, chapter.*
  85. FROM files
  86. INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
  87. INNER JOIN chapter ON files.path LIKE CONCAT('%', chapter.order_id, '%')
  88. WHERE book_link_file.book_id = '${book_id}'
  89. AND files.mimetype = 'text/html' ${level === 10 ? '' : `AND chapter.level = '${level}'`};
  90. `;
  91. console.log(9292, query)
  92. connection.query(query, [], (err, rows) => {
  93. if (err) {
  94. return reject(err);
  95. }
  96. resolve(rows);
  97. });
  98. });
  99. }