Root_.xba 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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="Root_" script:language="StarBasic">
  4. REM =======================================================================================================================
  5. REM === The Access2Base library is a part of the LibreOffice project. ===
  6. REM === Full documentation is available on http://www.access2base.com ===
  7. REM =======================================================================================================================
  8. Option Compatible
  9. Option ClassModule
  10. Option Explicit
  11. REM -----------------------------------------------------------------------------------------------------------------------
  12. REM --- FOR INTERNAL USE ONLY ---
  13. REM -----------------------------------------------------------------------------------------------------------------------
  14. REM -----------------------------------------------------------------------------------------------------------------------
  15. REM --- CLASS ROOT FIELDS ---
  16. REM -----------------------------------------------------------------------------------------------------------------------
  17. Private ErrorHandler As Boolean
  18. Private MinimalTraceLevel As Integer
  19. Private TraceLogs() As Variant
  20. Private TraceLogCount As Integer
  21. Private TraceLogLast As Integer
  22. Private TraceLogMaxEntries As Integer
  23. Private LastErrorCode As Integer
  24. Private LastErrorLevel As String
  25. Private ErrorText As String
  26. Private ErrorLongText As String
  27. Private CalledSub As String
  28. Private DebugPrintShort As Boolean
  29. Private Introspection As Object &apos; com.sun.star.beans.Introspection
  30. Private VersionNumber As String &apos; Actual Access2Base version number
  31. Private Locale As String
  32. Private ExcludeA2B As Boolean
  33. Private TextSearch As Object
  34. Private SearchOptions As Variant
  35. Private FindRecord As Object
  36. Private StatusBar As Object
  37. Private Dialogs As Object &apos; Collection
  38. Private TempVars As Object &apos; Collection
  39. Private CurrentDoc() As Variant &apos; Array of document containers - [0] = Base document, [1 ... N] = other documents
  40. Private PythonCache() As Variant &apos; Array of objects created in Python scripts
  41. REM -----------------------------------------------------------------------------------------------------------------------
  42. REM --- CONSTRUCTORS / DESTRUCTORS ---
  43. REM -----------------------------------------------------------------------------------------------------------------------
  44. Private Sub Class_Initialize()
  45. VersionNumber = Access2Base_Version
  46. ErrorHandler = True
  47. MinimalTraceLevel = 0
  48. TraceLogs() = Array()
  49. TraceLogCount = 0
  50. TraceLogLast = 0
  51. TraceLogMaxEntries = 0
  52. LastErrorCode = 0
  53. LastErrorLevel = &quot;&quot;
  54. ErrorText = &quot;&quot;
  55. ErrorLongText = &quot;&quot;
  56. CalledSub = &quot;&quot;
  57. DebugPrintShort = True
  58. Locale = L10N._GetLocale()
  59. ExcludeA2B = True
  60. Set Introspection = CreateUnoService(&quot;com.sun.star.beans.Introspection&quot;)
  61. Set TextSearch = CreateUnoService(&quot;com.sun.star.util.TextSearch&quot;)
  62. SearchOptions = New com.sun.star.util.SearchOptions
  63. With SearchOptions
  64. .algorithmType = com.sun.star.util.SearchAlgorithms.REGEXP
  65. .searchFlag = 0
  66. .transliterateFlags = com.sun.star.i18n.TransliterationModules.IGNORE_CASE
  67. End With
  68. Set FindRecord = Nothing
  69. Set StatusBar = Nothing
  70. Set Dialogs = New Collection
  71. Set TempVars = New Collection
  72. CurrentDoc = Array()
  73. ReDim CurrentDoc(0 To 0)
  74. Set CurrentDoc(0) = Nothing
  75. PythonCache = Array()
  76. End Sub &apos; Constructor
  77. REM -----------------------------------------------------------------------------------------------------------------------
  78. Private Sub Class_Terminate()
  79. Call Class_Initialize()
  80. End Sub &apos; Destructor
  81. REM -----------------------------------------------------------------------------------------------------------------------
  82. Public Sub Dispose()
  83. Call Class_Terminate()
  84. End Sub &apos; Explicit destructor
  85. REM -----------------------------------------------------------------------------------------------------------------------
  86. REM --- CLASS GET/LET/SET PROPERTIES ---
  87. REM -----------------------------------------------------------------------------------------------------------------------
  88. REM -----------------------------------------------------------------------------------------------------------------------
  89. REM --- CLASS METHODS ---
  90. REM -----------------------------------------------------------------------------------------------------------------------
  91. REM -----------------------------------------------------------------------------------------------------------------------
  92. Public Function AddPython(ByRef pvObject As Variant) As Long
  93. &apos; Store the object as a new entry in PythonCache and return its entry number
  94. Dim lVars As Long, vObject As Variant
  95. lVars = UBound(PythonCache) + 1
  96. ReDim Preserve PythonCache(0 To lVars)
  97. PythonCache(lVars) = pvObject
  98. AddPython = lVars
  99. End Function &apos; AddPython V6.4
  100. REM -----------------------------------------------------------------------------------------------------------------------
  101. Public Sub CloseConnection()
  102. &apos; Close all connections established by current document to free memory.
  103. &apos; - if Base document =&gt; close the one concerned database connection
  104. &apos; - if non-Base documents =&gt; close the connections of each individual standalone form
  105. Dim i As Integer, iCurrentDoc As Integer
  106. Dim vDbContainer As Variant, vDbContainers() As Variant, vDocContainer As Variant
  107. If ErrorHandler Then On Local Error Goto Error_Sub
  108. If Not IsArray(CurrentDoc) Then Goto Exit_Sub
  109. If UBound(CurrentDoc) &lt; 0 Then Goto Exit_Sub
  110. iCurrentDoc = CurrentDocIndex( , False) &apos; False prevents error raising if not found
  111. If iCurrentDoc &lt; 0 Then GoTo Exit_Sub &apos; If not found ignore
  112. vDocContainer = CurrentDocument(iCurrentDoc)
  113. With vDocContainer
  114. If Not .Active Then GoTo Exit_Sub &apos; e.g. if multiple calls to CloseConnection()
  115. For i = 0 To UBound(.DbContainers)
  116. If Not IsNull(.DbContainers(i).Database) Then
  117. .DbContainers(i).Database.Dispose()
  118. Set .DbContainers(i).Database = Nothing
  119. End If
  120. TraceLog(TRACEANY, UCase(CalledSub) &amp; &quot; &quot; &amp; .URL &amp; Iif(i = 0, &quot;&quot;, &quot; Form=&quot; &amp; .DbContainers(i).FormName), False)
  121. Set .DbContainers(i) = Nothing
  122. Next i
  123. .DbContainers = Array()
  124. .URL = &quot;&quot;
  125. .DbConnect = 0
  126. .Active = False
  127. Set .Document = Nothing
  128. End With
  129. CurrentDoc(iCurrentDoc) = vDocContainer
  130. Exit_Sub:
  131. Exit Sub
  132. Error_Sub:
  133. TraceError(TRACEABORT, Err, CalledSub, Erl, False) &apos; No error message addressed to the user, only stored in console
  134. GoTo Exit_Sub
  135. End Sub &apos; CloseConnection
  136. REM -----------------------------------------------------------------------------------------------------------------------
  137. Public Function CurrentDb() As Object
  138. &apos; Returns _A2B_.CurrentDocument().Database as an object to allow access to its properties
  139. Dim iCurrentDoc As Integer
  140. Set CurrentDb = Nothing
  141. If Not IsArray(CurrentDoc) Then Goto Exit_Function
  142. If UBound(CurrentDoc) &lt; 0 Then Goto Exit_Function
  143. iCurrentDoc = CurrentDocIndex(, False) &apos; False = no abort
  144. If iCurrentDoc &gt;= 0 Then
  145. If UBound(CurrentDoc(iCurrentDoc).DbContainers) &gt;= 0 Then Set CurrentDb = CurrentDoc(iCurrentDoc).DbContainers(0).Database
  146. End If
  147. Exit_Function:
  148. Exit Function
  149. End Function &apos; CurrentDb
  150. REM -----------------------------------------------------------------------------------------------------------------------
  151. Public Function CurrentDocIndex(Optional pvURL As Variant, Optional pbAbort As Variant) As Integer
  152. &apos; Returns the entry in CurrentDoc(...) referring to the current document
  153. Dim i As Integer, bFound As Boolean, sURL As String
  154. Const cstBase = &quot;com.sun.star.comp.dba.ODatabaseDocument&quot;
  155. bFound = False
  156. CurrentDocIndex = -1
  157. If Not IsArray(CurrentDoc) Then Goto Trace_Error
  158. If UBound(CurrentDoc) &lt; 0 Then Goto Trace_Error
  159. For i = 1 To UBound(CurrentDoc) &apos; [0] reserved to database .odb document
  160. If IsMissing(pvURL) Then &apos; Not on 1 single line ?!?
  161. If Utils._hasUNOProperty(ThisComponent, &quot;URL&quot;) Then
  162. sURL = ThisComponent.URL
  163. Else
  164. Exit For &apos; f.i. ThisComponent = Basic IDE ...
  165. End If
  166. Else
  167. sURL = pvURL &apos; To support the SelectObject action
  168. End If
  169. If CurrentDoc(i).Active And CurrentDoc(i).URL = sURL Then
  170. CurrentDocIndex = i
  171. bFound = True
  172. Exit For
  173. End If
  174. Next i
  175. If Not bFound Then
  176. If IsNull(CurrentDoc(0)) Then GoTo Trace_Error
  177. With CurrentDoc(0)
  178. If Not .Active Then GoTo Trace_Error
  179. If IsNull(.Document) Then GoTo Trace_Error
  180. End With
  181. CurrentDocIndex = 0
  182. End If
  183. Exit_Function:
  184. Exit Function
  185. Trace_Error:
  186. If IsMissing(pbAbort) Then pbAbort = True
  187. If pbAbort Then TraceError(TRACEABORT, ERRDBNOTCONNECTED, Utils._CalledSub(), 0, 1) Else CurrentDocIndex = -1
  188. Goto Exit_Function
  189. End Function &apos; CurrentDocIndex
  190. REM -----------------------------------------------------------------------------------------------------------------------
  191. Public Function CurrentDocument(ByVal Optional piDocIndex As Integer) As Variant
  192. &apos; Returns the CurrentDoc(...) referring to the current document or to the argument
  193. Dim iDocIndex As Integer
  194. If IsMissing(piDocIndex) Then iDocIndex = CurrentDocIndex(, False) Else iDocIndex = piDocIndex
  195. If iDocIndex &gt;= 0 And iDocIndex &lt;= UBound(CurrentDoc) Then Set CurrentDocument = CurrentDoc(iDocIndex) Else Set CurrentDocument = Nothing
  196. End Function
  197. REM -----------------------------------------------------------------------------------------------------------------------
  198. Public Sub Dump()
  199. &apos; For debugging purposes
  200. Dim i As Integer, j As Integer, vCurrentDoc As Variant
  201. On Local Error Resume Next
  202. DebugPrint &quot;Version&quot;, VersionNumber
  203. DebugPrint &quot;TraceLevel&quot;, MinimalTraceLevel
  204. DebugPrint &quot;TraceCount&quot;, TraceLogCount
  205. DebugPrint &quot;CalledSub&quot;, CalledSub
  206. If IsArray(CurrentDoc) Then
  207. For i = 0 To UBound(CurrentDoc)
  208. vCurrentDoc = CurrentDoc(i)
  209. If Not IsNull(vCurrentDoc) Then
  210. DebugPrint i, &quot;URL&quot;, vCurrentDoc.URL
  211. For j = 0 To UBound(vCurrentDoc.DbContainers)
  212. DebugPrint i, j, &quot;Form&quot;, vCurrentDoc.DbContainers(j).FormName
  213. DebugPrint i, j, &quot;Database&quot;, vCurrentDoc.DbContainers(j).Database.Title
  214. Next j
  215. End If
  216. Next i
  217. End If
  218. End Sub
  219. REM -----------------------------------------------------------------------------------------------------------------------
  220. Public Function hasItem(psCollType As String, ByVal psName As String) As Boolean
  221. &apos; Return True if psName if in the collection
  222. Dim oItem As Object
  223. On Local Error Goto Error_Function &apos; Whatever ErrorHandler !
  224. hasItem = True
  225. Select Case psCollType
  226. Case COLLALLDIALOGS
  227. Set oItem = Dialogs.Item(UCase(psName))
  228. Case COLLTEMPVARS
  229. Set oItem = TempVars.Item(UCase(psName))
  230. Case Else
  231. hasItem = False
  232. End Select
  233. Exit_Function:
  234. Exit Function
  235. Error_Function: &apos; Item by key aborted
  236. hasItem = False
  237. GoTo Exit_Function
  238. End Function &apos; hasItem
  239. REM -----------------------------------------------------------------------------------------------------------------------
  240. REM --- PRIVATE FUNCTIONS ---
  241. REM -----------------------------------------------------------------------------------------------------------------------
  242. REM -----------------------------------------------------------------------------------------------------------------------
  243. Public Function _CurrentDb(ByVal Optional piDocEntry As Integer, ByVal Optional piDbEntry As Integer) As Variant
  244. REM Without arguments same as CurrentDb() except that it generates an error if database not connected (internal use)
  245. REM With 2 arguments return the corresponding entry in Root
  246. Dim odbDatabase As Variant
  247. If IsMissing(piDocEntry) Then
  248. Set odbDatabase = CurrentDb()
  249. Else
  250. If Not IsArray(CurrentDoc) Then Goto Trace_Error
  251. If piDocEntry &lt; 0 Or piDbEntry &lt; 0 Then Goto Trace_Error
  252. If piDocEntry &gt; UBound(CurrentDoc) Then Goto Trace_Error
  253. If piDbEntry &gt; UBound(CurrentDoc(piDocEntry).DbContainers) Then Goto Trace_Error
  254. Set odbDatabase = CurrentDoc(piDocEntry).DbContainers(piDbEntry).Database
  255. End If
  256. If IsNull(odbDatabase) Then GoTo Trace_Error
  257. Exit_Function:
  258. Set _CurrentDb = odbDatabase
  259. Exit Function
  260. Trace_Error:
  261. TraceError(TRACEABORT, ERRDBNOTCONNECTED, Utils._CalledSub(), 0, 1)
  262. Goto Exit_Function
  263. End Function &apos; _CurrentDb
  264. </script:module>