home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / soto_en / f_0017 / DBMeta.xba next >
Encoding:
Extensible Markup Language  |  2002-01-18  |  7.6 KB  |  268 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 NullList()
  22. Dim OldsDBname as String
  23. Dim DBIndex as Integer
  24.     If Ubound(oDialogModel.lstDatabases.SelectedItems()) > -1 Then
  25.         ToggleDatabasePage(False)
  26.         DBIndex = oDialogModel.lstDatabases.SelectedItems(0)
  27.         sDBName = sDatabaseList(DBIndex)
  28.         If GetConnection(sDBName) Then
  29.             If GetDBMetaData() Then
  30.                 With oDialogModel
  31.                     .lstTables.Enabled = True
  32.                     .lblTables.Enabled = True
  33.                     .lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  34.                     iCommandTypes() = CreateCommandTypeList()
  35.                     .lstFields.StringItemList() = NullList()
  36.                     .lstSelFields.StringItemList() = NullList()
  37.                 End With
  38.             End If
  39.         End If
  40.         bEnableBinaryOptionGroup = False
  41.         ToggleDatabasePage(True)
  42.     End If
  43. End Sub
  44.  
  45.  
  46. Function GetConnection(sDBName as String)
  47. Dim oInteractionHandler as Object
  48. Dim bExitLoop as Boolean
  49. Dim bGetConnection as Boolean
  50. Dim iMsg as Integer
  51. Dim Nulllist()
  52.     If Not IsNull(oDBConnection) Then
  53.         oDBConnection.Dispose()
  54.     End If
  55.     oDataSource = oDBContext.GetByName(sDBName)
  56.     If Not oDataSource.IsPasswordRequired Then
  57.         oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","")
  58.         GetConnection() = True
  59.     Else
  60.         oInteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  61.         oDataSource = oDBContext.GetByName(sDBName)
  62.         On Local Error Goto NOCONNECTION
  63.         Do
  64.             bExitLoop = True
  65.             oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  66.             NOCONNECTION:
  67.             bGetConnection = Err = 0
  68.             If bGetConnection Then
  69.                 bGetConnection = Not IsNull(oDBConnection)
  70.                 If Not bGetConnection Then
  71.                     Exit Do
  72.                 End If
  73.             End If
  74.             If Not bGetConnection Then
  75.                 iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)    ' '?' & ' Repeat and Cancel'
  76.                 bExitLoop = iMsg = SBCANCEL
  77.                 Resume CLERROR
  78.                 CLERROR:
  79.             End If
  80.         Loop Until bExitLoop
  81.         On Local Error Goto 0
  82.         If Not bGetConnection Then
  83.             oDialogModel.lstDatabases.SelectedItems() = Nulllist()
  84.             oDialogModel.lstTables.StringItemList() = NullList()
  85.             oDialogModel.lstFields.StringItemList() = NullList()
  86.             oDialogModel.lstSelFields.StringItemList() = NullList()
  87.         End If
  88.         GetConnection() = bGetConnection
  89.     End If
  90. End Function
  91.  
  92.  
  93. Function GetDBMetaData()
  94.     If oDBContext.HasElements Then
  95.         Tablenames() = oDBConnection.Tables.ElementNames()
  96.         Querynames() = oDBConnection.Queries.ElementNames()
  97.         GetDBMetaData = True
  98.     Else
  99.         MsgBox(sMsgErrNoDatabase, 64, sMsgWizardName)
  100.         GetDBMetaData = False
  101.     End If
  102. End Function
  103.  
  104.  
  105. Sub GetTableMetaData()
  106. Dim iType as Long
  107. Dim m as Integer
  108. Dim Found as Boolean
  109. Dim i as Integer
  110. Dim sFieldName as String
  111. Dim n as Integer
  112. Dim WidthIndex as Integer
  113. Dim oField as Object
  114.     MaxIndex = Ubound(oDialogModel.lstSelFields.StringItemList())
  115.     Dim ColumnMap(MaxIndex)as Integer    
  116.     FieldNames() = oDialogModel.lstSelFields.StringItemList()
  117.     ' Build a structure which maps the position of a selected field (within the selection) to the the column position within
  118.     ' the table. So we ensure that the controls are placed in the same order the according fields are selected.
  119.     For i = 0 To Ubound(FieldNames())
  120.         sFieldName = FieldNames(i)
  121.         Found = False
  122.         n = 0
  123.         While (n< MaxIndex And (Not Found))
  124.             If (FieldNames(n) = sFieldName) Then
  125.                 Found = True
  126.                 ColumnMap(n) = i
  127.             End If
  128.             n = n + 1
  129.         Wend
  130.     Next i
  131.     For n = 0 to MaxIndex
  132.         sFieldname = FieldNames(n)
  133.         oField = oColumns.GetByName(sFieldName) 
  134.         iType = oField.Type
  135.         FieldMetaValues(n,0) = iType
  136.         FieldMetaValues(n,1) = oField.Precision         
  137.         FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)        
  138.         FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  139.     Next
  140.     ReDim oDBShapeList(MaxIndex) as Object
  141.     ReDim oTCShapeList(MaxIndex) as Object
  142.     ReDim oDBModelList(MaxIndex) as Object
  143. '    ReDim oGroupShapeList(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.     If Not bDebug Then
  210.         On Local Error GoTo WIZARDERROR
  211.     End If
  212.     If oDialogModel.optBinariesasGraphics.State = 1 Then
  213.         OldMaxIndex = Ubound(WidthList(),1)
  214.         If OldMaxIndex = 15 Then
  215.             MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  216.             ReDim Preserve WidthList(MaxIndex,4)
  217.             s = 0
  218.             For n = OldMaxIndex + 1 To MaxIndex
  219.                 For m = 0 To 3
  220.                     WidthList(n,m) = ImgWidthList(s,m)
  221.                 Next m
  222.                 s = s + 1
  223.             Next n
  224.             MergeList(oDialogModel.lstFields, ImgFieldNames())
  225.         End If
  226.     Else
  227.         ReDim Preserve WidthList(15, 4)
  228.         RemoveListItems(oDialogModel.lstFields(), oDialogModel.lstSelFields(), ImgFieldNames())
  229.     End If
  230. WIZARDERROR:
  231.     If Err <> 0 Then    
  232.         Msgbox(sMsgErrMsg, 16, GetProductName())
  233.         Resume LOCERROR
  234.         LOCERROR:        
  235.     End If
  236. End Sub
  237.  
  238.  
  239. Function CreateCommandTypeList()
  240. Dim MaxTableIndex as Integer
  241. Dim MaxQueryIndex as Integer
  242. Dim MaxIndex as Integer
  243. Dim i as Integer
  244. Dim a as Integer
  245.     MaxTableIndex = Ubound(TableNames()
  246.     MaxQueryIndex = Ubound(QueryNames()
  247.     MaxIndex = MaxTableIndex + MaxQueryIndex + 1
  248.     If MaxIndex > -1 Then
  249.         Dim LocCommandTypes(MaxIndex) as Integer
  250.         For i = 0 To MaxTableIndex
  251.             LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  252.         Next i
  253.         a = i
  254.         For i = 0 To MaxQueryIndex
  255.             LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  256.         Next i
  257.     End If
  258.     CreateCommandTypeList() = LocCommandTypes()
  259. End Function
  260.  
  261.  
  262. Sub GetCurrentMetaValues(Index as Integer)
  263.     CurFieldType = FieldMetaValues(Index,0)
  264.     CurFieldLength = FieldMetaValues(Index,1)
  265.     CurControlType = FieldMetaValues(Index,2)
  266.     CurControlName = FieldMetaValues(Index,3)
  267.     CurFieldName = FieldNames(Index)
  268. End Sub</script:module>