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.

215 lignes
9.2KB

  1. <%
  2. '=======================================================================================================================
  3. ' DATABASE CLASS (native G3DB)
  4. '=======================================================================================================================
  5. ' Thin wrapper over AxonASP's G3DB library. Keeps the original Database_Class API (Query, PagedQuery, Execute,
  6. ' Begin/Commit/RollbackTransaction, Connection) so callers such as DAL() and the Migrator are unchanged.
  7. '
  8. ' Connection string: "<driver>:<dsn>", e.g.
  9. ' sqlite:/opt/axonasp/site/db/app.sqlite
  10. ' mysql:user:pass@tcp(127.0.0.1:3306)/app?parseTime=true
  11. ' postgres:host=127.0.0.1 port=5432 user=u password=p dbname=app sslmode=disable
  12. ' mssql:server=127.0.0.1;port=1433;user id=u;password=p;database=app
  13. '
  14. ' Behavior notes (G3DB):
  15. ' - Query returns a forward-only result set (EOF, MoveNext, rs("col"), rs(0), Fields.Count, Close).
  16. ' - Parameters are positional "?" values. G3DB does not expand an array, so this class does (max MAX_PARAMS).
  17. ' - G3DB signals failure with Empty plus LastError; this class turns that into a raised VBScript error that
  18. ' includes the SQL, so errors are never silently ignored.
  19. ' - Transactions are handle based; while one is open, Query/Execute are routed through it.
  20. Class Database_Class
  21. Private m_db
  22. Private m_tx
  23. Private m_driver
  24. Private m_dsn
  25. Private m_connection_string
  26. Private m_trace_enabled
  27. Public Sub set_trace(bool)
  28. m_trace_enabled = bool
  29. End Sub
  30. Public Property Get is_trace_enabled
  31. is_trace_enabled = m_trace_enabled
  32. End Property
  33. '---------------------------------------------------------------------------------------------------------------------
  34. Public Sub Initialize(connection_string)
  35. Dim p
  36. m_connection_string = "" & connection_string
  37. p = InStr(m_connection_string, ":")
  38. If p < 2 Then
  39. Err.Raise vbObjectError + 100, "Database_Class", "ConnectionString must look like '<driver>:<dsn>' (for example sqlite:/path/app.sqlite). Got: [" & m_connection_string & "]"
  40. End If
  41. m_driver = LCase(Left(m_connection_string, p - 1))
  42. m_dsn = Mid(m_connection_string, p + 1)
  43. If m_driver = "sqlite" Or m_driver = "sqlite3" Then
  44. If InStr(m_dsn, "?") = 0 Then m_dsn = m_dsn & "?_busy_timeout=5000"
  45. End If
  46. End Sub
  47. '---------------------------------------------------------------------------------------------------------------------
  48. ' The underlying G3DB object (opened on first use)
  49. Public Function Connection()
  50. If Not IsObject(m_db) Then
  51. Set m_db = Server.CreateObject("G3DB")
  52. If Not m_db.Open(m_driver, m_dsn) Then
  53. Dim msg
  54. msg = m_db.LastError
  55. Set m_db = Empty
  56. Err.Raise vbObjectError + 101, "Database_Class", "Could not open " & m_driver & " database: " & msg
  57. End If
  58. End If
  59. Set Connection = m_db
  60. End Function
  61. '---------------------------------------------------------------------------------------------------------------------
  62. ' Runs a SELECT. params: Empty/Null (none), a scalar (one value) or an array of values.
  63. Public Function Query(sql, params)
  64. Dim target, rs
  65. Set target = Handle()
  66. Set rs = RunQuery(target, sql, ParamList(params))
  67. If IsEmpty(rs) Then RaiseDbError "Query failed", sql
  68. Set Query = rs
  69. End Function
  70. '---------------------------------------------------------------------------------------------------------------------
  71. ' Runs INSERT/UPDATE/DELETE/DDL. Returns the G3DBResult (RowsAffected, LastInsertId).
  72. Public Function Exec(sql, params)
  73. Dim target, result
  74. Set target = Handle()
  75. Set result = RunExec(target, sql, ParamList(params))
  76. If IsEmpty(result) Then RaiseDbError "Statement failed", sql
  77. Set Exec = result
  78. End Function
  79. ' Same as Exec but discards the result (kept for the original Execute(sql, params) call style).
  80. Public Sub [Execute](sql, params)
  81. Dim result
  82. Set result = Exec(sql, params)
  83. Set result = Nothing
  84. End Sub
  85. '---------------------------------------------------------------------------------------------------------------------
  86. ' Returns one page of rows (1-based page_num) using LIMIT/OFFSET (SQLite, MySQL, PostgreSQL).
  87. ' sql must not already contain LIMIT/OFFSET.
  88. Public Function PagedQuery(sql, params, per_page, page_num)
  89. Dim size, page
  90. size = CLng(per_page)
  91. page = CLng(page_num)
  92. If size < 1 Then size = 1
  93. If page < 1 Then page = 1
  94. Set PagedQuery = Query(sql & " LIMIT " & size & " OFFSET " & ((page - 1) * size), params)
  95. End Function
  96. '---------------------------------------------------------------------------------------------------------------------
  97. Public Sub BeginTransaction()
  98. If IsObject(m_tx) Then
  99. Err.Raise vbObjectError + 102, "Database_Class", "A transaction is already open"
  100. End If
  101. Dim tx
  102. Set tx = Connection().Begin()
  103. If IsEmpty(tx) Then RaiseDbError "Could not begin transaction", ""
  104. Set m_tx = tx
  105. End Sub
  106. Public Sub CommitTransaction()
  107. Dim tx
  108. If Not IsObject(m_tx) Then Exit Sub
  109. Set tx = m_tx
  110. Set m_tx = Empty
  111. tx.Commit
  112. End Sub
  113. Public Sub RollbackTransaction()
  114. Dim tx
  115. If Not IsObject(m_tx) Then Exit Sub
  116. Set tx = m_tx
  117. Set m_tx = Empty
  118. tx.Rollback
  119. End Sub
  120. '---------------------------------------------------------------------------------------------------------------------
  121. ' Private helpers
  122. '---------------------------------------------------------------------------------------------------------------------
  123. ' The object to run statements on: the open transaction if there is one, otherwise the connection
  124. Private Function Handle()
  125. If IsObject(m_tx) Then
  126. Set Handle = m_tx
  127. Else
  128. Set Handle = Connection()
  129. End If
  130. End Function
  131. ' Normalizes params to a zero-based array (possibly empty)
  132. Private Function ParamList(params)
  133. If IsArray(params) Then
  134. ParamList = params
  135. ElseIf IsEmpty(params) Or IsNull(params) Then
  136. ParamList = Array()
  137. Else
  138. ParamList = Array(params)
  139. End If
  140. End Function
  141. Private Function TxNote()
  142. If IsObject(m_tx) Then
  143. TxNote = " (statement ran inside a transaction; G3DB does not expose the driver error there)"
  144. Else
  145. TxNote = ""
  146. End If
  147. End Function
  148. Private Sub RaiseDbError(prefix, sql)
  149. Dim detail
  150. detail = ""
  151. If IsObject(m_db) Then detail = m_db.LastError
  152. If Len(detail) = 0 Then
  153. ' G3DB reports no LastError for statements run inside a transaction handle
  154. detail = "no driver message available" & TxNote()
  155. End If
  156. Err.Raise vbObjectError + 103, "Database_Class", prefix & ": " & detail & " -- SQL: " & sql
  157. End Sub
  158. ' G3DB takes positional arguments, not an array, so expand explicitly
  159. Private Function RunQuery(target, sql, p)
  160. Select Case UBound(p)
  161. Case -1 : Set RunQuery = target.Query(sql)
  162. Case 0 : Set RunQuery = target.Query(sql, p(0))
  163. Case 1 : Set RunQuery = target.Query(sql, p(0), p(1))
  164. Case 2 : Set RunQuery = target.Query(sql, p(0), p(1), p(2))
  165. Case 3 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3))
  166. Case 4 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4))
  167. Case 5 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5))
  168. Case 6 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6))
  169. Case 7 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7))
  170. Case Else
  171. Err.Raise vbObjectError + 104, "Database_Class", "Too many parameters (" & (UBound(p) + 1) & "); the maximum is 8"
  172. End Select
  173. End Function
  174. Private Function RunExec(target, sql, p)
  175. Select Case UBound(p)
  176. Case -1 : Set RunExec = target.Exec(sql)
  177. Case 0 : Set RunExec = target.Exec(sql, p(0))
  178. Case 1 : Set RunExec = target.Exec(sql, p(0), p(1))
  179. Case 2 : Set RunExec = target.Exec(sql, p(0), p(1), p(2))
  180. Case 3 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3))
  181. Case 4 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4))
  182. Case 5 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5))
  183. Case 6 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6))
  184. Case 7 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7))
  185. Case Else
  186. Err.Raise vbObjectError + 104, "Database_Class", "Too many parameters (" & (UBound(p) + 1) & "); the maximum is 8"
  187. End Select
  188. End Function
  189. Private Sub Class_Terminate()
  190. On Error Resume Next
  191. If IsObject(m_tx) Then m_tx.Rollback
  192. If IsObject(m_db) Then m_db.Close
  193. On Error GoTo 0
  194. End Sub
  195. End Class
  196. %>

Powered by TurnKey Linux.