25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.6KB

  1. <%
  2. '=======================================================================================================================
  3. ' INDUSTRY REPOSITORY
  4. '=======================================================================================================================
  5. Class IndustryRepository_Class
  6. ' All industries with the number of businesses in each: list of Dictionary(id, name, n)
  7. Public Function All()
  8. Dim list, rs, d
  9. Set list = CreateObject("Scripting.Dictionary")
  10. Set rs = Db_Query("SELECT i.id, i.name, COUNT(b.id) AS n FROM industries i LEFT JOIN businesses b ON b.industry_id = i.id GROUP BY i.id, i.name ORDER BY i.name", Array())
  11. Do While Not rs.EOF
  12. Set d = CreateObject("Scripting.Dictionary")
  13. d("id") = CLng(rs(0)) : d("name") = rs(1) & "" : d("n") = CLng(rs(2))
  14. list.Add list.Count, d
  15. rs.MoveNext
  16. Loop
  17. rs.Close
  18. Set All = list
  19. End Function
  20. Public Function UncategorizedCount()
  21. Dim rs
  22. Set rs = Db_Query("SELECT COUNT(*) FROM businesses WHERE industry_id IS NULL", Array())
  23. UncategorizedCount = CLng(rs(0))
  24. rs.Close
  25. End Function
  26. Public Function Find(id)
  27. Dim rs
  28. Set rs = Db_Query("SELECT i.id, i.name, (SELECT COUNT(*) FROM businesses b WHERE b.industry_id = i.id) AS n FROM industries i WHERE i.id = ?", Array(Db_PI(id)))
  29. If rs.EOF Then
  30. Set Find = Nothing
  31. Else
  32. Dim d : Set d = CreateObject("Scripting.Dictionary")
  33. d("id") = CLng(rs(0)) : d("name") = rs(1) & "" : d("n") = CLng(rs(2))
  34. Set Find = d
  35. End If
  36. rs.Close
  37. End Function
  38. ' True when another industry (not excludeId) already has this name (case-insensitive).
  39. Public Function NameTaken(name, excludeId)
  40. Dim rs
  41. Set rs = Db_Query("SELECT COUNT(*) FROM industries WHERE name = ? AND id <> ?", Array(Db_PS(name), Db_PI(excludeId)))
  42. NameTaken = (CLng(rs(0)) > 0)
  43. rs.Close
  44. End Function
  45. Public Sub Create(name)
  46. Db_Exec "INSERT INTO industries (name) VALUES (?)", Array(Db_PS(name))
  47. End Sub
  48. Public Sub Rename(id, name)
  49. Db_Exec "UPDATE industries SET name = ? WHERE id = ?", Array(Db_PS(name), Db_PI(id))
  50. End Sub
  51. ' Moves every business from sourceId to targetId, then deletes sourceId. Returns the number of businesses moved.
  52. Public Function Merge(sourceId, targetId)
  53. Dim conn, moved, rs, msg
  54. Set conn = DAL().Connection
  55. Set rs = Db_Query("SELECT COUNT(*) FROM businesses WHERE industry_id = ?", Array(Db_PI(sourceId)))
  56. moved = CLng(rs(0))
  57. rs.Close
  58. conn.BeginTrans
  59. On Error Resume Next
  60. Db_Exec "UPDATE businesses SET industry_id = ? WHERE industry_id = ?", Array(Db_PI(targetId), Db_PI(sourceId))
  61. If Err.Number = 0 Then Db_Exec "DELETE FROM industries WHERE id = ?", Array(Db_PI(sourceId))
  62. If Err.Number <> 0 Then
  63. msg = Err.Description
  64. Err.Clear
  65. conn.RollbackTrans
  66. On Error GoTo 0
  67. Err.Raise 5, "IndustryRepository.Merge", msg
  68. End If
  69. conn.CommitTrans
  70. On Error GoTo 0
  71. Merge = moved
  72. End Function
  73. Public Sub Delete(id)
  74. Db_Exec "DELETE FROM industries WHERE id = ?", Array(Db_PI(id))
  75. End Sub
  76. End Class
  77. Dim IndustryRepository_Class__Singleton
  78. Function IndustryRepository()
  79. If IsEmpty(IndustryRepository_Class__Singleton) Then
  80. Set IndustryRepository_Class__Singleton = New IndustryRepository_Class
  81. End If
  82. Set IndustryRepository = IndustryRepository_Class__Singleton
  83. End Function
  84. %>

Powered by TurnKey Linux.