home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / MSOLAP / samples / Samples.exe / VbDSOExample / WriteBack.bas < prev   
Encoding:
BASIC Source File  |  1998-10-30  |  8.0 KB  |  223 lines

  1. Attribute VB_Name = "WriteBack"
  2. Option Explicit
  3. Option Compare Text
  4.  
  5.  
  6. '
  7. '   WriteEnableCube - enables the Sales cube for write back
  8. '
  9. '   Enabling a cube for writeback entails creation of a writeback partition.  Writeback
  10. '   partition is a ROLAP partition without any aggregations.  The source table of the
  11. '   writeback partition is called writeback table.  The writeback information is stored
  12. '   in this table.
  13. '
  14. '   We will create the writeback table in the Foodmart sample database
  15. '
  16. Public Sub WriteEnableCube()
  17.     ' get the cube's datasource
  18.     Dim dsoDatasource As DSO.DataSource
  19.     Set dsoDatasource = m_dsoCube.DataSources(1)
  20.  
  21.     ' make sure that we are still connected
  22.     If dsoDatasource.IsConnected = False Then
  23.         MsgBox "Cannot create write enable cube. Connection to the datasource cannot be established."
  24.         Exit Sub
  25.     End If
  26.  
  27.     ' what will be the writeback table name
  28.     Dim sWriteBackTableName As String
  29.     sWriteBackTableName = "Sample_Writeback_Sales"
  30.     
  31.     ' get the connection to the datasource
  32.     Dim adoConnection As ADODB.Connection
  33.     Set adoConnection = dsoDatasource.Connection
  34.  
  35.     ' create the writeback partition
  36.     Dim dsoWBPartition As DSO.MDStore
  37.     Set dsoWBPartition = m_dsoCube.MDStores.AddNew("Writeback")
  38.     
  39.     ' set the writeback flag
  40.     dsoWBPartition.IsReadWrite = True
  41.     
  42.     ' set the partition's source table
  43.     dsoWBPartition.SourceTable = dsoDatasource.OpenQuoteChar & sWriteBackTableName & dsoDatasource.CloseQuoteChar
  44.     
  45.     ' set the partition's storage mode
  46.     ' NOTE: the writeback partition should not have any aggregations
  47.     dsoWBPartition.OlapMode = olapmodeRolap
  48.     
  49.     ' begin transaction on the datasource connection
  50.     adoConnection.BeginTrans
  51.  
  52.     ' create the writeback table
  53.     ' and map the partition levels and measures to this table
  54.     On Error GoTo Err_CreateWriteBackTable
  55.     CreateWriteBackTable dsoWBPartition, dsoDatasource, adoConnection, sWriteBackTableName
  56.     
  57.     ' commit the transaction on the datasource
  58.     ' NOTE: the OLAP server uses a different connection to the
  59.     '       relational database, so we need to commit the
  60.     '       changes here
  61.     adoConnection.CommitTrans
  62.  
  63.     ' save the writeback partition
  64.     dsoWBPartition.Update
  65.     
  66.     ' process the write back partition
  67.     On Error GoTo Err_Process
  68.     dsoWBPartition.Process processFull
  69.     
  70. Exit Sub
  71.  
  72. Err_CreateWriteBackTable:
  73.     ' rollback transaction on the datasource
  74.     adoConnection.RollbackTrans
  75.     
  76.     ' remove the writeback partition from the cube
  77.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  78.     
  79.     MsgBox "Cannot create writeback table. " & Err.Description
  80.     Exit Sub
  81.  
  82. Err_Process:
  83.     ' remove the writeback table
  84.     adoConnection.Execute "DROP TABLE " & sWriteBackTableName
  85.     
  86.     ' remove the writeback partition from the cube
  87.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  88.     
  89.     MsgBox "Cannot process the writeback partition. " & Err.Description
  90.     Exit Sub
  91. End Sub
  92.  
  93.  
  94. '
  95. '   CreateWriteBackTable - create the writeback table
  96. '
  97. '   The writeback table must have one column for each measure
  98. '   and level in the cube.  It also has two columns used by
  99. '   the OLAP server for storing auditing information
  100. '
  101. '   This function creates the table and maps the writeback
  102. '   partition levels and measures to this table
  103. '
  104. Private Sub CreateWriteBackTable(dsoWBPartition As DSO.MDStore, _
  105.                                  dsoDatasource As DSO.DataSource, _
  106.                                  adoConnection As ADODB.Connection, _
  107.                                  TableName As String)
  108.  
  109.     ' drop the table if it already exists
  110.     On Error Resume Next
  111.     adoConnection.Execute "DROP TABLE " & TableName
  112.     Err.Clear
  113.     On Error GoTo 0
  114.     
  115.     ' construct the create table statement
  116.     Dim sCreateTable As String
  117.     sCreateTable = "CREATE TABLE " & TableName & " "
  118.     sCreateTable = sCreateTable & " ( "
  119.  
  120.     ' create two columns required by the OLAP server
  121.     ' for storing auditing information
  122.     sCreateTable = sCreateTable & _
  123.                     "MS_AUDIT_USER varchar(32), " & _
  124.                     "MS_AUDIT_TIME  DateTime"
  125.  
  126.     ' create a column for each level
  127.     ' since the column names have to be unique
  128.     '   add the level OrdinalPosition to the level name
  129.     Dim dsoPartDimension As DSO.Dimension
  130.     Dim dsoPartLevel As DSO.Level
  131.     Dim sColumnName As String
  132.     For Each dsoPartDimension In dsoWBPartition.Dimensions
  133.         For Each dsoPartLevel In dsoPartDimension.Levels
  134.             If Not dsoPartLevel.LevelType = levAll And _
  135.                Not dsoPartLevel.IsDisabled And _
  136.                Not dsoPartLevel.SubClassType = sbclsVirtual Then
  137.                 ' get the column name
  138.                 sColumnName = GetColumnName(dsoPartLevel.Name & "_L" & CStr(dsoPartLevel.OrdinalPosition))
  139.                 ' update the create table statement
  140.                 sCreateTable = sCreateTable & ", " & _
  141.                                sColumnName & " " & GetColumnTypeDefinition(dsoPartLevel.ColumnType, dsoPartLevel.ColumnSize)
  142.                 
  143.                 ' map the partition level to this column
  144.                 dsoPartLevel.MemberKeyColumn = dsoWBPartition.SourceTable & _
  145.                                                "." & _
  146.                                                dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  147.             End If
  148.         Next
  149.     Next
  150.  
  151.     ' create a column for each measure
  152.     ' and map the partition measure to this column
  153.     Dim dsoPartitionMeasure As DSO.Measure
  154.     For Each dsoPartitionMeasure In dsoWBPartition.Measures
  155.         ' get the column name
  156.         sColumnName = GetColumnName(dsoPartitionMeasure.Name)
  157.         ' update the create table statement
  158.         sCreateTable = sCreateTable & ", " & _
  159.                        sColumnName & " " & GetColumnTypeDefinition(dsoPartitionMeasure.SourceColumnType)
  160.     
  161.         ' map the partition measure to this column
  162.         dsoPartitionMeasure.SourceColumn = dsoWBPartition.SourceTable & _
  163.                                            "." & _
  164.                                            dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  165.     Next
  166.  
  167.     ' complete the create table statement
  168.     sCreateTable = sCreateTable & ")"
  169.  
  170.     ' execute the create table statement
  171.     adoConnection.Execute sCreateTable
  172.     
  173. End Sub
  174.  
  175.  
  176. '
  177. '   GetColumnTypeDefinition - return a string describing the data type
  178. '                             that can be used in create table statement
  179. '
  180. '   NOTE: this function works with Jet provider, other providers have different data type names
  181. '
  182. Private Function GetColumnTypeDefinition(ColumnType As ADODB.DataTypeEnum, Optional ColumnSize As Integer = 50) As String
  183.     Select Case ColumnType
  184.         Case adChar, adVarChar
  185.             GetColumnTypeDefinition = " VARCHAR (" & CStr(ColumnSize) & ") "
  186.         Case adCurrency
  187.             GetColumnTypeDefinition = " CURRENCY "
  188.         Case adInteger
  189.             GetColumnTypeDefinition = " INTEGER "
  190.         Case adSmallInt
  191.             GetColumnTypeDefinition = " SMALLINT "
  192.         Case adSingle
  193.             GetColumnTypeDefinition = " REAL "
  194.         Case adDouble
  195.             GetColumnTypeDefinition = " DOUBLE "
  196.         Case adDBTimeStamp, adDate
  197.             GetColumnTypeDefinition = " DATETIME "
  198.         Case Else
  199.             Debug.Assert False ' there is a new data type
  200.                                ' we need to update this function
  201.     End Select
  202. End Function
  203.  
  204. '
  205. '   GetColumnName   - create a column name based on the given string
  206. '                     get rid of spaces
  207. '
  208. Private Function GetColumnName(ObjectName As String) As String
  209.     Dim s As String
  210.     Dim c As String
  211.     Dim i As Integer
  212.     
  213.     For i = Len(ObjectName) To 1 Step -1
  214.         c = Mid(ObjectName, i, 1)
  215.         If c <> " " Then
  216.             ' legal char, use it
  217.             s = c + s
  218.         End If
  219.     Next
  220.     
  221.     GetColumnName = s
  222. End Function
  223.