SF_Register.xba 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 =======================================================================================================================
  8. Option Compatible
  9. Option Explicit
  10. &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;
  11. &apos;&apos;&apos; SF_Register
  12. &apos;&apos;&apos; ===========
  13. &apos;&apos;&apos; The ScriptForge framework includes
  14. &apos;&apos;&apos; the master ScriptForge library
  15. &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
  16. &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
  17. &apos;&apos;&apos;
  18. &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
  19. &apos;&apos;&apos; - RegisterScriptServices
  20. &apos;&apos;&apos; Register the list of services implemented by the current library
  21. &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;
  22. REM ================================================================== EXCEPTIONS
  23. Private Const BASEDOCUMENTOPENERROR = &quot;BASEDOCUMENTOPENERROR&quot;
  24. REM ============================================================== PUBLIC METHODS
  25. REM -----------------------------------------------------------------------------
  26. Public Sub RegisterScriptServices() As Variant
  27. &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
  28. &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
  29. &apos;&apos;&apos;
  30. &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
  31. &apos;&apos;&apos; with 2 arguments:
  32. &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
  33. &apos;&apos;&apos; ServiceReference: the reference as an object
  34. &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
  35. &apos;&apos;&apos; GlobalScope.Library.Module
  36. &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
  37. &apos;&apos;&apos; containing the New statement creating the instance
  38. &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
  39. With GlobalScope.ScriptForge.SF_Services
  40. .RegisterService(&quot;Database&quot;, &quot;SFDatabases.SF_Register._NewDatabase&quot;) &apos; Reference to the function initializing the service
  41. .RegisterService(&quot;DatabaseFromDocument&quot;, &quot;SFDatabases.SF_Register._NewDatabaseFromSource&quot;)
  42. End With
  43. End Sub &apos; SFDatabases.SF_Register.RegisterScriptServices
  44. REM =========================================================== PRIVATE FUNCTIONS
  45. REM -----------------------------------------------------------------------------
  46. Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object
  47. &apos;&apos;&apos; Create a new instance of the SF_Database class
  48. &apos; Args:
  49. &apos;&apos;&apos; FileName : the name of the file (compliant with the SF_FileSystem.FileNaming notation)
  50. &apos;&apos;&apos; RegistrationName: mutually exclusive with FileName. Used when database is registered
  51. &apos;&apos;&apos; ReadOnly : (boolean). Default = True
  52. &apos;&apos;&apos; User : connection parameters
  53. &apos;&apos;&apos; Password
  54. &apos;&apos;&apos; Returns:
  55. &apos;&apos;&apos; The instance or Nothing
  56. &apos;&apos;&apos; Exceptions:
  57. &apos;&apos;&apos; BASEDOCUMENTOPENERROR The database file could not be opened or connected
  58. Dim oDatabase As Object &apos; Return value
  59. Dim vFileName As Variant &apos; alias of pvArgs(0)
  60. Dim vRegistration As Variant &apos; Alias of pvArgs(1)
  61. Dim vReadOnly As Variant &apos; Alias of pvArgs(2)
  62. Dim vUser As Variant &apos; Alias of pvArgs(3)
  63. Dim vPassword As Variant &apos; Alias of pvArgs(4)
  64. Dim oDBContext As Object &apos; com.sun.star.sdb.DatabaseContext
  65. Const cstService = &quot;SFDatabases.Database&quot;
  66. Const cstGlobal = &quot;GlobalScope&quot;
  67. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  68. Check:
  69. If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
  70. If UBound(pvArgs) &gt;= 0 Then vFileName = pvArgs(0) Else vFileName = &quot;&quot;
  71. If IsEmpty(vFileName) Then vFileName = &quot;&quot;
  72. If UBound(pvArgs) &gt;= 1 Then vRegistration = pvArgs(1) Else vRegistration = &quot;&quot;
  73. If IsEmpty(vRegistration) Then vRegistration = &quot;&quot;
  74. If UBound(pvArgs) &gt;= 2 Then vReadOnly = pvArgs(2) Else vReadOnly = True
  75. If IsEmpty(vReadOnly) Then vReadOnly = True
  76. If UBound(pvArgs) &gt;= 3 Then vUser = pvArgs(3) Else vUser = &quot;&quot;
  77. If IsEmpty(vUser) Then vUser = &quot;&quot;
  78. If UBound(pvArgs) &gt;= 4 Then vPassword = pvArgs(4) Else vPassword = &quot;&quot;
  79. If IsEmpty(vPassword) Then vPassword = &quot;&quot;
  80. If Not ScriptForge.SF_Utils._Validate(vFileName, &quot;FileName&quot;, V_STRING) Then GoTo Finally
  81. If Not ScriptForge.SF_Utils._Validate(vRegistration, &quot;RegistrationName&quot;, V_STRING) Then GoTo Finally
  82. If Not ScriptForge.SF_Utils._Validate(vReadOnly, &quot;ReadOnly&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
  83. If Not ScriptForge.SF_Utils._Validate(vUser, &quot;User&quot;, V_STRING) Then GoTo Finally
  84. If Not ScriptForge.SF_Utils._Validate(vPassword, &quot;Password&quot;, V_STRING) Then GoTo Finally
  85. Set oDatabase = Nothing
  86. &apos; Check the existence of FileName
  87. With ScriptForge
  88. Set oDBContext = .SF_Utils._GetUNOService(&quot;DatabaseContext&quot;)
  89. If Len(vFileName) = 0 Then &apos; FileName has precedence over RegistrationName
  90. If Len(vRegistration) = 0 Then GoTo CatchError
  91. If Not oDBContext.hasRegisteredDatabase(vRegistration) Then GoTo CatchError
  92. vFileName = .SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(vRegistration))
  93. End If
  94. If Not .SF_FileSystem.FileExists(vFileName) Then GoTo CatchError
  95. End With
  96. Try:
  97. &apos; Create the database Basic object and initialize attributes
  98. Set oDatabase = New SF_Database
  99. With oDatabase
  100. Set .[Me] = oDatabase
  101. ._Location = ConvertToUrl(vFileName)
  102. Set ._DataSource = oDBContext.getByName(._Location)
  103. Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
  104. ._ReadOnly = vReadOnly
  105. Set ._MetaData = ._Connection.MetaData
  106. ._URL = ._MetaData.URL
  107. End With
  108. Finally:
  109. Set _NewDatabase = oDatabase
  110. Exit Function
  111. Catch:
  112. GoTo Finally
  113. CatchError:
  114. ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, &quot;FileName&quot;, vFileName, &quot;RegistrationName&quot;, vRegistration)
  115. GoTo Finally
  116. End Function &apos; SFDatabases.SF_Register._NewDatabase
  117. REM -----------------------------------------------------------------------------
  118. Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object
  119. &apos;ByRef poDataSource As Object _
  120. &apos; , ByVal psUser As String _
  121. &apos; , ByVal psPassword As String _
  122. &apos; ) As Object
  123. &apos;&apos;&apos; Create a new instance of the SF_Database class from the given datasource
  124. &apos;&apos;&apos; established in the SFDocuments.Base service
  125. &apos;&apos;&apos; THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
  126. &apos; Args:
  127. &apos;&apos;&apos; DataSource: com.sun.star.sdbc.XDataSource
  128. &apos;&apos;&apos; User, Password : connection parameters
  129. &apos;&apos;&apos; Returns:
  130. &apos;&apos;&apos; The instance or Nothing
  131. &apos;&apos;&apos; Exceptions:
  132. &apos;&apos;&apos; managed in the calling routines when Nothing is returned
  133. Dim oDatabase As Object &apos; Return value
  134. Dim oConnection As Object &apos; com.sun.star.sdbc.XConnection
  135. Dim oDataSource As Object &apos; Alias of pvArgs(0)
  136. Dim sUser As String &apos; Alias of pvARgs(1)
  137. Dim sPassword As String &apos; Alias of pvARgs(2)
  138. If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
  139. Set oDatabase = Nothing
  140. Try:
  141. &apos; Get arguments
  142. Set oDataSource = pvArgs(0)
  143. sUser = pvArgs(1)
  144. sPassword = pvArgs(2)
  145. &apos; Setup the connection
  146. If oDataSource.IsPasswordRequired Then
  147. Set oConnection = oDataSource.getConnection(sUser, sPassword)
  148. Else
  149. Set oConnection = oDataSource.getConnection(&quot;&quot;, &quot;&quot;)
  150. End If
  151. &apos; Create the database Basic object and initialize attributes
  152. If Not IsNull(oConnection) Then
  153. Set oDatabase = New SF_Database
  154. With oDatabase
  155. Set .[Me] = oDatabase
  156. ._Location = &quot;&quot;
  157. Set ._DataSource = oDataSource
  158. Set ._Connection = oConnection
  159. ._ReadOnly = oConnection.isReadOnly()
  160. Set ._MetaData = oConnection.MetaData
  161. ._URL = ._MetaData.URL
  162. End With
  163. End If
  164. Finally:
  165. Set _NewDatabaseFromSource = oDatabase
  166. Exit Function
  167. Catch:
  168. GoTo Finally
  169. End Function &apos; SFDatabases.SF_Register._NewDatabaseFromSource
  170. REM ============================================== END OF SFDATABASES.SF_REGISTER
  171. </script:module>