I need to write a wrapper (based on MySQL Connector/C++) which encapsulate specific SQL statements and provide user friendly interfaces, li
Each of the three functions have a specific use, which can be guessed from their return type.
This function is the most generic one. It returns a boolean value, which value is true if the query returns multiple results, or false if the query returns either nothing or an update count.
This is the function you'll want to use if you only want to use one to be as generic as possible.
If it returns true, you'll want to use ResultSet * getResultSet()
to get the results.
If it returns false, you'll want to use uint64_t getUpdateCount()
to get the number of updated rows.
This function directly returns a ResultSet
which is useful for SELECT
statements, and assumes there is indeed a result set to be returned.
It is equivalent to call execute()
followed by getResultSet()
.
You'll want to use this function when you know you are using SQL code that returns results such as rows.
This function returns an integer value which is useful for UPDATE
statements and assumes there is an update count to be returned.
It is equivalent to call execute()
followed by getUpdateCount()
, even though, for some reason, the return types are different (int vs uint64_t).
This is the function to use when executing SQL statements that modify data and you need to know whether some data was modified.
So,
why there're so many execution functions rather than a simple and unified one?
the unified one is in fact execute
, which can be used to execute arbitrary SQL and handle the result appropriately, while the two other ones are convenient wrappers when you know what kind of query you execute.
which execute function should i use for specify SQL statements?
In your case, since you are writing a wrapper around the SQL language, each of your functions knows which kind of statement it will execute, so use of the convenience functions will allow you to write shorter code.
For example:
insert(), update(), delete() ---> executeUpdate()
select() ---> executeQuery()