home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #3 / K-CD_2002-03.iso / OpenOffice / f_0018 / DBMeta.xba next >
Extensible Markup Language  |  2001-10-12  |  8KB  |  256 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. Dim 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 NullList()
  22. Dim OldsDBname as String
  23. Dim bGetMetaData as Boolean
  24. Dim DBIndex as Integer
  25.     If Ubound(oDialogModel.lstDatabases.SelectedItems()) > -1 Then
  26.         ToggleDatabasePage(False)
  27.         DBIndex = oDialogModel.lstDatabases.SelectedItems(0)
  28.         sDBName = sDatabaseList(DBIndex)
  29.         If GetConnection(sDBName) Then
  30.             bGetMetaData = GetDBMetaData()
  31.         End If
  32.         If bGetMetaData Then
  33.             With oDialogModel
  34.                 .lstTables.Enabled = True
  35.                 .lblTables.Enabled = True
  36.                 .lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  37.                 iCommandTypes() = CreateCommandTypeList()
  38.                 .lstFields.StringItemList() = NullList()
  39.                 .lstSelFields.StringItemList() = NullList()
  40.             End With
  41.         End If
  42.         ToggleDatabasePage(True)
  43.     End If
  44. End Sub
  45.  
  46.  
  47. Function GetConnection(sDBName as String)
  48. Dim oInteractionHandler as Object
  49. Dim bExitLoop as Boolean
  50. Dim bGetConnection as Boolean
  51. Dim iMsg as Integer
  52. Dim Nulllist()
  53.     If Not IsNull(oDBConnection) Then
  54.         oDBConnection.Dispose()
  55.     End If
  56.     oDataSource = oDBContext.GetByName(sDBName)
  57.     If Not oDataSource.IsPasswordRequired Then
  58.         oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","")
  59.         GetConnection() = True
  60.     Else
  61.         oInteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  62.         oDataSource = oDBContext.GetByName(sDBName)
  63.         On Local Error Goto NOCONNECTION
  64.         Do
  65.             bExitLoop = True
  66.             oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  67.             NOCONNECTION:
  68.             bGetConnection = Err = 0
  69.             If bGetConnection Then
  70.                 bGetConnection = Not IsNull(oDBConnection)
  71.                 If Not bGetConnection Then
  72.                     Exit Do
  73.                 End If
  74.             End If
  75.             If Not bGetConnection Then
  76.                 iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)    ' '?' & ' Repeat and Cancel'
  77.                 bExitLoop = iMsg = SBCANCEL
  78.                 Resume CLERROR
  79.                 CLERROR:
  80.             End If
  81.         Loop Until bExitLoop
  82.         On Local Error Goto 0
  83.         If Not bGetConnection Then
  84.             oDialogModel.lstDatabases.SelectedItems() = Nulllist()
  85.             oDialogModel.lstTables.StringItemList() = NullList()
  86.             oDialogModel.lstFields.StringItemList() = NullList()
  87.             oDialogModel.lstSelFields.StringItemList() = NullList()
  88.         End If
  89.         GetConnection() = bGetConnection
  90.     End If
  91. End Function
  92.  
  93.  
  94. Function GetDBMetaData()
  95.     If oDBContext.HasElements Then
  96.         Tablenames() = oDBConnection.Tables.ElementNames()
  97.         Querynames() = oDBConnection.Queries.ElementNames()
  98.         GetDBMetaData = True
  99.     Else
  100.         MsgBox(sMsgErrNoDatabase, 64, sMsgWizardName)
  101.         GetDBMetaData = False
  102.     End If
  103. End Function
  104.  
  105.  
  106. Sub GetTableMetaData()
  107. Dim iType as Long
  108. Dim m as Integer
  109. Dim Found as Boolean
  110. Dim i as Integer
  111. Dim sFieldName as String
  112. Dim n as Integer
  113. Dim WidthIndex as Integer
  114. Dim oField as Object
  115.     MaxIndex = Ubound(oDialogModel.lstSelFields.StringItemList())
  116.     Dim ColumnMap(MaxIndex)as Integer    
  117.     FieldNames() = oDialogModel.lstSelFields.StringItemList()
  118.     ' Build a structure which maps the position of a selected field (within the selection) to the the column position within
  119.     ' the table. So we ensure that the controls are placed in the same order the according fields are selected.
  120.     For i = 0 To Ubound(FieldNames())
  121.         sFieldName = FieldNames(i)
  122.         Found = False
  123.         n = 0
  124.         While (n< MaxIndex And (Not Found))
  125.             If (FieldNames(n) = sFieldName) Then
  126.                 Found = True
  127.                 ColumnMap(n) = i
  128.             End If
  129.             n = n + 1
  130.         Wend
  131.     Next i
  132.     For n = 0 to MaxIndex
  133.         sFieldname = FieldNames(n)
  134.         oField = oColumns.GetByName(sFieldName) 
  135.         iType = oField.Type
  136.         FieldMetaValues(n,0) = iType
  137.         FieldMetaValues(n,1) = oField.Precision         
  138.         FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)        
  139.         FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  140.     Next
  141.     ReDim oDBShapeList(MaxIndex) as Object
  142.     ReDim oTCShapeList(MaxIndex) as Object
  143.     ReDim oDBModelList(MaxIndex) as Object
  144. End Sub
  145.  
  146.  
  147. Sub GetSpecificFieldNames()
  148. Dim n as Integer
  149. Dim m as Integer
  150. Dim s as Integer
  151. Dim iType as Integer
  152. Dim oField as Object
  153. Dim MaxIndex as Integer
  154. Dim EmptyList()
  155.     If Ubound(oDialogModel.lstTables.StringItemList()) > -1 Then
  156.         FieldNames() = oColumns.GetElementNames()
  157.         MaxIndex = Ubound(FieldNames())
  158.         Dim ResultFieldNames(MaxIndex)
  159.         ReDim ImgFieldNames(MaxIndex)
  160.         m = 0
  161.         For n = 0 To MaxIndex
  162.             oField = oColumns.GetByName(FieldNames(n))
  163.             iType = oField.Type
  164.             If GetIndexInMultiArray(WidthList(), iType, 0) <> -1 Then
  165.                 ResultFieldNames(m) = FieldNames(n)
  166.                 m = m + 1
  167.             End If
  168.             If GetIndexInMultiArray(ImgWidthList(), iType, 0) <> -1 Then
  169.                 ImgFieldNames(s) = FieldNames(n)
  170.                 s = s + 1
  171.             End If
  172.         Next n
  173.         If s <> 0 Then
  174.             Redim Preserve ImgFieldNames(s-1)
  175.             bEnableBinaryOptionGroup = True
  176.         Else
  177.             bEnableBinaryOptionGroup = False        
  178.         End If
  179.         Redim Preserve ResultFieldNames(m-1)
  180.         Redim Preserve FieldNames(m-1)
  181.         FieldNames() = ResultFieldNames()
  182.         oDialogModel.lstFields.StringItemList = FieldNames()
  183.         InitializeListboxProcedures(oDialogModel, oDialogModel.lstFields, oDialogModel.lstSelFields)
  184.     End If
  185. End Sub
  186.  
  187.  
  188. Sub CreateDBForm()
  189.     If oDrawPage.Forms.Count = 0 Then
  190.           oDBForm = oDocument.CreateInstance("com.sun.star.form.component.Form")
  191.         oDrawpage.Forms.InsertByIndex (0, oDBForm)
  192.     Else
  193.         oDBForm = oDrawPage.Forms.GetByIndex(0)
  194.     End If
  195.     oDBForm.Name = "Standard"
  196.     oDBForm.DataSourceName = sDBName
  197.     oDBForm.Command = TableName
  198.     oDBForm.CommandType = CurCommandType
  199. End Sub
  200.  
  201.  
  202. Sub AddOrRemoveBinaryFieldsToWidthList()
  203. Dim LocWidthList()
  204. Dim MaxIndex as Integer
  205. Dim OldMaxIndex as Integer
  206. Dim s as Integer
  207. Dim n as Integer
  208. Dim m as Integer
  209. ' Todo: Die folgenden Zeilen k├╢nnten in einer allgemeinen Routine 
  210. ' bearbeitet werden, z. B. durch Umschreiben von AddListToList, wobei
  211. ' beim Abfragen des Ubounds der zweiten Dimension eine Fehlerabfrage n├╢tig w├ñre.
  212.     If oDialogModel.optBinariesasGraphics.State = 1 Then
  213. '        MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  214. '        OldMaxIndex = Ubound(WidthList(),1)
  215.         ReDim Preserve WidthList(18,4)
  216.         s = 0
  217.         For n = OldMaxIndex + 1 To MaxIndex
  218.             For m = 0 To 3
  219.                 WidthList(n,m) = ImgWidthList(s,m)
  220.             Next m
  221.             s = s + 1
  222.         Next n
  223.         MergeList(oDialogModel.lstFields, ImgFieldNames())
  224.     Else
  225.         ReDim Preserve WidthList(15, 4)
  226.         RemoveListItems(oDialogModel.lstFields(), oDialogModel.lstSelFields(), ImgFieldNames())
  227.     End If
  228. End Sub
  229.  
  230.  
  231. Function CreateCommandTypeList()
  232. Dim MaxTableIndex as Integer
  233. Dim MaxQueryIndex as Integer
  234. Dim i as Integer
  235. Dim a as Integer
  236.     MaxTableIndex = Ubound(TableNames()
  237.     MaxQueryIndex = Ubound(QueryNames()
  238. Dim LocCommandTypes(MaxTableIndex + MaxQueryIndex + 1) as Integer
  239.     For i = 0 To MaxTableIndex
  240.         LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  241.     Next i
  242.     a = i
  243.     For i = 0 To MaxQueryIndex
  244.         LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  245.     Next i
  246.     CreateCommandTypeList() = LocCommandTypes()
  247. End Function
  248.  
  249.  
  250. Sub GetCurrentMetaValues(Index as Integer)
  251.     CurFieldType = FieldMetaValues(Index,0)
  252.     CurFieldLength = FieldMetaValues(Index,1)
  253.     CurControlType = FieldMetaValues(Index,2)
  254.     CurControlName = FieldMetaValues(Index,3)
  255.     CurFieldName = FieldNames(Index)
  256. End Sub</script:module>