Advertisement
nandordudas

Database

May 17th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. ' 514: Doesn't executed
  5. ' 515: Query is empty
  6. ' 516: Recordset is closed
  7. ' 517: Recordset is empty
  8.  
  9. #If Debugging Then
  10. Private pLogMessage As String
  11. #End If
  12.  
  13. Private pConnection As Object
  14. Private pQuery As String
  15. Private pParameters As Object
  16. Private pRecords As Object
  17. Private pAffectedRows As Long
  18. Private pRecordCount As Long
  19.  
  20. Private Sub Class_Initialize()
  21.  
  22.     Set pConnection = CurrentProject.Connection
  23.     Set pParameters = CreateObject("Scripting.Dictionary")
  24.  
  25. End Sub
  26.  
  27. Private Sub Class_Terminate()
  28.  
  29.     Set pConnection = Nothing
  30.     Set pParameters = Nothing
  31.  
  32. End Sub
  33.  
  34. Public Property Get Query() As String
  35.  
  36.     Query = pQuery
  37.  
  38. End Property
  39.  
  40. Public Property Let Query(ByVal NewValue As String)
  41.  
  42.     pQuery = NewValue
  43.  
  44. #If Debugging Then
  45.     pLogMessage = pLogMessage & "Query: " & vbCrLf & vbTab & pQuery & _
  46.         vbCrLf & pLogMessage & "Parameters: " & vbCrLf
  47. #End If
  48.  
  49. End Property
  50.  
  51. Public Property Get RecordCount() As Long
  52.  
  53.     RecordCount = pRecordCount
  54.  
  55. End Property
  56.  
  57. Public Property Get AffectedRows() As Long
  58.  
  59.     AffectedRows = pAffectedRows
  60.  
  61. End Property
  62.  
  63. Public Function Execute() As Boolean
  64.  
  65.     On Error GoTo Catch
  66.  
  67.     If Not CBool(Len(pQuery)) Then _
  68.         Err.Raise vbObjectError + 515, , "Query is empty"
  69.  
  70.     Dim Command As Object, Parameter As Object, ParameterName As Variant, _
  71.         Result As Object, Executed As Boolean
  72.  
  73.     Set Command = CreateObject("ADODB.Command")
  74.     Set Result = CreateObject("Scripting.Dictionary")
  75.  
  76.     With Command
  77.         .ActiveConnection = pConnection
  78.         .ActiveConnection.CursorLocation = 3
  79.         .CommandType = 1
  80.         .CommandText = pQuery
  81.  
  82.         For Each ParameterName In pParameters.Keys
  83.  
  84.             Set Parameter = pParameters(ParameterName)
  85.  
  86.             .Parameters.Append Parameter
  87.  
  88.             DoEvents
  89.         Next ParameterName
  90.  
  91.         Set pRecords = .Execute(pAffectedRows)
  92.  
  93.         If Not pRecords Is Nothing Then _
  94.             If CBool(pRecords.State) Then pRecordCount = pRecords.RecordCount
  95.  
  96.         Executed = True
  97.  
  98.     End With
  99.  
  100.     GoTo Finally
  101.  
  102. Catch:
  103.     RollbackTransaction
  104.  
  105.     Handle Err.Number, Err.Description, "", "Function Execute", _
  106.         VBE.ActiveCodePane.CodeModule.Name
  107.  
  108. Finally:
  109.     Execute = Executed
  110.  
  111. #If Debugging Then
  112.     pLogMessage = pLogMessage & "Executed: " & Executed & vbCrLf
  113.     pLogMessage = pLogMessage & "RecordCount: " & pRecordCount & vbCrLf
  114.     pLogMessage = pLogMessage & "AffectedRows: " & pAffectedRows & vbCrLf
  115. #End If
  116.  
  117.     pParameters.RemoveAll
  118.     Set Parameter = Nothing
  119.     Set Result = Nothing
  120.     Set Command = Nothing
  121.  
  122. End Function
  123.  
  124. Public Sub BeginTransaction()
  125.  
  126.     pConnection.BeginTrans
  127.  
  128. #If Debugging Then
  129.     pLogMessage = pLogMessage & "Transaction initiated" & vbCrLf
  130. #End If
  131.  
  132. End Sub
  133.  
  134. Public Sub CommitTransaction()
  135.  
  136.     pConnection.CommitTrans
  137.  
  138. #If Debugging Then
  139.     pLogMessage = pLogMessage & "Transaction committed" & vbCrLf
  140. #End If
  141.  
  142. End Sub
  143.  
  144. Public Sub RollbackTransaction()
  145.  
  146.     pConnection.RollbackTrans
  147.  
  148. #If Debugging Then
  149.     pLogMessage = pLogMessage & "Transaction rolled back" & vbCrLf
  150. #End If
  151.  
  152. End Sub
  153.  
  154. Public Sub Bind(ByVal ParameterName As String, ByVal ParameterValue As Variant)
  155.  
  156.     On Error GoTo Catch
  157.  
  158.     If pParameters.Exists(ParameterName) Then _
  159.         Err.Raise vbObjectError + 513, , "Parameter exists(" & ParameterName _
  160.         & ")"
  161.  
  162.     Dim Parameter As Object
  163.  
  164.     Set Parameter = CreateObject("ADODB.Parameter")
  165.  
  166.     With Parameter
  167.         .Name = ParameterName
  168.  
  169.         Select Case VarType(ParameterValue)
  170.  
  171.             Case vbString
  172.                 .Type = 200
  173.                 .Value = CStr(ParameterValue)
  174.                 .Size = Len(.Value)
  175.  
  176.             Case vbInteger, vbLong
  177.                 .Type = vbLong
  178.                 .Value = CLng(ParameterValue)
  179.  
  180.             Case vbDouble
  181.                 .Type = vbDouble
  182.                 .Value = CDbl(ParameterValue)
  183.  
  184.             Case vbDate
  185.                 .Type = vbDate
  186.                 .Value = CDate(ParameterValue)
  187.  
  188.             Case vbBoolean
  189.                 .Type = vbBoolean
  190.                 .Value = CBool(ParameterValue)
  191.  
  192.             Case Else
  193.  
  194.         End Select
  195.  
  196. #If Debugging Then
  197.     pLogMessage = pLogMessage & vbTab & .Name & " = " & .Value & vbCrLf
  198. #End If
  199.  
  200.     End With
  201.  
  202.     pParameters.Add ParameterName, Parameter
  203.  
  204.     GoTo Finally
  205.  
  206. Catch:
  207.     Handle Err.Number, Err.Description, "", "Sub Bind", _
  208.         VBE.ActiveCodePane.CodeModule.Name
  209.  
  210. Finally:
  211.     Set Parameter = Nothing
  212.  
  213. End Sub
  214.  
  215. Public Function ResultSet() As Object
  216.  
  217.     On Error GoTo Catch
  218.  
  219.     Dim Row As Long, Rows As Variant, FieldCounter As Long, _
  220.         RecordRow As Object, Result As Object, FieldsCount As Long
  221.  
  222.     BeginTransaction
  223.  
  224.     If Not CBool(Execute) Then _
  225.         Err.Raise vbObjectError + 514, , "Doesn't executed"
  226.  
  227.     CommitTransaction
  228.  
  229.     Set Result = CreateObject("Scripting.Dictionary")
  230.  
  231.     If CBool(AffectedRows) Then GoTo Finally
  232.  
  233.     If pRecords Is Nothing Then _
  234.         Err.Raise vbObjectError + 517, , "Recordset is empty"
  235. '   BOF vagy EOF vizsgalata esetleg RecordCount?
  236.    If Not CBool(pRecords.State) Then _
  237.         Err.Raise vbObjectError + 516, , "Recordset is closed"
  238.  
  239.     FieldsCount = pRecords.Fields.Count
  240.  
  241.     Rows = pRecords.GetRows(pRecordCount)
  242.  
  243.     For Row = 0 To UBound(Rows, 2)
  244.  
  245.         Set RecordRow = CreateObject("Scripting.Dictionary")
  246.  
  247.         For FieldCounter = 0 To (FieldsCount - 1)
  248.  
  249.             RecordRow.Add pRecords.Fields(FieldCounter).Name, _
  250.                 Rows(FieldCounter, Row)
  251.         Next FieldCounter
  252.  
  253.         Result.Add Result.Count, RecordRow
  254.     Next Row
  255.  
  256.     Set ResultSet = IIf((1 = pRecordCount), Result(0), Result)
  257.  
  258.     Erase Rows
  259.  
  260.     GoTo Finally
  261.  
  262. Catch:
  263.     Handle Err.Number, Err.Description, "", "Function ResultSet", _
  264.         VBE.ActiveCodePane.CodeModule.Name
  265.  
  266. Finally:
  267.  
  268. #If Debugging Then
  269.     Debug.Print pLogMessage
  270.     pLogMessage = ""
  271. #End If
  272.  
  273.     Set RecordRow = Nothing
  274.     Set Result = Nothing
  275.  
  276. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement