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.

232 lignes
10KB

  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_txFailed ' first error raised inside the open transaction ("" while none)
  24. Private m_driver
  25. Private m_dsn
  26. Private m_connection_string
  27. Private m_trace_enabled
  28. Public Sub set_trace(bool)
  29. m_trace_enabled = bool
  30. End Sub
  31. Public Property Get is_trace_enabled
  32. is_trace_enabled = m_trace_enabled
  33. End Property
  34. '---------------------------------------------------------------------------------------------------------------------
  35. Public Sub Initialize(connection_string)
  36. Dim p
  37. m_connection_string = "" & connection_string
  38. p = InStr(m_connection_string, ":")
  39. If p < 2 Then
  40. Err.Raise vbObjectError + 100, "Database_Class", "ConnectionString must look like '<driver>:<dsn>' (for example sqlite:/path/app.sqlite). Got: [" & m_connection_string & "]"
  41. End If
  42. m_driver = LCase(Left(m_connection_string, p - 1))
  43. m_dsn = Mid(m_connection_string, p + 1)
  44. If m_driver = "sqlite" Or m_driver = "sqlite3" Then
  45. If InStr(m_dsn, "?") = 0 Then m_dsn = m_dsn & "?_busy_timeout=5000"
  46. End If
  47. End Sub
  48. '---------------------------------------------------------------------------------------------------------------------
  49. ' The underlying G3DB object (opened on first use)
  50. Public Function Connection()
  51. If Not IsObject(m_db) Then
  52. Set m_db = Server.CreateObject("G3DB")
  53. If Not m_db.Open(m_driver, m_dsn) Then
  54. Dim msg
  55. msg = m_db.LastError
  56. Set m_db = Empty
  57. Err.Raise vbObjectError + 101, "Database_Class", "Could not open " & m_driver & " database: " & msg
  58. End If
  59. End If
  60. Set Connection = m_db
  61. End Function
  62. '---------------------------------------------------------------------------------------------------------------------
  63. ' Runs a SELECT. params: Empty/Null (none), a scalar (one value) or an array of values.
  64. Public Function Query(sql, params)
  65. Dim target, rs
  66. Set target = Handle()
  67. Set rs = RunQuery(target, sql, ParamList(params))
  68. If IsEmpty(rs) Then RaiseDbError "Query failed", sql
  69. Set Query = rs
  70. End Function
  71. '---------------------------------------------------------------------------------------------------------------------
  72. ' Runs INSERT/UPDATE/DELETE/DDL. Returns the G3DBResult (RowsAffected, LastInsertId).
  73. Public Function Exec(sql, params)
  74. Dim target, result
  75. Set target = Handle()
  76. Set result = RunExec(target, sql, ParamList(params))
  77. If IsEmpty(result) Then RaiseDbError "Statement failed", sql
  78. Set Exec = result
  79. End Function
  80. ' Same as Exec but discards the result (kept for the original Execute(sql, params) call style).
  81. Public Sub [Execute](sql, params)
  82. Dim result
  83. Set result = Exec(sql, params)
  84. Set result = Nothing
  85. End Sub
  86. '---------------------------------------------------------------------------------------------------------------------
  87. ' Returns one page of rows (1-based page_num) using LIMIT/OFFSET (SQLite, MySQL, PostgreSQL).
  88. ' sql must not already contain LIMIT/OFFSET.
  89. Public Function PagedQuery(sql, params, per_page, page_num)
  90. Dim size, page
  91. size = CLng(per_page)
  92. page = CLng(page_num)
  93. If size < 1 Then size = 1
  94. If page < 1 Then page = 1
  95. Set PagedQuery = Query(sql & " LIMIT " & size & " OFFSET " & ((page - 1) * size), params)
  96. End Function
  97. '---------------------------------------------------------------------------------------------------------------------
  98. Public Sub BeginTransaction()
  99. If IsObject(m_tx) Then
  100. Err.Raise vbObjectError + 102, "Database_Class", "A transaction is already open"
  101. End If
  102. Dim tx
  103. Set tx = Connection().Begin()
  104. If IsEmpty(tx) Then RaiseDbError "Could not begin transaction", ""
  105. Set m_tx = tx
  106. m_txFailed = ""
  107. End Sub
  108. ' Refuses to commit when any statement failed inside the transaction: callers run several statements under
  109. ' On Error Resume Next and check Err once, but the next DAL call resets Err, so an early failure would otherwise
  110. ' be lost and a partial write committed. The transaction is rolled back and the first error is raised.
  111. Public Sub CommitTransaction()
  112. Dim tx, failed
  113. If Not IsObject(m_tx) Then Exit Sub
  114. Set tx = m_tx
  115. Set m_tx = Empty
  116. failed = "" & m_txFailed
  117. m_txFailed = ""
  118. If Len(failed) > 0 Then
  119. tx.Rollback
  120. Err.Raise vbObjectError + 105, "Database_Class", "Transaction rolled back because a statement in it failed: " & failed
  121. End If
  122. tx.Commit
  123. End Sub
  124. Public Sub RollbackTransaction()
  125. Dim tx
  126. m_txFailed = ""
  127. If Not IsObject(m_tx) Then Exit Sub
  128. Set tx = m_tx
  129. Set m_tx = Empty
  130. tx.Rollback
  131. End Sub
  132. '---------------------------------------------------------------------------------------------------------------------
  133. ' Private helpers
  134. '---------------------------------------------------------------------------------------------------------------------
  135. ' The object to run statements on: the open transaction if there is one, otherwise the connection
  136. Private Function Handle()
  137. If IsObject(m_tx) Then
  138. Set Handle = m_tx
  139. Else
  140. Set Handle = Connection()
  141. End If
  142. End Function
  143. ' Normalizes params to a zero-based array (possibly empty)
  144. Private Function ParamList(params)
  145. If IsArray(params) Then
  146. ParamList = params
  147. ElseIf IsEmpty(params) Or IsNull(params) Then
  148. ParamList = Array()
  149. Else
  150. ParamList = Array(params)
  151. End If
  152. End Function
  153. Private Function TxNote()
  154. If IsObject(m_tx) Then
  155. TxNote = " (statement ran inside a transaction; G3DB does not expose the driver error there)"
  156. Else
  157. TxNote = ""
  158. End If
  159. End Function
  160. Private Sub RaiseDbError(prefix, sql)
  161. Dim detail
  162. detail = ""
  163. If IsObject(m_db) Then detail = m_db.LastError
  164. If Len(detail) = 0 Then
  165. ' G3DB reports no LastError for statements run inside a transaction handle
  166. detail = "no driver message available" & TxNote()
  167. End If
  168. If IsObject(m_tx) Then
  169. If Len("" & m_txFailed) = 0 Then
  170. m_txFailed = prefix & " -- SQL: " & sql
  171. End If
  172. End If
  173. Err.Raise vbObjectError + 103, "Database_Class", prefix & ": " & detail & " -- SQL: " & sql
  174. End Sub
  175. ' G3DB takes positional arguments, not an array, so expand explicitly
  176. Private Function RunQuery(target, sql, p)
  177. Select Case UBound(p)
  178. Case -1 : Set RunQuery = target.Query(sql)
  179. Case 0 : Set RunQuery = target.Query(sql, p(0))
  180. Case 1 : Set RunQuery = target.Query(sql, p(0), p(1))
  181. Case 2 : Set RunQuery = target.Query(sql, p(0), p(1), p(2))
  182. Case 3 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3))
  183. Case 4 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4))
  184. Case 5 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5))
  185. Case 6 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6))
  186. Case 7 : Set RunQuery = target.Query(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7))
  187. Case Else
  188. Err.Raise vbObjectError + 104, "Database_Class", "Too many parameters (" & (UBound(p) + 1) & "); the maximum is 8"
  189. End Select
  190. End Function
  191. Private Function RunExec(target, sql, p)
  192. Select Case UBound(p)
  193. Case -1 : Set RunExec = target.Exec(sql)
  194. Case 0 : Set RunExec = target.Exec(sql, p(0))
  195. Case 1 : Set RunExec = target.Exec(sql, p(0), p(1))
  196. Case 2 : Set RunExec = target.Exec(sql, p(0), p(1), p(2))
  197. Case 3 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3))
  198. Case 4 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4))
  199. Case 5 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5))
  200. Case 6 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6))
  201. Case 7 : Set RunExec = target.Exec(sql, p(0), p(1), p(2), p(3), p(4), p(5), p(6), p(7))
  202. Case Else
  203. Err.Raise vbObjectError + 104, "Database_Class", "Too many parameters (" & (UBound(p) + 1) & "); the maximum is 8"
  204. End Select
  205. End Function
  206. Private Sub Class_Terminate()
  207. On Error Resume Next
  208. If IsObject(m_tx) Then m_tx.Rollback
  209. If IsObject(m_db) Then m_db.Close
  210. On Error GoTo 0
  211. End Sub
  212. End Class
  213. %>

Powered by TurnKey Linux.