wizards/source/sfdatabases/SF_Register.xba     |   20 ++++++++++++++------
 wizards/source/sfdocuments/SF_Form.xba         |    3 +++
 wizards/source/sfdocuments/SF_FormDocument.xba |    5 ++++-
 3 files changed, 21 insertions(+), 7 deletions(-)

New commits:
commit d2c1a4fa508572cc2908787deeebc5baa9d6e0c3
Author:     Jean-Pierre Ledure <j...@ledure.be>
AuthorDate: Wed Aug 9 12:33:39 2023 +0200
Commit:     Jean-Pierre Ledure <j...@ledure.be>
CommitDate: Wed Aug 9 14:45:23 2023 +0200

    SCriptForge (SFDatabases) manage database credentials
    
    When not embedded, databases usually
    require user and password to grant
    access to scripts.
    
    When the access was refused, so far the
    execution was stopped brutally with an
    error message provided by LO internal
    code.
    This was made visible during some tests
    with a MySql database server.
    
    The code to be reviewed is located in the
    creation of the "database" service. This
    service has the sungularity that it can
    be called from several other service
    instances.
    
    As from this commit unsuccessful tentatives
    to login to a database from code are
    rejected with a clean SF error message.
    
    The different scenarios to create a
    database service have been (re)tested.
    
    Change-Id: I695d108242872d27671688af76916784908aeebe
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155507
    Reviewed-by: Jean-Pierre Ledure <j...@ledure.be>
    Tested-by: Jenkins

diff --git a/wizards/source/sfdatabases/SF_Register.xba 
b/wizards/source/sfdatabases/SF_Register.xba
index 04c76ff59e94..cee09f94f3f3 100644
--- a/wizards/source/sfdatabases/SF_Register.xba
+++ b/wizards/source/sfdatabases/SF_Register.xba
@@ -25,6 +25,7 @@ Option Explicit
 REM ================================================================== 
EXCEPTIONS
 
 Private Const BASEDOCUMENTOPENERROR    =       
&quot;BASEDOCUMENTOPENERROR&quot;
+Private Const DBCONNECTERROR           =       &quot;DBCONNECTERROR&quot;
 
 REM ============================================================== PUBLIC 
METHODS
 
@@ -65,7 +66,8 @@ Public Function _NewDatabase(Optional ByVal pvArgs As 
Variant) As Object
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             The instance or Nothing
 &apos;&apos;&apos;     Exceptions:
-&apos;&apos;&apos;             BASEDOCUMENTOPENERROR           The database 
file could not be opened or connected
+&apos;&apos;&apos;             BASEDOCUMENTOPENERROR           The database 
file could not be opened
+&apos;&apos;&apos;             DBCONNECTERROR                          The 
database could not be connected, credentials are probably wrong
 
 Dim oDatabase As Object                                        &apos;  Return 
value
 Dim vFileName As Variant                               &apos;  alias of 
pvArgs(0)
@@ -116,7 +118,9 @@ Try:
                Set .[Me] = oDatabase
                ._Location = ConvertToUrl(vFileName)
                Set ._DataSource = oDBContext.getByName(._Location)
+               If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error 
GoTo CatchConnect
                Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
+               If IsNull(._Connection) Then GoTo CatchConnect
                ._ReadOnly = vReadOnly
                Set ._MetaData = ._Connection.MetaData
                ._URL = ._MetaData.URL
@@ -130,20 +134,23 @@ Catch:
 CatchError:
        ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, 
&quot;FileName&quot;, vFileName, &quot;RegistrationName&quot;, vRegistration)
        GoTo Finally
+CatchConnect:
+       ScriptForge.SF_Exception.RaiseFatal(DBCONNECTERROR, &quot;User&quot;, 
vUser, &quot;Password&quot;, vPassword, vFileName)
+       GoTo Finally
 End Function   &apos;  SFDatabases.SF_Register._NewDatabase
 
 REM 
-----------------------------------------------------------------------------
 Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As 
Object
-&apos;                                                                         
ByRef poDataSource As Object _
-&apos;                                                                         
        , ByVal psUser As String _
-&apos;                                                                         
        , ByVal psPassword As String _
+&apos;                                                                         
ByRef oDataSource As Object _
+&apos;                                                                         
        , ByVal sUser As String _
+&apos;                                                                         
        , ByVal sPassword As String _
 &apos;                                                                         
        ) As Object
 &apos;&apos;&apos;     Create a new instance of the SF_Database class from the 
given datasource
 &apos;&apos;&apos;     established in the SFDocuments.Base service
 &apos;&apos;&apos;     THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
 &apos;&apos;&apos;     Args:
-&apos;&apos;&apos;             DataSource:     com.sun.star.sdbc.XDataSource
-&apos;&apos;&apos;             User, Password : connection parameters
+&apos;&apos;&apos;             oDataSource:    com.sun.star.sdbc.XDataSource
+&apos;&apos;&apos;             sUser, sPassword : connection parameters
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             The instance or Nothing
 &apos;&apos;&apos;     Exceptions:
@@ -189,6 +196,7 @@ Finally:
        Set _NewDatabaseFromSource = oDatabase
        Exit Function
 Catch:
+       ScriptForge.SF_Exception.Clear()
        GoTo Finally
 End Function   &apos;  SFDatabases.SF_Register._NewDatabaseFromSource
 
diff --git a/wizards/source/sfdocuments/SF_Form.xba 
b/wizards/source/sfdocuments/SF_Form.xba
index cc7c2aecf34f..a5559ae06a8a 100644
--- a/wizards/source/sfdocuments/SF_Form.xba
+++ b/wizards/source/sfdocuments/SF_Form.xba
@@ -81,6 +81,7 @@ REM 
================================================================== EXCEPTION
 
 Private Const FORMDEADERROR                    =       
&quot;FORMDEADERROR&quot;
 Private Const SUBFORMNOTFOUNDERROR     =       &quot;SUBFORMNOTFOUNDERROR&quot;
+Private Const DBCONNECTERROR           =       &quot;DBCONNECTERROR&quot;
 
 REM ============================================================= PRIVATE 
MEMBERS
 
@@ -668,6 +669,8 @@ Public Function GetDatabase(Optional ByVal User As Variant _
 &apos;&apos;&apos;             User, Password: the login parameters as 
strings. Defaults = &quot;&quot;
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             A SFDatabases.Database instance or Nothing
+&apos;&apos;&apos;     Exceptions:
+&apos;&apos;&apos;             DBCONNECTERROR                          The 
database could not be connected, credentials are probably wrong
 &apos;&apos;&apos;     Example:
 &apos;&apos;&apos;             Dim myDb As Object
 &apos;&apos;&apos;                     Set myDb = oForm.GetDatabase()
diff --git a/wizards/source/sfdocuments/SF_FormDocument.xba 
b/wizards/source/sfdocuments/SF_FormDocument.xba
index 4270c08e2907..5269cea09c1c 100644
--- a/wizards/source/sfdocuments/SF_FormDocument.xba
+++ b/wizards/source/sfdocuments/SF_FormDocument.xba
@@ -65,6 +65,7 @@ Option Explicit
 REM ================================================================== 
EXCEPTIONS
 
 Private Const BASEFORMNOTFOUNDERROR    =       
&quot;BASEFORMNOTFOUNDERROR&quot;
+Private Const DBCONNECTERROR           =       &quot;DBCONNECTERROR&quot;
 
 REM ============================================================= PRIVATE 
MEMBERS
 
@@ -244,6 +245,8 @@ Public Function GetDatabase(Optional ByVal User As Variant _
 &apos;&apos;&apos;             User, Password: the login parameters as 
strings. Defaults = &quot;&quot;
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             A SFDatabases.Database instance or Nothing
+&apos;&apos;&apos;     Exceptions:
+&apos;&apos;&apos;             DBCONNECTERROR                          The 
database could not be connected, credentials are probably wrong
 &apos;&apos;&apos;     Example:
 &apos;&apos;&apos;             Dim myDb As Object
 &apos;&apos;&apos;                     Set myDb = oFormDoc.GetDatabase()
@@ -267,7 +270,7 @@ Check:
 Try:
        If IsNull(_DataSource) Then GoTo CatchConnect
        Set oDatabase = 
ScriptForge.SF_Services.CreateScriptService(&quot;SFDatabases.DatabaseFromDocument&quot;
 _
-                                               , _DataSource, Iif(User = 
&quot;&quot;, _User, User), Iif(Password = &quot;&quot;, _Password, 
&quot;&quot;))
+                                               , _DataSource, Iif(User = 
&quot;&quot;, _User, User), Iif(Password = &quot;&quot;, _Password, Password))
        If IsNull(oDatabase) Then GoTo CatchConnect
        oDatabase._Location = _DataSource.Name
 

Reply via email to