Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

145 lignes
5.1KB

  1. <%
  2. Class Database_Class
  3. Private m_connection
  4. Private m_connection_string
  5. Private m_shape_connections
  6. Private m_trace_enabled
  7. Public Sub set_trace(bool) : m_trace_enabled = bool : End Sub
  8. Public Property Get is_trace_enabled : is_trace_enabled = m_trace_enabled : End Property
  9. Private Sub Class_Initialize
  10. Set m_shape_connections = Server.CreateObject("Scripting.Dictionary")
  11. End Sub
  12. '---------------------------------------------------------------------------------------------------------------------
  13. Public Sub Initialize(connection_string)
  14. m_connection_string = connection_string
  15. End Sub
  16. '---------------------------------------------------------------------------------------------------------------------
  17. ' MSDataShape opens its own dedicated connection per call (it cannot reuse
  18. ' the plain Connection() used by Query/PagedQuery). That connection is
  19. ' tracked and closed in Class_Terminate rather than left open until the
  20. ' object is garbage collected.
  21. Public Function ShapeQuery(sql, params)
  22. Dim shapeConn, cmd, rs, key
  23. Set shapeConn = Server.CreateObject("adodb.connection")
  24. shapeConn.ConnectionString = "Provider=MSDataShape;Data " & m_connection_string
  25. shapeConn.Open
  26. key = m_shape_connections.Count
  27. m_shape_connections.Add key, shapeConn
  28. Set cmd = Server.CreateObject("adodb.command")
  29. Set cmd.ActiveConnection = shapeConn
  30. cmd.CommandText = sql
  31. If IsArray(params) then
  32. Set rs = cmd.Execute(, params)
  33. ElseIf Not IsEmpty(params) then ' one parameter
  34. Set rs = cmd.Execute(, Array(params))
  35. Else
  36. Set rs = cmd.Execute()
  37. End If
  38. Set ShapeQuery = rs
  39. End Function
  40. Public Function Query(sql, params)
  41. Dim cmd, rs
  42. Set cmd = Server.CreateObject("adodb.command")
  43. Set cmd.ActiveConnection = Connection
  44. cmd.CommandText = sql
  45. If IsArray(params) then
  46. Set rs = cmd.Execute(, params)
  47. ElseIf Not IsEmpty(params) then ' one parameter
  48. Set rs = cmd.Execute(, Array(params))
  49. Else
  50. Set rs = cmd.Execute()
  51. End If
  52. Set Query = rs
  53. End Function
  54. '---------------------------------------------------------------------------------------------------------------------
  55. ' per_page/page_num were previously accepted but ignored - PageSize,
  56. ' CacheSize, and AbsolutePage were hardcoded to 1 regardless of the
  57. ' arguments passed in, so paging never actually worked. page_num is
  58. ' clamped to a valid page (1..PageCount) since AbsolutePage raises an
  59. ' error outside that range, and honoring the caller's page_num means an
  60. ' out-of-range value is now reachable where it previously never was.
  61. Public Function PagedQuery(sql, params, per_page, page_num)
  62. Dim cmd, rs
  63. Set cmd = Server.CreateObject("adodb.command")
  64. Set cmd.ActiveConnection = Connection
  65. cmd.CommandText = sql
  66. cmd.CommandType = 1 'adCmdText
  67. cmd.ActiveConnection.CursorLocation = 3 'adUseClient
  68. If IsArray(params) then
  69. Set rs = cmd.Execute(, params)
  70. ElseIf Not IsEmpty(params) then ' one parameter
  71. Set rs = cmd.Execute(, Array(params))
  72. Else
  73. Set rs = cmd.Execute()
  74. End If
  75. If Not rs.EOF then
  76. rs.PageSize = per_page
  77. rs.CacheSize = per_page
  78. If page_num < 1 Then page_num = 1
  79. If page_num > rs.PageCount Then page_num = rs.PageCount
  80. rs.AbsolutePage = page_num
  81. End If
  82. Set PagedQuery = rs
  83. End Function
  84. '---------------------------------------------------------------------------------------------------------------------
  85. Public Sub [Execute](sql, params)
  86. me.query sql, params
  87. End Sub
  88. '---------------------------------------------------------------------------------------------------------------------
  89. Public Sub BeginTransaction
  90. Connection.BeginTrans
  91. End Sub
  92. Public Sub RollbackTransaction
  93. Connection.RollbackTrans
  94. End Sub
  95. Public Sub CommitTransaction
  96. Connection.CommitTrans
  97. End Sub
  98. '---------------------------------------------------------------------------------------------------------------------
  99. ' Private Methods
  100. '---------------------------------------------------------------------------------------------------------------------
  101. Private Sub Class_terminate
  102. Destroy m_connection
  103. Dim key
  104. If Not m_shape_connections Is Nothing Then
  105. For Each key In m_shape_connections.Keys
  106. Destroy m_shape_connections(key)
  107. Next
  108. Set m_shape_connections = Nothing
  109. End If
  110. End Sub
  111. Public Function Connection
  112. if not isobject(m_connection) then
  113. set m_connection = Server.CreateObject("adodb.connection")
  114. m_connection.open m_connection_string
  115. end if
  116. set Connection = m_connection
  117. End Function
  118. end Class
  119. %>

Powered by TurnKey Linux.