Best approach for inserting logic before and after the execution of a query ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Best approach for inserting logic before and after the execution of a query ?

Francisco Herrera

Hi, we need to port some old functionality from Ibatis into Mybatis, in which some logic is inserted before and after the execution of a query. We want to assess what would be a better approach from this two (or any other?):

a) Using a Mybatis plugin such as:

@Intercepts( {
        @Signature(type = Executor.class
                , method = "query"
                ,  args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
public class MyNewQueryInterceptor implements Interceptor {



b) Extending SqlSession such as:

public class MyNewSqlMapClientTemplate extends SqlSessionTemplate { <-- We are using Spring Mybatis


To give some context, on the old Ibatis, the code used to look like:

   @Override
    public Object queryForObject(String statementName, Object parameterObject)
            throws DataAccessException {

        return execute(new ScopedSqlMapClientCallback(statementName, parameterObject,
                new ExtendedSqlMapClientCallback() {

                    @Override
                    public Object callback(SqlMapExecutor executor, String statementName, Object params)
                            throws SQLException {
                        return executor.queryForObject(statementName, params);
                    }
                }));
    }

Thanks,
Francisco

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Best approach for inserting logic before and after the execution of a query ?

Guy Rouillier-2
You don't mention which DBMS you are using, or what kind of logic you need to apply.  One approach I've used with Oracle was to employ an anonymous procedure block within the mapper SQL.  This approach is of course limited to the capabilities of the stored procedure implementation, but for simple needs works well and is simple to implement.

--
Guy Rouillier

On 5/21/2020 3:19:10 PM, "Francisco Herrera" <[hidden email]> wrote:


Hi, we need to port some old functionality from Ibatis into Mybatis, in which some logic is inserted before and after the execution of a query. We want to assess what would be a better approach from this two (or any other?):

a) Using a Mybatis plugin such as:

@Intercepts( {
        @Signature(type = Executor.class
                , method = "query"
                ,  args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
public class MyNewQueryInterceptor implements Interceptor {



b) Extending SqlSession such as:

public class MyNewSqlMapClientTemplate extends SqlSessionTemplate { <-- We are using Spring Mybatis


To give some context, on the old Ibatis, the code used to look like:

   @Override
    public Object queryForObject(String statementName, Object parameterObject)
            throws DataAccessException {

        return execute(new ScopedSqlMapClientCallback(statementName, parameterObject,
                new ExtendedSqlMapClientCallback() {

                    @Override
                    public Object callback(SqlMapExecutor executor, String statementName, Object params)
                            throws SQLException {
                        return executor.queryForObject(statementName, params);
                    }
                }));
    }

Thanks,
Francisco

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emd878a6c4-def0-478b-9e95-13af2e531944%40asus.
Reply | Threaded
Open this post in threaded view
|

Re: Best approach for inserting logic before and after the execution of a query ?

Francisco Herrera
Hi Guy,

We use Oracle, and the ad-hoc logic is as follows. For all SQL/queries the system needs to:

1) Find if the SQL/query has variables with a special format $variable_X$
2) If so, run a special SQL/query that will establish some context on the DB side, and will return values to be passed for those $variable_X$ variables
3) Then, run the original SQL with the values fetched above for the $variable_X$ variables
4) And finally, run a final SQL/query to clean the context on DB side

The Mybatis plugin seems to be doing the job, but we are not sure if a plugin has any cons for this kind of logic described.

Any ideas ?

Francisco

On Thursday, May 21, 2020 at 3:18:59 PM UTC-7, Guy Rouillier wrote:
You don't mention which DBMS you are using, or what kind of logic you need to apply.  One approach I've used with Oracle was to employ an anonymous procedure block within the mapper SQL.  This approach is of course limited to the capabilities of the stored procedure implementation, but for simple needs works well and is simple to implement.

--
Guy Rouillier

On 5/21/2020 3:19:10 PM, "Francisco Herrera" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="tVHFZZ5XAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fcohe...@...> wrote:


Hi, we need to port some old functionality from Ibatis into Mybatis, in which some logic is inserted before and after the execution of a query. We want to assess what would be a better approach from this two (or any other?):

a) Using a Mybatis plugin such as:

@Intercepts( {
        @Signature(type = Executor.class
                , method = "query"
                ,  args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
public class MyNewQueryInterceptor implements Interceptor {



b) Extending SqlSession such as:

public class MyNewSqlMapClientTemplate extends SqlSessionTemplate { <-- We are using Spring Mybatis


To give some context, on the old Ibatis, the code used to look like:

   @Override
    public Object queryForObject(String statementName, Object parameterObject)
            throws DataAccessException {

        return execute(new ScopedSqlMapClientCallback(statementName, parameterObject,
                new ExtendedSqlMapClientCallback() {

                    @Override
                    public Object callback(SqlMapExecutor executor, String statementName, Object params)
                            throws SQLException {
                        return executor.queryForObject(statementName, params);
                    }
                }));
    }

Thanks,
Francisco

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="tVHFZZ5XAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/mybatis-user/18227fa5-3400-4c84-b327-5ee0969343d9%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/32e1dc11-f9ac-4853-bc2a-a8a4d2fb6bd4%40googlegroups.com.