What's the best way in a method to return BooleanValue AND error messages

In a mysql subclass, I have a method that updates a record

mAssignReplacement(Id_user as string, id_main as string) returns boolean

Var q As String

q = "UPDATE remplacement SET id_user_assigned = ? WHERE id_horaire_base = ? "

// note in a prepared statement you DO NOT put in the quotes
Var stmt As MySQLPreparedStatement
stmt = MySQLPreparedStatement(Self.Prepare(q))

// have to tell Mysql what types the items being bound are so it does the right thing
stmt.BindType(0, MySQLPreparedStatement.MYSQL_TYPE_STRING)
stmt.Bind(0, id_user)
stmt.BindType(1, MySQLPreparedStatement.MYSQL_TYPE_STRING)
stmt.Bind(1, id_main)

If Self.Connect Then

Try
stmt.ExecuteSQL
Return True
Catch error As DatabaseException
MessageBox("Erreur base de données: " + error.Message)
Return False
End Try

End If

What would be the best way to get back the boolean value and the error.message also if the update doesn’t work ?

I don’t want to use the message box in case of a database exception. Raise my own exception ? How would I do that ?

Thanks

Jean-Maurice

Raise your own exception, or just don’t catch the database exception.

1 Like

When you want to return more than one value, create a JSon structure and return it.

Or use Python :grinning:. More seriously being able to return more than one value is a Python interesting characteristic.

1 Like

Ok, it would be like this ?

Try
stmt.ExecuteSQL
Return True
Catch error As DatabaseException
Var e As New RuntimeException
e.Message= "Erreur base de données: " + error.Message
Raise e
Return False. // ?? do I have to return False ?? or it won’t go there since I Raise an exception ?
End Try

Thanks

Jean-Maurice

Something like this, no need of a boolean:

Public Sub AssignReplacement(db As MySQLCommunityServer, id_user As String, id_main As String)

  Var q As String = "UPDATE remplacement SET id_user_assigned = ? WHERE id_horaire_base = ? ;" 
  
  If db.Connect Then
    db.ExecuteSQL(q, id_user, id_main) // It may fail, catch the error at the caller level
  Else
    Raise New DatabaseException("Couldn't connect", -1) // failed, raise some exception with a message too
  End If
  
  // All done without errors, return
  
End Sub


//****** Now use it somewhere *****

Try
  AssignReplacement(mydb, "user123", "horaire12")
Catch e As DatabaseException   // it'll reach here if it fails
  MessageBox "Replacement failed : " + e.Message
End

1 Like

Or pass one or more variables into the method byRef.

2 Likes

The third line (return false) will never be reached. Raising an exception terminates processing immediately, unless you’re catching it in some way.

2 Likes

I agree with Tim, simply let the DatabaseException bubble to your code that will handle it. It will carry the message and error number for you.

1 Like

Mee too. As shown above.

If you don’t want to use the exception idea others have suggested, consider simply returning the error message. If there is not error, return an empty string. The caller can then detect if there is an error an deal with that/show the user. If there is no error, the empty string will indicate that.

Thanks again everybody :slight_smile:

Jean-Maurice

1 Like