home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / ThreadRunn60422382002.psc / BGTRDemo / DBWorks DLL / Classes / CBGTRUserQuery.cls < prev    next >
Encoding:
Visual Basic class definition  |  2002-03-03  |  14.1 KB  |  416 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CBGTRUserQuery"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. '********************************************************************************************
  17. '   CBGTRUserQuery Class Definition
  18. '   Class defines tasks for the ThreadRunner using a user defined query
  19. '
  20. '   Instancing is set to:  5 - MultiUse
  21. ''
  22. '********************************************************************************************
  23. Option Explicit
  24.  
  25. Implements IBGTRWork
  26.  
  27. Private m_baResults() As Byte
  28. Private m_bIsTaskCancel As Boolean
  29. Private m_pCancel As Long
  30.  
  31. Public Enum DBType_enum
  32.     dbtype_Unknown = 0
  33.     dbtype_SQLServer = 1
  34.     dbtype_Jet = 2
  35. End Enum
  36.  
  37. Private m_lngDBType As DBType_enum
  38. Private m_lngQueryType As QueryType_enum
  39. Private m_strSQLStatement As String
  40. Private m_arParameters() As Variant
  41. Private m_lngParameterCount As Long
  42. Private m_lngReturnsRecords As ReturnsRecords_enum
  43. Private m_lngUsesParameters As UsesParameters_enum
  44.  
  45. Private m_CDBConnection As CDBConnection
  46. Private m_strConnection As String
  47.  
  48. Private m_CADOProc As CADOProc
  49. Private WithEvents rsTarget As ADODB.Recordset
  50. Attribute rsTarget.VB_VarHelpID = -1
  51.  
  52. Private lngFetchCount As Long
  53.  
  54.  
  55. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. '^  Required IBGTRWork interface implementation
  57. '^  By making these simple wrapper functions that
  58. '^  delegate the call, they can be used as is from
  59. '^ class to class.
  60. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  61.  
  62. Private Property Get IBGTRWork_WorkResults() As Byte()
  63.     IBGTRWork_WorkResults = m_baResults
  64. End Property
  65.  
  66. Private Sub IBGTRWork_SynchCancelPointers(ByVal pCancel As Long)
  67.     m_pCancel = pCancel
  68. End Sub
  69.  
  70. Private Sub IBGTRWork_InitializeWork()
  71.     InitializeWork
  72. End Sub
  73.  
  74. Private Sub IBGTRWork_ExecuteWork()
  75.     ExecuteWork
  76. End Sub
  77.  
  78. Private Sub IBGTRWork_TerminateWork()
  79.     TerminateWork
  80. End Sub
  81. Private Function IBGTRWork_GetWorkState() As Byte()
  82.     IBGTRWork_GetWorkState = GetSuperState
  83. End Function
  84. Private Sub IBGTRWork_SetWorkState(arWorkMemento() As Byte)
  85.     SetSuperState arWorkMemento
  86. End Sub
  87.  
  88. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  89. '^ Public interface exposed to client only, not Worker thread
  90. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  91. Public Property Let DBType(ByVal lngDBType As DBType_enum)
  92.     m_lngDBType = lngDBType
  93. End Property
  94. Public Property Get DBType() As DBType_enum
  95.     DBType = m_lngDBType
  96. End Property
  97.  
  98. Public Property Let Connection(ByRef oCDBConnection As CDBConnection)
  99.     Set m_CDBConnection = oCDBConnection
  100. End Property
  101. Public Property Get Connection() As CDBConnection
  102.     Set Connection = m_CDBConnection
  103. End Property
  104.  
  105. Public Property Let QueryType(ByVal lngQueryType As QueryType_enum)
  106.     m_lngQueryType = lngQueryType
  107. End Property
  108. Public Property Get QueryType() As QueryType_enum
  109.     QueryType = m_lngQueryType
  110. End Property
  111.  
  112. Public Property Let SQLStatement(ByVal strSQLStatement As String)
  113.     m_strSQLStatement = strSQLStatement
  114. End Property
  115. Public Property Get SQLStatement() As String
  116.     SQLStatement = m_strSQLStatement
  117. End Property
  118.  
  119. Public Property Let ADOParameters(ByRef arParameters As Variant)
  120.     m_arParameters = arParameters
  121. End Property
  122. Public Property Get ADOParameters() As Variant
  123.     ADOParameters = m_arParameters
  124. End Property
  125.  
  126. Public Property Let ParameterCount(ByVal lngParameterCount As Long)
  127.     m_lngParameterCount = lngParameterCount
  128. End Property
  129. Public Property Get ParameterCount() As Long
  130.     ParameterCount = m_lngParameterCount
  131. End Property
  132.  
  133. Public Property Let ReturnsRecords(ByVal lngReturnsRecords As ReturnsRecords_enum)
  134.     m_lngReturnsRecords = lngReturnsRecords
  135. End Property
  136. Public Property Get ReturnsRecords() As ReturnsRecords_enum
  137.     ReturnsRecords = m_lngReturnsRecords
  138. End Property
  139.  
  140. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  141. '^ Task specific implementation functions
  142. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  143. Private Sub InitializeWork()
  144. On Error GoTo CatchErr
  145.     
  146.     m_strConnection = MakeConnectionString
  147.     If m_lngParameterCount > 0 Then
  148.         m_lngUsesParameters = uprm_UsesParameters
  149.     End If
  150.     
  151. Exit Sub
  152. CatchErr:
  153.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.InitializeWork", Err.Description
  154. End Sub
  155.  
  156. Public Sub ExecuteWork()
  157. On Error GoTo CatchErr
  158. Dim i As Long
  159. Dim lngRecordsAffected As Long
  160. Dim cnConnection As ADODB.Connection
  161. Dim tmpADOStream As ADODB.Stream
  162. Dim tmpSize As String
  163. Dim lngSize As Long
  164.  
  165.     'Create and open connection
  166.     Set cnConnection = New ADODB.Connection
  167.     cnConnection.ConnectionString = m_strConnection
  168.     cnConnection.Open
  169.     
  170.     'Populate query parameters if required
  171.     If m_lngUsesParameters = uprm_UsesParameters Then
  172.         m_CADOProc.ParameterCount = m_lngParameterCount
  173.         For i = 0 To m_lngParameterCount - 1
  174.             'Size is required only for variable length data - strings generally
  175.             tmpSize = m_arParameters(i, paramcol_Size)
  176.             If Len(tmpSize) > 0 Then
  177.                 lngSize = CLng(tmpSize)
  178.             Else
  179.                 lngSize = 0
  180.             End If
  181.             
  182.             m_CADOProc.SetParameter CStr(m_arParameters(i, paramcol_Name)), _
  183.                                                        CLng(m_arParameters(i, paramcol_Type)), _
  184.                                                        CLng(m_arParameters(i, paramcol_Direction)), _
  185.                                                        lngSize, _
  186.                                                        m_arParameters(i, paramcol_Value)
  187.         Next i
  188.     End If
  189.     
  190.     
  191.     Set rsTarget = New ADODB.Recordset
  192.     
  193.     'Execute based upon type - SQLString or StoredProc
  194.     Select Case m_lngQueryType
  195.     
  196.         Case querytype_SQLString
  197.             m_CADOProc.ExecuteCommand m_strSQLStatement, _
  198.                                                                                             cnConnection, _
  199.                                                                                             m_lngUsesParameters, _
  200.                                                                                             m_lngReturnsRecords, _
  201.                                                                                             rsTarget, _
  202.                                                                                             rsevents_RaiseEvents
  203.                                                                                             
  204.         Case querytype_StoredProc
  205.                 m_CADOProc.ExecuteProcedure m_strSQLStatement, _
  206.                                                                                             cnConnection, _
  207.                                                                                             m_lngUsesParameters, _
  208.                                                                                             m_lngReturnsRecords, _
  209.                                                                                             rsTarget, _
  210.                                                                                             rsevents_RaiseEvents
  211.                                                                                             
  212.  
  213.     
  214.     End Select
  215.     
  216.     'Check cancel before returning the results
  217.     
  218.     If CheckCancel Then
  219.         rsTarget.Close
  220.         Set rsTarget = Nothing
  221.         Exit Sub
  222.     End If
  223.     
  224.     'Serve up the recordset as a binary stream if records are returned
  225.     If m_lngReturnsRecords = retrs_ReturnsRecords Then
  226.         Set tmpADOStream = New ADODB.Stream
  227.  
  228.         With rsTarget
  229.             .Save tmpADOStream, adPersistADTG
  230.             .Close
  231.         End With
  232.     
  233.         Set rsTarget = Nothing
  234.         m_baResults = tmpADOStream.Read
  235.  
  236.     Else
  237.         'Set the results array to default
  238.         ReDim m_baResults(0)
  239.         m_baResults = g_arNoData
  240.     End If
  241. Exit Sub
  242. CatchErr:
  243.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.ExecuteWork", Err.Description
  244. End Sub
  245.  
  246. Private Sub TerminateWork()
  247.     'no implementation for this task
  248. End Sub
  249.  
  250. Private Function CheckCancel() As Boolean
  251.     CopyMemory m_bIsTaskCancel, ByVal m_pCancel, BYTES_INT
  252.     CheckCancel = m_bIsTaskCancel
  253. End Function
  254.  
  255. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  256. '^ Various task support and auxiliary functions
  257. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  258. Public Function MakeConnectionString() As String
  259. On Error GoTo CatchErr
  260.     
  261.     Select Case m_lngDBType
  262.     
  263.         Case dbtype_SQLServer
  264.         
  265.             MakeConnectionString = _
  266.                            "Provider=" & LoadResString(RES_SQL_PROVIDER) _
  267.                 & ";" & "Driver=" & LoadResString(RES_SQL_DRIVER) _
  268.                 & ";" & "Server=" & m_CDBConnection.Server _
  269.                 & ";" & "Database=" & m_CDBConnection.Database _
  270.                 & ";" & "UID=" & m_CDBConnection.User _
  271.                 & ";" & "PWD=" & m_CDBConnection.Password _
  272.                 & ";"
  273.                 
  274.         Case dbtype_Jet
  275.             
  276.             MakeConnectionString = _
  277.                            "Provider=" & LoadResString(RES_JET_PROVIDER) _
  278.                 & ";" & "Data Source=" & m_CDBConnection.Server _
  279.                 & ";" & "User ID=" & m_CDBConnection.User _
  280.                 & ";" & "Password=" & m_CDBConnection.Password _
  281.                 & ";"
  282.         
  283.     End Select
  284.      
  285. Exit Function
  286. CatchErr:
  287.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.MakeConnectionString", Err.Description
  288. End Function
  289.  
  290. Private Sub rsTarget_FetchProgress(ByVal Progress As Long, ByVal MaxProgress As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
  291.     lngFetchCount = lngFetchCount + 1
  292.     If lngFetchCount > CHECK_CANCEL_INCR Then
  293.         lngFetchCount = 0
  294.         If CheckCancel Then
  295.             rsTarget.Cancel
  296.         End If
  297.     End If
  298. End Sub
  299.  
  300. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  301. '^ Serialization/Deserialization Functions
  302. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  303. Private Function GetState() As Byte()
  304. On Error GoTo CatchErr
  305. Dim pbTemp As PropertyBag
  306. Dim i As Long
  307. Dim j As Long
  308.  
  309.     Set pbTemp = New PropertyBag
  310.     With pbTemp
  311.         .WriteProperty "DBType", m_lngDBType
  312.         .WriteProperty "QueryType", m_lngQueryType
  313.         .WriteProperty "SQLStatement", m_strSQLStatement
  314.         .WriteProperty "ParameterCount", m_lngParameterCount
  315.         If m_lngParameterCount > 0 Then
  316.             For i = 0 To m_lngParameterCount - 1
  317.                 For j = paramcol_Name To paramcol_Value
  318.                     .WriteProperty "Parameters_" & i & "_" & j, m_arParameters(i, j)
  319.                 Next j
  320.             Next i
  321.         End If
  322.         .WriteProperty "ReturnsRecords", m_lngReturnsRecords
  323.         
  324.         GetState = .Contents
  325.     End With
  326.     Set pbTemp = Nothing
  327. Exit Function
  328. CatchErr:
  329.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.GetState", Err.Description
  330. End Function
  331.  
  332. Public Function GetSuperState() As Byte()
  333. On Error GoTo CatchErr
  334. Dim pbTemp As PropertyBag
  335.  
  336.     Set pbTemp = New PropertyBag
  337.     With pbTemp
  338.     
  339.         .WriteProperty "State", GetState
  340.         .WriteProperty "Connection", m_CDBConnection.GetSuperState
  341.         GetSuperState = .Contents
  342.         
  343.     End With
  344.     Set pbTemp = Nothing
  345. Exit Function
  346. CatchErr:
  347.     Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.GetSuperState", Err.Description
  348. End Function
  349.  
  350. Private Sub SetState(ByRef arState() As Byte)
  351. On Error GoTo CatchErr
  352. Dim pbTemp As PropertyBag
  353. Dim i As Long
  354. Dim j As Long
  355.  
  356.     Set pbTemp = New PropertyBag
  357.     With pbTemp
  358.         .Contents = arState
  359.         m_lngDBType = .ReadProperty("DBType")
  360.         m_lngQueryType = .ReadProperty("QueryType")
  361.         m_strSQLStatement = .ReadProperty("SQLStatement")
  362.         m_lngParameterCount = .ReadProperty("ParameterCount")
  363.                         
  364.         If m_lngParameterCount > 0 Then
  365.             ReDim m_arParameters(0 To m_lngParameterCount - 1, paramcol_Name To paramcol_Value)
  366.             For i = 0 To m_lngParameterCount - 1
  367.                 For j = paramcol_Name To paramcol_Value
  368.                     m_arParameters(i, j) = .ReadProperty("Parameters_" & i & "_" & j)
  369.                 Next j
  370.             Next i
  371.         End If
  372.              
  373.         m_lngReturnsRecords = .ReadProperty("ReturnsRecords")
  374.         
  375.     End With
  376.     Set pbTemp = Nothing
  377. Exit Sub
  378. CatchErr:
  379.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.SetState", Err.Description
  380. End Sub
  381.  
  382. Public Sub SetSuperState(ByRef arState() As Byte)
  383. On Error GoTo CatchErr
  384. Dim pbTemp As PropertyBag
  385.  
  386.     Set pbTemp = New PropertyBag
  387.     With pbTemp
  388.         .Contents = arState
  389.         SetState .ReadProperty("State")
  390.         
  391.         Set m_CDBConnection = New CDBConnection
  392.         m_CDBConnection.SetSuperState .ReadProperty("Connection")
  393.     End With
  394.     Set pbTemp = Nothing
  395. Exit Sub
  396. CatchErr:
  397.       Err.Raise Err.Number, Err.Source & " in BGTRDBWorks.CBGTRUserQuery.SetSuperState", Err.Description
  398. End Sub
  399.  
  400. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  401. '^ Class Constructor/Destructor
  402. '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  403. Private Sub Class_Initialize()
  404.     Set m_CDBConnection = New CDBConnection
  405.     Set m_CADOProc = New CADOProc
  406. End Sub
  407.  
  408. Private Sub Class_Terminate()
  409. On Error Resume Next
  410.     Set m_CDBConnection = Nothing
  411.     Set m_CADOProc = Nothing
  412. End Sub
  413.  
  414.  
  415.  
  416.