DBMeta.xba 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="DBMeta" script:language="StarBasic">REM ***** BASIC *****
  21. Option Explicit
  22. Public iCommandTypes() as Integer
  23. Public CurCommandType as Integer
  24. Public oDataSource as Object
  25. Public bEnableBinaryOptionGroup as Boolean
  26. &apos;Public bSelectContent as Boolean
  27. Function GetDatabaseNames(baddFirstListItem as Boolean)
  28. Dim sDatabaseList()
  29. If oDBContext.HasElements Then
  30. Dim LocDBList() as String
  31. Dim MaxIndex as Integer
  32. Dim i as Integer
  33. LocDBList = oDBContext.ElementNames()
  34. MaxIndex = Ubound(LocDBList())
  35. If baddfirstListItem Then
  36. ReDim Preserve sDatabaseList(MaxIndex + 1)
  37. sDatabaseList(0) = sSelectDatasource
  38. a = 1
  39. Else
  40. ReDim Preserve sDatabaseList(MaxIndex)
  41. a = 0
  42. End If
  43. For i = 0 To MaxIndex
  44. sDatabaseList(a) = oDBContext.ElementNames(i)
  45. a = a + 1
  46. Next i
  47. End If
  48. GetDatabaseNames() = sDatabaseList()
  49. End Function
  50. Sub GetSelectedDBMetaData(sDBName as String)
  51. Dim OldsDBname as String
  52. Dim DBIndex as Integer
  53. Dim LocList() as String
  54. &apos; If bStartUp Then
  55. &apos; bStartUp = false
  56. &apos; Exit Sub
  57. &apos; End Sub
  58. ToggleDatabasePage(False)
  59. With DialogModel
  60. If GetConnection(sDBName) Then
  61. If GetDBMetaData() Then
  62. LocList() = AddListToList(Array(sSelectDBTable), TableNames())
  63. .lstTables.StringItemList() = AddListToList(LocList(), QueryNames())
  64. &apos; bSelectContent = True
  65. .lstTables.SelectedItems() = Array(0)
  66. iCommandTypes() = CreateCommandTypeList()
  67. EmptyFieldsListboxes()
  68. End If
  69. End If
  70. bEnableBinaryOptionGroup = False
  71. .lstTables.Enabled = True
  72. .lblTables.Enabled = True
  73. &apos; Else
  74. &apos; DialogModel.lstTables.StringItemList = Array(sSelectDBTable)
  75. &apos; EmptyFieldsListboxes()
  76. &apos; End If
  77. ToggleDatabasePage(True)
  78. End With
  79. End Sub
  80. Function GetConnection(sDBName as String)
  81. Dim oInteractionHandler as Object
  82. Dim bExitLoop as Boolean
  83. Dim bGetConnection as Boolean
  84. Dim iMsg as Integer
  85. Dim Nulllist()
  86. If Not IsNull(oDBConnection) Then
  87. oDBConnection.Dispose()
  88. End If
  89. oDataSource = oDBContext.GetByName(sDBName)
  90. &apos; If Not oDBContext.hasbyName(sDBName) Then
  91. &apos; GetConnection() = False
  92. &apos; Exit Function
  93. &apos; End If
  94. If Not oDataSource.IsPasswordRequired Then
  95. oDBConnection = oDBContext.GetByName(sDBName).GetConnection(&quot;&quot;,&quot;&quot;)
  96. GetConnection() = True
  97. Else
  98. oInteractionHandler = createUnoService(&quot;com.sun.star.task.InteractionHandler&quot;)
  99. oDataSource = oDBContext.GetByName(sDBName)
  100. On Local Error Goto NOCONNECTION
  101. Do
  102. bExitLoop = True
  103. oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  104. NOCONNECTION:
  105. bGetConnection = Err = 0
  106. If bGetConnection Then
  107. bGetConnection = Not IsNull(oDBConnection)
  108. If Not bGetConnection Then
  109. Exit Do
  110. End If
  111. End If
  112. If Not bGetConnection Then
  113. iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)
  114. bExitLoop = iMsg = SBCANCEL
  115. Resume CLERROR
  116. CLERROR:
  117. End If
  118. Loop Until bExitLoop
  119. On Local Error Goto 0
  120. If Not bGetConnection Then
  121. DialogModel.lstTables.StringItemList() = Array(sSelectDBTable)
  122. DialogModel.lstFields.StringItemList() = NullList()
  123. DialogModel.lstSelFields.StringItemList() = NullList()
  124. End If
  125. GetConnection() = bGetConnection
  126. End If
  127. End Function
  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. Sub GetTableMetaData()
  139. Dim iType as Long
  140. Dim m as Integer
  141. Dim Found as Boolean
  142. Dim i as Integer
  143. Dim sFieldName as String
  144. Dim n as Integer
  145. Dim WidthIndex as Integer
  146. Dim oField as Object
  147. MaxIndex = Ubound(DialogModel.lstSelFields.StringItemList())
  148. Dim ColumnMap(MaxIndex)as Integer
  149. FieldNames() = DialogModel.lstSelFields.StringItemList()
  150. &apos; Build a structure which maps the position of a selected field (within the selection) to the column position within
  151. &apos; the table. So we ensure that the controls are placed in the same order the according fields are selected.
  152. For i = 0 To Ubound(FieldNames())
  153. sFieldName = FieldNames(i)
  154. Found = False
  155. n = 0
  156. While (n&lt; MaxIndex And (Not Found))
  157. If (FieldNames(n) = sFieldName) Then
  158. Found = True
  159. ColumnMap(n) = i
  160. End If
  161. n = n + 1
  162. Wend
  163. Next i
  164. For n = 0 to MaxIndex
  165. sFieldname = FieldNames(n)
  166. oField = oColumns.GetByName(sFieldName)
  167. iType = oField.Type
  168. FieldMetaValues(n,0) = oField.Type
  169. FieldMetaValues(n,1) = AssignFieldLength(oField.Precision)
  170. FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)
  171. FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  172. FieldMetaValues(n,4) = oField.FormatKey
  173. FieldMetaValues(n,5) = oField.DefaultValue
  174. FieldMetaValues(n,6) = oField.IsCurrency
  175. FieldMetaValues(n,7) = oField.Scale
  176. &apos; If oField.Description &lt;&gt; &quot;&quot; Then
  177. &apos;&apos; Todo: What&apos;s wrong with this line?
  178. &apos; Msgbox oField.Helptext
  179. &apos; End If
  180. FieldMetaValues(n,8) = oField.Description
  181. Next
  182. ReDim oDBShapeList(MaxIndex) as Object
  183. ReDim oTCShapeList(MaxIndex) as Object
  184. ReDim oDBModelList(MaxIndex) as Object
  185. ReDim oGroupShapeList(MaxIndex) as Object
  186. End Sub
  187. Function GetSpecificFieldNames() as Integer
  188. Dim n as Integer
  189. Dim m as Integer
  190. Dim s as Integer
  191. Dim iType as Integer
  192. Dim oField as Object
  193. Dim MaxIndex as Integer
  194. Dim EmptyList()
  195. If Ubound(DialogModel.lstTables.StringItemList()) &gt; -1 Then
  196. FieldNames() = oColumns.GetElementNames()
  197. MaxIndex = Ubound(FieldNames())
  198. If MaxIndex &lt;&gt; -1 Then
  199. Dim ResultFieldNames(MaxIndex)
  200. ReDim ImgFieldNames(MaxIndex)
  201. m = 0
  202. For n = 0 To MaxIndex
  203. oField = oColumns.GetByName(FieldNames(n))
  204. iType = oField.Type
  205. If GetIndexInMultiArray(WidthList(), iType, 0) &lt;&gt; -1 Then
  206. ResultFieldNames(m) = FieldNames(n)
  207. m = m + 1
  208. End If
  209. If GetIndexInMultiArray(ImgWidthList(), iType, 0) &lt;&gt; -1 Then
  210. ImgFieldNames(s) = FieldNames(n)
  211. s = s + 1
  212. End If
  213. Next n
  214. If s &lt;&gt; 0 Then
  215. Redim Preserve ImgFieldNames(s-1)
  216. bEnableBinaryOptionGroup = True
  217. Else
  218. bEnableBinaryOptionGroup = False
  219. End If
  220. If (DialogModel.optBinariesasGraphics.State = 1) And (s &lt;&gt; 0) Then
  221. ResultFieldNames() = AddListToList(ResultFieldNames(), ImgFieldNames())
  222. Else
  223. Redim Preserve ResultFieldNames(m-1)
  224. End If
  225. FieldNames() = ResultFieldNames()
  226. DialogModel.lstFields.StringItemList = FieldNames()
  227. InitializeListboxProcedures(DialogModel, DialogModel.lstFields, DialogModel.lstSelFields)
  228. End If
  229. GetSpecificFieldNames = MaxIndex
  230. Else
  231. GetSpecificFieldNames = -1
  232. End If
  233. End Function
  234. Sub CreateDBForm()
  235. If oDrawPage.Forms.Count = 0 Then
  236. oDBForm = oDocument.CreateInstance(&quot;com.sun.star.form.component.Form&quot;)
  237. oDrawpage.Forms.InsertByIndex (0, oDBForm)
  238. Else
  239. oDBForm = oDrawPage.Forms.GetByIndex(0)
  240. End If
  241. oDBForm.Name = &quot;Standard&quot;
  242. oDBForm.DataSourceName = sDBName
  243. oDBForm.Command = TableName
  244. oDBForm.CommandType = CurCommandType
  245. End Sub
  246. Sub AddOrRemoveBinaryFieldsToWidthList()
  247. Dim LocWidthList()
  248. Dim MaxIndex as Integer
  249. Dim OldMaxIndex as Integer
  250. Dim s as Integer
  251. Dim n as Integer
  252. Dim m as Integer
  253. If Not bDebug Then
  254. On Local Error GoTo WIZARDERROR
  255. End If
  256. If DialogModel.optBinariesasGraphics.State = 1 Then
  257. OldMaxIndex = Ubound(WidthList(),1)
  258. If OldMaxIndex = 15 Then
  259. MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  260. ReDim Preserve WidthList(MaxIndex,4)
  261. s = 0
  262. For n = OldMaxIndex + 1 To MaxIndex
  263. For m = 0 To 3
  264. WidthList(n,m) = ImgWidthList(s,m)
  265. Next m
  266. s = s + 1
  267. Next n
  268. MergeList(DialogModel.lstFields, ImgFieldNames())
  269. End If
  270. Else
  271. ReDim Preserve WidthList(15, 4)
  272. RemoveListItems(DialogModel.lstFields(), DialogModel.lstSelFields(), ImgFieldNames())
  273. End If
  274. DialogModel.lstSelFields.Tag = True
  275. WIZARDERROR:
  276. If Err &lt;&gt; 0 Then
  277. Msgbox(sMsgErrMsg, 16, GetProductName())
  278. Resume LOCERROR
  279. LOCERROR:
  280. End If
  281. End Sub
  282. Function CreateCommandTypeList()
  283. Dim MaxTableIndex as Integer
  284. Dim MaxQueryIndex as Integer
  285. Dim MaxIndex as Integer
  286. Dim i as Integer
  287. Dim a as Integer
  288. MaxTableIndex = Ubound(TableNames())
  289. MaxQueryIndex = Ubound(QueryNames())
  290. MaxIndex = MaxTableIndex + MaxQueryIndex + 1
  291. If MaxIndex &gt; -1 Then
  292. Dim LocCommandTypes(MaxIndex) as Integer
  293. For i = 0 To MaxTableIndex
  294. LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  295. Next i
  296. a = i
  297. For i = 0 To MaxQueryIndex
  298. LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  299. a = a + 1
  300. Next i
  301. End If
  302. CreateCommandTypeList() = LocCommandTypes()
  303. End Function
  304. Sub GetCurrentMetaValues(Index as Integer)
  305. CurFieldType = FieldMetaValues(Index,0)
  306. CurFieldLength = FieldMetaValues(Index,1)
  307. CurControlType = FieldMetaValues(Index,2)
  308. CurControlName = FieldMetaValues(Index,3)
  309. CurFormatKey = FieldMetaValues(Index,4)
  310. CurDefaultValue = FieldMetaValues(Index,5)
  311. CurIsCurrency = FieldMetaValues(Index,6)
  312. CurScale = FieldMetaValues(Index,7)
  313. CurHelpText = FieldMetaValues(Index,8)
  314. CurFieldName = FieldNames(Index)
  315. End Sub
  316. Function AssignFieldLength(FieldLength as Long) as Integer
  317. If FieldLength &gt;= 65535 Then
  318. AssignFieldLength() = -1
  319. Else
  320. AssignFieldLength() = FieldLength
  321. End If
  322. End Function
  323. </script:module>