2
0

api_w_xref.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package route
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "opennamu/route/tool"
  7. "strconv"
  8. )
  9. func Api_w_xref(call_arg []string) {
  10. db_set := map[string]string{}
  11. json.Unmarshal([]byte(call_arg[0]), &db_set)
  12. other_set := map[string]string{}
  13. json.Unmarshal([]byte(call_arg[1]), &other_set)
  14. db := tool.DB_connect(db_set)
  15. if db == nil {
  16. return
  17. }
  18. defer db.Close()
  19. page, _ := strconv.Atoi(other_set["page"])
  20. num := 0
  21. if page*50 > 0 {
  22. num = page*50 - 50
  23. }
  24. var link_case_insensitive string
  25. err := db.QueryRow(tool.DB_change(db_set, "select data from other where name = 'link_case_insensitive'")).Scan(&link_case_insensitive)
  26. if err != nil {
  27. if err == sql.ErrNoRows {
  28. link_case_insensitive = ""
  29. } else {
  30. return
  31. }
  32. }
  33. if link_case_insensitive != "" {
  34. link_case_insensitive = " collate nocase"
  35. }
  36. var stmt *sql.Stmt
  37. if other_set["do_type"] == "1" {
  38. stmt, err = db.Prepare(tool.DB_change(db_set, "select distinct link, type from back where title"+link_case_insensitive+" = ? and not type = 'no' and not type = 'nothing' order by type asc, link asc limit ?, 50"))
  39. } else {
  40. stmt, err = db.Prepare(tool.DB_change(db_set, "select distinct title, type from back where link"+link_case_insensitive+" = ? and not type = 'no' and not type = 'nothing' order by type asc, title asc limit ?, 50"))
  41. }
  42. if err != nil {
  43. return
  44. }
  45. defer stmt.Close()
  46. rows, err := stmt.Query(other_set["name"], num)
  47. if err != nil {
  48. return
  49. }
  50. defer rows.Close()
  51. var name string
  52. var type_data string
  53. var data_list [][]string
  54. for rows.Next() {
  55. err := rows.Scan(&name, &type_data)
  56. if err != nil {
  57. return
  58. }
  59. data_list = append(data_list, []string{name, type_data})
  60. }
  61. if len(data_list) == 0 {
  62. fmt.Print("{}")
  63. } else {
  64. json_data, _ := json.Marshal(data_list)
  65. fmt.Print(string(json_data))
  66. }
  67. }