home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 May / Gamestar_62_2004-05_dvd.iso / Programy / openoffice / f_0181 / DBMeta.xba next >
Extensible Markup Language  |  2003-06-06  |  10KB  |  337 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.  
  7. Public iCommandTypes() as Integer
  8. Public CurCommandType as Integer
  9. Public oDataSource as Object
  10. Public bEnableBinaryOptionGroup as Boolean
  11. 'Public bSelectContent as Boolean
  12.  
  13.  
  14. Function GetDatabaseNames(baddFirstListItem as Boolean)
  15. Dim sDatabaseList()
  16.     If oDBContext.HasElements Then
  17.         Dim LocDBList() as String
  18.         Dim MaxIndex as Integer
  19.         Dim i as Integer
  20.         LocDBList = oDBContext.ElementNames()
  21.         MaxIndex = Ubound(LocDBList())
  22.         If baddfirstListItem Then
  23.             ReDim Preserve sDatabaseList(MaxIndex + 1)
  24.             sDatabaseList(0) = sSelectDatasource
  25.             a = 1
  26.         Else
  27.             ReDim Preserve sDatabaseList(MaxIndex)
  28.             a = 0
  29.         End If
  30.         For i = 0 To MaxIndex
  31.             sDatabaseList(a) = oDBContext.ElementNames(i)
  32.             a = a + 1
  33.         Next i
  34.     End If
  35.     GetDatabaseNames() = sDatabaseList()
  36. End Function
  37.  
  38.  
  39. Sub GetSelectedDBMetaData()
  40. Dim OldsDBname as String
  41. Dim DBIndex as Integer
  42. Dim LocList() as String
  43. '    If bStartUp Then
  44. '        bStartUp = false
  45. '        Exit Sub
  46. '    End Sub
  47.     If Ubound(DialogModel.lstDatabases.SelectedItems()) > -1 Then
  48.         DeleteFirstListBoxEntry("lstDatabases", sSelectDatasource)
  49.         ToggleDatabasePage(False)
  50.         DBIndex = DialogModel.lstDatabases.SelectedItems(0)
  51.         With DialogModel
  52.             If DBIndex > -1 Then
  53.                 sDBName = DlgFormDB.getControl("lstDatabases").getSelectedItem()
  54.                 If GetConnection(sDBName) Then
  55.                     If GetDBMetaData() Then
  56.                         LocList() = AddListToList(Array(sSelectDBTable), TableNames())
  57.                         .lstTables.StringItemList() = AddListToList(LocList(), QueryNames())
  58. '                        bSelectContent = True
  59.                         .lstTables.SelectedItems() = Array(0)
  60.                         iCommandTypes() = CreateCommandTypeList()
  61.                         EmptyFieldsListboxes()
  62.                     End If
  63.                 End If
  64.                 bEnableBinaryOptionGroup = False
  65.                 .lstTables.Enabled = True
  66.                 .lblTables.Enabled = True
  67.             Else
  68.                 DialogModel.lstTables.StringItemList = Array(sSelectDBTable)
  69.                 EmptyFieldsListboxes()
  70.             End If
  71.             ToggleDatabasePage(True)
  72.         End With
  73.     End If
  74. End Sub
  75.  
  76.  
  77. Function GetConnection(sDBName as String)
  78. Dim oInteractionHandler as Object
  79. Dim bExitLoop as Boolean
  80. Dim bGetConnection as Boolean
  81. Dim iMsg as Integer
  82. Dim Nulllist()
  83.     If Not IsNull(oDBConnection) Then
  84.         oDBConnection.Dispose()
  85.     End If
  86.     oDataSource = oDBContext.GetByName(sDBName)
  87.     If Not oDBContext.hasbyName(sDBName) Then
  88.         GetConnection() = False
  89.         Exit Function
  90.     End If
  91.     If Not oDataSource.IsPasswordRequired Then
  92.         oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","")
  93.         GetConnection() = True
  94.     Else
  95.         oInteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  96.         oDataSource = oDBContext.GetByName(sDBName)
  97.         On Local Error Goto NOCONNECTION
  98.         Do
  99.             bExitLoop = True
  100.             oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  101.             NOCONNECTION:
  102.             bGetConnection = Err = 0
  103.             If bGetConnection Then
  104.                 bGetConnection = Not IsNull(oDBConnection)
  105.                 If Not bGetConnection Then
  106.                     Exit Do
  107.                 End If
  108.             End If
  109.             If Not bGetConnection Then
  110.                 iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)
  111.                 bExitLoop = iMsg = SBCANCEL
  112.                 Resume CLERROR
  113.                 CLERROR:
  114.             End If
  115.         Loop Until bExitLoop
  116.         On Local Error Goto 0
  117.         If Not bGetConnection Then
  118.             DialogModel.lstDatabases.SelectedItems() = Array(sSelectDatasource)
  119.             DialogModel.lstTables.StringItemList() = Array(sSelectDBTable)
  120.             DialogModel.lstFields.StringItemList() = NullList()
  121.             DialogModel.lstSelFields.StringItemList() = NullList()
  122.         End If
  123.         GetConnection() = bGetConnection
  124.     End If
  125. End Function
  126.  
  127.  
  128. Function GetDBMetaData()
  129.     If oDBContext.HasElements Then
  130.         Tablenames() = oDBConnection.Tables.ElementNames()
  131.         Querynames() = oDBConnection.Queries.ElementNames()
  132.         GetDBMetaData = True
  133.     Else
  134.         MsgBox(sMsgErrNoDatabase, 64, sMsgWizardName)
  135.         GetDBMetaData = False
  136.     End If
  137. End Function
  138.  
  139.  
  140. Sub GetTableMetaData()
  141. Dim iType as Long
  142. Dim m as Integer
  143. Dim Found as Boolean
  144. Dim i as Integer
  145. Dim sFieldName as String
  146. Dim n as Integer
  147. Dim WidthIndex as Integer
  148. Dim oField as Object
  149.     MaxIndex = Ubound(DialogModel.lstSelFields.StringItemList())
  150.     Dim ColumnMap(MaxIndex)as Integer
  151.     FieldNames() = DialogModel.lstSelFields.StringItemList()
  152.     ' Build a structure which maps the position of a selected field (within the selection) to the the column position within
  153.     ' the table. So we ensure that the controls are placed in the same order the according fields are selected.
  154.     For i = 0 To Ubound(FieldNames())
  155.         sFieldName = FieldNames(i)
  156.         Found = False
  157.         n = 0
  158.         While (n< MaxIndex And (Not Found))
  159.             If (FieldNames(n) = sFieldName) Then
  160.                 Found = True
  161.                 ColumnMap(n) = i
  162.             End If
  163.             n = n + 1
  164.         Wend
  165.     Next i
  166.     For n = 0 to MaxIndex
  167.         sFieldname = FieldNames(n)
  168.         oField = oColumns.GetByName(sFieldName)
  169.         iType = oField.Type
  170.         FieldMetaValues(n,0) = oField.Type
  171.         FieldMetaValues(n,1) = AssignFieldLength(oField.Precision)
  172.         FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)
  173.         FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  174.         FieldMetaValues(n,4) = oField.FormatKey
  175.         FieldMetaValues(n,5) = oField.DefaultValue
  176.         FieldMetaValues(n,6) = oField.IsCurrency
  177.         FieldMetaValues(n,7) = oField.Scale
  178. '        If oField.Description <> "" Then
  179. '' Todo: What's wrong with this line?
  180. '            Msgbox oField.Helptext
  181. '        End If
  182.         FieldMetaValues(n,8) = oField.Description
  183.     Next
  184.     ReDim oDBShapeList(MaxIndex) as Object
  185.     ReDim oTCShapeList(MaxIndex) as Object
  186.     ReDim oDBModelList(MaxIndex) as Object
  187.     ReDim oGroupShapeList(MaxIndex) as Object
  188. End Sub
  189.  
  190.  
  191. Function GetSpecificFieldNames() as Integer
  192. Dim n as Integer
  193. Dim m as Integer
  194. Dim s as Integer
  195. Dim iType as Integer
  196. Dim oField as Object
  197. Dim MaxIndex as Integer
  198. Dim EmptyList()
  199.     If Ubound(DialogModel.lstTables.StringItemList()) > -1 Then
  200.         FieldNames() = oColumns.GetElementNames()
  201.         MaxIndex = Ubound(FieldNames())
  202.         If MaxIndex <> -1 Then
  203.             Dim ResultFieldNames(MaxIndex)
  204.             ReDim ImgFieldNames(MaxIndex)
  205.             m = 0
  206.             For n = 0 To MaxIndex
  207.                 oField = oColumns.GetByName(FieldNames(n))
  208.                 iType = oField.Type
  209.                 If GetIndexInMultiArray(WidthList(), iType, 0) <> -1 Then
  210.                     ResultFieldNames(m) = FieldNames(n)
  211.                     m = m + 1
  212.                 End If
  213.                 If GetIndexInMultiArray(ImgWidthList(), iType, 0) <> -1 Then
  214.                     ImgFieldNames(s) = FieldNames(n)
  215.                     s = s + 1
  216.                 End If
  217.             Next n
  218.             If s <> 0 Then
  219.                 Redim Preserve ImgFieldNames(s-1)
  220.                 bEnableBinaryOptionGroup = True
  221.             Else
  222.                 bEnableBinaryOptionGroup = False
  223.             End If
  224.             If (DialogModel.optBinariesasGraphics.State = 1)  And (s <> 0) Then
  225.                 ResultFieldNames() = AddListToList(ResultFieldNames(), ImgFieldNames())
  226.             Else
  227.                 Redim Preserve ResultFieldNames(m-1)
  228.             End If
  229.             FieldNames() = ResultFieldNames()
  230.             DialogModel.lstFields.StringItemList = FieldNames()
  231.             InitializeListboxProcedures(DialogModel, DialogModel.lstFields, DialogModel.lstSelFields)
  232.         End If
  233.         GetSpecificFieldNames = MaxIndex
  234.     Else
  235.         GetSpecificFieldNames = -1
  236.     End If
  237. End Function
  238.  
  239.  
  240. Sub CreateDBForm()
  241.     If oDrawPage.Forms.Count = 0 Then
  242.           oDBForm = oDocument.CreateInstance("com.sun.star.form.component.Form")
  243.         oDrawpage.Forms.InsertByIndex (0, oDBForm)
  244.     Else
  245.         oDBForm = oDrawPage.Forms.GetByIndex(0)
  246.     End If
  247.     oDBForm.Name = "Standard"
  248.     oDBForm.DataSourceName = sDBName
  249.     oDBForm.Command = TableName
  250.     oDBForm.CommandType = CurCommandType
  251. End Sub
  252.  
  253.  
  254. Sub AddOrRemoveBinaryFieldsToWidthList()
  255. Dim LocWidthList()
  256. Dim MaxIndex as Integer
  257. Dim OldMaxIndex as Integer
  258. Dim s as Integer
  259. Dim n as Integer
  260. Dim m as Integer
  261.     If Not bDebug Then
  262.         On Local Error GoTo WIZARDERROR
  263.     End If
  264.     If DialogModel.optBinariesasGraphics.State = 1 Then
  265.         OldMaxIndex = Ubound(WidthList(),1)
  266.         If OldMaxIndex = 15 Then
  267.             MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  268.             ReDim Preserve WidthList(MaxIndex,4)
  269.             s = 0
  270.             For n = OldMaxIndex + 1 To MaxIndex
  271.                 For m = 0 To 3
  272.                     WidthList(n,m) = ImgWidthList(s,m)
  273.                 Next m
  274.                 s = s + 1
  275.             Next n
  276.             MergeList(DialogModel.lstFields, ImgFieldNames())
  277.         End If
  278.     Else
  279.         ReDim Preserve WidthList(15, 4)
  280.         RemoveListItems(DialogModel.lstFields(), DialogModel.lstSelFields(), ImgFieldNames())
  281.     End If
  282.     DialogModel.lstSelFields.Tag = True
  283. WIZARDERROR:
  284.     If Err <> 0 Then
  285.         Msgbox(sMsgErrMsg, 16, GetProductName())
  286.         Resume LOCERROR
  287.         LOCERROR:
  288.     End If
  289. End Sub
  290.  
  291.  
  292. Function CreateCommandTypeList()
  293. Dim MaxTableIndex as Integer
  294. Dim MaxQueryIndex as Integer
  295. Dim MaxIndex as Integer
  296. Dim i as Integer
  297. Dim a as Integer
  298.     MaxTableIndex = Ubound(TableNames()
  299.     MaxQueryIndex = Ubound(QueryNames()
  300.     MaxIndex = MaxTableIndex + MaxQueryIndex + 1
  301.     If MaxIndex > -1 Then
  302.         Dim LocCommandTypes(MaxIndex) as Integer
  303.         For i = 0 To MaxTableIndex
  304.             LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  305.         Next i
  306.         a = i
  307.         For i = 0 To MaxQueryIndex
  308.             LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  309.             a = a + 1
  310.         Next i
  311.     End If
  312.     CreateCommandTypeList() = LocCommandTypes()
  313. End Function
  314.  
  315.  
  316. Sub GetCurrentMetaValues(Index as Integer)
  317.     CurFieldType = FieldMetaValues(Index,0)
  318.     CurFieldLength = FieldMetaValues(Index,1)
  319.     CurControlType = FieldMetaValues(Index,2)
  320.     CurControlName = FieldMetaValues(Index,3)
  321.     CurFormatKey = FieldMetaValues(Index,4)
  322.     CurDefaultValue = FieldMetaValues(Index,5)
  323.     CurIsCurrency = FieldMetaValues(Index,6)
  324.     CurScale = FieldMetaValues(Index,7)
  325.     CurHelpText = FieldMetaValues(Index,8)
  326.     CurFieldName = FieldNames(Index)
  327. End Sub
  328.  
  329.  
  330. Function AssignFieldLength(FieldLength as Long) as Integer
  331.     If FieldLength >= 65535 Then
  332.         AssignFieldLength() = -1
  333.     Else
  334.         AssignFieldLength() = FieldLength
  335.     End If
  336. End Function
  337. </script:module>