Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / wizards / source / sfdatabases / SF_Register.xba
blob04c76ff59e949fa63556b30a88c213950496ee11
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="SF_Register" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFDatabases library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
9 Option Compatible
10 Option Explicit
12 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
13 &apos;&apos;&apos; SF_Register
14 &apos;&apos;&apos; ===========
15 &apos;&apos;&apos; The ScriptForge framework includes
16 &apos;&apos;&apos; the master ScriptForge library
17 &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
18 &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
19 &apos;&apos;&apos;
20 &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
21 &apos;&apos;&apos; - RegisterScriptServices
22 &apos;&apos;&apos; Register the list of services implemented by the current library
23 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
25 REM ================================================================== EXCEPTIONS
27 Private Const BASEDOCUMENTOPENERROR = &quot;BASEDOCUMENTOPENERROR&quot;
29 REM ============================================================== PUBLIC METHODS
31 REM -----------------------------------------------------------------------------
32 Public Sub RegisterScriptServices() As Variant
33 &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
34 &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
35 &apos;&apos;&apos;
36 &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
37 &apos;&apos;&apos; with 2 arguments:
38 &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
39 &apos;&apos;&apos; ServiceReference: the reference as an object
40 &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
41 &apos;&apos;&apos; GlobalScope.Library.Module
42 &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
43 &apos;&apos;&apos; containing the New statement creating the instance
44 &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
46 With GlobalScope.ScriptForge.SF_Services
47 .RegisterService(&quot;Database&quot;, &quot;SFDatabases.SF_Register._NewDatabase&quot;) &apos; Reference to the function initializing the service
48 .RegisterService(&quot;DatabaseFromDocument&quot;, &quot;SFDatabases.SF_Register._NewDatabaseFromSource&quot;)
49 .RegisterService(&quot;Datasheet&quot;, &quot;SFDatabases.SF_Register._NewDatasheet&quot;)
50 End With
52 End Sub &apos; SFDatabases.SF_Register.RegisterScriptServices
54 REM =========================================================== PRIVATE FUNCTIONS
56 REM -----------------------------------------------------------------------------
57 Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object
58 &apos;&apos;&apos; Create a new instance of the SF_Database class
59 &apos;&apos;&apos; Args:
60 &apos;&apos;&apos; FileName : the name of the file (compliant with the SF_FileSystem.FileNaming notation)
61 &apos;&apos;&apos; RegistrationName: mutually exclusive with FileName. Used when database is registered
62 &apos;&apos;&apos; ReadOnly : (boolean). Default = True
63 &apos;&apos;&apos; User : connection parameters
64 &apos;&apos;&apos; Password
65 &apos;&apos;&apos; Returns:
66 &apos;&apos;&apos; The instance or Nothing
67 &apos;&apos;&apos; Exceptions:
68 &apos;&apos;&apos; BASEDOCUMENTOPENERROR The database file could not be opened or connected
70 Dim oDatabase As Object &apos; Return value
71 Dim vFileName As Variant &apos; alias of pvArgs(0)
72 Dim vRegistration As Variant &apos; Alias of pvArgs(1)
73 Dim vReadOnly As Variant &apos; Alias of pvArgs(2)
74 Dim vUser As Variant &apos; Alias of pvArgs(3)
75 Dim vPassword As Variant &apos; Alias of pvArgs(4)
76 Dim oDBContext As Object &apos; com.sun.star.sdb.DatabaseContext
77 Const cstService = &quot;SFDatabases.Database&quot;
78 Const cstGlobal = &quot;GlobalScope&quot;
80 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
82 Check:
83 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
84 If UBound(pvArgs) &gt;= 0 Then vFileName = pvArgs(0) Else vFileName = &quot;&quot;
85 If IsEmpty(vFileName) Then vFileName = &quot;&quot;
86 If UBound(pvArgs) &gt;= 1 Then vRegistration = pvArgs(1) Else vRegistration = &quot;&quot;
87 If IsEmpty(vRegistration) Then vRegistration = &quot;&quot;
88 If UBound(pvArgs) &gt;= 2 Then vReadOnly = pvArgs(2) Else vReadOnly = True
89 If IsEmpty(vReadOnly) Then vReadOnly = True
90 If UBound(pvArgs) &gt;= 3 Then vUser = pvArgs(3) Else vUser = &quot;&quot;
91 If IsEmpty(vUser) Then vUser = &quot;&quot;
92 If UBound(pvArgs) &gt;= 4 Then vPassword = pvArgs(4) Else vPassword = &quot;&quot;
93 If IsEmpty(vPassword) Then vPassword = &quot;&quot;
94 If Not ScriptForge.SF_Utils._Validate(vFileName, &quot;FileName&quot;, V_STRING) Then GoTo Finally
95 If Not ScriptForge.SF_Utils._Validate(vRegistration, &quot;RegistrationName&quot;, V_STRING) Then GoTo Finally
96 If Not ScriptForge.SF_Utils._Validate(vReadOnly, &quot;ReadOnly&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
97 If Not ScriptForge.SF_Utils._Validate(vUser, &quot;User&quot;, V_STRING) Then GoTo Finally
98 If Not ScriptForge.SF_Utils._Validate(vPassword, &quot;Password&quot;, V_STRING) Then GoTo Finally
99 Set oDatabase = Nothing
101 &apos; Check the existence of FileName
102 With ScriptForge
103 Set oDBContext = .SF_Utils._GetUNOService(&quot;DatabaseContext&quot;)
104 If Len(vFileName) = 0 Then &apos; FileName has precedence over RegistrationName
105 If Len(vRegistration) = 0 Then GoTo CatchError
106 If Not oDBContext.hasRegisteredDatabase(vRegistration) Then GoTo CatchError
107 vFileName = .SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(vRegistration))
108 End If
109 If Not .SF_FileSystem.FileExists(vFileName) Then GoTo CatchError
110 End With
112 Try:
113 &apos; Create the database Basic object and initialize attributes
114 Set oDatabase = New SF_Database
115 With oDatabase
116 Set .[Me] = oDatabase
117 ._Location = ConvertToUrl(vFileName)
118 Set ._DataSource = oDBContext.getByName(._Location)
119 Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
120 ._ReadOnly = vReadOnly
121 Set ._MetaData = ._Connection.MetaData
122 ._URL = ._MetaData.URL
123 End With
125 Finally:
126 Set _NewDatabase = oDatabase
127 Exit Function
128 Catch:
129 GoTo Finally
130 CatchError:
131 ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, &quot;FileName&quot;, vFileName, &quot;RegistrationName&quot;, vRegistration)
132 GoTo Finally
133 End Function &apos; SFDatabases.SF_Register._NewDatabase
135 REM -----------------------------------------------------------------------------
136 Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object
137 &apos; ByRef poDataSource As Object _
138 &apos; , ByVal psUser As String _
139 &apos; , ByVal psPassword As String _
140 &apos; ) As Object
141 &apos;&apos;&apos; Create a new instance of the SF_Database class from the given datasource
142 &apos;&apos;&apos; established in the SFDocuments.Base service
143 &apos;&apos;&apos; THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
144 &apos;&apos;&apos; Args:
145 &apos;&apos;&apos; DataSource: com.sun.star.sdbc.XDataSource
146 &apos;&apos;&apos; User, Password : connection parameters
147 &apos;&apos;&apos; Returns:
148 &apos;&apos;&apos; The instance or Nothing
149 &apos;&apos;&apos; Exceptions:
150 &apos;&apos;&apos; managed in the calling routines when Nothing is returned
152 Dim oDatabase As Object &apos; Return value
153 Dim oConnection As Object &apos; com.sun.star.sdbc.XConnection
154 Dim oDataSource As Object &apos; Alias of pvArgs(0)
155 Dim sUser As String &apos; Alias of pvArgs(1)
156 Dim sPassword As String &apos; Alias of pvArgs(2)
158 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
159 Set oDatabase = Nothing
161 Try:
162 &apos; Get arguments
163 Set oDataSource = pvArgs(0)
164 sUser = pvArgs(1)
165 sPassword = pvArgs(2)
167 &apos; Setup the connection
168 If oDataSource.IsPasswordRequired Then
169 Set oConnection = oDataSource.getConnection(sUser, sPassword)
170 Else
171 Set oConnection = oDataSource.getConnection(&quot;&quot;, &quot;&quot;)
172 End If
174 &apos; Create the database Basic object and initialize attributes
175 If Not IsNull(oConnection) Then
176 Set oDatabase = New SF_Database
177 With oDatabase
178 Set .[Me] = oDatabase
179 ._Location = &quot;&quot;
180 Set ._DataSource = oDataSource
181 Set ._Connection = oConnection
182 ._ReadOnly = oConnection.isReadOnly()
183 Set ._MetaData = oConnection.MetaData
184 ._URL = ._MetaData.URL
185 End With
186 End If
188 Finally:
189 Set _NewDatabaseFromSource = oDatabase
190 Exit Function
191 Catch:
192 GoTo Finally
193 End Function &apos; SFDatabases.SF_Register._NewDatabaseFromSource
195 REM -----------------------------------------------------------------------------
196 Public Function _NewDatasheet(Optional ByVal pvArgs As Variant) As Object
197 &apos; Optional ByRef poComponent As Object _
198 &apos; , Optional ByRef poParent As Object _
199 &apos; ) As Object
200 &apos;&apos;&apos; Create a new instance of the SF_Datasheet class
201 &apos;&apos;&apos; Called from
202 &apos;&apos;&apos; base.Datasheets()
203 &apos;&apos;&apos; base.OpenTable()
204 &apos;&apos;&apos; base.OpenQuery()
205 &apos;&apos;&apos; database.OpenTable()
206 &apos;&apos;&apos; database.OpenQuery()
207 &apos;&apos;&apos; database.OpenSql()
208 &apos;&apos;&apos; Args:
209 &apos;&apos;&apos; Component: the component of the new datasheet
210 &apos;&apos;&apos; com.sun.star.lang.XComponent - org.openoffice.comp.dbu.ODatasourceBrowser
211 &apos;&apos;&apos; Parent: the parent SF_Database or SF_Base instance having produced the new datasheet
212 &apos;&apos;&apos; When absent, the SF_Database instance will be derived from the component
213 &apos;&apos;&apos; Returns:
214 &apos;&apos;&apos; The instance or Nothing
216 Dim oDatasheet As Object &apos; Return value
217 Dim oParent As Object &apos; The parent SF_Database or SF_Base instance having produced the new datasheet
218 Dim oComponent As Object &apos; The component of the new datasheet
219 Dim oWindow As Object &apos; ui.Window user-defined type
220 Dim oUi As Object : Set oUi = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.UI&quot;)
222 Const TABLEDATA = &quot;TableData&quot;
223 Const QUERYDATA = &quot;QueryData&quot;
224 Const SQLDATA = &quot;SqlData&quot;
226 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
227 Set oDatasheet = Nothing
229 Check:
230 &apos; Get, check and assign arguments
231 If Not IsArray(pvArgs) Then GoTo Catch
232 If UBound(pvArgs) &gt;= 0 Then
233 Set oComponent = pvArgs(0)
234 End If
235 If UBound(pvArgs) = 0 Then
236 Set oParent = Nothing
237 ElseIf UBound(pvArgs) = 1 Then
238 Set oParent = pvArgs(1)
239 Else
240 GoTo Catch
241 End If
243 &apos; Check the validity of the proposed window: is it really a datasheet ? Otherwise, do nothing
244 If IsNull(oComponent) Then GoTo Catch
245 Set oWindow = oUi._IdentifyWindow(oComponent)
246 With oWindow
247 If .DocumentType &lt;&gt; TABLEDATA And .DocumentType &lt;&gt; QUERYDATA And .DocumentType &lt;&gt; SQLDATA Then GoTo Catch
248 End With
249 If IsEmpty(oComponent.Selection) Then GoTo Catch
251 Try:
252 Set oDatasheet = New SF_Datasheet
253 With oDatasheet
254 Set .[Me] = oDatasheet
255 Set .[_Parent] = oParent
256 Set ._Component = oComponent
257 &apos; Achieve the initialization
258 ._Initialize()
259 End With
261 Finally:
262 Set _NewDatasheet = oDatasheet
263 Exit Function
264 Catch:
265 Set oDatasheet = Nothing
266 GoTo Finally
267 End Function &apos; SFDatabases.SF_Register._NewDatasheet
269 REM ============================================== END OF SFDATABASES.SF_REGISTER
270 </script:module>