chapter.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, book_id, author_id, content, level, order_index, order_id, old_path, path,
  32. ];
  33. return new Promise((resolve, reject) => {
  34. connection.execute(sql, values, (error, result) => {
  35. if (error) {
  36. console.error('Database error:', error);
  37. return reject(error); // 使用 reject 处理错误
  38. }
  39. resolve(result); // 返回查询结果
  40. });
  41. });
  42. }
  43. /*根据文件路径查询章节数据*/
  44. // select * from files where source_id like '%part0042%'
  45. export async function searchChapterInfoForPath(path, book_id) {
  46. return new Promise((resolve, reject) => {
  47. const query = `
  48. SELECT files.file_id
  49. FROM files
  50. INNER JOIN book_link_file ON files.file_id = book_link_file.file_id
  51. WHERE book_link_file.book_id = ?
  52. AND files.source_id LIKE ?;`; // 确保 `source_id` 上有索引
  53. // 调整参数顺序以匹配 SQL 中的占位符顺序
  54. const queryParams = [book_id, `%${path}%`];
  55. connection.query(query, queryParams, (err, rows) => {
  56. if (err) {
  57. return reject(err);
  58. }
  59. resolve(rows.length > 0 ? rows[0] : false);
  60. });
  61. });
  62. }