home *** CD-ROM | disk | FTP | other *** search
/ com!online 2001 December / COMCD1201.iso / openoffice / f_0020 / DBMeta.xba next >
Encoding:
Extensible Markup Language  |  2001-08-24  |  7.0 KB  |  237 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.  
  10.  
  11. Sub GetDatabaseNames()
  12.     If oDBContext.HasElements Then
  13.         sDatabaseList() = oDBContext.ElementNames()
  14.     End If
  15. End Sub
  16.  
  17.  
  18. Sub GetSelectedDBMetaData()
  19. Dim NullList()
  20. Dim OldsDBname as String
  21. Dim bGetMetaData as Boolean
  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.             bGetMetaData = GetDBMetaData()
  29.         End If
  30.         If bGetMetaData Then
  31.             With oDialogModel
  32.                 .lstTables.Enabled = True
  33.                 .lblTables.Enabled = True
  34.                 .lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  35.                 iCommandTypes() = CreateCommandTypeList()
  36.                 .lstFields.StringItemList() = NullList()
  37.                 .lstSelFields.StringItemList() = NullList()
  38.             End With
  39.         End If
  40.         ToggleDatabasePage(True)
  41.     End If
  42. End Sub
  43.  
  44.  
  45. Function GetConnection(sDBName as String)
  46. Dim oDatabase as Object
  47. Dim oInteractionHandler as Object
  48. Dim oDataSource 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.     oDatabase = oDBContext.GetByName(sDBName)
  57.     If Not oDatabase.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.     MaxIndex = Ubound(oDialogModel.lstSelFields.StringItemList())
  115.     Dim ColumnMap(MaxIndex)as Integer    
  116.     FieldNames() = oDialogModel.lstSelFields.StringItemList()
  117.  
  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.  
  133.     For n = 0 to MaxIndex
  134.         sFieldname = FieldNames(n)
  135.         iType = oColumns.GetByName(sFieldName).Type
  136.         FieldMetaValues(n,0) = iType
  137.         FieldMetaValues(n,1) = GetValueoutofList(iType, WidthList(),1, WidthIndex)        
  138.         FieldMetaValues(n,2) = WidthList(WidthIndex,2)
  139.         FieldMetaValues(n,3) = WidthList(WidthIndex,4)
  140.     Next
  141.     ReDim oDBShapeList(MaxIndex) as Object
  142.     ReDim oTCShapeList(MaxIndex) as Object
  143. End Sub
  144.  
  145.  
  146. Sub GetSpecificFieldNames()
  147. Dim n as Integer
  148. Dim m as Integer
  149. Dim iType as Integer
  150. Dim oField as Object
  151. Dim MaxIndex as Integer
  152. Dim EmptyList()
  153.     If Ubound(oDialogModel.lstTables.StringItemList()) > -1 Then
  154.         FieldNames() = oColumns.GetElementNames()
  155.         MaxIndex = Ubound(FieldNames())
  156.         Dim ResultFieldNames(MaxIndex)
  157.         m = 0
  158.         For n = 0 To MaxIndex
  159.             oField = oColumns.GetByName(FieldNames(n))
  160.             iType = oField.Type
  161.             If GetIndexInMultiArray(WidthList(), iType, 0) <> -1 Then
  162.                 ResultFieldNames(m) = FieldNames(n)
  163.                 m = m + 1
  164.             End If
  165.         Next n
  166.         Redim Preserve ResultFieldNames(m-1)
  167.         Redim Preserve FieldNames(m-1)
  168.         FieldNames() = ResultFieldNames()
  169.         oDialogModel.lstFields.StringItemList = FieldNames()
  170.         InitializeListboxProcedures(oDialogModel, oDialogModel.lstFields, oDialogModel.lstSelFields)
  171.     End If
  172. End Sub
  173.  
  174.  
  175. Sub CreateDBForm()
  176.     If oDrawPage.Forms.Count = 0 Then
  177.           oDBForm = oDocument.CreateInstance("com.sun.star.form.component.Form")
  178.         oDrawpage.Forms.InsertByIndex (0, oDBForm)
  179.     Else
  180.         oDBForm = oDrawPage.Forms.GetByIndex(0)
  181.     End If
  182.     oDBForm.Name = "Standard"
  183.     oDBForm.DataSourceName = sDBName
  184.     oDBForm.Command = TableName
  185.     oDBForm.CommandType = CurCommandType
  186. End Sub
  187.  
  188.  
  189. Sub AddBinaryFieldsToWidthList()
  190. Dim LocWidthList()
  191. Dim MaxIndex as Integer
  192. Dim OldMaxIndex as Integer
  193. Dim s as Integer
  194. Dim n as Integer
  195. Dim m as Integer
  196. ' Todo: Die folgenden Zeilen k├╢nnten in einer allgemeinen Routine 
  197. ' bearbeitet werden, z. B. durch Umschreiben von AddListToList, wobei
  198. ' beim Abfragen des Ubounds der zweiten Dimension eine Fehlerabfrage n├╢tig w├ñre.
  199.     MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  200.     OldMaxIndex = Ubound(WidthList(),1)
  201.     ReDim Preserve WidthList(MaxIndex,4)
  202.     s = 0
  203.     For n = OldMaxIndex + 1 To MaxIndex
  204.         For m = 0 To 4
  205.             WidthList(n,m) = ImgWidthList(s,m)
  206.         Next m
  207.         s = s + 1
  208.     Next n
  209.     FillUpFieldsListbox(True)
  210. End Sub
  211.  
  212.  
  213. Sub RemoveBinaryFieldsFromWidthList()
  214. Dim MaxIndex as Integer
  215.     MaxIndex = Ubound(WidthList(),1) - Ubound(ImgWidthList(),1) - 1
  216.     ReDim Preserve WidthList(MaxIndex, 4)
  217.     FillUpFieldsListbox(True)
  218. End Sub
  219.  
  220.  
  221. Function CreateCommandTypeList()
  222. Dim MaxTableIndex as Integer
  223. Dim MaxQueryIndex as Integer
  224. Dim i as Integer
  225. Dim a as Integer
  226.     MaxTableIndex = Ubound(TableNames()
  227.     MaxQueryIndex = Ubound(QueryNames()
  228. Dim LocCommandTypes(MaxTableIndex + MaxQueryIndex + 1) as Integer
  229.     For i = 0 To MaxTableIndex
  230.         LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  231.     Next i      
  232.     a = i
  233.     For i = 0 To MaxQueryIndex
  234.         LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  235.     Next i
  236.     CreateCommandTypeList() = LocCommandTypes()
  237. End Function</script:module>