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">REM =======================================================================================================================
4 REM === The Access2Base library is a part of the LibreOffice project. ===
5 REM === Full documentation is available on http://www.access2base.com ===
6 REM =======================================================================================================================
13 REM -----------------------------------------------------------------------------------------------------------------------
14 REM --- FOR INTERNAL USE ONLY ---
15 REM -----------------------------------------------------------------------------------------------------------------------
17 REM -----------------------------------------------------------------------------------------------------------------------
18 REM --- CLASS ROOT FIELDS ---
19 REM -----------------------------------------------------------------------------------------------------------------------
21 Private ErrorHandler As Boolean
22 Private MinimalTraceLevel As Integer
23 Private TraceLogs() As Variant
24 Private TraceLogCount As Integer
25 Private TraceLogLast As Integer
26 Private TraceLogMaxEntries As Integer
27 Private CalledSub As String
28 Private Introspection As Object
' com.sun.star.beans.Introspection
29 Private VersionNumber As String
' Actual Access2Base version number
30 Private FindRecord As Object
31 Private StatusBar As Object
32 Private Dialogs As Object
' Collection
33 Private TempVars As Object
' Collection
34 Private CurrentDoc() As Variant
' Array of document containers - [
0] = Base document, [
1 ... N] = other documents
37 Document As Object
' com.sun.star.comp.dba.ODatabaseDocument or SwXTextDocument or ScModelObj
39 DbConnect As Integer
' DBCONNECTxxx constants
41 DbContainers() As Variant
' One entry by (data-aware) form
45 FormName As String
' name of data-aware form
46 Database As Object
' Database type
49 REM -----------------------------------------------------------------------------------------------------------------------
50 REM --- CONSTRUCTORS / DESTRUCTORS ---
51 REM -----------------------------------------------------------------------------------------------------------------------
52 Private Sub Class_Initialize()
53 Dim vCurrentDoc() As Variant
54 VersionNumber = Access2Base_Version
60 TraceLogMaxEntries =
0
61 CalledSub =
""
62 Set Introspection = CreateUnoService(
"com.sun.star.beans.Introspection
")
63 Set FindRecord = Nothing
64 Set StatusBar = Nothing
65 Set Dialogs = New Collection
66 Set TempVars = New Collection
67 vCurrentDoc() = Array()
68 ReDim vCurrentDoc(
0 To
0)
69 Set vCurrentDoc(
0) = Nothing
70 Set CurrentDoc() = vCurrentDoc()
71 End Sub
' Constructor
73 REM -----------------------------------------------------------------------------------------------------------------------
74 Private Sub Class_Terminate()
75 Call Class_Initialize()
76 End Sub
' Destructor
78 REM -----------------------------------------------------------------------------------------------------------------------
80 Call Class_Terminate()
81 End Sub
' Explicit destructor
83 REM -----------------------------------------------------------------------------------------------------------------------
84 REM --- CLASS GET/LET/SET PROPERTIES ---
85 REM -----------------------------------------------------------------------------------------------------------------------
87 REM -----------------------------------------------------------------------------------------------------------------------
88 REM --- CLASS METHODS ---
89 REM -----------------------------------------------------------------------------------------------------------------------
91 REM -----------------------------------------------------------------------------------------------------------------------
92 Public Sub CloseConnection()
93 ' Close all connections established by current document to free memory.
94 ' - if Base document =
> close the one concerned database connection
95 ' - if non-Base documents =
> close the connections of each individual standalone form
97 Dim i As Integer, iCurrentDoc As Integer
98 Dim vDbContainer As Variant, vDbContainers() As Variant, vDocContainer As Variant
100 If ErrorHandler Then On Local Error Goto Error_Sub
102 If Not IsArray(CurrentDoc) Then Goto Exit_Sub
103 If UBound(CurrentDoc)
< 0 Then Goto Exit_Sub
104 iCurrentDoc = CurrentDocIndex( , False)
' False prevents error raising if not found
105 If iCurrentDoc
< 0 Then GoTo Exit_Sub
' If not found ignore
107 vDocContainer = CurrentDocument(iCurrentDoc)
109 If Not .Active Then GoTo Exit_Sub
' e.g. if successive calls to CloseConnection()
110 For i =
0 To UBound(.DbContainers)
111 If Not IsNull(.DbContainers(i).Database) Then
112 .DbContainers(i).Database.Dispose()
113 Set .DbContainers(i).Database = Nothing
115 TraceLog(TRACEANY, UCase(CalledSub)
& " " & .URL
& Iif(i =
0,
"",
" Form=
" & .DbContainers(i).FormName), False)
116 Set .DbContainers(i) = Nothing
118 .DbContainers = Array()
122 Set .Document = Nothing
124 CurrentDoc(iCurrentDoc) = vDocContainer
129 TraceError(TRACEABORT, Err, CalledSub, Erl, False)
' No error message addressed to the user, only stored in console
131 End Sub
' CloseConnection
133 REM -----------------------------------------------------------------------------------------------------------------------
134 Public Function CurrentDb() As Object
135 ' Returns _A2B_.CurrentDocument().Database as an object to allow access to its properties
137 Dim iCurrentDoc As Integer
139 Set CurrentDb = Nothing
141 If Not IsArray(CurrentDoc) Then Goto Exit_Function
142 If UBound(CurrentDoc)
< 0 Then Goto Exit_Function
143 iCurrentDoc = CurrentDocIndex(, False)
' False = no abort
144 If iCurrentDoc
>=
0 Then
145 If UBound(CurrentDoc(iCurrentDoc).DbContainers)
>=
0 Then Set CurrentDb = CurrentDoc(iCurrentDoc).DbContainers(
0).Database
150 End Function
' CurrentDb
152 REM -----------------------------------------------------------------------------------------------------------------------
153 Public Function CurrentDocIndex(Optional pvURL As Variant, Optional pbAbort As Variant) As Integer
154 ' Returns the entry in CurrentDoc(...) referring to the current document
156 Dim i As Integer, bFound As Boolean, sURL As String
157 Const cstBase =
"com.sun.star.comp.dba.ODatabaseDocument
"
162 If Not IsArray(CurrentDoc) Then Goto Trace_Error
163 If UBound(CurrentDoc)
< 0 Then Goto Trace_Error
164 For i =
1 To UBound(CurrentDoc)
' [
0] reserved to database .odb document
165 If IsMissing(pvURL) Then
' Not on
1 single line ?!?
166 If Utils._hasUNOProperty(ThisComponent,
"URL
") Then
167 sURL = ThisComponent.URL
169 Exit For
' f.i. ThisComponent = Basic IDE ...
172 sURL = pvURL
' To support the SelectObject action
174 If CurrentDoc(i).Active And CurrentDoc(i).URL = sURL Then
182 If IsNull(CurrentDoc(
0)) Then GoTo Trace_Error
184 If Not .Active Then GoTo Trace_Error
185 If IsNull(.Document) Then GoTo Trace_Error
193 If IsMissing(pbAbort) Then pbAbort = True
194 If pbAbort Then TraceError(TRACEABORT, ERRDBNOTCONNECTED, Utils._CalledSub(),
0,
1) Else CurrentDocIndex = -
1
196 End Function
' CurrentDocIndex
198 REM -----------------------------------------------------------------------------------------------------------------------
199 Public Function CurrentDocument(ByVal Optional piDocIndex As Integer) As Variant
200 ' Returns the CurrentDoc(...) referring to the current document or to the argument
202 Dim iDocIndex As Integer
203 If IsMissing(piDocIndex) Then iDocIndex = CurrentDocIndex() Else iDocIndex = piDocIndex
204 If iDocIndex
>=
0 And iDocIndex
<= UBound(CurrentDoc) Then Set CurrentDocument = CurrentDoc(iDocIndex) Else Set CurrentDocument = Nothing
208 REM -----------------------------------------------------------------------------------------------------------------------
210 ' For debugging purposes
211 Dim i As Integer, j As Integer, vCurrentDoc As Variant
212 On Local Error Resume Next
214 DebugPrint
"Version
", VersionNumber
215 DebugPrint
"TraceLevel
", MinimalTraceLevel
216 DebugPrint
"TraceCount
", TraceLogCount
217 DebugPrint
"CalledSub
", CalledSub
218 If IsArray(CurrentDoc) Then
219 For i =
0 To UBound(CurrentDoc)
220 vCurrentDoc = CurrentDoc(i)
221 If Not IsNull(vCurrentDoc) Then
222 DebugPrint i,
"URL
", vCurrentDoc.URL
223 For j =
0 To UBound(vCurrentDoc.DbContainers)
224 DebugPrint i, j,
"Form
", vCurrentDoc.DbContainers(j).FormName
225 DebugPrint i, j,
"Database
", vCurrentDoc.DbContainers(j).Database.Title
233 REM -----------------------------------------------------------------------------------------------------------------------
234 Public Function hasItem(psCollType As String, ByVal psName As String) As Boolean
235 ' Return True if psName if in the collection
238 On Local Error Goto Error_Function
' Whatever ErrorHandler !
241 Select Case psCollType
243 Set oItem = Dialogs.Item(UCase(psName))
245 Set oItem = TempVars.Item(UCase(psName))
252 Error_Function:
' Item by key aborted
255 End Function
' hasItem
257 REM -----------------------------------------------------------------------------------------------------------------------
258 REM --- PRIVATE FUNCTIONS ---
259 REM -----------------------------------------------------------------------------------------------------------------------
261 REM -----------------------------------------------------------------------------------------------------------------------
262 Public Function _CurrentDb(ByVal Optional piDocEntry As Integer, ByVal Optional piDbEntry As Integer) As Variant
263 REM Without arguments same as CurrentDb() except that it generates an error if database not connected (internal use)
264 REM With
2 arguments return the corresponding entry in Root
266 Dim odbDatabase As Variant
267 If IsMissing(piDocEntry) Then
268 Set odbDatabase = CurrentDb()
270 If Not IsArray(CurrentDoc) Then Goto Trace_Error
271 If piDocEntry
< 0 Or piDbEntry
< 0 Then Goto Trace_Error
272 If piDocEntry
> UBound(CurrentDoc) Then Goto Trace_Error
273 If piDbEntry
> UBound(CurrentDoc(piDocEntry).DbContainers) Then Goto Trace_Error
274 Set odbDatabase = CurrentDoc(piDocEntry).DbContainers(piDbEntry).Database
276 If IsNull(odbDatabase) Then GoTo Trace_Error
279 Set _CurrentDb = odbDatabase
282 TraceError(TRACEABORT, ERRDBNOTCONNECTED, Utils._CalledSub(),
0,
1)
284 End Function
' _CurrentDb