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 =======================================================================================================================
12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
13 ''' SF_Register
14 ''' ===========
15 ''' The ScriptForge framework includes
16 ''' the master ScriptForge library
17 ''' a number of
"associated
" libraries SF*
18 ''' any user/contributor extension wanting to fit into the framework
20 ''' The main methods in this module allow the current library to cling to ScriptForge
21 ''' - RegisterScriptServices
22 ''' Register the list of services implemented by the current library
23 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
25 REM ================================================================== EXCEPTIONS
27 Private Const BASEDOCUMENTOPENERROR =
"BASEDOCUMENTOPENERROR
"
28 Private Const DBCONNECTERROR =
"DBCONNECTERROR
"
30 REM ============================================================== PUBLIC METHODS
32 REM -----------------------------------------------------------------------------
33 Public Sub RegisterScriptServices() As Variant
34 ''' Register into ScriptForge the list of the services implemented by the current library
35 ''' Each library pertaining to the framework must implement its own version of this method
37 ''' It consists in successive calls to the RegisterService() and RegisterEventManager() methods
38 ''' with
2 arguments:
39 ''' ServiceName: the name of the service as a case-insensitive string
40 ''' ServiceReference: the reference as an object
41 ''' If the reference refers to a module, then return the module as an object:
42 ''' GlobalScope.Library.Module
43 ''' If the reference is a class instance, then return a string referring to the method
44 ''' containing the New statement creating the instance
45 ''' "libraryname.modulename.function
"
47 With GlobalScope.ScriptForge.SF_Services
48 .RegisterService(
"Database
",
"SFDatabases.SF_Register._NewDatabase
")
' Reference to the function initializing the service
49 .RegisterService(
"DatabaseFromDocument
",
"SFDatabases.SF_Register._NewDatabaseFromSource
")
50 .RegisterService(
"Datasheet
",
"SFDatabases.SF_Register._NewDatasheet
")
53 End Sub
' SFDatabases.SF_Register.RegisterScriptServices
55 REM =========================================================== PRIVATE FUNCTIONS
57 REM -----------------------------------------------------------------------------
58 Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object
59 ''' Create a new instance of the SF_Database class
60 ''' Args:
61 ''' FileName : the name of the file (compliant with the SF_FileSystem.FileNaming notation)
62 ''' RegistrationName: mutually exclusive with FileName. Used when database is registered
63 ''' ReadOnly : (boolean). Default = True
64 ''' User : connection parameters
65 ''' Password
66 ''' Returns:
67 ''' The instance or Nothing
68 ''' Exceptions:
69 ''' BASEDOCUMENTOPENERROR The database file could not be opened
70 ''' DBCONNECTERROR The database could not be connected, credentials are probably wrong
72 Dim oDatabase As Object
' Return value
73 Dim vFileName As Variant
' alias of pvArgs(
0)
74 Dim vRegistration As Variant
' Alias of pvArgs(
1)
75 Dim vReadOnly As Variant
' Alias of pvArgs(
2)
76 Dim vUser As Variant
' Alias of pvArgs(
3)
77 Dim vPassword As Variant
' Alias of pvArgs(
4)
78 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
79 Const cstService =
"SFDatabases.Database
"
80 Const cstGlobal =
"GlobalScope
"
82 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
85 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
86 If UBound(pvArgs)
>=
0 Then vFileName = pvArgs(
0) Else vFileName =
""
87 If IsEmpty(vFileName) Then vFileName =
""
88 If UBound(pvArgs)
>=
1 Then vRegistration = pvArgs(
1) Else vRegistration =
""
89 If IsEmpty(vRegistration) Then vRegistration =
""
90 If UBound(pvArgs)
>=
2 Then vReadOnly = pvArgs(
2) Else vReadOnly = True
91 If IsEmpty(vReadOnly) Then vReadOnly = True
92 If UBound(pvArgs)
>=
3 Then vUser = pvArgs(
3) Else vUser =
""
93 If IsEmpty(vUser) Then vUser =
""
94 If UBound(pvArgs)
>=
4 Then vPassword = pvArgs(
4) Else vPassword =
""
95 If IsEmpty(vPassword) Then vPassword =
""
96 If Not ScriptForge.SF_Utils._Validate(vFileName,
"FileName
", V_STRING) Then GoTo Finally
97 If Not ScriptForge.SF_Utils._Validate(vRegistration,
"RegistrationName
", V_STRING) Then GoTo Finally
98 If Not ScriptForge.SF_Utils._Validate(vReadOnly,
"ReadOnly
", ScriptForge.V_BOOLEAN) Then GoTo Finally
99 If Not ScriptForge.SF_Utils._Validate(vUser,
"User
", V_STRING) Then GoTo Finally
100 If Not ScriptForge.SF_Utils._Validate(vPassword,
"Password
", V_STRING) Then GoTo Finally
101 Set oDatabase = Nothing
103 ' Check the existence of FileName
105 Set oDBContext = .SF_Utils._GetUNOService(
"DatabaseContext
")
106 If Len(vFileName) =
0 Then
' FileName has precedence over RegistrationName
107 If Len(vRegistration) =
0 Then GoTo CatchError
108 If Not oDBContext.hasRegisteredDatabase(vRegistration) Then GoTo CatchError
109 vFileName = .SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(vRegistration))
111 If Not .SF_FileSystem.FileExists(vFileName) Then GoTo CatchError
115 ' Create the database Basic object and initialize attributes
116 Set oDatabase = New SF_Database
118 Set .[Me] = oDatabase
119 ._Location = ConvertToUrl(vFileName)
120 Set ._DataSource = oDBContext.getByName(._Location)
121 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchConnect
122 Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
123 If IsNull(._Connection) Then GoTo CatchConnect
125 ._Password = vPassword
126 ._ReadOnly = vReadOnly
127 Set ._MetaData = ._Connection.MetaData
128 ._URL = ._MetaData.URL
132 Set _NewDatabase = oDatabase
137 ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR,
"FileName
", vFileName,
"RegistrationName
", vRegistration)
140 ScriptForge.SF_Exception.RaiseFatal(DBCONNECTERROR,
"User
", vUser,
"Password
", vPassword, vFileName)
142 End Function
' SFDatabases.SF_Register._NewDatabase
144 REM -----------------------------------------------------------------------------
145 Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object
146 ' ByRef oDataSource As Object _
147 ' , ByVal sUser As String _
148 ' , ByVal sPassword As String _
150 ''' Create a new instance of the SF_Database class from the given datasource
151 ''' established in the SFDocuments.Base service
152 ''' THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
153 ''' Args:
154 ''' oDataSource: com.sun.star.sdbc.XDataSource
155 ''' sUser, sPassword : connection parameters
156 ''' Returns:
157 ''' The instance or Nothing
158 ''' Exceptions:
159 ''' managed in the calling routines when Nothing is returned
161 Dim oDatabase As Object
' Return value
162 Dim oConnection As Object
' com.sun.star.sdbc.XConnection
163 Dim oDataSource As Object
' Alias of pvArgs(
0)
164 Dim sUser As String
' Alias of pvArgs(
1)
165 Dim sPassword As String
' Alias of pvArgs(
2)
167 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
168 Set oDatabase = Nothing
172 Set oDataSource = pvArgs(
0)
174 sPassword = pvArgs(
2)
176 ' Setup the connection
177 If oDataSource.IsPasswordRequired Then
178 Set oConnection = oDataSource.getConnection(sUser, sPassword)
180 Set oConnection = oDataSource.getConnection(
"",
"")
183 ' Create the database Basic object and initialize attributes
184 If Not IsNull(oConnection) Then
185 Set oDatabase = New SF_Database
187 Set .[Me] = oDatabase
188 ._Location =
""
189 Set ._DataSource = oDataSource
190 Set ._Connection = oConnection
191 ._ReadOnly = oConnection.isReadOnly()
192 Set ._MetaData = oConnection.MetaData
193 ._URL = ._MetaData.URL
198 Set _NewDatabaseFromSource = oDatabase
201 ScriptForge.SF_Exception.Clear()
203 End Function
' SFDatabases.SF_Register._NewDatabaseFromSource
205 REM -----------------------------------------------------------------------------
206 Public Function _NewDatasheet(Optional ByVal pvArgs As Variant) As Object
207 ' Optional ByRef poComponent As Object _
208 ' , Optional ByRef poParent As Object _
210 ''' Create a new instance of the SF_Datasheet class
211 ''' Called from
212 ''' base.Datasheets()
213 ''' base.OpenTable()
214 ''' base.OpenQuery()
215 ''' database.OpenTable()
216 ''' database.OpenQuery()
217 ''' database.OpenSql()
218 ''' Args:
219 ''' Component: the component of the new datasheet
220 ''' com.sun.star.lang.XComponent - org.openoffice.comp.dbu.ODatasourceBrowser
221 ''' Parent: the parent SF_Database or SF_Base instance having produced the new datasheet
222 ''' When absent, the SF_Database instance will be derived from the component
223 ''' Returns:
224 ''' The instance or Nothing
226 Dim oDatasheet As Object
' Return value
227 Dim oParent As Object
' The parent SF_Database or SF_Base instance having produced the new datasheet
228 Dim oComponent As Object
' The component of the new datasheet
229 Dim oWindow As Object
' ui.Window user-defined type
230 Dim oUi As Object : Set oUi = ScriptForge.SF_Services.CreateScriptService(
"ScriptForge.UI
")
232 Const TABLEDATA =
"TableData
"
233 Const QUERYDATA =
"QueryData
"
234 Const SQLDATA =
"SqlData
"
236 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
237 Set oDatasheet = Nothing
240 ' Get, check and assign arguments
241 If Not IsArray(pvArgs) Then GoTo Catch
242 If UBound(pvArgs)
>=
0 Then
243 Set oComponent = pvArgs(
0)
245 If UBound(pvArgs) =
0 Then
246 Set oParent = Nothing
247 ElseIf UBound(pvArgs) =
1 Then
248 Set oParent = pvArgs(
1)
253 ' Check the validity of the proposed window: is it really a datasheet ? Otherwise, do nothing
254 If IsNull(oComponent) Then GoTo Catch
255 Set oWindow = oUi._IdentifyWindow(oComponent)
257 If .DocumentType
<> TABLEDATA And .DocumentType
<> QUERYDATA And .DocumentType
<> SQLDATA Then GoTo Catch
259 If IsEmpty(oComponent.Selection) Then GoTo Catch
262 Set oDatasheet = New SF_Datasheet
264 Set .[Me] = oDatasheet
265 Set .[_Parent] = oParent
266 Set ._Component = oComponent
267 ' Achieve the initialization
272 Set _NewDatasheet = oDatasheet
275 Set oDatasheet = Nothing
277 End Function
' SFDatabases.SF_Register._NewDatasheet
279 REM ============================================== END OF SFDATABASES.SF_REGISTER