home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 November / VPR0211A.ISO / OPENOFFICE / f_0138 / DBMeta.xba next >
Extensible Markup Language  |  2001-12-18  |  9KB  |  293 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="DBMeta" script:language="StarBasic">REM  *****  BASIC  *****
  4. Option Explicit
  5.  
  6. Public sDatabaseList()
  7. Public iCommandTypes() as Integer
  8. Public CurCommandType as Integer
  9. Public oDataSource as Object
  10. Public bEnableBinaryOptionGroup as Boolean
  11.  
  12.  
  13. Sub GetDatabaseNames()
  14.     If oDBContext.HasElements Then
  15.         sDatabaseList() = oDBContext.ElementNames()
  16.     End If
  17. End Sub
  18.  
  19.  
  20. Sub GetSelectedDBMetaData()
  21. Dim OldsDBname as String
  22. Dim DBIndex as Integer
  23.     If Ubound(oDialogModel.lstDatabases.SelectedItems()) > -1 Then
  24.         ToggleDatabasePage(False)
  25.         DBIndex = oDialogModel.lstDatabases.SelectedItems(0)
  26.         sDBName = sDatabaseList(DBIndex)
  27.         If GetConnection(sDBName) Then
  28.             If GetDBMetaData() Then
  29.                 With oDialogModel
  30.                     .lstTables.Enabled = True
  31.                     .lblTables.Enabled = True
  32.                     .lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  33.                     iCommandTypes() = CreateCommandTypeList()
  34.                     EmptyFieldsListboxes()
  35.                 End With
  36.             End If
  37.         End If
  38.         bEnableBinaryOptionGroup = False
  39.         ToggleDatabasePage(True)
  40.     End If
  41. End Sub
  42.  
  43.  
  44. Function GetConnection(sDBName as String)
  45. Dim oInteractionHandler as Object
  46. Dim bExitLoop as Boolean
  47. Dim bGetConnection as Boolean
  48. Dim iMsg as Integer
  49. Dim Nulllist()
  50.     If Not IsNull(oDBConnection) Then
  51.         oDBConnection.Dispose()
  52.     End If
  53.     oDataSource = oDBContext.GetByName(sDBName)
  54.     If Not oDataSource.IsPasswordRequired Then
  55.         oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","")
  56.         GetConnection() = True
  57.     Else
  58.         oInteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  59.         oDataSource = oDBContext.GetByName(sDBName)
  60.         On Local Error Goto NOCONNECTION
  61.         Do
  62.             bExitLoop = True
  63.             oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  64.             NOCONNECTION:
  65.             bGetConnection = Err = 0
  66.             If bGetConnection Then
  67.                 bGetConnection = Not IsNull(oDBConnection)
  68.                 If Not bGetConnection Then
  69.                     Exit Do
  70.                 End If
  71.             End If
  72.             If Not bGetConnection Then
  73.                 iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)    ' '?' & ' Repeat and Cancel'
  74.                 bExitLoop = iMsg = SBCANCEL
  75.                 Resume CLERROR
  76.                 CLERROR:
  77.             End If
  78.         Loop Until bExitLoop
  79.         On Local Error Goto 0
  80.         If Not bGetConnection Then
  81.             oDialogModel.lstDatabases.SelectedItems() = Nulllist()
  82.             oDialogModel.lstTables.StringItemList() = NullList()
  83.             oDialogModel.lstFields.StringItemList() = NullList()
  84.             oDialogModel.lstSelFields.StringItemList() = NullList()
  85.         End If
  86.         GetConnection() = bGetConnection
  87.     End If
  88. End Function
  89.  
  90.  
  91. Function GetDBMetaData()
  92.     If oDBContext.HasElements Then
  93.         Tablenames() = oDBConnection.Tables.ElementNames()
  94.         Querynames() = oDBConnection.Queries.ElementNames()
  95.         GetDBMetaData = True
  96.     Else
  97.         MsgBox(sMsgErrNoDatabase, 64, sMsgWizardName)
  98.         GetDBMetaData = False
  99.     End If
  100. End Function
  101.  
  102.  
  103. Sub GetTableMetaData()
  104. Dim iType as Long
  105. Dim m as Integer
  106. Dim Found as Boolean
  107. Dim i as Integer
  108. Dim sFieldName as String
  109. Dim n as Integer
  110. Dim WidthIndex as Integer
  111. Dim oField as Object
  112.     MaxIndex = Ubound(oDialogModel.lstSelFields.StringItemList())
  113.     Dim ColumnMap(MaxIndex)as Integer    
  114.     FieldNames() = oDialogModel.lstSelFields.StringItemList()
  115.     ' Build a structure which maps the position of a selected field (within the selection) to the the column position within
  116.     ' the table. So we ensure that the controls are placed in the same order the according fields are selected.
  117.     For i = 0 To Ubound(FieldNames())
  118.         sFieldName = FieldNames(i)
  119.         Found = False
  120.         n = 0
  121.         While (n< MaxIndex And (Not Found))
  122.             If (FieldNames(n) = sFieldName) Then
  123.                 Found = True
  124.                 ColumnMap(n) = i
  125.             End If
  126.             n = n + 1
  127.         Wend
  128.     Next i
  129.     For n = 0 to MaxIndex
  130.         sFieldname = FieldNames(n)
  131.         oField = oColumns.GetByName(sFieldName)
  132.         iType = oField.Type
  133. '        Msgbox IsEmpty(oField.HelpText)
  134. '        Msgbox oField.HelpText
  135. '        Msgbox oField.ControlDefault
  136. '        Msgbox sFieldName & ":" & chr(13) & oField.dbg_Properties
  137.         FieldMetaValues(n,0) = oField.Type
  138.         FieldMetaValues(n,1) = oField.Precision
  139.         FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)
  140.         FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  141.         FieldMetaValues(n,4) = oField.FormatKey
  142. ' Todo: Check why the following lines do not work
  143. '        Msgbox oField.ControlDefault
  144.         FieldMetaValues(n,5) = oField.DefaultValue
  145.         FieldMetaValues(n,6) = oField.IsCurrency
  146.         FieldMetaValues(n,7) = oField.Scale
  147. '        If oField.Description <> "" Then
  148. '' Todo: What's wrong with this line?
  149. '            Msgbox oField.Helptext
  150. '        End If
  151.         FieldMetaValues(n,8) = oField.Description
  152.     Next
  153.     ReDim oDBShapeList(MaxIndex) as Object
  154.     ReDim oTCShapeList(MaxIndex) as Object
  155.     ReDim oDBModelList(MaxIndex) as Object
  156.     ReDim oGroupShapeList(MaxIndex) as Object
  157. End Sub
  158.  
  159.  
  160. Function GetSpecificFieldNames() as Integer
  161. Dim n as Integer
  162. Dim m as Integer
  163. Dim s as Integer
  164. Dim iType as Integer
  165. Dim oField as Object
  166. Dim MaxIndex as Integer
  167. Dim EmptyList()
  168.     If Ubound(oDialogModel.lstTables.StringItemList()) > -1 Then
  169.         FieldNames() = oColumns.GetElementNames()
  170.         MaxIndex = Ubound(FieldNames())
  171.         If MaxIndex <> -1 Then
  172.             Dim ResultFieldNames(MaxIndex)
  173.             ReDim ImgFieldNames(MaxIndex)
  174.             m = 0
  175.             For n = 0 To MaxIndex
  176.                 oField = oColumns.GetByName(FieldNames(n))
  177.                 iType = oField.Type
  178.                 If GetIndexInMultiArray(WidthList(), iType, 0) <> -1 Then
  179.                     ResultFieldNames(m) = FieldNames(n)
  180.                     m = m + 1
  181.                 End If
  182.                 If GetIndexInMultiArray(ImgWidthList(), iType, 0) <> -1 Then
  183.                     ImgFieldNames(s) = FieldNames(n)
  184.                     s = s + 1
  185.                 End If
  186.             Next n
  187.             If s <> 0 Then
  188.                 Redim Preserve ImgFieldNames(s-1)
  189.                 bEnableBinaryOptionGroup = True
  190.             Else
  191.                 bEnableBinaryOptionGroup = False
  192.             End If
  193.             Redim Preserve ResultFieldNames(m-1)
  194.             Redim Preserve FieldNames(m-1)
  195.             FieldNames() = ResultFieldNames()
  196.             oDialogModel.lstFields.StringItemList = FieldNames()
  197.             InitializeListboxProcedures(oDialogModel, oDialogModel.lstFields, oDialogModel.lstSelFields)
  198.         End If
  199.         GetSpecificFieldNames = MaxIndex
  200.     Else
  201.         GetSpecificFieldNames = -1    
  202.     End If
  203. End Function
  204.  
  205.  
  206. Sub CreateDBForm()
  207.     If oDrawPage.Forms.Count = 0 Then
  208.           oDBForm = oDocument.CreateInstance("com.sun.star.form.component.Form")
  209.         oDrawpage.Forms.InsertByIndex (0, oDBForm)
  210.     Else
  211.         oDBForm = oDrawPage.Forms.GetByIndex(0)
  212.     End If
  213.     oDBForm.Name = "Standard"
  214.     oDBForm.DataSourceName = sDBName
  215.     oDBForm.Command = TableName
  216.     oDBForm.CommandType = CurCommandType
  217. End Sub
  218.  
  219.  
  220. Sub AddOrRemoveBinaryFieldsToWidthList()
  221. Dim LocWidthList()
  222. Dim MaxIndex as Integer
  223. Dim OldMaxIndex as Integer
  224. Dim s as Integer
  225. Dim n as Integer
  226. Dim m as Integer
  227.     If Not bDebug Then
  228.         On Local Error GoTo WIZARDERROR
  229.     End If
  230.     If oDialogModel.optBinariesasGraphics.State = 1 Then
  231.         OldMaxIndex = Ubound(WidthList(),1)
  232.         If OldMaxIndex = 15 Then
  233.             MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  234.             ReDim Preserve WidthList(MaxIndex,4)
  235.             s = 0
  236.             For n = OldMaxIndex + 1 To MaxIndex
  237.                 For m = 0 To 3
  238.                     WidthList(n,m) = ImgWidthList(s,m)
  239.                 Next m
  240.                 s = s + 1
  241.             Next n
  242.             MergeList(oDialogModel.lstFields, ImgFieldNames())
  243.         End If
  244.     Else
  245.         ReDim Preserve WidthList(15, 4)
  246.         RemoveListItems(oDialogModel.lstFields(), oDialogModel.lstSelFields(), ImgFieldNames())
  247.     End If
  248.     oDialogModel.lstSelFields.Tag = True
  249. WIZARDERROR:
  250.     If Err <> 0 Then    
  251.         Msgbox(sMsgErrMsg, 16, GetProductName())
  252.         Resume LOCERROR
  253.         LOCERROR:        
  254.     End If
  255. End Sub
  256.  
  257.  
  258. Function CreateCommandTypeList()
  259. Dim MaxTableIndex as Integer
  260. Dim MaxQueryIndex as Integer
  261. Dim MaxIndex as Integer
  262. Dim i as Integer
  263. Dim a as Integer
  264.     MaxTableIndex = Ubound(TableNames()
  265.     MaxQueryIndex = Ubound(QueryNames()
  266.     MaxIndex = MaxTableIndex + MaxQueryIndex + 1
  267.     If MaxIndex > -1 Then
  268.         Dim LocCommandTypes(MaxIndex) as Integer
  269.         For i = 0 To MaxTableIndex
  270.             LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  271.         Next i
  272.         a = i
  273.         For i = 0 To MaxQueryIndex
  274.             LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  275.         Next i
  276.     End If
  277.     CreateCommandTypeList() = LocCommandTypes()
  278. End Function
  279.  
  280.  
  281. Sub GetCurrentMetaValues(Index as Integer)
  282.     CurFieldType = FieldMetaValues(Index,0)
  283.     CurFieldLength = FieldMetaValues(Index,1)
  284.     CurControlType = FieldMetaValues(Index,2)
  285.     CurControlName = FieldMetaValues(Index,3)
  286.     CurFormatKey = FieldMetaValues(Index,4)
  287.     CurDefaultValue = FieldMetaValues(Index,5)
  288.     CurIsCurrency = FieldMetaValues(Index,6)
  289.     CurScale = FieldMetaValues(Index,7)
  290. ' Todo: Is this really the HelpText?
  291.     CurHelpText = FieldMetaValues(Index,8)
  292.     CurFieldName = FieldNames(Index)
  293. End Sub</script:module>